The spritesheets we added as textures actually include multiple frames that can be played as an animation. With a couple simple changes to the TextureSystem and AppearanceSystem we can play these animations.
|
@@ -5,7 +5,7 @@ export function createAppearanceSystem(context: Context) {
|
|
|
5
5
|
const {
|
|
6
6
|
world,
|
|
7
7
|
components: { Appearance, Texture },
|
|
8
|
-
phaser: { objectPool },
|
|
8
|
+
phaser: { objectPool, scene },
|
|
9
9
|
} = context;
|
|
10
10
|
|
|
11
11
|
const appearances = defineUpdateQuery(world, [Has(Appearance)]);
|
|
@@ -20,7 +20,11 @@ export function createAppearanceSystem(context: Context) {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
const object = objectPool.get(entity);
|
|
23
|
-
if (hasComponent(Texture, appearance.value))
|
|
23
|
+
if (hasComponent(Texture, appearance.value)) {
|
|
24
|
+
object.setTexture(String(appearance.value));
|
|
25
|
+
const animKey = "anim" + String(appearance.value);
|
|
26
|
+
if (scene.anims.get(animKey)) object.anims.play(animKey);
|
|
27
|
+
}
|
|
24
28
|
}
|
|
25
29
|
});
|
|
26
30
|
}
|
|
@@ -29,11 +29,25 @@ export function createTextureSystem(context: Context) {
|
|
|
29
29
|
loader.spritesheet(String(textureEntity), texture.value, { frameWidth: 16, frameHeight: 16 })
|
|
30
30
|
);
|
|
31
31
|
|
|
32
|
+
// Create an animation from the spritesheet
|
|
33
|
+
const frames = scene.anims.generateFrameNumbers(String(textureEntity), { start: 0 });
|
|
34
|
+
const animKey = "anim" + String(textureEntity);
|
|
35
|
+
if (frames.length > 1) {
|
|
36
|
+
scene.anims.create({
|
|
37
|
+
key: animKey,
|
|
38
|
+
frames,
|
|
39
|
+
repeat: -1,
|
|
40
|
+
frameRate: 5,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
32
44
|
// Update entities with this texture
|
|
33
45
|
const entities = defineQuery([HasValue(Appearance, { value: textureEntity })]).get();
|
|
34
46
|
for (const entity of entities) {
|
|
35
|
-
// Since we used the entity id as key when loading, we can refer to it when setting the phaser texture
|
|
36
|
-
objectPool.get(entity)
|
|
47
|
+
// Since we used the entity id as key when loading, we can refer to it when setting the phaser texture and play the animation
|
|
48
|
+
const object = objectPool.get(entity);
|
|
49
|
+
object.setTexture(String(textureEntity));
|
|
50
|
+
if (scene.anims.get(animKey)) object.anims.play(animKey);
|
|
37
51
|
}
|
|
38
52
|
}
|
|
39
53
|
});
|