BlueGit

timfa frameworkv3

move canvas and windows stuff to Scene, refactor Scene, make vector into a class

timfa Tim · pushed by timfa

Authored 2022-10-15 · 4 years ago

Hash
edc98a099fa286415fe0a386c85cbd8db2a6ea93
Parent
d238f51f
5 files changed +96 −130
framework/canvas.js deleted +0 −25
@@ -1,25 +0,0 @@
1 // Wrapper for some canvas drawing functions
2
3 var canvas =
4 {
5 get canvas()
6 {
7 return GameWindow.canvas;
8 },
9
10 get context()
11 {
12 return GameWindow.canvasContext;
13 },
14
15 drawText: function(text, x, y, size, font, color)
16 {
17 this.context.font = font? size + 'px ' + font : size + 'px Arial';
18
19 this.context.fillStyle = color;
20 this.context.textAlign = "center";
21 this.context.textBaseline="middle";
22
23 this.context.fillText(text, x, y);
24 }
25 }
@@ -1,25 +0,0 @@
1// Wrapper for some canvas drawing functions
2
3var canvas =
4{
5 get canvas()
6 {
7 return GameWindow.canvas;
8 },
9
10 get context()
11 {
12 return GameWindow.canvasContext;
13 },
14
15 drawText: function(text, x, y, size, font, color)
16 {
17 this.context.font = font? size + 'px ' + font : size + 'px Arial';
18
19 this.context.fillStyle = color;
20 this.context.textAlign = "center";
21 this.context.textBaseline="middle";
22
23 this.context.fillText(text, x, y);
24 }
25}
framework/engine.js +1 −0
@@ -96,6 +96,7 @@ var Game =
96 96 if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); }
97 97
98 98 this.stepPrevious = stepTime;
99 TWEEN.update();
99 100 },
100 101
101 102 reorderObjects: function()
@@ -96,6 +96,7 @@ var Game =
96 if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); }
96 if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); }
97
97
98 this.stepPrevious = stepTime;
98 this.stepPrevious = stepTime;
99 TWEEN.update();
99 },
100 },
100
101
101 reorderObjects: function()
102 reorderObjects: function()
framework/scene.js +71 −4
@@ -1,17 +1,84 @@
1 function Scene()
1 class Scene
2 2 {
3 this.activeObjects = [];
3 static idCount = 0;
4 activeObjects = [];
5 position = new vector(0, 0);
6 size = new vector(0, 0);
7 cameraPosition = new vector(0, 0);
8 parentScene = null;
9 fullscreen = false;
10
11 cv_canvas = null;
12 cv_context = null;
13
14 constructor(x, y, width, height, fullscreen = false)
15 {
16 Scene.idCount++;
17
18 this.position.x = x;
19 this.position.y = y;
20 this.size.x = width;
21 this.size.y = height;
22 this.fullscreen = fullscreen;
23
24 let body = document.body;
25
26 body.insertAdjacentHTML('beforeend', '<canvas id="canvas_' + Scene.idCount + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; border: 1px solid black;" width="' + GameWindow.width + '" height="' + GameWindow.height + '"></canvas>');
27
28 this.cv_canvas = document.getElementById('canvas_' + Scene.idCount);
29 this.cv_canvasContext = this.cv_canvas.getContext('2d');
30 }
31
32 backgroundColor =
33 {
34 r: 255,
35 g: 255,
36 b: 255
37 }
38
39 get canvas()
40 {
41 return this.canvas;
42 }
43
44 get context()
45 {
46 return this.canvasContext;
47 }
4 48
5 this.addObject = function(obj)
49 addObject(obj)
6 50 {
7 51 this.activeObjects.push(obj);
8 52 }
9 53
10 this.destroy = function()
54 destroy()
11 55 {
12 56 for(var i = 0; i < this.activeObjects.length; i++)
13 57 {
14 58 this.activeObjects[i].destroy();
15 59 }
16 60 }
61
62 redraw()
63 {
64 if(this.fullscreen)
65 {
66 this.size.width = window.innerWidth - 2;
67 this.size.height = window.innerHeight - 2;
68 }
69
70 this.canvas.width = this.size.width;
71 this.canvas.height = this.size.height;
72
73 this.canvasContext.fillStyle = "rgb(" + this.backgroundColor.r + "," + GameWindow.backgroundColor.g + "," + GameWindow.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
74 this.canvasContext.fillRect(0, 0, this.size.width, this.size.height);
75
76 for(var i = 0; i < this.activeObjects.length; i++)
77 {
78 if(this.activeObjects[i].active && this.activeObjects[i].renderer.visible)
79 this.activeObjects[i].g_draw(this.canvasContext);
80 }
81
82 //requestAnimationFrame(GameWindow.redraw); // <- This must be called in global game update loop!
83 }
17 84 }
@@ -1,17 +1,84 @@
1function Scene()
1class Scene
2{
2{
3 this.activeObjects = [];
3 static idCount = 0;
4 activeObjects = [];
5 position = new vector(0, 0);
6 size = new vector(0, 0);
7 cameraPosition = new vector(0, 0);
8 parentScene = null;
9 fullscreen = false;
10
11 cv_canvas = null;
12 cv_context = null;
13
14 constructor(x, y, width, height, fullscreen = false)
15 {
16 Scene.idCount++;
17
18 this.position.x = x;
19 this.position.y = y;
20 this.size.x = width;
21 this.size.y = height;
22 this.fullscreen = fullscreen;
23
24 let body = document.body;
25
26 body.insertAdjacentHTML('beforeend', '<canvas id="canvas_' + Scene.idCount + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; border: 1px solid black;" width="' + GameWindow.width + '" height="' + GameWindow.height + '"></canvas>');
27
28 this.cv_canvas = document.getElementById('canvas_' + Scene.idCount);
29 this.cv_canvasContext = this.cv_canvas.getContext('2d');
30 }
31
32 backgroundColor =
33 {
34 r: 255,
35 g: 255,
36 b: 255
37 }
38
39 get canvas()
40 {
41 return this.canvas;
42 }
43
44 get context()
45 {
46 return this.canvasContext;
47 }
4
48
5 this.addObject = function(obj)
49 addObject(obj)
6 {
50 {
7 this.activeObjects.push(obj);
51 this.activeObjects.push(obj);
8 }
52 }
9
53
10 this.destroy = function()
54 destroy()
11 {
55 {
12 for(var i = 0; i < this.activeObjects.length; i++)
56 for(var i = 0; i < this.activeObjects.length; i++)
13 {
57 {
14 this.activeObjects[i].destroy();
58 this.activeObjects[i].destroy();
15 }
59 }
16 }
60 }
61
62 redraw()
63 {
64 if(this.fullscreen)
65 {
66 this.size.width = window.innerWidth - 2;
67 this.size.height = window.innerHeight - 2;
68 }
69
70 this.canvas.width = this.size.width;
71 this.canvas.height = this.size.height;
72
73 this.canvasContext.fillStyle = "rgb(" + this.backgroundColor.r + "," + GameWindow.backgroundColor.g + "," + GameWindow.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
74 this.canvasContext.fillRect(0, 0, this.size.width, this.size.height);
75
76 for(var i = 0; i < this.activeObjects.length; i++)
77 {
78 if(this.activeObjects[i].active && this.activeObjects[i].renderer.visible)
79 this.activeObjects[i].g_draw(this.canvasContext);
80 }
81
82 //requestAnimationFrame(GameWindow.redraw); // <- This must be called in global game update loop!
83 }
17}
84}
framework/vector.js +24 −21
@@ -1,26 +1,29 @@
1 1
2
3 function vector(x, y)
2 class vector
4 3 {
4 x = 0;
5 y = 0;
6
7 constructor(x, y)
8 {
9 this.x = x;
10
11 this.y = y;
12 }
5 13
6 this.x = x;
7
8 this.y = y;
9
10
11 this.length = function()
14 length()
12 15 {
13 16 return Math.sqrt((this.x * this.x) + (this.y * this.y));
14 17 }
15 18
16 19
17 this.toAngle = function()
20 toAngle()
18 21 {
19 22 return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
20 23 }
21 24
22 25
23 this.normalize = function()
26 normalize()
24 27 {
25 28 var newVector = this.getCopy();
26 29
@@ -35,7 +38,7 @@ function vector(x, y)
35 38 }
36 39
37 40
38 this.lerpTo = function(otherVector, t)
41 lerpTo(otherVector, t)
39 42 {
40 43 var delta = otherVector.subtract(this);
41 44
@@ -43,7 +46,7 @@ function vector(x, y)
43 46 }
44 47
45 48
46 this.distanceTo = function(otherVector)
49 distanceTo(otherVector)
47 50 {
48 51 var dX = this.x - otherVector.x;
49 52 var dY = this.y - otherVector.y;
@@ -52,7 +55,7 @@ function vector(x, y)
52 55 }
53 56
54 57
55 this.add = function(otherVector)
58 add(otherVector)
56 59 {
57 60 var newVector = this.getCopy();
58 61
@@ -63,7 +66,7 @@ function vector(x, y)
63 66 }
64 67
65 68
66 this.subtract = function(otherVector)
69 subtract(otherVector)
67 70 {
68 71 var newVector = this.getCopy();
69 72
@@ -74,7 +77,7 @@ function vector(x, y)
74 77 }
75 78
76 79
77 this.multiply = function(otherVector)
80 multiply(otherVector)
78 81 {
79 82 var newVector = this.getCopy();
80 83
@@ -85,7 +88,7 @@ function vector(x, y)
85 88 }
86 89
87 90
88 this.divide = function(otherVector)
91 divide(otherVector)
89 92 {
90 93 var newVector = this.getCopy();
91 94
@@ -96,7 +99,7 @@ function vector(x, y)
96 99 }
97 100
98 101
99 this.stretch = function(length)
102 stretch(length)
100 103 {
101 104 var newVector = this.getCopy();
102 105
@@ -106,14 +109,14 @@ function vector(x, y)
106 109 return newVector;
107 110 }
108 111
109 this.rotate = function(degrees)
112 rotate(degrees)
110 113 {
111 114 var ca = Math.cos(degrees * (Math.PI / 180));
112 115 var sa = Math.sin(degrees * (Math.PI / 180));
113 116 return new vector(ca*this.x - sa*this.y, sa*this.x + ca*this.y);
114 117 }
115 118
116 this.deltaAngle = function(otherVector)
119 deltaAngle(otherVector)
117 120 {
118 121 var a = this.toAngle() * (Math.PI / 180);
119 122 var b = otherVector.toAngle() * (Math.PI / 180);
@@ -121,13 +124,13 @@ function vector(x, y)
121 124 return Math.atan2(Math.sin(a - b), Math.cos(a - b)) * (180 / Math.PI)
122 125 }
123 126
124 this.cross = function(otherVector)
127 cross(otherVector)
125 128 {
126 129 return this.x * otherVector.y - this.y * otherVector.x;
127 130 }
128 131
129 132
130 this.getCopy = function()
133 getCopy()
131 134 {
132 135 return new vector(this.x, this.y);
133 136 }
@@ -1,26 +1,29 @@
1
1
2
2class vector
3function vector(x, y)
4{
3{
4 x = 0;
5 y = 0;
6
7 constructor(x, y)
8 {
9 this.x = x;
10
11 this.y = y;
12 }
5
13
6 this.x = x;
14 length()
7
8 this.y = y;
9
10
11 this.length = function()
12 {
15 {
13 return Math.sqrt((this.x * this.x) + (this.y * this.y));
16 return Math.sqrt((this.x * this.x) + (this.y * this.y));
14 }
17 }
15
18
16
19
17 this.toAngle = function()
20 toAngle()
18 {
21 {
19 return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
22 return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
20 }
23 }
21
24
22
25
23 this.normalize = function()
26 normalize()
24 {
27 {
25 var newVector = this.getCopy();
28 var newVector = this.getCopy();
26
29
@@ -35,7 +38,7 @@ function vector(x, y)
35 }
38 }
36
39
37
40
38 this.lerpTo = function(otherVector, t)
41 lerpTo(otherVector, t)
39 {
42 {
40 var delta = otherVector.subtract(this);
43 var delta = otherVector.subtract(this);
41
44
@@ -43,7 +46,7 @@ function vector(x, y)
43 }
46 }
44
47
45
48
46 this.distanceTo = function(otherVector)
49 distanceTo(otherVector)
47 {
50 {
48 var dX = this.x - otherVector.x;
51 var dX = this.x - otherVector.x;
49 var dY = this.y - otherVector.y;
52 var dY = this.y - otherVector.y;
@@ -52,7 +55,7 @@ function vector(x, y)
52 }
55 }
53
56
54
57
55 this.add = function(otherVector)
58 add(otherVector)
56 {
59 {
57 var newVector = this.getCopy();
60 var newVector = this.getCopy();
58
61
@@ -63,7 +66,7 @@ function vector(x, y)
63 }
66 }
64
67
65
68
66 this.subtract = function(otherVector)
69 subtract(otherVector)
67 {
70 {
68 var newVector = this.getCopy();
71 var newVector = this.getCopy();
69
72
@@ -74,7 +77,7 @@ function vector(x, y)
74 }
77 }
75
78
76
79
77 this.multiply = function(otherVector)
80 multiply(otherVector)
78 {
81 {
79 var newVector = this.getCopy();
82 var newVector = this.getCopy();
80
83
@@ -85,7 +88,7 @@ function vector(x, y)
85 }
88 }
86
89
87
90
88 this.divide = function(otherVector)
91 divide(otherVector)
89 {
92 {
90 var newVector = this.getCopy();
93 var newVector = this.getCopy();
91
94
@@ -96,7 +99,7 @@ function vector(x, y)
96 }
99 }
97
100
98
101
99 this.stretch = function(length)
102 stretch(length)
100 {
103 {
101 var newVector = this.getCopy();
104 var newVector = this.getCopy();
102
105
@@ -106,14 +109,14 @@ function vector(x, y)
106 return newVector;
109 return newVector;
107 }
110 }
108
111
109 this.rotate = function(degrees)
112 rotate(degrees)
110 {
113 {
111 var ca = Math.cos(degrees * (Math.PI / 180));
114 var ca = Math.cos(degrees * (Math.PI / 180));
112 var sa = Math.sin(degrees * (Math.PI / 180));
115 var sa = Math.sin(degrees * (Math.PI / 180));
113 return new vector(ca*this.x - sa*this.y, sa*this.x + ca*this.y);
116 return new vector(ca*this.x - sa*this.y, sa*this.x + ca*this.y);
114 }
117 }
115
118
116 this.deltaAngle = function(otherVector)
119 deltaAngle(otherVector)
117 {
120 {
118 var a = this.toAngle() * (Math.PI / 180);
121 var a = this.toAngle() * (Math.PI / 180);
119 var b = otherVector.toAngle() * (Math.PI / 180);
122 var b = otherVector.toAngle() * (Math.PI / 180);
@@ -121,13 +124,13 @@ function vector(x, y)
121 return Math.atan2(Math.sin(a - b), Math.cos(a - b)) * (180 / Math.PI)
124 return Math.atan2(Math.sin(a - b), Math.cos(a - b)) * (180 / Math.PI)
122 }
125 }
123
126
124 this.cross = function(otherVector)
127 cross(otherVector)
125 {
128 {
126 return this.x * otherVector.y - this.y * otherVector.x;
129 return this.x * otherVector.y - this.y * otherVector.x;
127 }
130 }
128
131
129
132
130 this.getCopy = function()
133 getCopy()
131 {
134 {
132 return new vector(this.x, this.y);
135 return new vector(this.x, this.y);
133 }
136 }
framework/window.js deleted +0 −80
@@ -1,80 +0,0 @@
1
2
3 var GameWindow =
4 {
5
6 width: 0,
7
8 height: 0,
9
10 g_width: 0,
11 g_height: 0,
12
13 cameraPosition: new vector(0, 0),
14
15 fullscreen: false,
16
17
18 backgroundColor:
19 {
20 r: 255,
21 g: 255,
22 b: 255
23 },
24
25 windowElement: null,
26 elementID: "GameBuilder_Window",
27
28 init: function(x, y, width, height)
29 {
30 GameWindow.x = x;
31 GameWindow.y = y;
32
33 GameWindow.width = width;
34 GameWindow.height = height;
35
36 GameWindow.g_width = width;
37 GameWindow.g_height = height;
38
39 var body = document.body;
40 body.insertAdjacentHTML('beforeend', '<canvas id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; border: 1px solid black;" width="' + GameWindow.width + '" height="' + GameWindow.height + '"></canvas>');
41
42 GameWindow.windowElement = document.getElementById(GameWindow.elementID);
43
44 GameWindow.canvas = document.getElementById(GameWindow.elementID);
45
46 GameWindow.canvasContext = GameWindow.canvas.getContext('2d');
47
48 console.log("GAME: Window initialized: [" + width + ", " + height + "]");
49 },
50
51 redraw: function()
52 {
53 TWEEN.update();
54
55 if(GameWindow.fullscreen)
56 {
57 GameWindow.width = window.innerWidth - 2;
58 GameWindow.height = window.innerHeight - 2;
59 }
60 else
61 {
62 GameWindow.width = GameWindow.g_width;
63 GameWindow.height = GameWindow.g_height;
64 }
65
66 GameWindow.canvas.width = GameWindow.width;
67 GameWindow.canvas.height = GameWindow.height;
68
69 GameWindow.canvasContext.fillStyle = "rgb(" + GameWindow.backgroundColor.r + "," + GameWindow.backgroundColor.g + "," + GameWindow.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
70 GameWindow.canvasContext.fillRect(0, 0, GameWindow.width, GameWindow.height);
71
72 for(var i = 0; i < Game.activeScene.activeObjects.length; i++)
73 {
74 if(Game.activeScene.activeObjects[i].active && Game.activeScene.activeObjects[i].renderer.visible)
75 Game.activeScene.activeObjects[i].g_draw(GameWindow.canvasContext);
76 }
77
78 requestAnimationFrame(GameWindow.redraw);
79 }
80 }
@@ -1,80 +0,0 @@
1
2
3var GameWindow =
4{
5
6 width: 0,
7
8 height: 0,
9
10 g_width: 0,
11 g_height: 0,
12
13 cameraPosition: new vector(0, 0),
14
15 fullscreen: false,
16
17
18 backgroundColor:
19 {
20 r: 255,
21 g: 255,
22 b: 255
23 },
24
25 windowElement: null,
26 elementID: "GameBuilder_Window",
27
28 init: function(x, y, width, height)
29 {
30 GameWindow.x = x;
31 GameWindow.y = y;
32
33 GameWindow.width = width;
34 GameWindow.height = height;
35
36 GameWindow.g_width = width;
37 GameWindow.g_height = height;
38
39 var body = document.body;
40 body.insertAdjacentHTML('beforeend', '<canvas id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; border: 1px solid black;" width="' + GameWindow.width + '" height="' + GameWindow.height + '"></canvas>');
41
42 GameWindow.windowElement = document.getElementById(GameWindow.elementID);
43
44 GameWindow.canvas = document.getElementById(GameWindow.elementID);
45
46 GameWindow.canvasContext = GameWindow.canvas.getContext('2d');
47
48 console.log("GAME: Window initialized: [" + width + ", " + height + "]");
49 },
50
51 redraw: function()
52 {
53 TWEEN.update();
54
55 if(GameWindow.fullscreen)
56 {
57 GameWindow.width = window.innerWidth - 2;
58 GameWindow.height = window.innerHeight - 2;
59 }
60 else
61 {
62 GameWindow.width = GameWindow.g_width;
63 GameWindow.height = GameWindow.g_height;
64 }
65
66 GameWindow.canvas.width = GameWindow.width;
67 GameWindow.canvas.height = GameWindow.height;
68
69 GameWindow.canvasContext.fillStyle = "rgb(" + GameWindow.backgroundColor.r + "," + GameWindow.backgroundColor.g + "," + GameWindow.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
70 GameWindow.canvasContext.fillRect(0, 0, GameWindow.width, GameWindow.height);
71
72 for(var i = 0; i < Game.activeScene.activeObjects.length; i++)
73 {
74 if(Game.activeScene.activeObjects[i].active && Game.activeScene.activeObjects[i].renderer.visible)
75 Game.activeScene.activeObjects[i].g_draw(GameWindow.canvasContext);
76 }
77
78 requestAnimationFrame(GameWindow.redraw);
79 }
80}