BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / scene.js

scene.js

DisplayMode
timfa Tim · pushed by timfa · 4 years ago
40dfccf8 History
scene.js 3.6 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
13 static DisplayModes = {absolute: 0, relativeToParent: 1}
14
15 displayMode = Scene.DisplayModes.absolute;
16
17 cv_canvas = null;
18 cv_context = null;
19 destroyed = false;
20
21 game = null;
22
23 constructor(x, y, width, height, displayMode = 0)
24 {
25 Scene.idCount++;
26 this.id = 'canvas_' + Scene.idCount;
27
28 this.position = new vector(x, y);
29 this.size = new vector(width, height);
30 this.real_position = new vector(x, y);
31 this.real_size = new vector(width, height);
32 this.cameraPosition = new vector(0, 0)
33 this.displayMode = displayMode;
34
35 let body = document.body;
36
37 body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>');
38
39 this.cv_canvas = document.getElementById(this.id);
40 this.cv_context = this.cv_canvas.getContext('2d');
41 }
42
43 onLoad()
44 {
45
46 }
47
48 loadChildScene(scene)
49 {
50 scene.parentScene = this;
51 this.game.loadScene(scene);
52 }
53
54 destroy()
55 {
56 this.destroyed = true
57 for(var i = 0; i < this.activeObjects.length; i++)
58 {
59 this.activeObjects[i].destroy();
60 }
61 }
62
63 backgroundColor =
64 {
65 r: 255,
66 g: 255,
67 b: 255
68 }
69
70 get canvas()
71 {
72 return this.cv_canvas;
73 }
74
75 get context()
76 {
77 return this.cv_context;
78 }
79
80 addObject(obj)
81 {
82 if(this.destroyed)
83 return;
84
85 this.activeObjects.push(obj);
86 obj.scene = this;
87 }
88
89 removeObject(obj)
90 {
91 if(this.activeObjects.contains(obj))
92 {
93 this.activeObjects.remove(obj)
94 }
95 }
96
97 update(deltaTime)
98 {
99 for(let i = 0; i < this.activeObjects.length; i++)
100 {
101 this.activeObjects[i].update(deltaTime)
102 }
103 }
104
105 draw()
106 {
107 if(this.destroyed)
108 return;
109
110 if(this.displayMode == Scene.DisplayModes.relativeToParent)
111 {
112 if(this.parentScene)
113 {
114
115 }
116 else
117 {
118 this.real_position.x = window.innerWidth * this.position.x;
119 this.real_position.y = window.innerHeight * this.position.y;
120
121 this.real_size.x = window.innerWidth * this.size.x;
122 this.real_size.y = window.innerHeight * this.size.y;
123 }
124 }
125 else if (this.displayMode == Scene.DisplayModes.absolute)
126 {
127 this.real_position.x = this.position.x;
128 this.real_position.y = this.position.y;
129
130 this.real_size.x = this.size.x;
131 this.real_size.y = this.size.y;
132 }
133
134 this.canvas.width = this.real_size.x;
135 this.canvas.height = this.real_size.y;
136
137 this.canvas.style = 'position: absolute; left: ' + this.real_position.x + 'px; top: ' + this.real_position.y + 'px;'; //todo: only call when changed?
138
139 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
140 this.context.fillRect(0, 0, this.real_size.x, this.real_size.y);
141
142 for(var i = 0; i < this.activeObjects.length; i++)
143 {
144 if(!this.activeObjects[i].destroyed && this.activeObjects[i].renderer.visible)
145 this.activeObjects[i].renderer.draw(this.context);
146 }
147 }
148}