BlueGit

timfa frameworkv3

Merge branch 'dev3'

timfa Tim Falken · pushed by timfa

Authored 2019-10-01 · 7 years ago

Hash
c040fa2bc35aa91a0199d793b9288e2ac4f25790
4 files changed +38 −13
framework/extentions.js +5 −0
@@ -26,4 +26,9 @@ Array.prototype.contains = function(element)
26 26 Math.clamp = function(number, min, max)
27 27 {
28 28 return Math.min(max, Math.max(min, number));
29 }
30
31 Array.prototype.random = function ()
32 {
33 return this[Math.floor((Math.random()*this.length))];
29 34 }
@@ -26,4 +26,9 @@ Array.prototype.contains = function(element)
26Math.clamp = function(number, min, max)
26Math.clamp = function(number, min, max)
27{
27{
28 return Math.min(max, Math.max(min, number));
28 return Math.min(max, Math.max(min, number));
29}
30
31Array.prototype.random = function ()
32{
33 return this[Math.floor((Math.random()*this.length))];
29}
34}
framework/input.js +20 −5
@@ -19,6 +19,7 @@ var GameInput =
19 19 g_middleMouseDown: false,
20 20 g_middleMouseDownThisFrame: false,
21 21
22 trueMousePosition: new vector(0, 0),
22 23 mousePosition: new vector(0, 0),
23 24
24 25 MouseButtons: {
@@ -46,17 +47,29 @@ var GameInput =
46 47
47 48 document.onmouseup = function(e)
48 49 {
49 GameInput.g_mouseDownThisFrame = false;
50 GameInput.g_mouseDown = false;
50 switch(e.button)
51 {
52 case GameInput.MouseButtons.Left:
53 GameInput.g_mouseDownThisFrame = false;
54 GameInput.g_mouseDown = false;
55 break;
56 case GameInput.MouseButtons.Middle:
57 GameInput.g_middleMouseDownThisFrame = false;
58 GameInput.g_middleMouseDown = false;
59 break;
60 case GameInput.MouseButtons.Right:
61 GameInput.g_altMouseDownThisFrame = false;
62 GameInput.g_altMouseDown = false;
63 break;
64 }
51 65 }
52 66
53 67 document.onmousemove = function(e)
54 68 {
55 GameInput.mousePosition.x = e.x + GameWindow.x;
56 GameInput.mousePosition.y = e.y + GameWindow.y;
69 GameInput.trueMousePosition.x = e.x + GameWindow.x;
70 GameInput.trueMousePosition.y = e.y + GameWindow.y;
57 71 }
58 72
59
60 73 var el = document.getElementsByTagName("canvas")[0];
61 74 el.addEventListener("touchstart", function(){GameInput.g_mouseDownThisFrame = true;}, false);
62 75 el.addEventListener("touchend", function(){GameInput.g_mouseDownThisFrame = false;GameInput.g_mouseDown = false;}, false);
@@ -76,6 +89,8 @@ var GameInput =
76 89
77 90 g_updateKeys: function()
78 91 {
92 GameInput.mousePosition = GameInput.trueMousePosition.subtract(GameWindow.cameraPosition);
93
79 94 GameInput.g_pKeyStatus = GameInput.g_keyStatus;
80 95 GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;
81 96 GameInput.g_pressKeyStatus = [];
@@ -19,6 +19,7 @@ var GameInput =
19 g_middleMouseDown: false,
19 g_middleMouseDown: false,
20 g_middleMouseDownThisFrame: false,
20 g_middleMouseDownThisFrame: false,
21
21
22 trueMousePosition: new vector(0, 0),
22 mousePosition: new vector(0, 0),
23 mousePosition: new vector(0, 0),
23
24
24 MouseButtons: {
25 MouseButtons: {
@@ -46,17 +47,29 @@ var GameInput =
46
47
47 document.onmouseup = function(e)
48 document.onmouseup = function(e)
48 {
49 {
49 GameInput.g_mouseDownThisFrame = false;
50 switch(e.button)
50 GameInput.g_mouseDown = false;
51 {
52 case GameInput.MouseButtons.Left:
53 GameInput.g_mouseDownThisFrame = false;
54 GameInput.g_mouseDown = false;
55 break;
56 case GameInput.MouseButtons.Middle:
57 GameInput.g_middleMouseDownThisFrame = false;
58 GameInput.g_middleMouseDown = false;
59 break;
60 case GameInput.MouseButtons.Right:
61 GameInput.g_altMouseDownThisFrame = false;
62 GameInput.g_altMouseDown = false;
63 break;
64 }
51 }
65 }
52
66
53 document.onmousemove = function(e)
67 document.onmousemove = function(e)
54 {
68 {
55 GameInput.mousePosition.x = e.x + GameWindow.x;
69 GameInput.trueMousePosition.x = e.x + GameWindow.x;
56 GameInput.mousePosition.y = e.y + GameWindow.y;
70 GameInput.trueMousePosition.y = e.y + GameWindow.y;
57 }
71 }
58
72
59
60 var el = document.getElementsByTagName("canvas")[0];
73 var el = document.getElementsByTagName("canvas")[0];
61 el.addEventListener("touchstart", function(){GameInput.g_mouseDownThisFrame = true;}, false);
74 el.addEventListener("touchstart", function(){GameInput.g_mouseDownThisFrame = true;}, false);
62 el.addEventListener("touchend", function(){GameInput.g_mouseDownThisFrame = false;GameInput.g_mouseDown = false;}, false);
75 el.addEventListener("touchend", function(){GameInput.g_mouseDownThisFrame = false;GameInput.g_mouseDown = false;}, false);
@@ -76,6 +89,8 @@ var GameInput =
76
89
77 g_updateKeys: function()
90 g_updateKeys: function()
78 {
91 {
92 GameInput.mousePosition = GameInput.trueMousePosition.subtract(GameWindow.cameraPosition);
93
79 GameInput.g_pKeyStatus = GameInput.g_keyStatus;
94 GameInput.g_pKeyStatus = GameInput.g_keyStatus;
80 GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;
95 GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;
81 GameInput.g_pressKeyStatus = [];
96 GameInput.g_pressKeyStatus = [];
framework/object.js +11 −6
@@ -6,7 +6,6 @@ var GameObject =
6 6
7 7 destroyed: false,
8 8
9
10 9 create: function(template, x, y, image, layer, scene)
11 10 {
12 11 var newObj = $.extend(true, {}, template);
@@ -17,8 +16,11 @@ var GameObject =
17 16
18 17 newObj.renderer.layerDepth = layer;
19 18
20 newObj.base = template;
21
19 if(!GameObject.allObjects)
20 GameObject.allObjects = [];
21
22 GameObject.allObjects.push(newObj);
23
22 24 return newObj;
23 25 },
24 26
@@ -189,7 +191,10 @@ var GameObject =
189 191 {
190 192 if(this.renderer.image)
191 193 {
192 this.renderer.draw(this.transform.position.x, this.transform.position.y,
194 var offX = GameWindow.cameraPosition.x;
195 var offY = GameWindow.cameraPosition.y;
196
197 this.renderer.draw(this.transform.position.x + offX, this.transform.position.y + offY,
193 198 this.transform.width * this.transform.scale.x,
194 199 this.transform.height * this.transform.scale.y,
195 200 this.transform.rotation);
@@ -202,7 +207,6 @@ var GameObject =
202 207 {
203 208 if(this.draw)
204 209 {
205
206 210 this.draw();
207 211 }
208 212 else
@@ -347,12 +351,13 @@ var GameObject =
347 351 {
348 352 if(!this.g_destroy)
349 353 {
350
351 354 if (typeof this.onDestroy == 'function') { this.onDestroy(); }
352 355
353 356 this.destroyed = true;
354 357
355 358 this.g_destroy = true;
359
360 GameObject.allObjects.remove(this);
356 361 }
357 362 }
358 363
@@ -6,7 +6,6 @@ var GameObject =
6
6
7 destroyed: false,
7 destroyed: false,
8
8
9
10 create: function(template, x, y, image, layer, scene)
9 create: function(template, x, y, image, layer, scene)
11 {
10 {
12 var newObj = $.extend(true, {}, template);
11 var newObj = $.extend(true, {}, template);
@@ -17,8 +16,11 @@ var GameObject =
17
16
18 newObj.renderer.layerDepth = layer;
17 newObj.renderer.layerDepth = layer;
19
18
20 newObj.base = template;
19 if(!GameObject.allObjects)
21
20 GameObject.allObjects = [];
21
22 GameObject.allObjects.push(newObj);
23
22 return newObj;
24 return newObj;
23 },
25 },
24
26
@@ -189,7 +191,10 @@ var GameObject =
189 {
191 {
190 if(this.renderer.image)
192 if(this.renderer.image)
191 {
193 {
192 this.renderer.draw(this.transform.position.x, this.transform.position.y,
194 var offX = GameWindow.cameraPosition.x;
195 var offY = GameWindow.cameraPosition.y;
196
197 this.renderer.draw(this.transform.position.x + offX, this.transform.position.y + offY,
193 this.transform.width * this.transform.scale.x,
198 this.transform.width * this.transform.scale.x,
194 this.transform.height * this.transform.scale.y,
199 this.transform.height * this.transform.scale.y,
195 this.transform.rotation);
200 this.transform.rotation);
@@ -202,7 +207,6 @@ var GameObject =
202 {
207 {
203 if(this.draw)
208 if(this.draw)
204 {
209 {
205
206 this.draw();
210 this.draw();
207 }
211 }
208 else
212 else
@@ -347,12 +351,13 @@ var GameObject =
347 {
351 {
348 if(!this.g_destroy)
352 if(!this.g_destroy)
349 {
353 {
350
351 if (typeof this.onDestroy == 'function') { this.onDestroy(); }
354 if (typeof this.onDestroy == 'function') { this.onDestroy(); }
352
355
353 this.destroyed = true;
356 this.destroyed = true;
354
357
355 this.g_destroy = true;
358 this.g_destroy = true;
359
360 GameObject.allObjects.remove(this);
356 }
361 }
357 }
362 }
358
363
framework/window.js +2 −2
@@ -9,6 +9,8 @@ var GameWindow =
9 9
10 10 g_width: 0,
11 11 g_height: 0,
12
13 cameraPosition: new vector(0, 0),
12 14
13 15 fullscreen: false,
14 16
@@ -40,10 +42,8 @@ var GameWindow =
40 42 //$("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>');
41 43 GameWindow.windowElement = document.getElementById(GameWindow.elementID);
42 44
43
44 45 GameWindow.canvas = document.getElementById(GameWindow.elementID);
45 46
46
47 47 GameWindow.canvasContext = GameWindow.canvas.getContext('2d');
48 48
49 49 console.log("GAME: Window initialized: [" + width + ", " + height + "]");
@@ -9,6 +9,8 @@ var GameWindow =
9
9
10 g_width: 0,
10 g_width: 0,
11 g_height: 0,
11 g_height: 0,
12
13 cameraPosition: new vector(0, 0),
12
14
13 fullscreen: false,
15 fullscreen: false,
14
16
@@ -40,10 +42,8 @@ var GameWindow =
40 //$("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>');
42 //$("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>');
41 GameWindow.windowElement = document.getElementById(GameWindow.elementID);
43 GameWindow.windowElement = document.getElementById(GameWindow.elementID);
42
44
43
44 GameWindow.canvas = document.getElementById(GameWindow.elementID);
45 GameWindow.canvas = document.getElementById(GameWindow.elementID);
45
46
46
47 GameWindow.canvasContext = GameWindow.canvas.getContext('2d');
47 GameWindow.canvasContext = GameWindow.canvas.getContext('2d');
48
48
49 console.log("GAME: Window initialized: [" + width + ", " + height + "]");
49 console.log("GAME: Window initialized: [" + width + ", " + height + "]");