BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / object.js

object.js

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