-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.mjs
More file actions
149 lines (127 loc) · 3.43 KB
/
Scene.mjs
File metadata and controls
149 lines (127 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"use strict";
import { Engine } from "./Engine.mjs";
import { Sprite } from "./Sprite.mjs";
import { Static } from "./Static.mjs";
/**
* Represents a scene, which is a collection of sprites that are rendered together.
* @extends Sprite
*/
export class Scene extends Sprite {
/**
* Creates a new Scene.
*/
constructor() {
super();
this.height = Engine.height;
this.width = Engine.width;
this._sprites = [];
this._spritesQueue = [];
}
/**
* Initializes the scene.
*/
init() {
// no default behavior
}
/**
* Adds a sprite to the scene.
* @param {Sprite} sprite The sprite to add.
* @returns {Scene} This scene.
*/
add(sprite) {
this._spritesQueue.push(sprite);
sprite.scene = this;
sprite.init();
sprite.x -= sprite.speedX;
sprite.y -= sprite.speedY;
return this;
}
/**
* Builds a tilemap from a map.
* @param {string[][]} map The map.
* @param {function(string): Sprite} [tileFactory=null] A function that creates a tile from an ID.
* @param {number} [offsetX=0] The horizontal offset between tiles.
* @param {number} [offsetY=0] The vertical offset between tiles.
* @param {number} [x=0] The horizontal position of the tilemap.
* @param {number} [y=0] The vertical position of the tilemap.
* @returns {Scene} This scene.
*/
build(map, tileFactory = null, offsetX = 0, offsetY = 0, x = 0, y = 0) {
tileFactory =
tileFactory ??
function baseTileFactory(id) {
return new Sprite().addTag("tile").setImage(id);
};
for (let i = 0; i < map.length; ++i) {
const LINE = map[i];
for (let j = 0; j < LINE.length; ++j) {
const ID = map[i][j];
if (!ID || "" == ID.trim()) {
continue;
}
const TILE = tileFactory(ID);
if (TILE) {
const X = offsetX || TILE.width;
const Y = offsetY || TILE.height;
TILE.x = x + j * X;
TILE.y = y + i * Y;
this.add(TILE);
}
}
}
return this;
}
/**
* Synchronizes the scene.
* @returns {boolean} Whether the scene has expired.
*/
sync() {
Engine.paint(this, this.layerIndex);
let sprites = [];
const solidSprites = [];
for (let i = 0; i < this._sprites.length; ++i) {
const sprite = this._sprites[i];
if (sprite.sync()) {
if (sprite.essential) {
this.setExpired();
}
} else {
if (sprite.solid) {
solidSprites.push(sprite);
}
sprites.push(sprite);
Engine.paint(sprite, sprite.layerIndex);
}
sprite.collided = false;
}
Static.checkCollisions(solidSprites);
this._sprites = sprites.concat(this._spritesQueue);
this._spritesQueue = [];
return Sprite.prototype.sync.call(this);
}
/**
* Gets all sprites with a specific tag.
* @param {string} tag The tag.
* @returns {Sprite[]} The sprites with the tag.
*/
getObjectsWithTag(tag) {
const RESULT = [];
const SPRITES = this._sprites.concat(this._spritesQueue);
for (let i = 0; i < SPRITES.length; ++i) {
const SPRITE = SPRITES[i];
if (SPRITE.hasTag(tag)) {
RESULT.push(SPRITE);
}
}
return RESULT;
}
/**
* Sets the transition to the next scene.
* @param {Transition} transition The transition.
* @returns {Scene} This scene.
*/
setTransition(transition) {
this.transition = transition;
return this;
}
}