BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / object.js

object.js

Add GameWindow.cameraPosition
timfa Tim Falken · pushed by timfa · 7 years ago
71d13943 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 var offX = GameWindow.cameraPosition.x;
195 var offY = GameWindow.cameraPosition.y;
196
197 this.renderer.draw(this.transform.position.x + offX, this.transform.position.y + offY,
198 this.transform.width * this.transform.scale.x,
199 this.transform.height * this.transform.scale.y,
200 this.transform.rotation);
201 }
202 }
203
204 this.g_draw = function()
205 {
206 if(!this.g_destroy)
207 {
208 if(this.draw)
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 if (typeof this.onDestroy == 'function') { this.onDestroy(); }
355
356 this.destroyed = true;
357
358 this.g_destroy = true;
359
360 GameObject.allObjects.remove(this);
361 }
362 }
363
364 this.g_init(scene);
365 }
366}