-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scene.mjs
108 lines (86 loc) · 2.06 KB
/
Scene.mjs
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
"use strict";
import { Engine } from "./Engine.mjs";
import { Sprite } from "./Sprite.mjs";
import { Static } from "./Static.mjs";
export class Scene extends Sprite {
constructor() {
super();
this.height = Engine.height;
this.width = Engine.width;
this._sprites = [];
this._spritesQueue = [];
}
init() {
// no default behavior
}
add(sprite) {
this._spritesQueue.push(sprite);
sprite.scene = this;
sprite.init();
sprite.x -= sprite.speedX;
sprite.y -= sprite.speedY;
return this;
}
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;
}
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);
}
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;
}
setTransition(transition) {
this.transition = transition;
return this;
}
}