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