BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / scene.js

scene.js

Added child scaling
timfa wsadhelios · pushed by timfa · 4 years ago
69325627 History
scene.js 4.0 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 this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x);
115 this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y);
116
117 this.real_size.x = this.parentScene.real_size.x * this.size.x;
118 this.real_size.y = this.parentScene.real_size.y * this.size.y;
119 }
120 else
121 {
122 this.real_position.x = window.innerWidth * this.position.x;
123 this.real_position.y = window.innerHeight * this.position.y;
124
125 this.real_size.x = window.innerWidth * this.size.x;
126 this.real_size.y = window.innerHeight * this.size.y;
127 }
128 }
129 else if (this.displayMode == Scene.DisplayModes.absolute)
130 {
131 this.real_position.x = this.position.x;
132 this.real_position.y = this.position.y;
133
134 this.real_size.x = this.size.x;
135 this.real_size.y = this.size.y;
136 }
137
138 this.canvas.width = this.real_size.x;
139 this.canvas.height = this.real_size.y;
140
141 this.canvas.style = 'position: absolute; left: ' + this.real_position.x + 'px; top: ' + this.real_position.y + 'px;'; //todo: only call when changed?
142
143 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
144 this.context.fillRect(0, 0, this.real_size.x, this.real_size.y);
145
146 for(var i = 0; i < this.activeObjects.length; i++)
147 {
148 if(!this.activeObjects[i].destroyed && this.activeObjects[i].renderer.visible)
149 this.activeObjects[i].renderer.draw(this.context);
150 }
151 }
152}