56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
import { Sprite } from './sprite.js'
|
|
import { Rotation } from '../logic.js'
|
|
|
|
|
|
export const startGraphicsUpdater = () => {
|
|
|
|
let sprites = []
|
|
|
|
/**
|
|
* @type {(data: import("../logic.js").GameState) => void}
|
|
*/
|
|
return (data) => {
|
|
|
|
if (!data.started) return
|
|
|
|
let players = Object.keys(data.players).filter(k => data.players[k] !== undefined)
|
|
|
|
if (sprites.length !== players.length) {
|
|
|
|
for (const sprite of sprites) {
|
|
if (sprite !== undefined) {
|
|
sprite.destroy()
|
|
}
|
|
}
|
|
|
|
sprites = Array(players)
|
|
sprites.fill(undefined)
|
|
|
|
for (let id of players) {
|
|
let sprite = new Sprite("/static/tux.png", data.map)
|
|
sprite.show()
|
|
sprite.resize(1.5,1.5)
|
|
sprites[id] = sprite
|
|
}
|
|
}
|
|
|
|
for (let id of players) {
|
|
let pos = data.players[id].pos
|
|
sprites[id].move(pos[0], pos[1])
|
|
switch (data.players[id].move_rot) {
|
|
case Rotation.NORTH:
|
|
sprites[id].rotate(270)
|
|
break
|
|
case Rotation.EAST:
|
|
sprites[id].rotate(0)
|
|
break
|
|
case Rotation.SOUTH:
|
|
sprites[id].rotate(90)
|
|
break
|
|
case Rotation.WEST:
|
|
sprites[id].rotate(180)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|