tuxman/client/js/sprite.js

44 lines
866 B
JavaScript
Raw Normal View History

2023-06-13 03:47:43 +00:00
export class Sprite {
constructor(image_src, map) {
this.element = document.createElement("img")
this.element.src = image_src
this.element.className = "sprite"
this.map = map
this.map.container.appendChild(this.element)
this.x = 0
this.y = 0
this.hide()
}
#update_pos() {
this.element.style.left = `${100/this.map.width*this.x}%`,
this.element.style.top = `${100/this.map.height*this.y}%`
}
set_pos(x, y) {
this.x = x
this.y = y
this.#update_pos()
}
add_pos(x, y) {
this.x += x
this.y += y
this.#update_pos()
}
hide() {
this.element.style.display = "none"
}
show() {
this.element.style.display = "initial"
}
destory() {
this.element.remove()
}
}