BlueGit

timfa frameworkv3

Files

frameworkv3 / object.js

object.js

Initial Commit
timfa Tim Falken · pushed by timfa · 10 years ago
d075adff History
object.js 9.3 KiB Download Raw
1//<DOC> category GameObject
2
3var GameObject =
4{
5 textWarned: false,
6
7 //<DOC> <function> GameObject.create(template, x, y)<br><br>Creates a new Game Object using the object template 'template' at the position ('x', 'y')
8 create: function(template, x, y)
9 {
10 var newObj = $.extend(true, {}, template);
11
12 GameObject.object.call(newObj, x, y);
13
14 return newObj;
15 },
16
17 object: function(x, y) //DO NOT use any functions with prefix g_ in your game!
18 {
19 //<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.
20 this.active = true;
21 //<DOC> <parameter> object.name<br><br>The name of the current object. Defaults to "Object".
22 this.name = "Object";
23 //<DOC> <parameter> object.image<br><br>An image object.
24 this.image = "";
25
26 //<DOC> <parameter> object.ignoreCollisions<br><br>Ignores this object when checking for collisions. Useful for background objects.
27 this.ignoreCollisions = false;
28
29 //<DOC> <parameter> object.renderer<br><br>Used for methods and variables related to drawing the object on the screen.
30 this.renderer = {
31 //<DOC> <parameter> object.renderer.image<br><br>The image to draw.
32 image: null,
33
34 //<DOC> <parameter> object.renderer.subImage<br><br>The current subimage to draw using the default draw method
35 subImage: 0,
36
37 //<DOC> <parameter> object.renderer.alpha<br><br>Image transparency (0-1) default: 1
38 alpha: 1,
39
40 //<DOC> <parameter> object.renderer.visible<br><br>Value indicating if the object should be drawn (true/false)
41 visible: true,
42
43 //used to correct NaN
44 g_subImgOld: 0,
45
46 //<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.
47 draw: function(x, y, w, h, rotation)
48 {
49 if(isNaN(this.subImage))
50 {
51 this.subImage = this.g_subImgOld;
52 }
53 else
54 {
55 this.g_subImgOld = this.subImage;
56 }
57
58 var subImage = Math.round(this.subImage);
59 subImage %= this.image.subImages;
60
61 var c = subImage % this.image.subImagesPerRow;
62 var r = Math.floor(subImage / this.image.subImagesPerRow);
63 var sw = (this.image.g_image.width / this.image.subImagesPerRow);
64 var sh = (this.image.g_image.height / Math.ceil(this.image.subImages / this.image.subImagesPerRow));
65 var sx = sw * c;
66 var sy = sh * r;
67
68 this.image.draw(sx, sy, sw, sh, x, y, w, h, rotation, this.alpha);
69 //context.drawImage(this.image.g_image, sx, sy, sw, sh, -w/2, -h/2, w, h
70 }
71 }
72
73 this.transform =
74 {
75 //<DOC> <parameter> object.transform.position<br><br>A vector() containing the object's position. (measured from object center)
76 position: new vector(x, y),
77 //<DOC> <parameter> object.transform.velocity<br><br>A vector() containing the object's speed. (measured from object center)
78 velocity: new vector(0, 0),
79 //<DOC> <parameter> object.transform.rotation<br><br>the object's rotation in degrees. Default is zero.
80 rotation: 0,
81
82 //<DOC> <parameter> object.transform.width<br><br>the width of the object, defaults to 32.
83 width: 32,
84 //<DOC> <parameter> object.transform.height<br><br>the height of the object, defaults to 32.
85 height: 32,
86
87 //<DOC> <function> object.transform.pointTowards(point)<br><br>Adjust object.transform.rotation to point at vector2() 'point'.
88 pointTowards: function(point)
89 {
90 var delta = point.subtract(this.position);
91
92 this.rotation = delta.toAngle();
93 },
94
95 //<DOC> <function> object.transform.left()<br><br>A single pixel left of the object, accounting for rotation.
96 left: function()
97 {
98 return this.g_dir(0);
99 },
100
101 //<DOC> <function> object.transform.right()<br><br>A single pixel right of the object, accounting for rotation.
102 right: function()
103 {
104 return this.g_dir(180);
105 },
106
107 //<DOC> <function> object.transform.forward()<br><br>A single pixel forwards of the object, accounting for rotation.
108 forward: function()
109 {
110 return this.g_dir(90);
111 },
112
113 //<DOC> <function> object.transform.back()<br><br>A single pixel backwards of the object, accounting for rotation.
114 back: function()
115 {
116 return this.g_dir(270);
117 },
118
119 g_dir: function(offset)
120 {
121 var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180);
122
123 return angleVector = new vector( -Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians) );
124 }
125 };
126
127 this.g_destroy = false;
128
129 this.g_init = function()
130 {
131 Game.activeObjects.push(this);
132
133 //<DOC> <function> object.start()<br><br>Called on object's creation. Override this on your own object templates!
134 if (typeof this.start == 'function') { this.start(); }
135 }
136
137 /*this.g_toHTML = function()
138 {
139 return '<object id="' + this.name + '" style="' +
140 'position: absolute;' +
141 'left:' + (this.transform.position.x - (this.transform.width / 2)) + 'px;' +
142 'top:' + (this.transform.position.y - (this.transform.height / 2)) + 'px;' +
143 'width:' + this.transform.width + 'px;' +
144 'height:' + this.transform.height + 'px;' +
145 (this.image != ''? 'background-image: url(' + this.image + ');' : 'border: 1px solid black;') +
146 'transform:rotate(' + this.transform.rotation + 'deg);' +
147 'background-color:' + this.color + ';' +
148 'background-repeat:no-repeat;' +
149 'background-size:100% 100%;' +
150 '">' + this.text + '</object>';
151 }*/
152
153 this.g_draw = function()
154 {
155 if(!this.g_destroy)
156 {
157 if(this.draw)
158 {
159 //<DOC> <function> object.draw()<br><br>Called on each draw step. If not overridden, default draw is used!
160 this.draw();
161 }
162 else
163 {
164 if(this.renderer.image)
165 this.renderer.draw(this.transform.position.x, this.transform.position.y, this.transform.width, this.transform.height, this.transform.rotation);
166 /*else //??? makes entire screen colored.
167 {
168 GameWindow.canvasContext.fillStyle = "rgb("+ this.transform.position.y+" ," + this.transform.position.x + ",0)"; // sets the color to fill in the rectangle with
169 GameWindow.canvasContext.fillRect( this.transform.position.x - this.transform.width / 2,
170 this.transform.position.y - this.transform.height / 2,
171 this.transform.width,
172 this.transform.height);
173 }*/
174 }
175 }
176 }
177
178 this.g_updateStep = function(deltaTime)
179 {
180 if(this.g_destroy)
181 {
182 Game.activeObjects.splice(Game.activeObjects.indexOf(this), 1);
183 delete this;
184 }
185 else
186 {
187 //<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!
188 if (typeof this.update == 'function') { this.update(deltaTime); }
189
190 this.transform.position.x += this.transform.velocity.x;
191 this.transform.position.y += this.transform.velocity.y;
192
193 if(!this.ignoreCollisions)
194 {
195 for(var i = 0; i < Game.activeObjects.length; i++)
196 {
197 if(Game.activeObjects[i] != this && !Game.activeObjects[i].ignoreCollisions)
198 {
199 var hit = false;
200 var o2 = Game.activeObjects[i];
201
202 //hw = halfWidth, hh = halfHeight
203 var hw = this.transform.width / 2;
204 var hh = this.transform.height / 2;
205
206 var ohw = o2.transform.width / 2;
207 var ohh = o2.transform.height / 2;
208
209 //CHECK FROM OBJ PERSPECTIVE
210 var p =
211 [
212 [this.transform.position.x - hw, this.transform.position.y - hh],
213 [this.transform.position.x + hw, this.transform.position.y - hh],
214 [this.transform.position.x - hw, this.transform.position.y + hh],
215 [this.transform.position.x + hw, this.transform.position.y + hh]
216 ];
217
218 for(var pi = 0; pi < p.length; pi++)
219 {
220 if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw)
221 {
222 if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh)
223 {
224 hit = true;
225 }
226 }
227 }
228
229 //CHECK FROM OTHER PERSPECTIVE
230 var p =
231 [
232 [o2.transform.position.x - ohw, o2.transform.position.y - ohh],
233 [o2.transform.position.x + ohw, o2.transform.position.y - ohh],
234 [o2.transform.position.x - ohw, o2.transform.position.y + ohh],
235 [o2.transform.position.x + ohw, o2.transform.position.y + ohh]
236 ];
237
238 for(var pi = 0; pi < p.length; pi++)
239 {
240 if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw)
241 {
242 if(p[pi][1] > this.transform.position.y - hh && p[pi][1] < this.transform.position.y + hh)
243 {
244 hit = true;
245 }
246 }
247 }
248
249 //<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!
250 if(hit)
251 if (typeof this.onCollision == 'function') { this.onCollision(o2); }
252 }
253 }
254 }
255 }
256 }
257
258 //<DOC> <function> object.destroy()<br><br>Destroys the current object at the end of the current update step.
259 this.destroy = function()
260 {
261 if(!this.g_destroy)
262 {
263 //<DOC> <function> object.onDestroy()<br><br>Called on object's destruction. Override this on your own object templates!
264 if (typeof this.onDestroy == 'function') { this.onDestroy(); }
265
266 this.g_destroy = true;
267 }
268 }
269
270 this.g_init();
271 }
272}