BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / object.js

object.js

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