BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / object.js

object.js

object.js 8.0 KiB Download Raw
1
2
3var GameObject =
4{
5 textWarned: false,
6
7 destroyed: false,
8
9
10 create: function(template, x, y, image, layer, scene)
11 {
12 var newObj = $.extend(true, {}, template);
13
14 GameObject.object.call(newObj, x, y, image, scene);
15
16 layer = layer || 0;
17
18 newObj.renderer.layerDepth = layer;
19
20 newObj.base = template;
21
22 return newObj;
23 },
24
25 inherit: function(base, child)
26 {
27 var newObj = $.extend(true, {}, base);
28
29 var newObj = $.extend(true, newObj, child);
30
31 newObj.base = base;
32
33 return newObj
34 },
35
36 object: function(x, y, image, scene) //DO NOT use any functions with prefix g_ in your game!
37 {
38 this.active = true;
39
40 this.name = "Object";
41
42 this.image = "";
43
44 this.ignoreCollisions = false;
45
46
47 this.renderer = {
48
49 image: image,
50
51 subImage: 0,
52
53 alpha: 1,
54
55 visible: true,
56
57 //used to correct NaN
58 g_subImgOld: 0,
59
60 g_layerDepth: 0,
61
62 draw: function(x, y, w, h, rotation)
63 {
64 if(isNaN(this.subImage))
65 {
66 this.subImage = this.g_subImgOld;
67 }
68 else
69 {
70 this.g_subImgOld = this.subImage;
71 }
72
73 var subImage = Math.round(this.subImage);
74 subImage %= this.image.subImages;
75
76 var c = subImage % this.image.subImagesPerRow;
77 var r = Math.floor(subImage / this.image.subImagesPerRow);
78 var sw = (this.image.g_image.width / this.image.subImagesPerRow);
79 var sh = (this.image.g_image.height / Math.ceil(this.image.subImages / this.image.subImagesPerRow));
80 var sx = sw * c;
81 var sy = sh * r;
82
83 this.image.draw(sx, sy, sw, sh, x, y, w, h, rotation, this.alpha);
84 }
85 }
86
87 Object.defineProperty(this.renderer, "layerDepth",
88 {
89 get: function()
90 {
91 return this.g_layerDepth;
92 },
93
94 set: function(val)
95 {
96 if (this.g_layerDepth != val)
97 {
98 this.g_layerDepth = val;
99
100 Game.reorderObjects();
101 }
102 }
103 })
104
105 this.transform =
106 {
107
108 position: new vector(x, y),
109
110 velocity: new vector(0, 0),
111
112 rotation: 0,
113
114
115 width: image? image.width : 0,
116
117 height: image? image.height: 0,
118
119 scale: new vector(1, 1),
120
121 pointTowards: function(point)
122 {
123 var delta = point.subtract(this.position);
124
125 this.rotation = delta.toAngle();
126 },
127
128 left: function()
129 {
130 return this.g_dir(0);
131 },
132
133 right: function()
134 {
135 return this.g_dir(180);
136 },
137
138 forward: function()
139 {
140 return this.g_dir(90);
141 },
142
143 back: function()
144 {
145 return this.g_dir(270);
146 },
147
148 g_dir: function(offset)
149 {
150 var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180);
151
152 return angleVector = new vector( -Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians) );
153 }
154 };
155
156 this.g_destroy = false;
157
158 this.g_init = function(scene)
159 {
160 if(scene == null)
161 {
162 Game.activeScene.addObject(this);
163 }
164 else
165 {
166 scene.addObject(this);
167 }
168
169 if (typeof this.start == 'function') { this.start(); }
170 }
171
172 /*this.g_toHTML = function()
173 {
174 return '<object id="' + this.name + '" style="' +
175 'position: absolute;' +
176 'left:' + (this.transform.position.x - (this.transform.width / 2)) + 'px;' +
177 'top:' + (this.transform.position.y - (this.transform.height / 2)) + 'px;' +
178 'width:' + this.transform.width + 'px;' +
179 'height:' + this.transform.height + 'px;' +
180 (this.image != ''? 'background-image: url(' + this.image + ');' : 'border: 1px solid black;') +
181 'transform:rotate(' + this.transform.rotation + 'deg);' +
182 'background-color:' + this.color + ';' +
183 'background-repeat:no-repeat;' +
184 'background-size:100% 100%;' +
185 '">' + this.text + '</object>';
186 }*/
187
188 this.defaultDraw = function()
189 {
190 if(this.renderer.image)
191 {
192 this.renderer.draw(this.transform.position.x, this.transform.position.y,
193 this.transform.width * this.transform.scale.x,
194 this.transform.height * this.transform.scale.y,
195 this.transform.rotation);
196 }
197 }
198
199 this.g_draw = function()
200 {
201 if(!this.g_destroy)
202 {
203 if(this.draw)
204 {
205
206 this.draw();
207 }
208 else
209 {
210 this.defaultDraw();
211 /*else //??? makes entire screen colored.
212 {
213 GameWindow.canvasContext.fillStyle = "rgb("+ this.transform.position.y+" ," + this.transform.position.x + ",0)"; // sets the color to fill in the rectangle with
214 GameWindow.canvasContext.fillRect( this.transform.position.x - this.transform.width / 2,
215 this.transform.position.y - this.transform.height / 2,
216 this.transform.width,
217 this.transform.height);
218 }*/
219 }
220 }
221 }
222
223 this.g_checkClick = function(thisFrame)
224 {
225 var hit = false;
226 var clickPos = GameInput.mousePosition;
227
228 //hw = halfWidth, hh = halfHeight
229 var hw = this.transform.width / 2;
230 var hh = this.transform.height / 2;
231
232 if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw)
233 {
234 if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh)
235 {
236 hit = true;
237 }
238 }
239
240 if (hit)
241 {
242 if (thisFrame)
243 {
244 this.onClick && this.onClick();
245 }
246 else
247 {
248 this.onMouseHeld && this.onMouseHeld();
249 }
250 }
251 }
252
253 this.g_updateStep = function(deltaTime)
254 {
255 if(this.g_destroy)
256 {
257 Game.activeScene.activeObjects.splice(Game.activeScene.activeObjects.indexOf(this), 1);
258 delete this;
259 }
260 else
261 {
262
263 if (typeof this.update == 'function') { this.update(deltaTime); }
264
265 this.transform.position.x += this.transform.velocity.x;
266 this.transform.position.y += this.transform.velocity.y;
267
268 //check for clicks
269 if (GameInput.mousePressed())
270 {
271 this.g_checkClick(true);
272 }
273
274 //check for clicks
275 if (GameInput.mouseHeld())
276 {
277 this.g_checkClick(false);
278 }
279
280 if(!this.ignoreCollisions && !this.destroyed)
281 {
282 for(var i = 0; i < Game.activeScene.activeObjects.length; i++)
283 {
284 if(Game.activeScene.activeObjects[i] != this && !Game.activeScene.activeObjects[i].ignoreCollisions)
285 {
286 var hit = false;
287 var o2 = Game.activeScene.activeObjects[i];
288
289 //hw = halfWidth, hh = halfHeight
290 var hw = (this.transform.width * this.transform.scale.x) / 2;
291 var hh = (this.transform.height * this.transform.scale.y) / 2;
292
293 var ohw = (o2.transform.width * o2.transform.scale.x) / 2;
294 var ohh = (o2.transform.height * o2.transform.scale.y) / 2;
295
296 //CHECK FROM OBJ PERSPECTIVE
297 var p =
298 [
299 [this.transform.position.x - hw, this.transform.position.y - hh],
300 [this.transform.position.x + hw, this.transform.position.y - hh],
301 [this.transform.position.x - hw, this.transform.position.y + hh],
302 [this.transform.position.x + hw, this.transform.position.y + hh]
303 ];
304
305 for(var pi = 0; pi < p.length; pi++)
306 {
307 if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw)
308 {
309 if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh)
310 {
311 hit = true;
312 }
313 }
314 }
315
316 //CHECK FROM OTHER PERSPECTIVE
317 var p =
318 [
319 [o2.transform.position.x - ohw, o2.transform.position.y - ohh],
320 [o2.transform.position.x + ohw, o2.transform.position.y - ohh],
321 [o2.transform.position.x - ohw, o2.transform.position.y + ohh],
322 [o2.transform.position.x + ohw, o2.transform.position.y + ohh]
323 ];
324
325 for(var pi = 0; pi < p.length; pi++)
326 {
327 if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw)
328 {
329 if(p[pi][1] > this.transform.position.y - hh && p[pi][1] < this.transform.position.y + hh)
330 {
331 hit = true;
332 }
333 }
334 }
335
336
337 if(hit)
338 if (!this.destroyed && typeof this.onCollision == 'function') { this.onCollision(o2); }
339 }
340 }
341 }
342 }
343 }
344
345
346 this.destroy = function()
347 {
348 if(!this.g_destroy)
349 {
350
351 if (typeof this.onDestroy == 'function') { this.onDestroy(); }
352
353 this.destroyed = true;
354
355 this.g_destroy = true;
356 }
357 }
358
359 this.g_init(scene);
360 }
361}