BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / scene.js

scene.js

scene.js 2.2 KiB Download Raw
1class Scene
2{
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 }
48
49 addObject(obj)
50 {
51 this.activeObjects.push(obj);
52 }
53
54 destroy()
55 {
56 for(var i = 0; i < this.activeObjects.length; i++)
57 {
58 this.activeObjects[i].destroy();
59 }
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 }
84}