diff --git a/app/ecs-components/sound.js b/app/ecs-components/sound.js new file mode 100644 index 0000000..ed7b792 --- /dev/null +++ b/app/ecs-components/sound.js @@ -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: {}, + }); + } +} diff --git a/app/engine.js b/app/engine.js index 826fdb6..3671cf1 100644 --- a/app/engine.js +++ b/app/engine.js @@ -188,6 +188,7 @@ export default class Engine { Position: {x: 368, y: 368}, VisibleAabb: {}, Speed: {speed: 100}, + Sound: {}, Sprite: { anchor: {x: 0.5, y: 0.8}, animation: 'moving:down', diff --git a/app/react-components/ui.jsx b/app/react-components/ui.jsx index ef2404a..b72e838 100644 --- a/app/react-components/ui.jsx +++ b/app/react-components/ui.jsx @@ -159,6 +159,11 @@ export default function Ui({disconnected}) { for (const id in payload.ecs) { const entity = ecs.get(id); const update = payload.ecs[id]; + if (update.Sound?.play) { + for (const sound of update.Sound.play) { + (new Audio(sound)).play(); + } + } if (update?.MainEntity) { setMainEntity(localMainEntity = id); } diff --git a/public/assets/hoe/dig.wav b/public/assets/hoe/dig.wav new file mode 100644 index 0000000..41bff0b Binary files /dev/null and b/public/assets/hoe/dig.wav differ diff --git a/public/assets/hoe/start.js b/public/assets/hoe/start.js index 96e8a00..47e4568 100644 --- a/public/assets/hoe/start.js +++ b/public/assets/hoe/start.js @@ -1,4 +1,4 @@ -const {Controlled, Position, Sprite, Wielder} = wielder +const {Controlled, Position, Sound, Sprite, Wielder} = wielder const {TileLayers} = ecs.get(1) const layer = TileLayers.layer(0) const projected = Wielder.project(Position.tile, item.tool.projection) @@ -7,6 +7,7 @@ Controlled.locked = 1; const [, direction] = Sprite.animation.split(':'); for (let i = 0; i < 2; ++i) { + Sound.play('/assets/hoe/dig.wav'); Sprite.animation = ['moving', direction].join(':'); await wait(300) Sprite.animation = ['idle', direction].join(':');