feat: sound

This commit is contained in:
cha0s 2024-06-25 12:29:09 -05:00
parent 220acccc08
commit 694cd90645
5 changed files with 37 additions and 1 deletions

View File

@ -0,0 +1,29 @@
import Schema from '@/ecs/schema.js';
export default function(Component) {
return class Sound extends Component {
mergeDiff(original, update) {
const merged = {};
if (update.play) {
merged.play = [
...(original.play ?? []),
...update.play,
];
}
return merged;
}
instanceFromSchema() {
const Component = this;
const Instance = super.instanceFromSchema();
return class SoundInstance extends Instance {
play(source) {
Component.markChange(this.entity, 'play', [source]);
}
};
}
static schema = new Schema({
type: 'object',
properties: {},
});
}
}

View File

@ -188,6 +188,7 @@ export default class Engine {
Position: {x: 368, y: 368}, Position: {x: 368, y: 368},
VisibleAabb: {}, VisibleAabb: {},
Speed: {speed: 100}, Speed: {speed: 100},
Sound: {},
Sprite: { Sprite: {
anchor: {x: 0.5, y: 0.8}, anchor: {x: 0.5, y: 0.8},
animation: 'moving:down', animation: 'moving:down',

View File

@ -159,6 +159,11 @@ export default function Ui({disconnected}) {
for (const id in payload.ecs) { for (const id in payload.ecs) {
const entity = ecs.get(id); const entity = ecs.get(id);
const update = payload.ecs[id]; const update = payload.ecs[id];
if (update.Sound?.play) {
for (const sound of update.Sound.play) {
(new Audio(sound)).play();
}
}
if (update?.MainEntity) { if (update?.MainEntity) {
setMainEntity(localMainEntity = id); setMainEntity(localMainEntity = id);
} }

BIN
public/assets/hoe/dig.wav Normal file

Binary file not shown.

View File

@ -1,4 +1,4 @@
const {Controlled, Position, Sprite, Wielder} = wielder const {Controlled, Position, Sound, Sprite, Wielder} = wielder
const {TileLayers} = ecs.get(1) const {TileLayers} = ecs.get(1)
const layer = TileLayers.layer(0) const layer = TileLayers.layer(0)
const projected = Wielder.project(Position.tile, item.tool.projection) const projected = Wielder.project(Position.tile, item.tool.projection)
@ -7,6 +7,7 @@ Controlled.locked = 1;
const [, direction] = Sprite.animation.split(':'); const [, direction] = Sprite.animation.split(':');
for (let i = 0; i < 2; ++i) { for (let i = 0; i < 2; ++i) {
Sound.play('/assets/hoe/dig.wav');
Sprite.animation = ['moving', direction].join(':'); Sprite.animation = ['moving', direction].join(':');
await wait(300) await wait(300)
Sprite.animation = ['idle', direction].join(':'); Sprite.animation = ['idle', direction].join(':');