BlueGit

blue-star speeltuin

DisplayMode

timfa Tim · pushed by timfa

Authored 2022-10-15 · 4 years ago

Hash
b7f76fde95b4a0a35f7fa10278e88609ae9546b5
Parent
95f32ae3
2 files changed +46 −21
build/framework.js +44 −19
@@ -23,7 +23,8 @@ class Game
23 23 loadScene(scene)
24 24 {
25 25 this.activeScenes.push(scene);
26 scene.Game = this;
26 scene.game = this;
27 scene.onLoad();
27 28 }
28 29
29 30 update()
@@ -139,10 +140,10 @@ function image(url, subImgTotal, perRow)
139 140 if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
140 141 shouldDraw = false;
141 142
142 if(dx > scene.size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
143 if(dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
143 144 shouldDraw = false;
144 145
145 if(dy > scene.size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
146 if(dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
146 147 shouldDraw = false;
147 148
148 149 if(shouldDraw)
@@ -458,9 +459,14 @@ class Scene
458 459 activeObjects = [];
459 460 position = null;
460 461 size = null;
462 real_position = null;
463 real_size = null;
461 464 cameraPosition = null;
462 465 parentScene = null;
463 fullscreen = false;
466
467 static DisplayModes = {absolute: 0, relativeToParent: 1}
468
469 displayMode = Scene.DisplayModes.absolute;
464 470
465 471 cv_canvas = null;
466 472 cv_context = null;
@@ -468,21 +474,17 @@ class Scene
468 474
469 475 game = null;
470 476
471 constructor(x, y, width, height, fullscreen = false)
477 constructor(x, y, width, height, displayMode = 0)
472 478 {
473 479 Scene.idCount++;
474 480 this.id = 'canvas_' + Scene.idCount;
475 481
476 482 this.position = new vector(x, y);
477 483 this.size = new vector(width, height);
484 this.real_position = new vector(x, y);
485 this.real_size = new vector(width, height);
478 486 this.cameraPosition = new vector(0, 0)
479 this.fullscreen = fullscreen;
480
481 if(this.fullscreen)
482 {
483 this.size.x = window.innerWidth;
484 this.size.y = window.innerHeight;
485 }
487 this.displayMode = displayMode;
486 488
487 489 let body = document.body;
488 490
@@ -492,6 +494,11 @@ class Scene
492 494 this.cv_context = this.cv_canvas.getContext('2d');
493 495 }
494 496
497 onLoad()
498 {
499
500 }
501
495 502 loadChildScene(scene)
496 503 {
497 504 scene.parentScene = this;
@@ -554,19 +561,37 @@ class Scene
554 561 if(this.destroyed)
555 562 return;
556 563
557 if(this.fullscreen)
564 if(this.displayMode == Scene.DisplayModes.relativeToParent)
558 565 {
559 this.size.x = window.innerWidth;
560 this.size.y = window.innerHeight;
566 if(this.parentScene)
567 {
568
569 }
570 else
571 {
572 this.real_position.x = window.innerWidth * this.position.x;
573 this.real_position.y = window.innerHeight * this.position.y;
574
575 this.real_size.x = window.innerWidth * this.size.x;
576 this.real_size.y = window.innerHeight * this.size.y;
577 }
561 578 }
579 else if (this.displayMode == Scene.DisplayModes.absolute)
580 {
581 this.real_position.x = this.position.x;
582 this.real_position.y = this.position.y;
583
584 this.real_size.x = this.size.x;
585 this.real_size.y = this.size.y;
586 }
562 587
563 this.canvas.width = this.size.x;
564 this.canvas.height = this.size.y;
588 this.canvas.width = this.real_size.x;
589 this.canvas.height = this.real_size.y;
565 590
566 this.canvas.style = 'position: absolute; left: ' + this.position.x + 'px; top: ' + this.position.y + 'px;'; //todo: only call when changed?
591 this.canvas.style = 'position: absolute; left: ' + this.real_position.x + 'px; top: ' + this.real_position.y + 'px;'; //todo: only call when changed?
567 592
568 593 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
569 this.context.fillRect(0, 0, this.size.x, this.size.y);
594 this.context.fillRect(0, 0, this.real_size.x, this.real_size.y);
570 595
571 596 for(var i = 0; i < this.activeObjects.length; i++)
572 597 {
@@ -23,7 +23,8 @@ class Game
23 loadScene(scene)
23 loadScene(scene)
24 {
24 {
25 this.activeScenes.push(scene);
25 this.activeScenes.push(scene);
26 scene.Game = this;
26 scene.game = this;
27 scene.onLoad();
27 }
28 }
28
29
29 update()
30 update()
@@ -139,10 +140,10 @@ function image(url, subImgTotal, perRow)
139 if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
140 if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
140 shouldDraw = false;
141 shouldDraw = false;
141
142
142 if(dx > scene.size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
143 if(dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
143 shouldDraw = false;
144 shouldDraw = false;
144
145
145 if(dy > scene.size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
146 if(dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
146 shouldDraw = false;
147 shouldDraw = false;
147
148
148 if(shouldDraw)
149 if(shouldDraw)
@@ -458,9 +459,14 @@ class Scene
458 activeObjects = [];
459 activeObjects = [];
459 position = null;
460 position = null;
460 size = null;
461 size = null;
462 real_position = null;
463 real_size = null;
461 cameraPosition = null;
464 cameraPosition = null;
462 parentScene = null;
465 parentScene = null;
463 fullscreen = false;
466
467 static DisplayModes = {absolute: 0, relativeToParent: 1}
468
469 displayMode = Scene.DisplayModes.absolute;
464
470
465 cv_canvas = null;
471 cv_canvas = null;
466 cv_context = null;
472 cv_context = null;
@@ -468,21 +474,17 @@ class Scene
468
474
469 game = null;
475 game = null;
470
476
471 constructor(x, y, width, height, fullscreen = false)
477 constructor(x, y, width, height, displayMode = 0)
472 {
478 {
473 Scene.idCount++;
479 Scene.idCount++;
474 this.id = 'canvas_' + Scene.idCount;
480 this.id = 'canvas_' + Scene.idCount;
475
481
476 this.position = new vector(x, y);
482 this.position = new vector(x, y);
477 this.size = new vector(width, height);
483 this.size = new vector(width, height);
484 this.real_position = new vector(x, y);
485 this.real_size = new vector(width, height);
478 this.cameraPosition = new vector(0, 0)
486 this.cameraPosition = new vector(0, 0)
479 this.fullscreen = fullscreen;
487 this.displayMode = displayMode;
480
481 if(this.fullscreen)
482 {
483 this.size.x = window.innerWidth;
484 this.size.y = window.innerHeight;
485 }
486
488
487 let body = document.body;
489 let body = document.body;
488
490
@@ -492,6 +494,11 @@ class Scene
492 this.cv_context = this.cv_canvas.getContext('2d');
494 this.cv_context = this.cv_canvas.getContext('2d');
493 }
495 }
494
496
497 onLoad()
498 {
499
500 }
501
495 loadChildScene(scene)
502 loadChildScene(scene)
496 {
503 {
497 scene.parentScene = this;
504 scene.parentScene = this;
@@ -554,19 +561,37 @@ class Scene
554 if(this.destroyed)
561 if(this.destroyed)
555 return;
562 return;
556
563
557 if(this.fullscreen)
564 if(this.displayMode == Scene.DisplayModes.relativeToParent)
558 {
565 {
559 this.size.x = window.innerWidth;
566 if(this.parentScene)
560 this.size.y = window.innerHeight;
567 {
568
569 }
570 else
571 {
572 this.real_position.x = window.innerWidth * this.position.x;
573 this.real_position.y = window.innerHeight * this.position.y;
574
575 this.real_size.x = window.innerWidth * this.size.x;
576 this.real_size.y = window.innerHeight * this.size.y;
577 }
561 }
578 }
579 else if (this.displayMode == Scene.DisplayModes.absolute)
580 {
581 this.real_position.x = this.position.x;
582 this.real_position.y = this.position.y;
583
584 this.real_size.x = this.size.x;
585 this.real_size.y = this.size.y;
586 }
562
587
563 this.canvas.width = this.size.x;
588 this.canvas.width = this.real_size.x;
564 this.canvas.height = this.size.y;
589 this.canvas.height = this.real_size.y;
565
590
566 this.canvas.style = 'position: absolute; left: ' + this.position.x + 'px; top: ' + this.position.y + 'px;'; //todo: only call when changed?
591 this.canvas.style = 'position: absolute; left: ' + this.real_position.x + 'px; top: ' + this.real_position.y + 'px;'; //todo: only call when changed?
567
592
568 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
593 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
569 this.context.fillRect(0, 0, this.size.x, this.size.y);
594 this.context.fillRect(0, 0, this.real_size.x, this.real_size.y);
570
595
571 for(var i = 0; i < this.activeObjects.length; i++)
596 for(var i = 0; i < this.activeObjects.length; i++)
572 {
597 {
src/game.js +2 −2
@@ -3,7 +3,7 @@ window.onload = function()
3 3 {
4 4 var game = new Game();
5 5
6 var scene = new Scene(0, 0, 5, 5, true);
6 var scene = new Scene(0, 0, 1, 1, Scene.DisplayModes.relativeToParent);
7 7 game.activeScenes.push(scene);
8 8
9 9 game.images =
@@ -12,6 +12,6 @@ window.onload = function()
12 12 }
13 13
14 14 game.startGame();
15
15
16 16 scene.addObject(new Arrow(50, 50, game.images.arrow, 0));
17 17 }
@@ -3,7 +3,7 @@ window.onload = function()
3{
3{
4 var game = new Game();
4 var game = new Game();
5
5
6 var scene = new Scene(0, 0, 5, 5, true);
6 var scene = new Scene(0, 0, 1, 1, Scene.DisplayModes.relativeToParent);
7 game.activeScenes.push(scene);
7 game.activeScenes.push(scene);
8
8
9 game.images =
9 game.images =
@@ -12,6 +12,6 @@ window.onload = function()
12 }
12 }
13
13
14 game.startGame();
14 game.startGame();
15
15
16 scene.addObject(new Arrow(50, 50, game.images.arrow, 0));
16 scene.addObject(new Arrow(50, 50, game.images.arrow, 0));
17}
17}