BlueGit

timfa frameworkv3

Initial Commit

timfa Tim Falken · pushed by timfa

Authored 2016-09-17 · 10 years ago

Hash
d075adffe3452490c56ceb4c23ffe0bbe7e9a0ca
44 files changed +1495 −0
.gitignore added +1 −0
@@ -0,0 +1 @@
1 /output
@@ -0,0 +1 @@
1/output
audio.js added +50 −0
@@ -0,0 +1,50 @@
1 //<DOC> category GameAudio
2
3 var GameAudio =
4 {
5 active: false,
6
7 //<DOC> <function> GameAudio.init(collection, containingFolder)<br><br>Starts the audio system, using an array of audio names 'collection' and a path 'containingFolder' in which the sounds can be found. Do not include file extentions in sound names!
8 init: function(collection, containingFolder)
9 {
10 console.log("GAME: Loading sounds. (Note: Only supported from online-source, will fail when executed on local files.)");
11
12 try
13 {
14 var soundColl = [];
15
16 for(var i = 0; i < collection.length; i++)
17 {
18 soundColl.push({name: collection[i], preload: true});
19 }
20
21 if(containingFolder == null)
22 containingFolder = "";
23
24 if(containingFolder != "")
25 containingFolder += "/";
26
27 ion.sound
28 ({
29 sounds: soundColl,
30 volume: 1,
31 preload: true,
32 multiplay: true,
33 path: containingFolder
34 });
35
36 this.active = true;
37 }
38 catch(err)
39 {
40 console.log("GAME: ERROR: Sounds could not be loaded. Sound will not be available.");
41 }
42 },
43
44 //<DOC> <function> GameAudio.play(name)<br><br>plays a sound with the name 'name'.
45 play: function(name)
46 {
47 if(this.active)
48 ion.sound.play(name);
49 }
50 }
@@ -0,0 +1,50 @@
1//<DOC> category GameAudio
2
3var GameAudio =
4{
5 active: false,
6
7 //<DOC> <function> GameAudio.init(collection, containingFolder)<br><br>Starts the audio system, using an array of audio names 'collection' and a path 'containingFolder' in which the sounds can be found. Do not include file extentions in sound names!
8 init: function(collection, containingFolder)
9 {
10 console.log("GAME: Loading sounds. (Note: Only supported from online-source, will fail when executed on local files.)");
11
12 try
13 {
14 var soundColl = [];
15
16 for(var i = 0; i < collection.length; i++)
17 {
18 soundColl.push({name: collection[i], preload: true});
19 }
20
21 if(containingFolder == null)
22 containingFolder = "";
23
24 if(containingFolder != "")
25 containingFolder += "/";
26
27 ion.sound
28 ({
29 sounds: soundColl,
30 volume: 1,
31 preload: true,
32 multiplay: true,
33 path: containingFolder
34 });
35
36 this.active = true;
37 }
38 catch(err)
39 {
40 console.log("GAME: ERROR: Sounds could not be loaded. Sound will not be available.");
41 }
42 },
43
44 //<DOC> <function> GameAudio.play(name)<br><br>plays a sound with the name 'name'.
45 play: function(name)
46 {
47 if(this.active)
48 ion.sound.play(name);
49 }
50}
build.bat added +6 −0
@@ -0,0 +1,6 @@
1 copy /b *.js output\framework.js
2
3 @echo off
4 For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
5 For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a:%%b)
6 echo %mydate% %mytime% >> output\lastbuild.txt
@@ -0,0 +1,6 @@
1copy /b *.js output\framework.js
2
3@echo off
4For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
5For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a:%%b)
6echo %mydate% %mytime% >> output\lastbuild.txt
ease.js added +368 −0
@@ -0,0 +1,368 @@
1 var ease = {
2
3 //
4 // Basic ease functions
5 //
6
7 easeConstant: function (time, begin, change, duration) {
8 "use strict";
9 return begin;
10 },
11
12 easeLinear: function (time, begin, change, duration) {
13 "use strict";
14 var val = time/duration;
15 return begin + change * val;
16 },
17
18 easeInPower: function (time, begin, change, duration, power) {
19 "use strict";
20 var val;
21 if (power === undefined) { power = 3; }
22 val = Math.pow(time/duration, power);
23 return begin + change * val;
24 },
25
26 easeOutPower: function (time, begin, change, duration, power) {
27 "use strict";
28 var val = tbEase.easeInPower(duration-time, 1, -1, duration, power);
29 return begin + change * val;
30 },
31
32 easeInOutPower: function (time, begin, change, duration, power) {
33 "use strict";
34 var val;
35 if (time < duration/2) {
36 val = tbEase.easeInPower(time, 0, 0.5, duration/2, power);
37 } else {
38 val = tbEase.easeInPower(duration-time, 1, -0.5, duration/2, power);
39 }
40 return begin + change * val;
41 },
42
43 easeOutInPower: function (time, begin, change, duration, power) {
44 "use strict";
45 var val;
46 if (time < duration/2) {
47 val = tbEase.easeInPower(duration/2-time, 0.5, -0.5, duration/2, power);
48 } else {
49 val = tbEase.easeInPower(time-duration/2, 0.5, 0.5, duration/2, power);
50 }
51 return begin + change * val;
52 },
53
54 easeInQuadratic: function (time, begin, change, duration) {
55 "use strict";
56 return tbEase.easeInPower(time, begin, change, duration, 2);
57 },
58
59 easeOutQuadratic: function (time, begin, change, duration) {
60 "use strict";
61 return tbEase.easeOutPower(time, begin, change, duration, 2);
62 },
63
64 easeInOutQuadratic: function (time, begin, change, duration) {
65 "use strict";
66 return tbEase.easeInOutPower(time, begin, change, duration, 2);
67 },
68
69 easeOutInQuadratic: function (time, begin, change, duration) {
70 "use strict";
71 return tbEase.easeOutInPower(time, begin, change, duration, 2);
72 },
73
74 easeInCubic: function (time, begin, change, duration) {
75 "use strict";
76 return tbEase.easeInPower(time, begin, change, duration, 3);
77 },
78
79 easeOutCubic: function (time, begin, change, duration) {
80 "use strict";
81 return tbEase.easeOutPower(time, begin, change, duration, 3);
82 },
83
84 easeInOutCubic: function (time, begin, change, duration) {
85 "use strict";
86 return tbEase.easeInOutPower(time, begin, change, duration, 3);
87 },
88
89 easeOutInCubic: function (time, begin, change, duration) {
90 "use strict";
91 return tbEase.easeOutInPower(time, begin, change, duration, 3);
92 },
93
94 easeInQuartic: function (time, begin, change, duration) {
95 "use strict";
96 return tbEase.easeInPower(time, begin, change, duration, 4);
97 },
98
99 easeOutQuartic: function (time, begin, change, duration) {
100 "use strict";
101 return tbEase.easeOutPower(time, begin, change, duration, 4);
102 },
103
104 easeInOutQuartic: function (time, begin, change, duration) {
105 "use strict";
106 return tbEase.easeInOutPower(time, begin, change, duration, 4);
107 },
108
109 easeOutInQuartic: function (time, begin, change, duration) {
110 "use strict";
111 return tbEase.easeOutInPower(time, begin, change, duration, 4);
112 },
113
114 easeInQuintic: function (time, begin, change, duration) {
115 "use strict";
116 return tbEase.easeInPower(time, begin, change, duration, 5);
117 },
118
119 easeOutQuintic: function (time, begin, change, duration) {
120 "use strict";
121 return tbEase.easeOutPower(time, begin, change, duration, 5);
122 },
123
124 easeInOutQuintic: function (time, begin, change, duration) {
125 "use strict";
126 return tbEase.easeInOutPower(time, begin, change, duration, 5);
127 },
128
129 easeOutInQuintic: function (time, begin, change, duration) {
130 "use strict";
131 return tbEase.easeOutInPower(time, begin, change, duration, 5);
132 },
133
134 easeInExponential: function (time, begin, change, duration, power) {
135 "use strict";
136 var val, base;
137 if (power === undefined) { power = 8; }
138 base = Math.pow(2, -power); // The exponential function will not cover the whole range from 0-1 so we must correct it
139 val = (Math.pow(2, power * time/duration - power) - base) / (1-base);
140 return begin + change * val;
141 },
142
143 easeOutExponential: function (time, begin, change, duration, power) {
144 "use strict";
145 var val = tbEase.easeInExponential(duration-time, 1, -1, duration, power);
146 return begin + change * val;
147 },
148
149 easeInOutExponential: function (time, begin, change, duration, power) {
150 "use strict";
151 var val;
152 if (time < duration/2) {
153 val = tbEase.easeInExponential(time, 0, 0.5, duration/2, power);
154 } else {
155 val = tbEase.easeInExponential(duration-time, 1, -0.5, duration/2, power);
156 }
157 return begin + change * val;
158 },
159
160 easeOutInExponential: function (time, begin, change, duration, power) {
161 "use strict";
162 var val;
163 if (time < duration/2) {
164 val = tbEase.easeInExponential(duration/2-time, 0.5, -0.5, duration/2, power);
165 } else {
166 val = tbEase.easeInExponential(time-duration/2, 0.5, 0.5, duration/2, power);
167 }
168 return begin + change * val;
169 },
170
171 easeInSine: function (time, begin, change, duration) {
172 "use strict";
173 var val = 1 - Math.cos(time/duration * Math.PI/2);
174 return begin + change * val;
175 },
176
177 easeOutSine: function (time, begin, change, duration) {
178 "use strict";
179 var val = tbEase.easeInSine(duration-time, 1, -1, duration);
180 return begin + change * val;
181 },
182
183 easeInOutSine: function (time, begin, change, duration) {
184 "use strict";
185 var val;
186 if (time < duration/2) {
187 val = tbEase.easeInSine(time, 0, 0.5, duration/2);
188 } else {
189 val = tbEase.easeInSine(duration-time, 1, -0.5, duration/2);
190 }
191 return begin + change * val;
192 },
193
194 easeOutInSine: function (time, begin, change, duration) {
195 "use strict";
196 var val;
197 if (time < duration/2) {
198 val = tbEase.easeInSine(duration/2-time, 0.5, -0.5, duration/2);
199 } else {
200 val = tbEase.easeInSine(time-duration/2, 0.5, 0.5, duration/2);
201 }
202 return begin + change * val;
203 },
204
205 easeInCircular: function (time, begin, change, duration) {
206 "use strict";
207 var val = 1 - Math.sqrt(1 - Math.pow(time/duration, 2));
208 return begin + change * val;
209 },
210
211 easeOutCircular: function (time, begin, change, duration) {
212 "use strict";
213 var val = tbEase.easeInCircular(duration-time, 1, -1, duration);
214 return begin + change * val;
215 },
216
217 easeInOutCircular: function (time, begin, change, duration) {
218 "use strict";
219 var val;
220 if (time < duration/2) {
221 val = tbEase.easeInCircular(time, 0, 0.5, duration/2);
222 } else {
223 val = tbEase.easeInCircular(duration-time, 1, -0.5, duration/2);
224 }
225 return begin + change * val;
226 },
227
228 easeOutInCircular: function (time, begin, change, duration) {
229 "use strict";
230 var val;
231 if (time < duration/2) {
232 val = tbEase.easeInCircular(duration/2-time, 0.5, -0.5, duration/2);
233 } else {
234 val = tbEase.easeInCircular(time-duration/2, 0.5, 0.5, duration/2);
235 }
236 return begin + change * val;
237 },
238
239 easeInElastic: function (time, begin, change, duration, oscillations, stiffness) {
240 "use strict";
241 var val;
242 if (oscillations === undefined) { oscillations = 3; }
243 if (stiffness === undefined) { stiffness = 8; }
244 val = Math.sin((1-time/duration)*2*Math.PI*oscillations + Math.PI/2); // Create the correct sign
245 val *= tbEase.easeInExponential(time, 0, 1, duration, stiffness); // Multiply with an exponential ease in
246 return begin + change * val;
247 },
248
249 easeOutElastic: function (time, begin, change, duration, oscillations, stiffness) {
250 "use strict";
251 var val = tbEase.easeInElastic(duration-time, 1, -1, duration, oscillations, stiffness);
252 return begin + change * val;
253 },
254
255 easeInOutElastic: function (time, begin, change, duration, oscillations, stiffness) {
256 "use strict";
257 var val;
258 if (time < duration/2) {
259 val = tbEase.easeInElastic(time, 0, 0.5, duration/2, oscillations, stiffness);
260 } else {
261 val = tbEase.easeInElastic(duration-time, 1, -0.5, duration/2, oscillations, stiffness);
262 }
263 return begin + change * val;
264 },
265
266 easeOutInElastic: function (time, begin, change, duration, oscillations, stiffness) {
267 "use strict";
268 var val;
269 if (time < duration/2) {
270 val = tbEase.easeInElastic(duration/2-time, 0.5, -0.5, duration/2, oscillations, stiffness);
271 } else {
272 val = tbEase.easeInElastic(time-duration/2, 0.5, 0.5, duration/2, oscillations, stiffness);
273 }
274 return begin + change * val;
275 },
276
277 easeInOvershoot: function (time, begin, change, duration, overshoot) {
278 "use strict";
279 var val;
280 if (overshoot === undefined) { overshoot = 1.70158; }
281 val = (1+overshoot) * Math.pow(time/duration, 3) - overshoot * Math.pow(time/duration, 2);
282 return begin + change * val;
283
284 },
285
286 easeOutOvershoot: function (time, begin, change, duration, overshoot) {
287 "use strict";
288 var val = tbEase.easeInOvershoot(duration-time, 1, -1, duration, overshoot);
289 return begin + change * val;
290 },
291
292 easeInOutOvershoot: function (time, begin, change, duration, overshoot) {
293 "use strict";
294 var val;
295 if (time < duration/2) {
296 val = tbEase.easeInOvershoot(time, 0, 0.5, duration/2, overshoot);
297 } else {
298 val = tbEase.easeInOvershoot(duration-time, 1, -0.5, duration/2, overshoot);
299 }
300 return begin + change * val;
301 },
302
303 easeOutInOvershoot: function (time, begin, change, duration, overshoot) {
304 "use strict";
305 var val;
306 if (time < duration/2) {
307 val = tbEase.easeInOvershoot(duration/2-time, 0.5, -0.5, duration/2, overshoot);
308 } else {
309 val = tbEase.easeInOvershoot(time-duration/2, 0.5, 0.5, duration/2, overshoot);
310 }
311 return begin + change * val;
312 },
313
314 easeInBounce: function (time, begin, change, duration, number, factor) {
315 "use strict";
316 var bounces, i, w, width, height, val;
317 // Correct the arguments
318 if (number === undefined) { number = 4; }
319 if (factor === undefined) { factor = 2; } else { factor = Math.sqrt(factor); }
320 // Create the list of bounces
321 bounces = [1];
322 width = 1;
323 for (i = 1; i < number; i += 1) {
324 bounces.push(bounces[i-1]*factor);
325 width += bounces[i];
326 }
327 // Compute total size
328 width -= bounces[number-1]/2; // We don't use the last half bounce
329 height = Math.pow(bounces[number-1],2);
330 time = time/duration * width;
331 // Find the correct bounce
332 w = 0;
333 for (i = 0; i < number; i += 1) {
334 if (time > bounces[i]) { time -= bounces[i];} else { w = bounces[i]; break; }
335 }
336 // Now compute the value
337 val = -4 * Math.pow(time-w/2,2) + w * w;
338 return begin + change * val / height;
339 },
340
341 easeOutBounce: function (time, begin, change, duration, number, factor) {
342 "use strict";
343 var val = tbEase.easeInBounce(duration-time, 1, -1, duration, number, factor);
344 return begin + change * val;
345 },
346
347 easeInOutBounce: function (time, begin, change, duration, number, factor) {
348 "use strict";
349 var val;
350 if (time < duration/2) {
351 val = tbEase.easeInBounce(time, 0, 0.5, duration/2, number, factor);
352 } else {
353 val = tbEase.easeInBounce(duration-time, 1, -0.5, duration/2, number, factor);
354 }
355 return begin + change * val;
356 },
357
358 easeOutInBounce: function (time, begin, change, duration, number, factor) {
359 "use strict";
360 var val;
361 if (time < duration/2) {
362 val = tbEase.easeInBounce(duration/2-time, 0.5, -0.5, duration/2, number, factor);
363 } else {
364 val = tbEase.easeInBounce(time-duration/2, 0.5, 0.5, duration/2, number, factor);
365 }
366 return begin + change * val;
367 }
368 };
@@ -0,0 +1,368 @@
1var ease = {
2
3//
4// Basic ease functions
5//
6
7 easeConstant: function (time, begin, change, duration) {
8 "use strict";
9 return begin;
10 },
11
12 easeLinear: function (time, begin, change, duration) {
13 "use strict";
14 var val = time/duration;
15 return begin + change * val;
16 },
17
18 easeInPower: function (time, begin, change, duration, power) {
19 "use strict";
20 var val;
21 if (power === undefined) { power = 3; }
22 val = Math.pow(time/duration, power);
23 return begin + change * val;
24 },
25
26 easeOutPower: function (time, begin, change, duration, power) {
27 "use strict";
28 var val = tbEase.easeInPower(duration-time, 1, -1, duration, power);
29 return begin + change * val;
30 },
31
32 easeInOutPower: function (time, begin, change, duration, power) {
33 "use strict";
34 var val;
35 if (time < duration/2) {
36 val = tbEase.easeInPower(time, 0, 0.5, duration/2, power);
37 } else {
38 val = tbEase.easeInPower(duration-time, 1, -0.5, duration/2, power);
39 }
40 return begin + change * val;
41 },
42
43 easeOutInPower: function (time, begin, change, duration, power) {
44 "use strict";
45 var val;
46 if (time < duration/2) {
47 val = tbEase.easeInPower(duration/2-time, 0.5, -0.5, duration/2, power);
48 } else {
49 val = tbEase.easeInPower(time-duration/2, 0.5, 0.5, duration/2, power);
50 }
51 return begin + change * val;
52 },
53
54 easeInQuadratic: function (time, begin, change, duration) {
55 "use strict";
56 return tbEase.easeInPower(time, begin, change, duration, 2);
57 },
58
59 easeOutQuadratic: function (time, begin, change, duration) {
60 "use strict";
61 return tbEase.easeOutPower(time, begin, change, duration, 2);
62 },
63
64 easeInOutQuadratic: function (time, begin, change, duration) {
65 "use strict";
66 return tbEase.easeInOutPower(time, begin, change, duration, 2);
67 },
68
69 easeOutInQuadratic: function (time, begin, change, duration) {
70 "use strict";
71 return tbEase.easeOutInPower(time, begin, change, duration, 2);
72 },
73
74 easeInCubic: function (time, begin, change, duration) {
75 "use strict";
76 return tbEase.easeInPower(time, begin, change, duration, 3);
77 },
78
79 easeOutCubic: function (time, begin, change, duration) {
80 "use strict";
81 return tbEase.easeOutPower(time, begin, change, duration, 3);
82 },
83
84 easeInOutCubic: function (time, begin, change, duration) {
85 "use strict";
86 return tbEase.easeInOutPower(time, begin, change, duration, 3);
87 },
88
89 easeOutInCubic: function (time, begin, change, duration) {
90 "use strict";
91 return tbEase.easeOutInPower(time, begin, change, duration, 3);
92 },
93
94 easeInQuartic: function (time, begin, change, duration) {
95 "use strict";
96 return tbEase.easeInPower(time, begin, change, duration, 4);
97 },
98
99 easeOutQuartic: function (time, begin, change, duration) {
100 "use strict";
101 return tbEase.easeOutPower(time, begin, change, duration, 4);
102 },
103
104 easeInOutQuartic: function (time, begin, change, duration) {
105 "use strict";
106 return tbEase.easeInOutPower(time, begin, change, duration, 4);
107 },
108
109 easeOutInQuartic: function (time, begin, change, duration) {
110 "use strict";
111 return tbEase.easeOutInPower(time, begin, change, duration, 4);
112 },
113
114 easeInQuintic: function (time, begin, change, duration) {
115 "use strict";
116 return tbEase.easeInPower(time, begin, change, duration, 5);
117 },
118
119 easeOutQuintic: function (time, begin, change, duration) {
120 "use strict";
121 return tbEase.easeOutPower(time, begin, change, duration, 5);
122 },
123
124 easeInOutQuintic: function (time, begin, change, duration) {
125 "use strict";
126 return tbEase.easeInOutPower(time, begin, change, duration, 5);
127 },
128
129 easeOutInQuintic: function (time, begin, change, duration) {
130 "use strict";
131 return tbEase.easeOutInPower(time, begin, change, duration, 5);
132 },
133
134 easeInExponential: function (time, begin, change, duration, power) {
135 "use strict";
136 var val, base;
137 if (power === undefined) { power = 8; }
138 base = Math.pow(2, -power); // The exponential function will not cover the whole range from 0-1 so we must correct it
139 val = (Math.pow(2, power * time/duration - power) - base) / (1-base);
140 return begin + change * val;
141 },
142
143 easeOutExponential: function (time, begin, change, duration, power) {
144 "use strict";
145 var val = tbEase.easeInExponential(duration-time, 1, -1, duration, power);
146 return begin + change * val;
147 },
148
149 easeInOutExponential: function (time, begin, change, duration, power) {
150 "use strict";
151 var val;
152 if (time < duration/2) {
153 val = tbEase.easeInExponential(time, 0, 0.5, duration/2, power);
154 } else {
155 val = tbEase.easeInExponential(duration-time, 1, -0.5, duration/2, power);
156 }
157 return begin + change * val;
158 },
159
160 easeOutInExponential: function (time, begin, change, duration, power) {
161 "use strict";
162 var val;
163 if (time < duration/2) {
164 val = tbEase.easeInExponential(duration/2-time, 0.5, -0.5, duration/2, power);
165 } else {
166 val = tbEase.easeInExponential(time-duration/2, 0.5, 0.5, duration/2, power);
167 }
168 return begin + change * val;
169 },
170
171 easeInSine: function (time, begin, change, duration) {
172 "use strict";
173 var val = 1 - Math.cos(time/duration * Math.PI/2);
174 return begin + change * val;
175 },
176
177 easeOutSine: function (time, begin, change, duration) {
178 "use strict";
179 var val = tbEase.easeInSine(duration-time, 1, -1, duration);
180 return begin + change * val;
181 },
182
183 easeInOutSine: function (time, begin, change, duration) {
184 "use strict";
185 var val;
186 if (time < duration/2) {
187 val = tbEase.easeInSine(time, 0, 0.5, duration/2);
188 } else {
189 val = tbEase.easeInSine(duration-time, 1, -0.5, duration/2);
190 }
191 return begin + change * val;
192 },
193
194 easeOutInSine: function (time, begin, change, duration) {
195 "use strict";
196 var val;
197 if (time < duration/2) {
198 val = tbEase.easeInSine(duration/2-time, 0.5, -0.5, duration/2);
199 } else {
200 val = tbEase.easeInSine(time-duration/2, 0.5, 0.5, duration/2);
201 }
202 return begin + change * val;
203 },
204
205 easeInCircular: function (time, begin, change, duration) {
206 "use strict";
207 var val = 1 - Math.sqrt(1 - Math.pow(time/duration, 2));
208 return begin + change * val;
209 },
210
211 easeOutCircular: function (time, begin, change, duration) {
212 "use strict";
213 var val = tbEase.easeInCircular(duration-time, 1, -1, duration);
214 return begin + change * val;
215 },
216
217 easeInOutCircular: function (time, begin, change, duration) {
218 "use strict";
219 var val;
220 if (time < duration/2) {
221 val = tbEase.easeInCircular(time, 0, 0.5, duration/2);
222 } else {
223 val = tbEase.easeInCircular(duration-time, 1, -0.5, duration/2);
224 }
225 return begin + change * val;
226 },
227
228 easeOutInCircular: function (time, begin, change, duration) {
229 "use strict";
230 var val;
231 if (time < duration/2) {
232 val = tbEase.easeInCircular(duration/2-time, 0.5, -0.5, duration/2);
233 } else {
234 val = tbEase.easeInCircular(time-duration/2, 0.5, 0.5, duration/2);
235 }
236 return begin + change * val;
237 },
238
239 easeInElastic: function (time, begin, change, duration, oscillations, stiffness) {
240 "use strict";
241 var val;
242 if (oscillations === undefined) { oscillations = 3; }
243 if (stiffness === undefined) { stiffness = 8; }
244 val = Math.sin((1-time/duration)*2*Math.PI*oscillations + Math.PI/2); // Create the correct sign
245 val *= tbEase.easeInExponential(time, 0, 1, duration, stiffness); // Multiply with an exponential ease in
246 return begin + change * val;
247 },
248
249 easeOutElastic: function (time, begin, change, duration, oscillations, stiffness) {
250 "use strict";
251 var val = tbEase.easeInElastic(duration-time, 1, -1, duration, oscillations, stiffness);
252 return begin + change * val;
253 },
254
255 easeInOutElastic: function (time, begin, change, duration, oscillations, stiffness) {
256 "use strict";
257 var val;
258 if (time < duration/2) {
259 val = tbEase.easeInElastic(time, 0, 0.5, duration/2, oscillations, stiffness);
260 } else {
261 val = tbEase.easeInElastic(duration-time, 1, -0.5, duration/2, oscillations, stiffness);
262 }
263 return begin + change * val;
264 },
265
266 easeOutInElastic: function (time, begin, change, duration, oscillations, stiffness) {
267 "use strict";
268 var val;
269 if (time < duration/2) {
270 val = tbEase.easeInElastic(duration/2-time, 0.5, -0.5, duration/2, oscillations, stiffness);
271 } else {
272 val = tbEase.easeInElastic(time-duration/2, 0.5, 0.5, duration/2, oscillations, stiffness);
273 }
274 return begin + change * val;
275 },
276
277 easeInOvershoot: function (time, begin, change, duration, overshoot) {
278 "use strict";
279 var val;
280 if (overshoot === undefined) { overshoot = 1.70158; }
281 val = (1+overshoot) * Math.pow(time/duration, 3) - overshoot * Math.pow(time/duration, 2);
282 return begin + change * val;
283
284 },
285
286 easeOutOvershoot: function (time, begin, change, duration, overshoot) {
287 "use strict";
288 var val = tbEase.easeInOvershoot(duration-time, 1, -1, duration, overshoot);
289 return begin + change * val;
290 },
291
292 easeInOutOvershoot: function (time, begin, change, duration, overshoot) {
293 "use strict";
294 var val;
295 if (time < duration/2) {
296 val = tbEase.easeInOvershoot(time, 0, 0.5, duration/2, overshoot);
297 } else {
298 val = tbEase.easeInOvershoot(duration-time, 1, -0.5, duration/2, overshoot);
299 }
300 return begin + change * val;
301 },
302
303 easeOutInOvershoot: function (time, begin, change, duration, overshoot) {
304 "use strict";
305 var val;
306 if (time < duration/2) {
307 val = tbEase.easeInOvershoot(duration/2-time, 0.5, -0.5, duration/2, overshoot);
308 } else {
309 val = tbEase.easeInOvershoot(time-duration/2, 0.5, 0.5, duration/2, overshoot);
310 }
311 return begin + change * val;
312 },
313
314 easeInBounce: function (time, begin, change, duration, number, factor) {
315 "use strict";
316 var bounces, i, w, width, height, val;
317 // Correct the arguments
318 if (number === undefined) { number = 4; }
319 if (factor === undefined) { factor = 2; } else { factor = Math.sqrt(factor); }
320 // Create the list of bounces
321 bounces = [1];
322 width = 1;
323 for (i = 1; i < number; i += 1) {
324 bounces.push(bounces[i-1]*factor);
325 width += bounces[i];
326 }
327 // Compute total size
328 width -= bounces[number-1]/2; // We don't use the last half bounce
329 height = Math.pow(bounces[number-1],2);
330 time = time/duration * width;
331 // Find the correct bounce
332 w = 0;
333 for (i = 0; i < number; i += 1) {
334 if (time > bounces[i]) { time -= bounces[i];} else { w = bounces[i]; break; }
335 }
336 // Now compute the value
337 val = -4 * Math.pow(time-w/2,2) + w * w;
338 return begin + change * val / height;
339 },
340
341 easeOutBounce: function (time, begin, change, duration, number, factor) {
342 "use strict";
343 var val = tbEase.easeInBounce(duration-time, 1, -1, duration, number, factor);
344 return begin + change * val;
345 },
346
347 easeInOutBounce: function (time, begin, change, duration, number, factor) {
348 "use strict";
349 var val;
350 if (time < duration/2) {
351 val = tbEase.easeInBounce(time, 0, 0.5, duration/2, number, factor);
352 } else {
353 val = tbEase.easeInBounce(duration-time, 1, -0.5, duration/2, number, factor);
354 }
355 return begin + change * val;
356 },
357
358 easeOutInBounce: function (time, begin, change, duration, number, factor) {
359 "use strict";
360 var val;
361 if (time < duration/2) {
362 val = tbEase.easeInBounce(duration/2-time, 0.5, -0.5, duration/2, number, factor);
363 } else {
364 val = tbEase.easeInBounce(time-duration/2, 0.5, 0.5, duration/2, number, factor);
365 }
366 return begin + change * val;
367 }
368};
engine.js added +178 −0
@@ -0,0 +1,178 @@
1 var baseURL = "http://www.timfalken.com/GameBuilder/v2";
2
3 var requiredFiles =
4 [
5 baseURL + "/vector.js",
6 baseURL + "/object.js",
7 baseURL + "/window.js",
8 baseURL + "/input.js",
9 baseURL + "/ion.sound.min.js",
10 baseURL + "/audio.js"
11 ];
12
13 var Engine =
14 {
15 x: 0,
16 y: 0,
17 w: 0,
18 h: 0,//stored for window-building
19
20 loadRequiredFiles: function(callback)
21 {
22 Engine.loadScripts(requiredFiles, callback);
23 },
24
25 /*
26 //function that can include an array of scripts one-by-one
27
28 Usage: Engine.loadScripts(scripts, callback)
29
30 scripts: Array of strings, each an URL to a .js file.
31 callback: A function to call when loading has finished.
32
33 Note: Function has an "index" parameter. This is for internal use and should be ignored.
34 */
35
36 //<DOC> category Engine
37 //<DOC> <function> Engine.loadScripts(scripts, callback)<br><br>Include an array 'scripts' from an external source. When finished, start the function 'callback'.
38 loadScripts: function(scripts, callback, index)
39 {
40 if(index == null)
41 {
42 index = 0;
43 console.log("GAME: Loading " + scripts.length + " scripts.");
44 }
45
46 console.log("GAME: Loading [" + scripts[index] + "]");
47
48 $.getScript(scripts[index], function() //jQuery required for convenience! It's either that, or manually loading each required file.
49 {
50 if(index <= scripts.length - 2)
51 Engine.loadScripts(scripts, callback, index + 1);
52 else
53 {
54 console.log("GAME: Scripts loaded.");
55 callback();
56 }
57 });
58 }
59 }
60 //<DOC> category Game
61 var Game =
62 {
63 stepPrevious: 0,
64
65 //<DOC> <parameter> Game.activeObjects<br><br>array containing all existing objects.
66 activeObjects: [],
67 userUpdateCallback: null,
68
69 //<DOC> <parameter> Game.stepsSinceStart<br><br> Total amount of update steps since the start of the game.
70 stepsSinceStart: 0,
71
72 //<DOC> <function> Game.initialize(windowX, windowY, windowWidth, windowHeight, fps, loadedCallback)<br><br>Starts the game, and creates a window at ('windowX', 'windowY'), using 'windowWidth' and 'windowHeight' as sizes for the game window in pixels. 'fps' controls the game's frames-per-second'. As soon as the game is loaded up, it'll call the function 'loadedCallback', so you can properly start the game.
73 initialize: function(windowX, windowY, windowWidth, windowHeight, fps, loadedCallback)
74 {
75 console.log("GAME: Initializing. Loading dependencies...");
76 Engine.x = windowX;
77 Engine.y = windowY;
78 Engine.w = windowWidth;
79 Engine.h = windowHeight;
80
81 Engine.loadRequiredFiles(function()
82 {
83 GameWindow.init(Engine.x, Engine.y, Engine.w, Engine.h);
84 GameInput.g_init();
85 window.setInterval(Game.gameStep, 1000 / fps);
86 loadedCallback();
87 });
88 },
89
90 //<DOC> <function> Game.setUpdateFunction(callback)<br><br>Assigns a function 'callback(deltaTime)' to be called every frame of the game. You can use this to run global code for your game.
91 setUpdateFunction: function(callback)
92 {
93 Game.userUpdateCallback = callback;
94 },
95
96 //<DOC> <function> Game.clearScene()<br><br>destroys every object in the scene.
97 clearScene: function()
98 {
99 for(var i = 0; i < Game.activeObjects.length; i++)
100 Game.activeObjects[i].destroy();
101 },
102
103 gameStep: function()
104 {
105 var stepTime = Date.now();
106
107 var stepTimeCurr = (stepTime - this.stepPrevious) * 0.001;
108
109 if(isNaN(stepTimeCurr))
110 stepTimeCurr = 0;
111
112 Game.stepsSinceStart++;
113
114 GameInput.g_updateKeys();
115
116 var toUpdate = Game.activeObjects.slice();
117
118 for(var i = 0; i < toUpdate.length; i++)
119 {
120 if(toUpdate[i].active)
121 toUpdate[i].g_updateStep(stepTimeCurr);
122 }
123
124 if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); }
125
126 GameWindow.redraw();
127
128 this.stepPrevious = stepTime;
129 }
130 };
131
132 //<DOC> <category> Image
133 //<DOC> <function> Constructor: new image(urlToImage, subImages = 1, subImagesPerRow = 1)
134 function image(url, subImgTotal = 1, perRow = 1)
135 {
136 //<DOC> <parameter> image.subImages<br><br>The total number of subimages in the image. Defaults to 1.
137 this.subImages = subImgTotal;
138 //<DOC> <parameter> image.subImagesPerRow<br><br>The total number of subimages per row. Defaults to 1.
139 this.subImagesPerRow = perRow;
140
141 $("body").append('<img id="image" src="' + url + '" style="display:none;"></img>');
142 this.g_image = $("#image")[0];
143 $("#image").attr("id","");
144
145 //<DOC> <function> image.draw(sourceX, sourceY, sourceWidth, destX, destY, destWidth, destHeight, rotation, alpha)<br><br>Draw the image on screen.
146 this.draw = function(sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha)
147 {
148 var shouldDraw = true;
149
150 if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
151 shouldDraw = false;
152
153 if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
154 shouldDraw = false;
155
156 if(dx > GameWindow.width + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
157 shouldDraw = false;
158
159 if(dy > GameWindow.height + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
160 shouldDraw = false;
161
162 if(shouldDraw)
163 {
164 var context = GameWindow.canvasContext;
165 context.save();
166 var oldAlpha = context.globalAlpha;
167 context.globalAlpha = alpha;
168
169 context.translate(dx, dy);
170 context.rotate(rotation * (Math.PI/180));
171
172 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
173
174 context.restore();
175 context.globalAlpha = oldAlpha;
176 }
177 }
178 }
@@ -0,0 +1,178 @@
1var baseURL = "http://www.timfalken.com/GameBuilder/v2";
2
3var requiredFiles =
4[
5 baseURL + "/vector.js",
6 baseURL + "/object.js",
7 baseURL + "/window.js",
8 baseURL + "/input.js",
9 baseURL + "/ion.sound.min.js",
10 baseURL + "/audio.js"
11];
12
13var Engine =
14{
15 x: 0,
16 y: 0,
17 w: 0,
18 h: 0,//stored for window-building
19
20 loadRequiredFiles: function(callback)
21 {
22 Engine.loadScripts(requiredFiles, callback);
23 },
24
25 /*
26 //function that can include an array of scripts one-by-one
27
28 Usage: Engine.loadScripts(scripts, callback)
29
30 scripts: Array of strings, each an URL to a .js file.
31 callback: A function to call when loading has finished.
32
33 Note: Function has an "index" parameter. This is for internal use and should be ignored.
34 */
35
36 //<DOC> category Engine
37 //<DOC> <function> Engine.loadScripts(scripts, callback)<br><br>Include an array 'scripts' from an external source. When finished, start the function 'callback'.
38 loadScripts: function(scripts, callback, index)
39 {
40 if(index == null)
41 {
42 index = 0;
43 console.log("GAME: Loading " + scripts.length + " scripts.");
44 }
45
46 console.log("GAME: Loading [" + scripts[index] + "]");
47
48 $.getScript(scripts[index], function() //jQuery required for convenience! It's either that, or manually loading each required file.
49 {
50 if(index <= scripts.length - 2)
51 Engine.loadScripts(scripts, callback, index + 1);
52 else
53 {
54 console.log("GAME: Scripts loaded.");
55 callback();
56 }
57 });
58 }
59}
60//<DOC> category Game
61var Game =
62{
63 stepPrevious: 0,
64
65 //<DOC> <parameter> Game.activeObjects<br><br>array containing all existing objects.
66 activeObjects: [],
67 userUpdateCallback: null,
68
69 //<DOC> <parameter> Game.stepsSinceStart<br><br> Total amount of update steps since the start of the game.
70 stepsSinceStart: 0,
71
72 //<DOC> <function> Game.initialize(windowX, windowY, windowWidth, windowHeight, fps, loadedCallback)<br><br>Starts the game, and creates a window at ('windowX', 'windowY'), using 'windowWidth' and 'windowHeight' as sizes for the game window in pixels. 'fps' controls the game's frames-per-second'. As soon as the game is loaded up, it'll call the function 'loadedCallback', so you can properly start the game.
73 initialize: function(windowX, windowY, windowWidth, windowHeight, fps, loadedCallback)
74 {
75 console.log("GAME: Initializing. Loading dependencies...");
76 Engine.x = windowX;
77 Engine.y = windowY;
78 Engine.w = windowWidth;
79 Engine.h = windowHeight;
80
81 Engine.loadRequiredFiles(function()
82 {
83 GameWindow.init(Engine.x, Engine.y, Engine.w, Engine.h);
84 GameInput.g_init();
85 window.setInterval(Game.gameStep, 1000 / fps);
86 loadedCallback();
87 });
88 },
89
90 //<DOC> <function> Game.setUpdateFunction(callback)<br><br>Assigns a function 'callback(deltaTime)' to be called every frame of the game. You can use this to run global code for your game.
91 setUpdateFunction: function(callback)
92 {
93 Game.userUpdateCallback = callback;
94 },
95
96 //<DOC> <function> Game.clearScene()<br><br>destroys every object in the scene.
97 clearScene: function()
98 {
99 for(var i = 0; i < Game.activeObjects.length; i++)
100 Game.activeObjects[i].destroy();
101 },
102
103 gameStep: function()
104 {
105 var stepTime = Date.now();
106
107 var stepTimeCurr = (stepTime - this.stepPrevious) * 0.001;
108
109 if(isNaN(stepTimeCurr))
110 stepTimeCurr = 0;
111
112 Game.stepsSinceStart++;
113
114 GameInput.g_updateKeys();
115
116 var toUpdate = Game.activeObjects.slice();
117
118 for(var i = 0; i < toUpdate.length; i++)
119 {
120 if(toUpdate[i].active)
121 toUpdate[i].g_updateStep(stepTimeCurr);
122 }
123
124 if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); }
125
126 GameWindow.redraw();
127
128 this.stepPrevious = stepTime;
129 }
130};
131
132//<DOC> <category> Image
133//<DOC> <function> Constructor: new image(urlToImage, subImages = 1, subImagesPerRow = 1)
134function image(url, subImgTotal = 1, perRow = 1)
135{
136 //<DOC> <parameter> image.subImages<br><br>The total number of subimages in the image. Defaults to 1.
137 this.subImages = subImgTotal;
138 //<DOC> <parameter> image.subImagesPerRow<br><br>The total number of subimages per row. Defaults to 1.
139 this.subImagesPerRow = perRow;
140
141 $("body").append('<img id="image" src="' + url + '" style="display:none;"></img>');
142 this.g_image = $("#image")[0];
143 $("#image").attr("id","");
144
145 //<DOC> <function> image.draw(sourceX, sourceY, sourceWidth, destX, destY, destWidth, destHeight, rotation, alpha)<br><br>Draw the image on screen.
146 this.draw = function(sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha)
147 {
148 var shouldDraw = true;
149
150 if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
151 shouldDraw = false;
152
153 if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
154 shouldDraw = false;
155
156 if(dx > GameWindow.width + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
157 shouldDraw = false;
158
159 if(dy > GameWindow.height + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
160 shouldDraw = false;
161
162 if(shouldDraw)
163 {
164 var context = GameWindow.canvasContext;
165 context.save();
166 var oldAlpha = context.globalAlpha;
167 context.globalAlpha = alpha;
168
169 context.translate(dx, dy);
170 context.rotate(rotation * (Math.PI/180));
171
172 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
173
174 context.restore();
175 context.globalAlpha = oldAlpha;
176 }
177 }
178}
examples/index.html added +21 −0
@@ -0,0 +1,21 @@
1 <html>
2 <head>
3 <link rel="stylesheet" href="style.css"/>
4 </head>
5
6 <body>
7 <section id="textBox">
8
9 <div class="exampleContainer">
10 <p class="exampleName"> <a href="template"> Empty Template </a> </p>
11 <p class="exampleDescription"> An empty template to start a project with! </p>
12 </div>
13
14 <div class="exampleContainer">
15 <p class="exampleName"> <a href="space"> Spaceship! </a> </p>
16 <p class="exampleDescription"> Fly around in space and shoot stuff. Infinite scrolling! </p>
17 </div>
18
19 </section>
20 </body>
21 </html>
@@ -0,0 +1,21 @@
1<html>
2 <head>
3 <link rel="stylesheet" href="style.css"/>
4 </head>
5
6 <body>
7 <section id="textBox">
8
9 <div class="exampleContainer">
10 <p class="exampleName"> <a href="template"> Empty Template </a> </p>
11 <p class="exampleDescription"> An empty template to start a project with! </p>
12 </div>
13
14 <div class="exampleContainer">
15 <p class="exampleName"> <a href="space"> Spaceship! </a> </p>
16 <p class="exampleDescription"> Fly around in space and shoot stuff. Infinite scrolling! </p>
17 </div>
18
19 </section>
20 </body>
21</html>
examples/space/images/bullet.gif added Binary file +0 −0

The contents of this binary file are not shown.

examples/space/images/enemy1.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/space/images/enemy2.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/space/images/enemy3.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/space/images/ship.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/space/images/star.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/space/images/timerPlaceholder.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/space/index.html added +17 −0
@@ -0,0 +1,17 @@
1 <html>
2
3 <head>
4 </head>
5
6 <body>
7 <script src="http://www.timfalken.com/jquery.js"></script> <!--jQuery, which is required by game engine -->
8 <script src="http://www.timfalken.com/GameBuilder/v2/engine.js"></script> <!-- the actual game engine to do cool stuff with -->
9 <script src="main.js"></script> <!-- Our own script, which will interact with the engine to make a game! -->
10
11 <div style="position: absolute; top: 90%;"> <!-- tiny block of text containing download link, you can delete these as I'm guessing you won't need them. -->
12 Arrow keys to move, space to shoot!<br>
13 <a href="space.zip" target="blank">Download this example</a>
14 </div>
15 </body>
16
17 </html>
@@ -0,0 +1,17 @@
1<html>
2
3 <head>
4 </head>
5
6 <body>
7 <script src="http://www.timfalken.com/jquery.js"></script> <!--jQuery, which is required by game engine -->
8 <script src="http://www.timfalken.com/GameBuilder/v2/engine.js"></script> <!-- the actual game engine to do cool stuff with -->
9 <script src="main.js"></script> <!-- Our own script, which will interact with the engine to make a game! -->
10
11 <div style="position: absolute; top: 90%;"> <!-- tiny block of text containing download link, you can delete these as I'm guessing you won't need them. -->
12 Arrow keys to move, space to shoot!<br>
13 <a href="space.zip" target="blank">Download this example</a>
14 </div>
15 </body>
16
17</html>
examples/space/main.js added +202 −0
@@ -0,0 +1,202 @@
1 $(init);
2
3 //load in images
4 var s_bullet = new image("images/bullet.gif");
5 var s_ship = new image("images/ship.png");
6 var s_star = new image("images/star.png");
7 var s_timer = new image("images/timerPlaceholder.png", 10, 10); //10 subimages, with 10 per row. (So just one row in this example.)
8
9 var soundsFolder = "sounds";
10 var sounds =
11 [
12 "shoot"
13 ]; //List of all sound files as strings (do not include file extension)
14
15 function init()
16 {
17 Game.initialize(20, 20, 500, 500, 60, gameStart); //Start game engine. Create a window at 20,20, width 500 and height 500, 60 frames per second. As soon as loading is done, it'll call gameStart().
18 }
19
20 function gameStart()
21 {
22 GameWindow.backgroundColor = {r:0, g:0, b:0};
23
24 Game.setUpdateFunction(update); //Let the game engine know we want to link our 'update()' to the game's update step.
25
26 GameAudio.init(sounds, soundsFolder);
27
28 GameObject.create(backgroundTemplate, 250, 250); //create the background object! (Big black block)
29
30 player = GameObject.create(player, 250, 250); //create the player ship at the center of the screen
31 GameObject.create(block, 300, 300); //create an empty block which does nothing
32
33 for(var i = 0; i < 20; i++) //create a bunch of stars!
34 GameObject.create(star, Math.random() * GameWindow.width, Math.random() * GameWindow.height);
35 }
36
37 function update()
38 {
39 for(var i = 0; i < Game.activeObjects.length; i++)//loop through all existing objects
40 {
41 if(Game.activeObjects[i].name != "player") //if the current object is not the player...
42 {
43 var obj = Game.activeObjects[i]; //put object reference into a variable
44 obj.transform.position = obj.transform.position.add(player.velocity.stretch(-1)); //move object according to player's velocity vector.
45
46 /*
47 * We're doing this to create an illusion of movement. By moving everything but the player,
48 * it will appear as if the player is moving, and the camera follows.
49 * Objects can still manipulate their position.
50 */
51
52 if(obj.transform.position.distanceTo(player.transform.position) > 1000) //If the object is really far away...
53 obj.destroy(); //just delete it. (If you're expanding this, make sure not to delete important objects!)
54 }
55 }
56 //You can add code here which will be executed every frame. (recommended to do this in individual objects whenever possible)
57 }
58
59 var block =
60 {
61 start: function()
62 {
63 this.renderer.image = s_timer;
64 },
65
66 update: function(deltaTime)
67 {
68 this.renderer.subImage += deltaTime;
69 }
70 }
71
72 var bullet =
73 {
74 start: function()
75 {
76 this.additionalSpeed = player.velocity.getCopy(); //Bullets copy the initial speed of the shooter. Like throwing an apple out of a moving car; it'll still be moving along with the car until it hits the ground and explodes. Waste of an apple, really.
77 this.renderer.image = s_bullet;
78 this.transform.width = 10;
79 this.transform.height = 30; //it's a bit stretched, but it'll look fun that way. Collision detection will be off, but there are no enemies here anyway so whatever.
80 },
81
82 update: function()
83 {
84 this.transform.position = this.transform.position.add( this.transform.forward().stretch(25) ).add(this.additionalSpeed); //Make bullet move forwards!
85 },
86
87 onCollision: function(other)
88 {
89 if(other.name != "player") //If i'm colliding with something that's not the player, delete myself.
90 this.destroy();
91 }
92 }
93
94 var star =
95 {
96 start: function()
97 {
98 this.ignoreCollisions = true; //Stars are background objects, so we don't want to check for collisions.
99 this.name = "star"; //It's a star, what else is there to say?
100 this.transform.width = 2; //tiny star.
101 this.transform.height = 2;
102 this.color = "white"; //You can change this, but most stars are white so I made these white too.
103 this.renderer.image = s_star;
104 },
105
106 update: function()
107 {
108 if(this.transform.position.x < 0 || this.transform.position.x > GameWindow.width || this.transform.position.y < 0 || this.transform.position.y > GameWindow.height)
109 this.reposition(); //If I'm at the border of the screen, or past it, reposition myself!
110 },
111
112 reposition: function()
113 {
114 //Put myself at a random spot at a screen border
115
116 if(Math.random() > 0.5) //Horizontal of vertical? Coin flip it is!
117 {
118 this.transform.position.x = (Math.random() > 0.5? 0 : GameWindow.width); //Another coinflip to decide which side to go to.
119 this.transform.position.y = Math.random() * GameWindow.width; //random point along the border.
120 }
121 else
122 {
123 this.transform.position.y = (Math.random() > 0.5? 0 : GameWindow.height); //Same as above, but on another axis.
124 this.transform.position.x = Math.random() * GameWindow.height;
125 }
126 },
127
128 onDestroy: function()
129 {
130 GameObject.create(star, -100, -100); //Stars should never be destroyed. If it is anyway, make a new one.
131 }
132 }
133
134 var player = //Player ship that flies around and shoots and stuff.
135 {
136 start: function()
137 {
138 this.cooldown = 30; //gun cooldown variable!
139 this.name = "player";
140 this.renderer.image = s_ship;
141 this.rotSpeed = 2; //how fast can we turn?
142 this.accelleration = 0.2; //Engine power!
143
144 this.gunOffset = 13; //This will help shoot the bullets from the wings instead of the center.
145
146 this.velocity = new vector(0, 0); //We start out stationary.
147 },
148
149 update: function()
150 {
151 if(GameInput.isHeld(37)) //Left key pressed? Turn left!
152 this.transform.rotation -= this.rotSpeed;
153
154 if(GameInput.isHeld(39)) //right key pressed? Turn right!
155 this.transform.rotation += this.rotSpeed;
156
157 if(GameInput.isHeld(38)) //Forward key held? Geronimo!
158 this.velocity = this.velocity.add(this.transform.forward().stretch(this.accelleration));
159
160 if(GameInput.isHeld(40)) //Backwards key held? Retreat!
161 this.velocity = this.velocity.add(this.transform.back().stretch(this.accelleration));
162
163 this.cooldown--; //let gun cooldown timer run
164
165 if(GameInput.isHeld(32)) //fire key held?
166 {
167 if(this.cooldown < 0) //cooldown timer expired?
168 {
169 GameAudio.play("shoot"); //Pew pew pew
170
171 this.gunOffset *= -1; //reverse gun offset to alternate the bullet origin
172 var creationPos = this.transform.position.add(this.transform.forward().stretch(1).add(this.transform.right().stretch(this.gunOffset))); //determine position at which the bullet will be created.
173 var newBullet = GameObject.create(bullet, creationPos.x, creationPos.y); //make a new bullet
174 newBullet.transform.rotation = this.transform.rotation; //rotate the bullet
175
176 newBullet.transform.rotation += -5 + (Math.random() * 10); //wiggle the bullet for a bit so we're not insanely accurate. Looks cooler.
177
178 this.cooldown = 5; //set the cooldown to some number to delay the next shot.
179
180 this.velocity = this.velocity.add(this.transform.back().stretch(this.accelleration * 0.1)); //Knockback in space!
181 }
182 }
183 }
184 }
185
186 var backgroundTemplate = //Big block which sticks to screen center!
187 {
188 start: function()
189 {
190 this.ignoreCollisions = true;
191 this.name = "background";
192 this.color = "black";
193 this.transform.width = 1000;
194 this.transform.height = 1000;
195 },
196
197 update: function()
198 {
199 this.transform.position.x = 250;
200 this.transform.position.y = 250;
201 }
202 }
@@ -0,0 +1,202 @@
1$(init);
2
3//load in images
4var s_bullet = new image("images/bullet.gif");
5var s_ship = new image("images/ship.png");
6var s_star = new image("images/star.png");
7var s_timer = new image("images/timerPlaceholder.png", 10, 10); //10 subimages, with 10 per row. (So just one row in this example.)
8
9var soundsFolder = "sounds";
10var sounds =
11[
12 "shoot"
13]; //List of all sound files as strings (do not include file extension)
14
15function init()
16{
17 Game.initialize(20, 20, 500, 500, 60, gameStart); //Start game engine. Create a window at 20,20, width 500 and height 500, 60 frames per second. As soon as loading is done, it'll call gameStart().
18}
19
20function gameStart()
21{
22 GameWindow.backgroundColor = {r:0, g:0, b:0};
23
24 Game.setUpdateFunction(update); //Let the game engine know we want to link our 'update()' to the game's update step.
25
26 GameAudio.init(sounds, soundsFolder);
27
28 GameObject.create(backgroundTemplate, 250, 250); //create the background object! (Big black block)
29
30 player = GameObject.create(player, 250, 250); //create the player ship at the center of the screen
31 GameObject.create(block, 300, 300); //create an empty block which does nothing
32
33 for(var i = 0; i < 20; i++) //create a bunch of stars!
34 GameObject.create(star, Math.random() * GameWindow.width, Math.random() * GameWindow.height);
35}
36
37function update()
38{
39 for(var i = 0; i < Game.activeObjects.length; i++)//loop through all existing objects
40 {
41 if(Game.activeObjects[i].name != "player") //if the current object is not the player...
42 {
43 var obj = Game.activeObjects[i]; //put object reference into a variable
44 obj.transform.position = obj.transform.position.add(player.velocity.stretch(-1)); //move object according to player's velocity vector.
45
46 /*
47 * We're doing this to create an illusion of movement. By moving everything but the player,
48 * it will appear as if the player is moving, and the camera follows.
49 * Objects can still manipulate their position.
50 */
51
52 if(obj.transform.position.distanceTo(player.transform.position) > 1000) //If the object is really far away...
53 obj.destroy(); //just delete it. (If you're expanding this, make sure not to delete important objects!)
54 }
55 }
56 //You can add code here which will be executed every frame. (recommended to do this in individual objects whenever possible)
57}
58
59var block =
60{
61 start: function()
62 {
63 this.renderer.image = s_timer;
64 },
65
66 update: function(deltaTime)
67 {
68 this.renderer.subImage += deltaTime;
69 }
70}
71
72var bullet =
73{
74 start: function()
75 {
76 this.additionalSpeed = player.velocity.getCopy(); //Bullets copy the initial speed of the shooter. Like throwing an apple out of a moving car; it'll still be moving along with the car until it hits the ground and explodes. Waste of an apple, really.
77 this.renderer.image = s_bullet;
78 this.transform.width = 10;
79 this.transform.height = 30; //it's a bit stretched, but it'll look fun that way. Collision detection will be off, but there are no enemies here anyway so whatever.
80 },
81
82 update: function()
83 {
84 this.transform.position = this.transform.position.add( this.transform.forward().stretch(25) ).add(this.additionalSpeed); //Make bullet move forwards!
85 },
86
87 onCollision: function(other)
88 {
89 if(other.name != "player") //If i'm colliding with something that's not the player, delete myself.
90 this.destroy();
91 }
92}
93
94var star =
95{
96 start: function()
97 {
98 this.ignoreCollisions = true; //Stars are background objects, so we don't want to check for collisions.
99 this.name = "star"; //It's a star, what else is there to say?
100 this.transform.width = 2; //tiny star.
101 this.transform.height = 2;
102 this.color = "white"; //You can change this, but most stars are white so I made these white too.
103 this.renderer.image = s_star;
104 },
105
106 update: function()
107 {
108 if(this.transform.position.x < 0 || this.transform.position.x > GameWindow.width || this.transform.position.y < 0 || this.transform.position.y > GameWindow.height)
109 this.reposition(); //If I'm at the border of the screen, or past it, reposition myself!
110 },
111
112 reposition: function()
113 {
114 //Put myself at a random spot at a screen border
115
116 if(Math.random() > 0.5) //Horizontal of vertical? Coin flip it is!
117 {
118 this.transform.position.x = (Math.random() > 0.5? 0 : GameWindow.width); //Another coinflip to decide which side to go to.
119 this.transform.position.y = Math.random() * GameWindow.width; //random point along the border.
120 }
121 else
122 {
123 this.transform.position.y = (Math.random() > 0.5? 0 : GameWindow.height); //Same as above, but on another axis.
124 this.transform.position.x = Math.random() * GameWindow.height;
125 }
126 },
127
128 onDestroy: function()
129 {
130 GameObject.create(star, -100, -100); //Stars should never be destroyed. If it is anyway, make a new one.
131 }
132}
133
134var player = //Player ship that flies around and shoots and stuff.
135{
136 start: function()
137 {
138 this.cooldown = 30; //gun cooldown variable!
139 this.name = "player";
140 this.renderer.image = s_ship;
141 this.rotSpeed = 2; //how fast can we turn?
142 this.accelleration = 0.2; //Engine power!
143
144 this.gunOffset = 13; //This will help shoot the bullets from the wings instead of the center.
145
146 this.velocity = new vector(0, 0); //We start out stationary.
147 },
148
149 update: function()
150 {
151 if(GameInput.isHeld(37)) //Left key pressed? Turn left!
152 this.transform.rotation -= this.rotSpeed;
153
154 if(GameInput.isHeld(39)) //right key pressed? Turn right!
155 this.transform.rotation += this.rotSpeed;
156
157 if(GameInput.isHeld(38)) //Forward key held? Geronimo!
158 this.velocity = this.velocity.add(this.transform.forward().stretch(this.accelleration));
159
160 if(GameInput.isHeld(40)) //Backwards key held? Retreat!
161 this.velocity = this.velocity.add(this.transform.back().stretch(this.accelleration));
162
163 this.cooldown--; //let gun cooldown timer run
164
165 if(GameInput.isHeld(32)) //fire key held?
166 {
167 if(this.cooldown < 0) //cooldown timer expired?
168 {
169 GameAudio.play("shoot"); //Pew pew pew
170
171 this.gunOffset *= -1; //reverse gun offset to alternate the bullet origin
172 var creationPos = this.transform.position.add(this.transform.forward().stretch(1).add(this.transform.right().stretch(this.gunOffset))); //determine position at which the bullet will be created.
173 var newBullet = GameObject.create(bullet, creationPos.x, creationPos.y); //make a new bullet
174 newBullet.transform.rotation = this.transform.rotation; //rotate the bullet
175
176 newBullet.transform.rotation += -5 + (Math.random() * 10); //wiggle the bullet for a bit so we're not insanely accurate. Looks cooler.
177
178 this.cooldown = 5; //set the cooldown to some number to delay the next shot.
179
180 this.velocity = this.velocity.add(this.transform.back().stretch(this.accelleration * 0.1)); //Knockback in space!
181 }
182 }
183 }
184}
185
186var backgroundTemplate = //Big block which sticks to screen center!
187{
188 start: function()
189 {
190 this.ignoreCollisions = true;
191 this.name = "background";
192 this.color = "black";
193 this.transform.width = 1000;
194 this.transform.height = 1000;
195 },
196
197 update: function()
198 {
199 this.transform.position.x = 250;
200 this.transform.position.y = 250;
201 }
202}
examples/space/sounds/shoot.mp3 added Binary file +0 −0

The contents of this binary file are not shown.

examples/space/space.zip added Binary file +0 −0

The contents of this binary file are not shown.

examples/style.css added +24 −0
@@ -0,0 +1,24 @@
1 #textBox
2 {
3 align-content: center;
4 position: absolute;
5 }
6
7 .exampleName
8 {
9 border-bottom: 1px dotted black;
10 font-size: 30;
11 padding: 10px;
12 }
13
14 .exampleDescription
15 {
16 padding: 10px;
17 }
18
19 .exampleContainer
20 {
21 border: 1px solid black;
22 float: left;
23 margin: 5px;
24 }
@@ -0,0 +1,24 @@
1#textBox
2{
3 align-content: center;
4 position: absolute;
5}
6
7.exampleName
8{
9 border-bottom: 1px dotted black;
10 font-size: 30;
11 padding: 10px;
12}
13
14.exampleDescription
15{
16 padding: 10px;
17}
18
19.exampleContainer
20{
21 border: 1px solid black;
22 float: left;
23 margin: 5px;
24}
examples/template/images/bullet.gif added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/images/bullet.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/images/enemy1.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/images/enemy2.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/images/enemy3.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/images/ship.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/images/star.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/images/target1.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/images/target2.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/images/target3.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/images/timerPlaceholder.png added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/index.html added +17 −0
@@ -0,0 +1,17 @@
1 <html>
2
3 <head>
4 </head>
5
6 <body>
7 <script src="http://www.timfalken.com/jquery.js"></script> <!--jQuery, which is required by game engine -->
8 <script src="http://www.timfalken.com/GameBuilder/v2/engine.js"></script> <!-- the actual game engine to do cool stuff with -->
9 <script src="main.js"></script> <!-- Our own script, which will interact with the engine to make a game! -->
10
11 <div style="position: absolute; top: 90%;"> <!-- tiny block of text containing download link, you can delete these as I'm guessing you won't need them. -->
12 Arrow keys to move, space to shoot!<br>
13 <a href="space.zip" target="blank">Download this example</a>
14 </div>
15 </body>
16
17 </html>
@@ -0,0 +1,17 @@
1<html>
2
3 <head>
4 </head>
5
6 <body>
7 <script src="http://www.timfalken.com/jquery.js"></script> <!--jQuery, which is required by game engine -->
8 <script src="http://www.timfalken.com/GameBuilder/v2/engine.js"></script> <!-- the actual game engine to do cool stuff with -->
9 <script src="main.js"></script> <!-- Our own script, which will interact with the engine to make a game! -->
10
11 <div style="position: absolute; top: 90%;"> <!-- tiny block of text containing download link, you can delete these as I'm guessing you won't need them. -->
12 Arrow keys to move, space to shoot!<br>
13 <a href="space.zip" target="blank">Download this example</a>
14 </div>
15 </body>
16
17</html>
examples/template/main.js added +46 −0
@@ -0,0 +1,46 @@
1 $(init);
2
3 //load in images
4 var s_timer = new image("images/timerPlaceholder.png", 10, 10); //10 subimages, with 10 per row. (So just one row in this example.)
5
6 var soundsFolder = "sounds";
7 var sounds =
8 [
9 "shoot"
10 ]; //List of all sound files as strings (do not include file extension)
11
12 function init()
13 {
14 Game.initialize(20, 20, 500, 500, 60, gameStart); //Start game engine. Create a window at 20,20, width 500 and height 500, 60 frames per second. As soon as loading is done, it'll call gameStart().
15 }
16
17 function gameStart()
18 {
19 GameWindow.backgroundColor = {r:0, g:0, b:0};
20
21 Game.setUpdateFunction(update); //Let the game engine know we want to link our 'update()' to the game's update step.
22
23 GameAudio.init(sounds, soundsFolder);
24
25 GameObject.create(block, 300, 300); //create an empty block which does nothing
26 }
27
28 function update()
29 {
30 //You can add code here which will be executed every frame. (recommended to do this in individual objects whenever possible)
31 }
32
33 var block =
34 {
35 start: function()
36 {
37 this.renderer.image = s_timer;
38 this.renderer.image.subImages = 10;
39 this.renderer.image.subImagesPerRow = 10;
40 },
41
42 update: function(deltaTime)
43 {
44 this.renderer.subImage += deltaTime;
45 }
46 }
@@ -0,0 +1,46 @@
1$(init);
2
3//load in images
4var s_timer = new image("images/timerPlaceholder.png", 10, 10); //10 subimages, with 10 per row. (So just one row in this example.)
5
6var soundsFolder = "sounds";
7var sounds =
8[
9 "shoot"
10]; //List of all sound files as strings (do not include file extension)
11
12function init()
13{
14 Game.initialize(20, 20, 500, 500, 60, gameStart); //Start game engine. Create a window at 20,20, width 500 and height 500, 60 frames per second. As soon as loading is done, it'll call gameStart().
15}
16
17function gameStart()
18{
19 GameWindow.backgroundColor = {r:0, g:0, b:0};
20
21 Game.setUpdateFunction(update); //Let the game engine know we want to link our 'update()' to the game's update step.
22
23 GameAudio.init(sounds, soundsFolder);
24
25 GameObject.create(block, 300, 300); //create an empty block which does nothing
26}
27
28function update()
29{
30 //You can add code here which will be executed every frame. (recommended to do this in individual objects whenever possible)
31}
32
33var block =
34{
35 start: function()
36 {
37 this.renderer.image = s_timer;
38 this.renderer.image.subImages = 10;
39 this.renderer.image.subImagesPerRow = 10;
40 },
41
42 update: function(deltaTime)
43 {
44 this.renderer.subImage += deltaTime;
45 }
46}
examples/template/sounds/About these sounds.txt added +2 −0
@@ -0,0 +1,2 @@
1 Sounds originated from ion.sound 3.0.0 and were included in the downloadable bundle, which is also used to produce sound in your games!
2 Find it here -> http://ionden.com/a/plugins/ion.sound/en.html
@@ -0,0 +1,2 @@
1Sounds originated from ion.sound 3.0.0 and were included in the downloadable bundle, which is also used to produce sound in your games!
2Find it here -> http://ionden.com/a/plugins/ion.sound/en.html
examples/template/sounds/cd_tray.mp3 added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/sounds/metal_plate.mp3 added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/sounds/shoot.mp3 added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/sounds/snap.mp3 added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/sounds/water_droplet.mp3 added Binary file +0 −0

The contents of this binary file are not shown.

examples/template/template.zip added Binary file +0 −0

The contents of this binary file are not shown.

image.js added +35 −0
@@ -0,0 +1,35 @@
1 //<DOC> category Image. Create using: new image(urlToImage)
2
3 function image(url)
4 {
5 //<DOC> <parameter> image.subImages<br><br>The total number of subimages in the image. Defaults to 1.
6 this.subImages = 1;
7 //<DOC> <parameter> image.subImagesPerRow<br><br>The total number of subimages per row. Defaults to 1.
8 this.subImagesPerRow = 1;
9
10 $("body").append('<img id="image" src="' + url + '" style="display:none;"></img>');
11 this.g_image = $("#image")[0];
12 $("#image").attr("id","");
13
14 //<DOC> <function> image.draw(subImg, x, y, w, h, rotation)<br><br>Draw an image at (x,y), size (w,h), at an angle in degrees.
15 this.draw = function(subImg, x, y, w, h, rotation)
16 {
17 subImg %= this.subImages;
18 var context = GameWindow.canvasContext;
19 context.save();
20
21 context.translate(x, y);
22 context.rotate(rotation * (Math.PI/180));
23
24 var c = subImg % this.subImagesPerRow;
25 var r = Math.ceil(subImg / this.subImagesPerRow);
26 var sw = (this.g_image.width / this.subImagesPerRow);
27 var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));
28 var sx = sw * c;
29 var sy = sh * r;
30
31 context.drawImage(this.g_image, sx, sy, sw, sh, -w/2, -h/2, w, h);
32
33 context.restore();
34 }
35 }
@@ -0,0 +1,35 @@
1//<DOC> category Image. Create using: new image(urlToImage)
2
3function image(url)
4{
5 //<DOC> <parameter> image.subImages<br><br>The total number of subimages in the image. Defaults to 1.
6 this.subImages = 1;
7 //<DOC> <parameter> image.subImagesPerRow<br><br>The total number of subimages per row. Defaults to 1.
8 this.subImagesPerRow = 1;
9
10 $("body").append('<img id="image" src="' + url + '" style="display:none;"></img>');
11 this.g_image = $("#image")[0];
12 $("#image").attr("id","");
13
14 //<DOC> <function> image.draw(subImg, x, y, w, h, rotation)<br><br>Draw an image at (x,y), size (w,h), at an angle in degrees.
15 this.draw = function(subImg, x, y, w, h, rotation)
16 {
17 subImg %= this.subImages;
18 var context = GameWindow.canvasContext;
19 context.save();
20
21 context.translate(x, y);
22 context.rotate(rotation * (Math.PI/180));
23
24 var c = subImg % this.subImagesPerRow;
25 var r = Math.ceil(subImg / this.subImagesPerRow);
26 var sw = (this.g_image.width / this.subImagesPerRow);
27 var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));
28 var sx = sw * c;
29 var sy = sh * r;
30
31 context.drawImage(this.g_image, sx, sy, sw, sh, -w/2, -h/2, w, h);
32
33 context.restore();
34 }
35}
input.js added +53 −0
@@ -0,0 +1,53 @@
1 //<DOC> category GameInput
2
3 var GameInput =
4 {
5 g_keyStatus: [],
6 g_pKeyStatus: [],
7
8 g_pressKeyStatus: [],
9 g_ppressKeyStatus: [],
10
11 g_pressBlacklist: [],
12
13 g_init: function()
14 {
15 document.onkeydown = GameInput.g_setKeyStatus;
16 document.onkeyup = GameInput.g_setKeyStatus;
17 },
18
19 g_setKeyStatus: function(e)
20 {
21 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
22
23 if(!GameInput.g_pressBlacklist[e.which])
24 GameInput.g_pressKeyStatus[e.which] = (e.type == "keydown");
25
26 GameInput.g_pressBlacklist[e.which] = (e.type == "keydown"); //only allow single presses if key has been released once
27 },
28
29 g_updateKeys: function()
30 {
31 GameInput.g_pKeyStatus = GameInput.g_keyStatus;
32 GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;
33 GameInput.g_pressKeyStatus = [];
34 },
35
36 //<DOC> <function> GameInput.IsPressed(keyCode)<br><br>Returns true if the key with given code 'keyCode' was pressed this frame. Only returns true the frame the key is pressed once until the key is released and pressed again.
37 isPressed: function(keyCode)
38 {
39 if(GameInput.g_ppressKeyStatus[keyCode] == null)
40 return false;
41 else
42 return GameInput.g_ppressKeyStatus[keyCode];
43 },
44
45 //<DOC> <function> GameInput.isHeld(keyCode)<br><br>returns true if the key with given code 'keyCode' is currently pressed.
46 isHeld: function(keyCode)
47 {
48 if(GameInput.g_pKeyStatus[keyCode] == null)
49 return false;
50 else
51 return GameInput.g_pKeyStatus[keyCode];
52 }
53 }
@@ -0,0 +1,53 @@
1//<DOC> category GameInput
2
3var GameInput =
4{
5 g_keyStatus: [],
6 g_pKeyStatus: [],
7
8 g_pressKeyStatus: [],
9 g_ppressKeyStatus: [],
10
11 g_pressBlacklist: [],
12
13 g_init: function()
14 {
15 document.onkeydown = GameInput.g_setKeyStatus;
16 document.onkeyup = GameInput.g_setKeyStatus;
17 },
18
19 g_setKeyStatus: function(e)
20 {
21 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
22
23 if(!GameInput.g_pressBlacklist[e.which])
24 GameInput.g_pressKeyStatus[e.which] = (e.type == "keydown");
25
26 GameInput.g_pressBlacklist[e.which] = (e.type == "keydown"); //only allow single presses if key has been released once
27 },
28
29 g_updateKeys: function()
30 {
31 GameInput.g_pKeyStatus = GameInput.g_keyStatus;
32 GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;
33 GameInput.g_pressKeyStatus = [];
34 },
35
36 //<DOC> <function> GameInput.IsPressed(keyCode)<br><br>Returns true if the key with given code 'keyCode' was pressed this frame. Only returns true the frame the key is pressed once until the key is released and pressed again.
37 isPressed: function(keyCode)
38 {
39 if(GameInput.g_ppressKeyStatus[keyCode] == null)
40 return false;
41 else
42 return GameInput.g_ppressKeyStatus[keyCode];
43 },
44
45 //<DOC> <function> GameInput.isHeld(keyCode)<br><br>returns true if the key with given code 'keyCode' is currently pressed.
46 isHeld: function(keyCode)
47 {
48 if(GameInput.g_pKeyStatus[keyCode] == null)
49 return false;
50 else
51 return GameInput.g_pKeyStatus[keyCode];
52 }
53}
ion.sound.min.js added +39 −0
@@ -0,0 +1,39 @@
1 // Ion.Sound | version 3.0.0 | https://github.com/IonDen/ion.sound
2 /*
3 MIT licence
4 Copyright (C) 2014 by Denis Ineshin
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
14 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 */
16
17 (function(l,e,n,r){l.ion=l.ion||{};if(!ion.sound){var m=function(a){a||(a="undefined");if(l.console){console.warn&&"function"===typeof console.warn?console.warn(a):console.log&&"function"===typeof console.log&&console.log(a);var g=n("#debug");if(g.length){var b=g.html();g.html(b+a+"<br/>")}}},f=function(a,b){var c;b=b||{};for(c in a)a.hasOwnProperty(c)&&(b[c]=a[c]);return b};if("function"!==typeof Audio&&"object"!==typeof Audio)e=function(){m("HTML5 Audio is not supported in this browser")},ion.sound=
18 e,ion.sound.play=e,ion.sound.stop=e,ion.sound.pause=e,ion.sound.preload=e,ion.sound.destroy=e,e();else{e=/iPad|iPhone|iPod/.test(e.appVersion);var q=0,c={},d={},b;!c.supported&&e?c.supported=["mp3","mp4","aac"]:c.supported||(c.supported=["mp3","ogg","mp4","aac","wav"]);ion.sound=function(a){f(a,c);c.path=c.path||"";c.volume=c.volume||1;c.preload=c.preload||!1;c.multiplay=c.multiplay||!1;c.loop=c.loop||!1;c.sprite=c.sprite||null;c.scope=c.scope||null;c.ready_callback=c.ready_callback||null;c.ended_callback=
19 c.ended_callback||null;if(q=c.sounds.length)for(b=0;b<q;b++){if(!d[b]){a=c.sounds[b];var g=a.alias||a.name;d[g]||(d[g]=new p(a),d[g].init())}}else m("No sound-files provided!")};ion.sound.VERSION="3.0.0";ion.sound._method=function(a,c,e){if(c)d[c]&&d[c][a](e);else for(b in d)if(d.hasOwnProperty(b))d[b][a](e)};ion.sound.preload=function(a,b){b=b||{};f({preload:!0},b);ion.sound._method("init",a,b)};ion.sound.destroy=function(a){ion.sound._method("destroy",a);if(a)d[a]=null;else for(b in d)d.hasOwnProperty(b)&&
20 d[b]&&(d[b]=null)};ion.sound.play=function(a,b){ion.sound._method("play",a,b)};ion.sound.stop=function(a,b){ion.sound._method("stop",a,b)};ion.sound.pause=function(a,b){ion.sound._method("pause",a,b)};n&&(n.ionSound=ion.sound);e=l.AudioContext||l.webkitAudioContext;var h;e&&(h=new e);var p=function(a){this.options=f(c);delete this.options.sounds;f(a,this.options);this.request=null;this.streams={};this.result={};this.ext=0;this.url="";this.autoplay=this.no_file=this.decoded=this.loaded=!1};p.prototype=
21 {init:function(a){a&&f(a,this.options);this.options.preload&&this.load()},destroy:function(){var a;for(b in this.streams)(a=this.streams[b])&&a.destroy();this.streams={};this.result=null;this.options=this.options.buffer=null;this.request&&(this.request.removeEventListener("load",this.ready.bind(this),!1),this.request.removeEventListener("error",this.error.bind(this),!1),this.request.abort(),this.request=null)},createUrl:function(){this.url=this.options.path+this.options.name+"."+this.options.supported[this.ext]+
22 "?"+(new Date).valueOf()},load:function(){this.no_file?m('No sources for "'+this.options.name+'" sound :('):(this.createUrl(),this.request=new XMLHttpRequest,this.request.open("GET",this.url,!0),this.request.responseType="arraybuffer",this.request.addEventListener("load",this.ready.bind(this),!1),this.request.addEventListener("error",this.error.bind(this),!1),this.request.send())},reload:function(){this.ext++;this.options.supported[this.ext]?this.load():(this.no_file=!0,m('No sources for "'+this.options.name+
23 '" sound :('))},ready:function(a){this.result=a.target;4!==this.result.readyState?this.reload():200!==this.result.status?(m(this.url+" was not found on server!"),this.reload()):(this.request.removeEventListener("load",this.ready.bind(this),!1),this.request.removeEventListener("error",this.error.bind(this),!1),this.request=null,this.loaded=!0,this.decode())},decode:function(){h&&h.decodeAudioData(this.result.response,this.setBuffer.bind(this),this.error.bind(this))},setBuffer:function(a){this.options.buffer=
24 a;this.decoded=!0;a={name:this.options.name,alias:this.options.alias,ext:this.options.supported[this.ext],duration:this.options.buffer.duration};this.options.ready_callback&&"function"===typeof this.options.ready_callback&&this.options.ready_callback.call(this.options.scope,a);if(this.options.sprite)for(b in this.options.sprite)this.options.start=this.options.sprite[b][0],this.options.end=this.options.sprite[b][1],this.streams[b]=new k(this.options,b);else this.streams[0]=new k(this.options);this.autoplay&&
25 (this.autoplay=!1,this.play())},error:function(){this.reload()},play:function(a){delete this.options.part;a&&f(a,this.options);if(!this.loaded)this.options.preload||(this.autoplay=!0,this.load());else if(!this.no_file&&this.decoded)if(this.options.sprite)if(this.options.part)this.streams[this.options.part].play(this.options);else for(b in this.options.sprite)this.streams[b].play(this.options);else this.streams[0].play(this.options)},stop:function(a){if(this.options.sprite)if(a)this.streams[a.part].stop();
26 else for(b in this.options.sprite)this.streams[b].stop();else this.streams[0].stop()},pause:function(a){if(this.options.sprite)if(a)this.streams[a.part].pause();else for(b in this.options.sprite)this.streams[b].pause();else this.streams[0].pause()}};var k=function(a,b){this.alias=a.alias;this.name=a.name;this.sprite_part=b;this.buffer=a.buffer;this.start=a.start||0;this.end=a.end||this.buffer.duration;this.multiplay=a.multiplay||!1;this.volume=a.volume||1;this.scope=a.scope;this.ended_callback=a.ended_callback;
27 this.setLoop(a);this.gain=this.source=null;this.paused=this.playing=!1;this.time_offset=this.time_played=this.time_ended=this.time_started=0};k.prototype={destroy:function(){this.stop();this.source=this.buffer=null;this.gain&&this.gain.disconnect();this.source&&this.source.disconnect();this.source=this.gain=null},setLoop:function(a){!0===a.loop?this.loop=9999999:"number"===typeof a.loop&&(this.loop=+a.loop-1)},update:function(a){this.setLoop(a);"volume"in a&&(this.volume=a.volume)},play:function(a){a&&
28 this.update(a);if(this.multiplay||!this.playing)this.gain=h.createGain(),this.source=h.createBufferSource(),this.source.buffer=this.buffer,this.source.connect(this.gain),this.gain.connect(h.destination),this.gain.gain.value=this.volume,this.source.onended=this.ended.bind(this),this._play()},_play:function(){var a,b;this.paused?(a=this.start+this.time_offset,b=this.end-this.time_offset):(a=this.start,b=this.end);0>=b?this.clear():(this.source.start(0,a,b),this.playing=!0,this.paused=!1,this.time_started=
29 (new Date).valueOf())},stop:function(){this.source&&this.source.stop(0);this.clear()},pause:function(){this.paused?this.play():this.playing&&(this.source&&this.source.stop(0),this.paused=!0)},ended:function(){this.playing=!1;this.time_ended=(new Date).valueOf();this.time_played=(this.time_ended-this.time_started)/1E3;this.time_offset+=this.time_played;if(this.time_offset>=this.end||.015>this.end-this.time_offset)this._ended(),this.clear(),this.loop&&(this.loop--,this.play())},_ended:function(){var a=
30 {name:this.name,alias:this.alias,part:this.sprite_part,start:this.start,duration:this.end};this.ended_callback&&"function"===typeof this.ended_callback&&this.ended_callback.call(this.scope,a)},clear:function(){this.time_offset=this.time_played=0;this.playing=this.paused=!1}};h||(function(){var a=new Audio,b=a.canPlayType("audio/mpeg"),e=a.canPlayType("audio/ogg"),a=a.canPlayType('audio/mp4; codecs="mp4a.40.2"'),d,f;for(f=0;f<c.supported.length;f++)d=c.supported[f],b||"mp3"!==d||c.supported.splice(f,
31 1),e||"ogg"!==d||c.supported.splice(f,1),a||"aac"!==d||c.supported.splice(f,1),a||"mp4"!==d||c.supported.splice(f,1)}(),p.prototype={init:function(a){a&&f(a,this.options);this.inited=!0;this.options.preload&&this.load()},destroy:function(){var a;for(b in this.streams)(a=this.streams[b])&&a.destroy();this.streams={};this.inited=this.loaded=!1},load:function(){var a;this.options.preload=!0;this.options._ready=this.ready;this.options._scope=this;if(this.options.sprite)for(b in this.options.sprite)a=
32 this.options.sprite[b],this.options.start=a[0],this.options.end=a[1],this.streams[b]=new k(this.options,b);else this.streams[0]=new k(this.options)},ready:function(a){this.loaded||(this.loaded=!0,a={name:this.options.name,alias:this.options.alias,ext:this.options.supported[this.ext],duration:a},this.options.ready_callback&&"function"===typeof this.options.ready_callback&&this.options.ready_callback.call(this.options.scope,a),this.autoplay&&(this.autoplay=!1,this.play()))},play:function(a){if(this.inited)if(delete this.options.part,
33 a&&f(a,this.options),this.loaded)if(this.options.sprite)if(this.options.part)this.streams[this.options.part].play(this.options);else for(b in this.options.sprite)this.streams[b].play(this.options);else this.streams[0].play(this.options);else this.options.preload||(this.autoplay=!0,this.load())},stop:function(a){if(this.inited)if(this.options.sprite)if(a)this.streams[a.part].stop();else for(b in this.options.sprite)this.streams[b].stop();else this.streams[0].stop()},pause:function(a){if(this.inited)if(this.options.sprite)if(a)this.streams[a.part].pause();
34 else for(b in this.options.sprite)this.streams[b].pause();else this.streams[0].pause()}},k=function(a,b){this.name=a.name;this.alias=a.alias;this.sprite_part=b;this.multiplay=a.multiplay;this.volume=a.volume;this.preload=a.preload;this.path=c.path;this.start=a.start||0;this.end=a.end||0;this.scope=a.scope;this.ended_callback=a.ended_callback;this._scope=a._scope;this._ready=a._ready;this.setLoop(a);this.url=this.sound=null;this.loaded=!1;this.played_time=this.paused_time=this.start_time=0;this.init()},
35 k.prototype={init:function(){this.sound=new Audio;this.sound.volume=this.volume;this.createUrl();this.sound.addEventListener("ended",this.ended.bind(this),!1);this.sound.addEventListener("canplaythrough",this.can_play_through.bind(this),!1);this.sound.addEventListener("timeupdate",this._update.bind(this),!1);this.load()},destroy:function(){this.stop();this.sound.removeEventListener("ended",this.ended.bind(this),!1);this.sound.removeEventListener("canplaythrough",this.can_play_through.bind(this),!1);
36 this.sound.removeEventListener("timeupdate",this._update.bind(this),!1);this.sound=null;this.loaded=!1},createUrl:function(){this.url=this.path+this.name+"."+c.supported[0]+"?"+(new Date).valueOf()},can_play_through:function(){this.preload&&this.ready()},load:function(){this.sound.src=this.url;this.sound.preload=this.preload?"auto":"none";this.preload&&this.sound.load()},setLoop:function(a){!0===a.loop?this.loop=9999999:"number"===typeof a.loop&&(this.loop=+a.loop-1)},update:function(a){this.setLoop(a);
37 "volume"in a&&(this.volume=a.volume)},ready:function(){!this.loaded&&this.sound&&(this.loaded=!0,this._ready.call(this._scope,this.sound.duration),this.end||(this.end=this.sound.duration))},play:function(a){a&&this.update(a);!this.multiplay&&this.playing||this._play()},_play:function(){if(this.paused)this.paused=!1;else try{this.sound.currentTime=this.start}catch(a){}this.playing=!0;this.start_time=(new Date).valueOf();this.sound.volume=this.volume;this.sound.play()},stop:function(){if(this.playing){this.paused=
38 this.playing=!1;this.sound.pause();this.clear();try{this.sound.currentTime=this.start}catch(a){}}},pause:function(){this.paused?this._play():(this.playing=!1,this.paused=!0,this.sound.pause(),this.paused_time=(new Date).valueOf(),this.played_time+=this.paused_time-this.start_time)},_update:function(){this.start_time&&(this.played_time+((new Date).valueOf()-this.start_time))/1E3>=this.end&&this.playing&&(this.stop(),this._ended())},ended:function(){this.playing&&(this.stop(),this._ended())},_ended:function(){this.playing=
39 !1;var a={name:this.name,alias:this.alias,part:this.sprite_part,start:this.start,duration:this.end};this.ended_callback&&"function"===typeof this.ended_callback&&this.ended_callback.call(this.scope,a);this.loop&&setTimeout(this.looper.bind(this),15)},looper:function(){this.loop--;this.play()},clear:function(){this.paused_time=this.played_time=this.start_time=0}})}}})(window,navigator,jQuery||$);
@@ -0,0 +1,39 @@
1// Ion.Sound | version 3.0.0 | https://github.com/IonDen/ion.sound
2/*
3MIT licence
4Copyright (C) 2014 by Denis Ineshin
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
7to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11
12THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
14WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15*/
16
17(function(l,e,n,r){l.ion=l.ion||{};if(!ion.sound){var m=function(a){a||(a="undefined");if(l.console){console.warn&&"function"===typeof console.warn?console.warn(a):console.log&&"function"===typeof console.log&&console.log(a);var g=n("#debug");if(g.length){var b=g.html();g.html(b+a+"<br/>")}}},f=function(a,b){var c;b=b||{};for(c in a)a.hasOwnProperty(c)&&(b[c]=a[c]);return b};if("function"!==typeof Audio&&"object"!==typeof Audio)e=function(){m("HTML5 Audio is not supported in this browser")},ion.sound=
18e,ion.sound.play=e,ion.sound.stop=e,ion.sound.pause=e,ion.sound.preload=e,ion.sound.destroy=e,e();else{e=/iPad|iPhone|iPod/.test(e.appVersion);var q=0,c={},d={},b;!c.supported&&e?c.supported=["mp3","mp4","aac"]:c.supported||(c.supported=["mp3","ogg","mp4","aac","wav"]);ion.sound=function(a){f(a,c);c.path=c.path||"";c.volume=c.volume||1;c.preload=c.preload||!1;c.multiplay=c.multiplay||!1;c.loop=c.loop||!1;c.sprite=c.sprite||null;c.scope=c.scope||null;c.ready_callback=c.ready_callback||null;c.ended_callback=
19c.ended_callback||null;if(q=c.sounds.length)for(b=0;b<q;b++){if(!d[b]){a=c.sounds[b];var g=a.alias||a.name;d[g]||(d[g]=new p(a),d[g].init())}}else m("No sound-files provided!")};ion.sound.VERSION="3.0.0";ion.sound._method=function(a,c,e){if(c)d[c]&&d[c][a](e);else for(b in d)if(d.hasOwnProperty(b))d[b][a](e)};ion.sound.preload=function(a,b){b=b||{};f({preload:!0},b);ion.sound._method("init",a,b)};ion.sound.destroy=function(a){ion.sound._method("destroy",a);if(a)d[a]=null;else for(b in d)d.hasOwnProperty(b)&&
20d[b]&&(d[b]=null)};ion.sound.play=function(a,b){ion.sound._method("play",a,b)};ion.sound.stop=function(a,b){ion.sound._method("stop",a,b)};ion.sound.pause=function(a,b){ion.sound._method("pause",a,b)};n&&(n.ionSound=ion.sound);e=l.AudioContext||l.webkitAudioContext;var h;e&&(h=new e);var p=function(a){this.options=f(c);delete this.options.sounds;f(a,this.options);this.request=null;this.streams={};this.result={};this.ext=0;this.url="";this.autoplay=this.no_file=this.decoded=this.loaded=!1};p.prototype=
21{init:function(a){a&&f(a,this.options);this.options.preload&&this.load()},destroy:function(){var a;for(b in this.streams)(a=this.streams[b])&&a.destroy();this.streams={};this.result=null;this.options=this.options.buffer=null;this.request&&(this.request.removeEventListener("load",this.ready.bind(this),!1),this.request.removeEventListener("error",this.error.bind(this),!1),this.request.abort(),this.request=null)},createUrl:function(){this.url=this.options.path+this.options.name+"."+this.options.supported[this.ext]+
22"?"+(new Date).valueOf()},load:function(){this.no_file?m('No sources for "'+this.options.name+'" sound :('):(this.createUrl(),this.request=new XMLHttpRequest,this.request.open("GET",this.url,!0),this.request.responseType="arraybuffer",this.request.addEventListener("load",this.ready.bind(this),!1),this.request.addEventListener("error",this.error.bind(this),!1),this.request.send())},reload:function(){this.ext++;this.options.supported[this.ext]?this.load():(this.no_file=!0,m('No sources for "'+this.options.name+
23'" sound :('))},ready:function(a){this.result=a.target;4!==this.result.readyState?this.reload():200!==this.result.status?(m(this.url+" was not found on server!"),this.reload()):(this.request.removeEventListener("load",this.ready.bind(this),!1),this.request.removeEventListener("error",this.error.bind(this),!1),this.request=null,this.loaded=!0,this.decode())},decode:function(){h&&h.decodeAudioData(this.result.response,this.setBuffer.bind(this),this.error.bind(this))},setBuffer:function(a){this.options.buffer=
24a;this.decoded=!0;a={name:this.options.name,alias:this.options.alias,ext:this.options.supported[this.ext],duration:this.options.buffer.duration};this.options.ready_callback&&"function"===typeof this.options.ready_callback&&this.options.ready_callback.call(this.options.scope,a);if(this.options.sprite)for(b in this.options.sprite)this.options.start=this.options.sprite[b][0],this.options.end=this.options.sprite[b][1],this.streams[b]=new k(this.options,b);else this.streams[0]=new k(this.options);this.autoplay&&
25(this.autoplay=!1,this.play())},error:function(){this.reload()},play:function(a){delete this.options.part;a&&f(a,this.options);if(!this.loaded)this.options.preload||(this.autoplay=!0,this.load());else if(!this.no_file&&this.decoded)if(this.options.sprite)if(this.options.part)this.streams[this.options.part].play(this.options);else for(b in this.options.sprite)this.streams[b].play(this.options);else this.streams[0].play(this.options)},stop:function(a){if(this.options.sprite)if(a)this.streams[a.part].stop();
26else for(b in this.options.sprite)this.streams[b].stop();else this.streams[0].stop()},pause:function(a){if(this.options.sprite)if(a)this.streams[a.part].pause();else for(b in this.options.sprite)this.streams[b].pause();else this.streams[0].pause()}};var k=function(a,b){this.alias=a.alias;this.name=a.name;this.sprite_part=b;this.buffer=a.buffer;this.start=a.start||0;this.end=a.end||this.buffer.duration;this.multiplay=a.multiplay||!1;this.volume=a.volume||1;this.scope=a.scope;this.ended_callback=a.ended_callback;
27this.setLoop(a);this.gain=this.source=null;this.paused=this.playing=!1;this.time_offset=this.time_played=this.time_ended=this.time_started=0};k.prototype={destroy:function(){this.stop();this.source=this.buffer=null;this.gain&&this.gain.disconnect();this.source&&this.source.disconnect();this.source=this.gain=null},setLoop:function(a){!0===a.loop?this.loop=9999999:"number"===typeof a.loop&&(this.loop=+a.loop-1)},update:function(a){this.setLoop(a);"volume"in a&&(this.volume=a.volume)},play:function(a){a&&
28this.update(a);if(this.multiplay||!this.playing)this.gain=h.createGain(),this.source=h.createBufferSource(),this.source.buffer=this.buffer,this.source.connect(this.gain),this.gain.connect(h.destination),this.gain.gain.value=this.volume,this.source.onended=this.ended.bind(this),this._play()},_play:function(){var a,b;this.paused?(a=this.start+this.time_offset,b=this.end-this.time_offset):(a=this.start,b=this.end);0>=b?this.clear():(this.source.start(0,a,b),this.playing=!0,this.paused=!1,this.time_started=
29(new Date).valueOf())},stop:function(){this.source&&this.source.stop(0);this.clear()},pause:function(){this.paused?this.play():this.playing&&(this.source&&this.source.stop(0),this.paused=!0)},ended:function(){this.playing=!1;this.time_ended=(new Date).valueOf();this.time_played=(this.time_ended-this.time_started)/1E3;this.time_offset+=this.time_played;if(this.time_offset>=this.end||.015>this.end-this.time_offset)this._ended(),this.clear(),this.loop&&(this.loop--,this.play())},_ended:function(){var a=
30{name:this.name,alias:this.alias,part:this.sprite_part,start:this.start,duration:this.end};this.ended_callback&&"function"===typeof this.ended_callback&&this.ended_callback.call(this.scope,a)},clear:function(){this.time_offset=this.time_played=0;this.playing=this.paused=!1}};h||(function(){var a=new Audio,b=a.canPlayType("audio/mpeg"),e=a.canPlayType("audio/ogg"),a=a.canPlayType('audio/mp4; codecs="mp4a.40.2"'),d,f;for(f=0;f<c.supported.length;f++)d=c.supported[f],b||"mp3"!==d||c.supported.splice(f,
311),e||"ogg"!==d||c.supported.splice(f,1),a||"aac"!==d||c.supported.splice(f,1),a||"mp4"!==d||c.supported.splice(f,1)}(),p.prototype={init:function(a){a&&f(a,this.options);this.inited=!0;this.options.preload&&this.load()},destroy:function(){var a;for(b in this.streams)(a=this.streams[b])&&a.destroy();this.streams={};this.inited=this.loaded=!1},load:function(){var a;this.options.preload=!0;this.options._ready=this.ready;this.options._scope=this;if(this.options.sprite)for(b in this.options.sprite)a=
32this.options.sprite[b],this.options.start=a[0],this.options.end=a[1],this.streams[b]=new k(this.options,b);else this.streams[0]=new k(this.options)},ready:function(a){this.loaded||(this.loaded=!0,a={name:this.options.name,alias:this.options.alias,ext:this.options.supported[this.ext],duration:a},this.options.ready_callback&&"function"===typeof this.options.ready_callback&&this.options.ready_callback.call(this.options.scope,a),this.autoplay&&(this.autoplay=!1,this.play()))},play:function(a){if(this.inited)if(delete this.options.part,
33a&&f(a,this.options),this.loaded)if(this.options.sprite)if(this.options.part)this.streams[this.options.part].play(this.options);else for(b in this.options.sprite)this.streams[b].play(this.options);else this.streams[0].play(this.options);else this.options.preload||(this.autoplay=!0,this.load())},stop:function(a){if(this.inited)if(this.options.sprite)if(a)this.streams[a.part].stop();else for(b in this.options.sprite)this.streams[b].stop();else this.streams[0].stop()},pause:function(a){if(this.inited)if(this.options.sprite)if(a)this.streams[a.part].pause();
34else for(b in this.options.sprite)this.streams[b].pause();else this.streams[0].pause()}},k=function(a,b){this.name=a.name;this.alias=a.alias;this.sprite_part=b;this.multiplay=a.multiplay;this.volume=a.volume;this.preload=a.preload;this.path=c.path;this.start=a.start||0;this.end=a.end||0;this.scope=a.scope;this.ended_callback=a.ended_callback;this._scope=a._scope;this._ready=a._ready;this.setLoop(a);this.url=this.sound=null;this.loaded=!1;this.played_time=this.paused_time=this.start_time=0;this.init()},
35k.prototype={init:function(){this.sound=new Audio;this.sound.volume=this.volume;this.createUrl();this.sound.addEventListener("ended",this.ended.bind(this),!1);this.sound.addEventListener("canplaythrough",this.can_play_through.bind(this),!1);this.sound.addEventListener("timeupdate",this._update.bind(this),!1);this.load()},destroy:function(){this.stop();this.sound.removeEventListener("ended",this.ended.bind(this),!1);this.sound.removeEventListener("canplaythrough",this.can_play_through.bind(this),!1);
36this.sound.removeEventListener("timeupdate",this._update.bind(this),!1);this.sound=null;this.loaded=!1},createUrl:function(){this.url=this.path+this.name+"."+c.supported[0]+"?"+(new Date).valueOf()},can_play_through:function(){this.preload&&this.ready()},load:function(){this.sound.src=this.url;this.sound.preload=this.preload?"auto":"none";this.preload&&this.sound.load()},setLoop:function(a){!0===a.loop?this.loop=9999999:"number"===typeof a.loop&&(this.loop=+a.loop-1)},update:function(a){this.setLoop(a);
37"volume"in a&&(this.volume=a.volume)},ready:function(){!this.loaded&&this.sound&&(this.loaded=!0,this._ready.call(this._scope,this.sound.duration),this.end||(this.end=this.sound.duration))},play:function(a){a&&this.update(a);!this.multiplay&&this.playing||this._play()},_play:function(){if(this.paused)this.paused=!1;else try{this.sound.currentTime=this.start}catch(a){}this.playing=!0;this.start_time=(new Date).valueOf();this.sound.volume=this.volume;this.sound.play()},stop:function(){if(this.playing){this.paused=
38this.playing=!1;this.sound.pause();this.clear();try{this.sound.currentTime=this.start}catch(a){}}},pause:function(){this.paused?this._play():(this.playing=!1,this.paused=!0,this.sound.pause(),this.paused_time=(new Date).valueOf(),this.played_time+=this.paused_time-this.start_time)},_update:function(){this.start_time&&(this.played_time+((new Date).valueOf()-this.start_time))/1E3>=this.end&&this.playing&&(this.stop(),this._ended())},ended:function(){this.playing&&(this.stop(),this._ended())},_ended:function(){this.playing=
39!1;var a={name:this.name,alias:this.alias,part:this.sprite_part,start:this.start,duration:this.end};this.ended_callback&&"function"===typeof this.ended_callback&&this.ended_callback.call(this.scope,a);this.loop&&setTimeout(this.looper.bind(this),15)},looper:function(){this.loop--;this.play()},clear:function(){this.paused_time=this.played_time=this.start_time=0}})}}})(window,navigator,jQuery||$);
object.js added +272 −0
@@ -0,0 +1,272 @@
1 //<DOC> category GameObject
2
3 var 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 }
@@ -0,0 +1,272 @@
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}
vector.js added +114 −0
@@ -0,0 +1,114 @@
1 //<DOC> category vector()
2
3 function vector(x, y)
4 {
5 //<DOC> <parameter> vector.x<br><br>the X axis of the vector.
6 this.x = x;
7 //<DOC> <parameter> vector.y<br><br>the Y axis of the vector.
8 this.y = y;
9
10 //<DOC> <function> vector.length()<br><br>returns the length of the vector.
11 this.length = function()
12 {
13 return Math.sqrt((this.x * this.x) + (this.y * this.y));
14 }
15
16 //<DOC> <function> vector.toAngle()<br><br>Get the angle towards the vector's direction in degrees.
17 this.toAngle = function()
18 {
19 return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
20 }
21
22 //<DOC> <function> vector.normalize()<br><br>returns a normalized copy of the vector. Original vector is not modified.
23 this.normalize = function()
24 {
25 var newVector = this.getCopy();
26
27 if(newVector.length() != 0)
28 {
29 var length = newVector.length();
30 newVector.x /= length;
31 newVector.y /= length;
32 }
33
34 return newVector;
35 }
36
37 //<DOC> <function> vector.lerpTo(otherVector, t)<br><br> Returns a new vector between this vector and 'otherVector' at point 't'. 0t = original vector, 1t = 'otherVector', 0.5t is halfway in between.
38 this.lerpTo = function(otherVector, t)
39 {
40 var delta = otherVector.subtract(this);
41
42 return this.add(delta.stretch(t));
43 }
44
45 //<DOC> <function> vector.distanceTo(otherVector)<br><br>returns the distance to another vector.
46 this.distanceTo = function(otherVector)
47 {
48 var dX = this.x - otherVector.x;
49 var dY = this.y - otherVector.y;
50
51 return new vector(dX, dY).length();
52 }
53
54 //<DOC> <function> vector.add(otherVector)<br><br>adds the two vectors together, and returns as a copy. Original vector is not modified.
55 this.add = function(otherVector)
56 {
57 var newVector = this.getCopy();
58
59 newVector.x += otherVector.x;
60 newVector.y += otherVector.y;
61
62 return newVector;
63 }
64
65 //<DOC> <function> vector.subtract(otherVector)<br><br>subtracts the second vector from this vector, and returns as a copy. Original vector is not modified.
66 this.subtract = function(otherVector)
67 {
68 var newVector = this.getCopy();
69
70 newVector.x -= otherVector.x;
71 newVector.y -= otherVector.y;
72
73 return newVector;
74 }
75
76 //<DOC> <function> vector.multiply(otherVector)<br><br>multiplies the first vector X by the second vector's X, and the first's Y by the second's Y, and returns as a copy. Original vector is not modified.
77 this.multiply = function(otherVector)
78 {
79 var newVector = this.getCopy();
80
81 newVector.x *= otherVector.x;
82 newVector.y *= otherVector.y;
83
84 return newVector;
85 }
86
87 //<DOC> <function> vector.divide(otherVector)<br><br>divides the first vector by the second, and returns as a copy. Original vector is not modified.
88 this.divide = function(otherVector)
89 {
90 var newVector = this.getCopy();
91
92 newVector.x /= otherVector.x;
93 newVector.y /= otherVector.y;
94
95 return newVector;
96 }
97
98 //<DOC> <function> vector.stretch(length)<br><br>Multiplies the vector by the given numeric value, and returns as a copy. Original vector is not modified.
99 this.stretch = function(length)
100 {
101 var newVector = this.getCopy();
102
103 newVector.x *= length;
104 newVector.y *= length;
105
106 return newVector;
107 }
108
109 //<DOC> <function> vector.getCopy()<br><br>returns a copy of this vector.
110 this.getCopy = function()
111 {
112 return new vector(this.x, this.y);
113 }
114 }
@@ -0,0 +1,114 @@
1//<DOC> category vector()
2
3function vector(x, y)
4{
5 //<DOC> <parameter> vector.x<br><br>the X axis of the vector.
6 this.x = x;
7 //<DOC> <parameter> vector.y<br><br>the Y axis of the vector.
8 this.y = y;
9
10 //<DOC> <function> vector.length()<br><br>returns the length of the vector.
11 this.length = function()
12 {
13 return Math.sqrt((this.x * this.x) + (this.y * this.y));
14 }
15
16 //<DOC> <function> vector.toAngle()<br><br>Get the angle towards the vector's direction in degrees.
17 this.toAngle = function()
18 {
19 return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
20 }
21
22 //<DOC> <function> vector.normalize()<br><br>returns a normalized copy of the vector. Original vector is not modified.
23 this.normalize = function()
24 {
25 var newVector = this.getCopy();
26
27 if(newVector.length() != 0)
28 {
29 var length = newVector.length();
30 newVector.x /= length;
31 newVector.y /= length;
32 }
33
34 return newVector;
35 }
36
37 //<DOC> <function> vector.lerpTo(otherVector, t)<br><br> Returns a new vector between this vector and 'otherVector' at point 't'. 0t = original vector, 1t = 'otherVector', 0.5t is halfway in between.
38 this.lerpTo = function(otherVector, t)
39 {
40 var delta = otherVector.subtract(this);
41
42 return this.add(delta.stretch(t));
43 }
44
45 //<DOC> <function> vector.distanceTo(otherVector)<br><br>returns the distance to another vector.
46 this.distanceTo = function(otherVector)
47 {
48 var dX = this.x - otherVector.x;
49 var dY = this.y - otherVector.y;
50
51 return new vector(dX, dY).length();
52 }
53
54 //<DOC> <function> vector.add(otherVector)<br><br>adds the two vectors together, and returns as a copy. Original vector is not modified.
55 this.add = function(otherVector)
56 {
57 var newVector = this.getCopy();
58
59 newVector.x += otherVector.x;
60 newVector.y += otherVector.y;
61
62 return newVector;
63 }
64
65 //<DOC> <function> vector.subtract(otherVector)<br><br>subtracts the second vector from this vector, and returns as a copy. Original vector is not modified.
66 this.subtract = function(otherVector)
67 {
68 var newVector = this.getCopy();
69
70 newVector.x -= otherVector.x;
71 newVector.y -= otherVector.y;
72
73 return newVector;
74 }
75
76 //<DOC> <function> vector.multiply(otherVector)<br><br>multiplies the first vector X by the second vector's X, and the first's Y by the second's Y, and returns as a copy. Original vector is not modified.
77 this.multiply = function(otherVector)
78 {
79 var newVector = this.getCopy();
80
81 newVector.x *= otherVector.x;
82 newVector.y *= otherVector.y;
83
84 return newVector;
85 }
86
87 //<DOC> <function> vector.divide(otherVector)<br><br>divides the first vector by the second, and returns as a copy. Original vector is not modified.
88 this.divide = function(otherVector)
89 {
90 var newVector = this.getCopy();
91
92 newVector.x /= otherVector.x;
93 newVector.y /= otherVector.y;
94
95 return newVector;
96 }
97
98 //<DOC> <function> vector.stretch(length)<br><br>Multiplies the vector by the given numeric value, and returns as a copy. Original vector is not modified.
99 this.stretch = function(length)
100 {
101 var newVector = this.getCopy();
102
103 newVector.x *= length;
104 newVector.y *= length;
105
106 return newVector;
107 }
108
109 //<DOC> <function> vector.getCopy()<br><br>returns a copy of this vector.
110 this.getCopy = function()
111 {
112 return new vector(this.x, this.y);
113 }
114}
window.js added +50 −0
@@ -0,0 +1,50 @@
1 //<DOC> category GameWindow
2
3 var GameWindow =
4 {
5 //<DOC> <parameter> GameWindow.width<br>The game window's width. Cannot be changed.
6 width: 0,
7 //<DOC> <parameter> GameWindow.height<br>The game window's height. Cannot be changed.
8 height: 0,
9
10 //<DOC> <parameter> GameWindow.backgroundColor<br>Background color. Contains an r, g and b component (0-255).
11 backgroundColor:
12 {
13 r: 255,
14 g: 255,
15 b: 255
16 },
17
18 windowElement: null,
19 elementID: "GameBuilder_Window",
20
21 init: function(x, y, width, height)
22 {
23 GameWindow.width = width;
24 GameWindow.height = height;
25
26 $("body").append('<canvas id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; border: 1px solid black;" width="' + GameWindow.width + '" height="' + GameWindow.height + '"></canvas>');
27
28 //$("body").append('<div id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; width: ' + GameWindow.width + 'px; height: ' + GameWindow.height + 'px; overflow: hidden; border: 1px solid black;"></div>');
29 GameWindow.windowElement = $("#" + GameWindow.elementID);
30
31 //<DOC> <parameter> GameWindow.canvas <br><br> The game's canvas element
32 GameWindow.canvas = $("#" + GameWindow.elementID)[0];
33 //<DOC> <parameter> GameWindow.canvasContext <br><br>The game's canvasContext. Use this for drawing.
34 GameWindow.canvasContext = GameWindow.canvas.getContext('2d');
35
36 console.log("GAME: Window initialized: [" + width + ", " + height + "]");
37 },
38
39 redraw: function()
40 {
41 this.canvasContext.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
42 this.canvasContext.fillRect(0, 0, this.width, this.height);
43
44 for(var i = 0; i < Game.activeObjects.length; i++)
45 {
46 if(Game.activeObjects[i].active && Game.activeObjects[i].renderer.visible)
47 Game.activeObjects[i].g_draw(GameWindow.canvasContext);
48 }
49 }
50 }
@@ -0,0 +1,50 @@
1//<DOC> category GameWindow
2
3var GameWindow =
4{
5 //<DOC> <parameter> GameWindow.width<br>The game window's width. Cannot be changed.
6 width: 0,
7 //<DOC> <parameter> GameWindow.height<br>The game window's height. Cannot be changed.
8 height: 0,
9
10 //<DOC> <parameter> GameWindow.backgroundColor<br>Background color. Contains an r, g and b component (0-255).
11 backgroundColor:
12 {
13 r: 255,
14 g: 255,
15 b: 255
16 },
17
18 windowElement: null,
19 elementID: "GameBuilder_Window",
20
21 init: function(x, y, width, height)
22 {
23 GameWindow.width = width;
24 GameWindow.height = height;
25
26 $("body").append('<canvas id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; border: 1px solid black;" width="' + GameWindow.width + '" height="' + GameWindow.height + '"></canvas>');
27
28 //$("body").append('<div id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; width: ' + GameWindow.width + 'px; height: ' + GameWindow.height + 'px; overflow: hidden; border: 1px solid black;"></div>');
29 GameWindow.windowElement = $("#" + GameWindow.elementID);
30
31 //<DOC> <parameter> GameWindow.canvas <br><br> The game's canvas element
32 GameWindow.canvas = $("#" + GameWindow.elementID)[0];
33 //<DOC> <parameter> GameWindow.canvasContext <br><br>The game's canvasContext. Use this for drawing.
34 GameWindow.canvasContext = GameWindow.canvas.getContext('2d');
35
36 console.log("GAME: Window initialized: [" + width + ", " + height + "]");
37 },
38
39 redraw: function()
40 {
41 this.canvasContext.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
42 this.canvasContext.fillRect(0, 0, this.width, this.height);
43
44 for(var i = 0; i < Game.activeObjects.length; i++)
45 {
46 if(Game.activeObjects[i].active && Game.activeObjects[i].renderer.visible)
47 Game.activeObjects[i].g_draw(GameWindow.canvasContext);
48 }
49 }
50}