BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / object.js

object.js

Framework 3
timfa Tim Falken · pushed by timfa · 9 years ago
4a56d325 History
object.js 11.0 KiB Download Raw
1//<DOC> category GameObject
2
3var GameObject =
4{
5 textWarned: false,
6
7 destroyed: false,
8
9 //<DOC> <function> GameObject.create(template, x, y)<br><br>Creates a new Game Object using the object template 'template' at the position ('x', 'y')
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 //<DOC> <parameter> object.active<br><br>Object is visible and executes it's code when this parameter is true. Set to false to hide and disable the object temporarily.
37 this.active = true;
38 //<DOC> <parameter> object.name<br><br>The name of the current object. Defaults to "Object".
39 this.name = "Object";
40 //<DOC> <parameter> object.image<br><br>An image object.
41 this.image = "";
42
43 //<DOC> <parameter> object.ignoreCollisions<br><br>Ignores this object when checking for collisions. Useful for background objects.
44 this.ignoreCollisions = false;
45
46 //<DOC> <parameter> object.renderer<br><br>Used for methods and variables related to drawing the object on the screen.
47 this.renderer = {
48 //<DOC> <parameter> object.renderer.image<br><br>The image to draw.
49 image: image,
50
51 //<DOC> <parameter> object.renderer.subImage<br><br>The current subimage to draw using the default draw method
52 subImage: 0,
53
54 //<DOC> <parameter> object.renderer.alpha<br><br>Image transparency (0-1) default: 1
55 alpha: 1,
56
57 //<DOC> <parameter> object.renderer.visible<br><br>Value indicating if the object should be drawn (true/false)
58 visible: true,
59
60 //used to correct NaN
61 g_subImgOld: 0,
62
63 g_layerDepth: 0,
64
65 //<DOC> <function> image.draw(x, y, w, h, rotation, alpha = 1)<br><br>Draw an image at (x,y), size (w,h), at an angle in degrees.
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 //<DOC> <parameter> object.transform.position<br><br>A vector() containing the object's position. (measured from object center)
113 position: new vector(x, y),
114 //<DOC> <parameter> object.transform.velocity<br><br>A vector() containing the object's speed. (measured from object center)
115 velocity: new vector(0, 0),
116 //<DOC> <parameter> object.transform.rotation<br><br>the object's rotation in degrees. Default is zero.
117 rotation: 0,
118
119 //<DOC> <parameter> object.transform.width<br><br>the width of the object, defaults to 32.
120 width: image? image.width : 0,
121 //<DOC> <parameter> object.transform.height<br><br>the height of the object, defaults to 32.
122 height: image? image.height: 0,
123
124 scale: new vector(1, 1),
125
126 //<DOC> <function> object.transform.pointTowards(point)<br><br>Adjust object.transform.rotation to point at vector2() 'point'.
127 pointTowards: function(point)
128 {
129 var delta = point.subtract(this.position);
130
131 this.rotation = delta.toAngle();
132 },
133
134 //<DOC> <function> object.transform.left()<br><br>A single pixel left of the object, accounting for rotation.
135 left: function()
136 {
137 return this.g_dir(0);
138 },
139
140 //<DOC> <function> object.transform.right()<br><br>A single pixel right of the object, accounting for rotation.
141 right: function()
142 {
143 return this.g_dir(180);
144 },
145
146 //<DOC> <function> object.transform.forward()<br><br>A single pixel forwards of the object, accounting for rotation.
147 forward: function()
148 {
149 return this.g_dir(90);
150 },
151
152 //<DOC> <function> object.transform.back()<br><br>A single pixel backwards of the object, accounting for rotation.
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.activeObjects.push(this);
171
172 //<DOC> <function> object.start()<br><br>Called on object's creation. Override this on your own object templates!
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 //<DOC> <function> object.draw()<br><br>Called on each draw step. If not overridden, default draw is used!
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.activeObjects.splice(Game.activeObjects.indexOf(this), 1);
262 delete this;
263 }
264 else
265 {
266 //<DOC> <function> object.update(deltaTime)<br><br>Called on each frame start! (Before collision checks of this frame) Override this on your own object templates!
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.activeObjects.length; i++)
287 {
288 if(Game.activeObjects[i] != this && !Game.activeObjects[i].ignoreCollisions)
289 {
290 var hit = false;
291 var o2 = Game.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 //<DOC> <function> object.onCollision(other)<br><br>Called on object collision. Parameter 'other' is a reference to the object you're colliding with! Override this on your own object templates!
341 if(hit)
342 if (!this.destroyed && typeof this.onCollision == 'function') { this.onCollision(o2); }
343 }
344 }
345 }
346 }
347 }
348
349 //<DOC> <function> object.destroy()<br><br>Destroys the current object at the end of the current update step.
350 this.destroy = function()
351 {
352 if(!this.g_destroy)
353 {
354 //<DOC> <function> object.onDestroy()<br><br>Called on object's destruction. Override this on your own object templates!
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}