BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / object.js

object.js

autobuild via task
timfa Tim Falken · pushed by timfa · 8 years ago
255745ff 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 }
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
126 pointTowards: function(point)
127 {
128 var delta = point.subtract(this.position);
129
130 this.rotation = delta.toAngle();
131 },
132
133
134 left: function()
135 {
136 return this.g_dir(0);
137 },
138
139
140 right: function()
141 {
142 return this.g_dir(180);
143 },
144
145
146 forward: function()
147 {
148 return this.g_dir(90);
149 },
150
151
152 back: function()
153 {
154 return this.g_dir(270);
155 },
156
157 g_dir: function(offset)
158 {
159 var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180);
160
161 return angleVector = new vector( -Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians) );
162 }
163 };
164
165 this.g_destroy = false;
166
167 this.g_init = function()
168 {
169 Game.activeScene.addObject(this);
170
171
172 if (typeof this.start == 'function') { this.start(); }
173 }
174
175 /*this.g_toHTML = function()
176 {
177 return '<object id="' + this.name + '" style="' +
178 'position: absolute;' +
179 'left:' + (this.transform.position.x - (this.transform.width / 2)) + 'px;' +
180 'top:' + (this.transform.position.y - (this.transform.height / 2)) + 'px;' +
181 'width:' + this.transform.width + 'px;' +
182 'height:' + this.transform.height + 'px;' +
183 (this.image != ''? 'background-image: url(' + this.image + ');' : 'border: 1px solid black;') +
184 'transform:rotate(' + this.transform.rotation + 'deg);' +
185 'background-color:' + this.color + ';' +
186 'background-repeat:no-repeat;' +
187 'background-size:100% 100%;' +
188 '">' + this.text + '</object>';
189 }*/
190
191 this.defaultDraw = function()
192 {
193 if(this.renderer.image)
194 {
195 this.renderer.draw(this.transform.position.x, this.transform.position.y,
196 this.transform.width * this.transform.scale.x,
197 this.transform.height * this.transform.scale.y,
198 this.transform.rotation);
199 }
200 }
201
202 this.g_draw = function()
203 {
204 if(!this.g_destroy)
205 {
206 if(this.draw)
207 {
208
209 this.draw();
210 }
211 else
212 {
213 this.defaultDraw();
214 /*else //??? makes entire screen colored.
215 {
216 GameWindow.canvasContext.fillStyle = "rgb("+ this.transform.position.y+" ," + this.transform.position.x + ",0)"; // sets the color to fill in the rectangle with
217 GameWindow.canvasContext.fillRect( this.transform.position.x - this.transform.width / 2,
218 this.transform.position.y - this.transform.height / 2,
219 this.transform.width,
220 this.transform.height);
221 }*/
222 }
223 }
224 }
225
226 this.g_checkClick = function(thisFrame)
227 {
228 var hit = false;
229 var clickPos = GameInput.mousePosition;
230
231 //hw = halfWidth, hh = halfHeight
232 var hw = this.transform.width / 2;
233 var hh = this.transform.height / 2;
234
235 if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw)
236 {
237 if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh)
238 {
239 hit = true;
240 }
241 }
242
243 if (hit)
244 {
245 if (thisFrame)
246 {
247 this.onClick && this.onClick();
248 }
249 else
250 {
251 this.onMouseHeld && this.onMouseHeld();
252 }
253 }
254 }
255
256 this.g_updateStep = function(deltaTime)
257 {
258 if(this.g_destroy)
259 {
260 Game.activeScene.activeObjects.splice(Game.activeScene.activeObjects.indexOf(this), 1);
261 delete this;
262 }
263 else
264 {
265
266 if (typeof this.update == 'function') { this.update(deltaTime); }
267
268 this.transform.position.x += this.transform.velocity.x;
269 this.transform.position.y += this.transform.velocity.y;
270
271 //check for clicks
272 if (GameInput.mousePressed())
273 {
274 this.g_checkClick(true);
275 }
276
277 //check for clicks
278 if (GameInput.mouseHeld())
279 {
280 this.g_checkClick(false);
281 }
282
283 if(!this.ignoreCollisions && !this.destroyed)
284 {
285 for(var i = 0; i < Game.activeScene.activeObjects.length; i++)
286 {
287 if(Game.activeScene.activeObjects[i] != this && !Game.activeScene.activeObjects[i].ignoreCollisions)
288 {
289 var hit = false;
290 var o2 = Game.activeScene.activeObjects[i];
291
292 //hw = halfWidth, hh = halfHeight
293 var hw = (this.transform.width * this.transform.scale.x) / 2;
294 var hh = (this.transform.height * this.transform.scale.y) / 2;
295
296 var ohw = (o2.transform.width * o2.transform.scale.x) / 2;
297 var ohh = (o2.transform.height * o2.transform.scale.y) / 2;
298
299 //CHECK FROM OBJ PERSPECTIVE
300 var p =
301 [
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 [this.transform.position.x + hw, this.transform.position.y + hh]
306 ];
307
308 for(var pi = 0; pi < p.length; pi++)
309 {
310 if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw)
311 {
312 if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh)
313 {
314 hit = true;
315 }
316 }
317 }
318
319 //CHECK FROM OTHER PERSPECTIVE
320 var p =
321 [
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 [o2.transform.position.x + ohw, o2.transform.position.y + ohh]
326 ];
327
328 for(var pi = 0; pi < p.length; pi++)
329 {
330 if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw)
331 {
332 if(p[pi][1] > this.transform.position.y - hh && p[pi][1] < this.transform.position.y + hh)
333 {
334 hit = true;
335 }
336 }
337 }
338
339
340 if(hit)
341 if (!this.destroyed && typeof this.onCollision == 'function') { this.onCollision(o2); }
342 }
343 }
344 }
345 }
346 }
347
348
349 this.destroy = function()
350 {
351 if(!this.g_destroy)
352 {
353
354 if (typeof this.onDestroy == 'function') { this.onDestroy(); }
355
356 this.destroyed = true;
357
358 this.g_destroy = true;
359 }
360 }
361
362 this.g_init();
363 }
364}