BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / scene.js

scene.js

at some point do left top
timfa wsadhelios · pushed by timfa · 4 years ago
44cb6497 History
scene.js 4.4 KiB Download Raw
1class Scene
2{
3 static idCount = 0;
4 id = null;
5 activeObjects = [];
6 position = null;
7 size = null;
8 real_position = null;
9 real_size = null;
10 cameraPosition = null;
11 parentScene = null;
12 renderBackground = true;
13
14 g_primary = false;
15
16 static DisplayModes = { absolute: 0, relativeToParent: 1 };
17
18 displayMode = Scene.DisplayModes.absolute;
19
20 cv_canvas = null;
21 cv_context = null;
22 destroyed = false;
23
24 game = null;
25
26 get primary ()
27 {
28 return this.g_primary;
29 }
30
31 constructor (x, y, width, height, displayMode = 0)
32 {
33 Scene.idCount++;
34 this.id = 'canvas_' + Scene.idCount;
35
36 this.position = new vector(x, y);
37 this.size = new vector(width, height);
38 this.real_position = new vector(x, y);
39 this.real_size = new vector(width, height);
40 this.cameraPosition = new vector(0, 0);
41 this.displayMode = displayMode;
42
43 let body = document.body;
44
45 body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>');
46
47 this.cv_canvas = document.getElementById(this.id);
48 this.cv_context = this.cv_canvas.getContext('2d');
49 }
50
51 setPrimary ()
52 {
53 this.game.setPrimaryScene(this);
54 }
55
56 onLoad ()
57 {
58
59 }
60
61 loadChildScene (scene)
62 {
63 scene.parentScene = this;
64 this.game.loadScene(scene);
65 }
66
67 destroy ()
68 {
69 this.destroyed = true;
70 for (var i = 0; i < this.activeObjects.length; i++)
71 {
72 this.activeObjects[i].destroy();
73 }
74 }
75
76 backgroundColor =
77 {
78 r: 255,
79 g: 255,
80 b: 255
81 };
82
83 get canvas ()
84 {
85 return this.cv_canvas;
86 }
87
88 get context ()
89 {
90 return this.cv_context;
91 }
92
93 addObject (obj)
94 {
95 if (this.destroyed)
96 return;
97
98 this.activeObjects.push(obj);
99 obj.scene = this;
100
101 obj.addedToScene && obj.addedToScene();
102 }
103
104 removeObject (obj)
105 {
106 if (this.activeObjects.contains(obj))
107 {
108 this.activeObjects.remove(obj);
109 }
110 }
111
112 update (deltaTime)
113 {
114 for (let i = 0; i < this.activeObjects.length; i++)
115 {
116 this.activeObjects[i].update(deltaTime);
117 }
118 }
119
120 draw ()
121 {
122 if (this.destroyed)
123 return;
124
125 if (this.displayMode == Scene.DisplayModes.relativeToParent)
126 {
127 if (this.parentScene)
128 {
129 this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x);
130 this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y);
131
132 this.real_size.x = this.parentScene.real_size.x * this.size.x;
133 this.real_size.y = this.parentScene.real_size.y * this.size.y;
134 }
135 else
136 {
137 this.real_position.x = window.innerWidth * this.position.x;
138 this.real_position.y = window.innerHeight * this.position.y;
139
140 this.real_size.x = window.innerWidth * this.size.x;
141 this.real_size.y = window.innerHeight * this.size.y;
142 }
143 }
144 else if (this.displayMode == Scene.DisplayModes.absolute)
145 {
146 this.real_position.x = this.position.x;
147 this.real_position.y = this.position.y;
148
149 this.real_size.x = this.size.x;
150 this.real_size.y = this.size.y;
151 }
152
153 if (this.canvas.width != this.real_size.x)
154 this.canvas.width = this.real_size.x;
155
156 if (this.canvas.height != this.real_size.y)
157 this.canvas.height = this.real_size.y;
158
159 if (this.renderBackground)
160 {
161 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
162 this.context.fillRect(0, 0, this.real_size.x, this.real_size.y);
163 }
164 else
165 {
166 this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);
167 }
168
169 for (var i = 0; i < this.activeObjects.length; i++)
170 {
171 if (!this.activeObjects[i].destroyed && this.activeObjects[i].renderer.visible)
172 this.activeObjects[i].draw(this.context);
173 }
174 }
175}