BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / object.js

object.js

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