BlueGit

timfa frameworkv3

Merge remote-tracking branch 'origin/master'

timfa Tim · pushed by timfa

Authored 2022-10-21 · 4 years ago

Hash
507a8ac7ce19ef5b3274551b9bcd48862602c858
2 files changed +40 −3
framework/engine/game.js +30 −0
@@ -1,6 +1,8 @@
1 1 class Game
2 2 {
3 3 activeScenes = [];
4 pendingRemoval = [];
5
4 6 previousStep = 0; //used for calculating deltaTime
5 7
6 8 images = {};
@@ -93,6 +95,16 @@ class Game
93 95 scene.onLoad();
94 96 }
95 97
98 removeScene (scene)
99 {
100 let index = this.activeScenes.indexOf(scene);
101 if (index >= 0 && !scene.destroyed)
102 {
103 if (!this.pendingRemoval.contains(index))
104 this.pendingRemoval.push(index);
105 }
106 }
107
96 108 update ()
97 109 {
98 110 this.previousStep = this.previousStep || 0;
@@ -110,10 +122,28 @@ class Game
110 122 this.activeScenes[i].draw();
111 123 }
112 124
125 this.cleanActiveScenes();
126
113 127 this.previousStep = stepTime;
114 128
115 129 TWEEN.update();
116 130
117 131 requestAnimationFrame(this.update);
118 132 }
133
134 cleanActiveScenes ()
135 {
136 if (this.pendingRemoval.length > 0)
137 {
138 this.pendingRemoval.sort((a, b) => a - b);
139
140 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
141 {
142 if (this.activeScenes[this.pendingRemoval[i]].destroyed)
143 this.activeScenes.removeAt(this.pendingRemoval[i]);
144
145 this.pendingRemoval.splice(i, 1);
146 }
147 }
148 }
119 149 }
@@ -1,6 +1,8 @@
1class Game
1class Game
2{
2{
3 activeScenes = [];
3 activeScenes = [];
4 pendingRemoval = [];
5
4 previousStep = 0; //used for calculating deltaTime
6 previousStep = 0; //used for calculating deltaTime
5
7
6 images = {};
8 images = {};
@@ -93,6 +95,16 @@ class Game
93 scene.onLoad();
95 scene.onLoad();
94 }
96 }
95
97
98 removeScene (scene)
99 {
100 let index = this.activeScenes.indexOf(scene);
101 if (index >= 0 && !scene.destroyed)
102 {
103 if (!this.pendingRemoval.contains(index))
104 this.pendingRemoval.push(index);
105 }
106 }
107
96 update ()
108 update ()
97 {
109 {
98 this.previousStep = this.previousStep || 0;
110 this.previousStep = this.previousStep || 0;
@@ -110,10 +122,28 @@ class Game
110 this.activeScenes[i].draw();
122 this.activeScenes[i].draw();
111 }
123 }
112
124
125 this.cleanActiveScenes();
126
113 this.previousStep = stepTime;
127 this.previousStep = stepTime;
114
128
115 TWEEN.update();
129 TWEEN.update();
116
130
117 requestAnimationFrame(this.update);
131 requestAnimationFrame(this.update);
118 }
132 }
133
134 cleanActiveScenes ()
135 {
136 if (this.pendingRemoval.length > 0)
137 {
138 this.pendingRemoval.sort((a, b) => a - b);
139
140 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
141 {
142 if (this.activeScenes[this.pendingRemoval[i]].destroyed)
143 this.activeScenes.removeAt(this.pendingRemoval[i]);
144
145 this.pendingRemoval.splice(i, 1);
146 }
147 }
148 }
119}
149}
framework/engine/scene.js +10 −3
@@ -69,10 +69,17 @@ class Scene
69 69
70 70 destroy ()
71 71 {
72 this.destroyed = true;
73 for (var i = 0; i < this.activeObjects.length; i++)
72 if (!this.destroyed)
74 73 {
75 this.activeObjects[i].destroy();
74 for (var i = 0; i < this.activeObjects.length; i++)
75 {
76 this.activeObjects[i].destroy();
77 }
78 this.game.removeScene(this);
79
80 this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);
81
82 this.destroyed = true;
76 83 }
77 84 }
78 85
@@ -69,10 +69,17 @@ class Scene
69
69
70 destroy ()
70 destroy ()
71 {
71 {
72 this.destroyed = true;
72 if (!this.destroyed)
73 for (var i = 0; i < this.activeObjects.length; i++)
74 {
73 {
75 this.activeObjects[i].destroy();
74 for (var i = 0; i < this.activeObjects.length; i++)
75 {
76 this.activeObjects[i].destroy();
77 }
78 this.game.removeScene(this);
79
80 this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);
81
82 this.destroyed = true;
76 }
83 }
77 }
84 }
78
85