BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / object.js

object.js

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