2023-09-26 04:40:05 -05:00
|
|
|
class Sprite{
|
2024-02-25 06:43:20 -06:00
|
|
|
constructor({position,skin, imageSrc,frames = 1,center,atPosition,sprites={up:'',down:'',left:'',right:''}}){
|
2023-09-26 04:40:05 -05:00
|
|
|
this.position = position
|
|
|
|
this.atPosition = atPosition
|
|
|
|
this.center = center
|
|
|
|
this.image = new Image()
|
|
|
|
this.image.onload = () => {
|
|
|
|
this.loaded = true
|
|
|
|
this.width = this.image.width / this.frames
|
|
|
|
this.height = this.image.height
|
|
|
|
}
|
|
|
|
this.image.src = imageSrc
|
|
|
|
this.loaded = false
|
|
|
|
this.frames = frames
|
|
|
|
this.currentFrame = 0
|
|
|
|
this.elapsedFrames = 0
|
2024-02-24 07:43:25 -06:00
|
|
|
this.frameBuffer = 7
|
2023-09-26 04:40:05 -05:00
|
|
|
this.sprites = sprites
|
2024-02-25 06:43:20 -06:00
|
|
|
this.skin = skin
|
2023-09-26 04:40:05 -05:00
|
|
|
this.Up = new Image()
|
|
|
|
this.Down = new Image()
|
|
|
|
this.Left = new Image()
|
|
|
|
this.Right = new Image()
|
|
|
|
this.Up.src = sprites.up
|
2024-02-25 05:08:57 -06:00
|
|
|
this.Down.src = sprites.down
|
2023-09-26 04:40:05 -05:00
|
|
|
this.Left.src = sprites.left
|
|
|
|
this.Right.src = sprites.right
|
|
|
|
}
|
|
|
|
draw(){
|
|
|
|
if(!this.loaded) return
|
|
|
|
const cropbox = {
|
|
|
|
position: {
|
|
|
|
x: this.width * this.currentFrame,
|
|
|
|
y:0
|
|
|
|
},
|
|
|
|
width:this.width,
|
|
|
|
height:this.height
|
|
|
|
}
|
|
|
|
if(this.center){
|
|
|
|
ctx.drawImage(
|
|
|
|
this.image,
|
|
|
|
cropbox.position.x,
|
|
|
|
cropbox.position.y,
|
|
|
|
cropbox.width,
|
|
|
|
cropbox.height,
|
|
|
|
this.position.x-cropbox.width/2,
|
|
|
|
this.position.y-cropbox.height,
|
|
|
|
this.width,
|
|
|
|
this.height
|
|
|
|
)
|
|
|
|
}else{
|
|
|
|
ctx.drawImage(
|
|
|
|
this.image,
|
|
|
|
cropbox.position.x,
|
|
|
|
cropbox.position.y,
|
|
|
|
cropbox.width,
|
|
|
|
cropbox.height,
|
|
|
|
this.position.x,
|
|
|
|
this.position.y,
|
|
|
|
this.width,
|
|
|
|
this.height
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!this.atPosition) {this.updateFrames()}else{this.currentFrame=0}
|
|
|
|
}
|
|
|
|
updateFrames(){
|
|
|
|
this.elapsedFrames++
|
|
|
|
// this.currentFrame++
|
2024-02-24 07:43:25 -06:00
|
|
|
// console.log(this.currentFrame);
|
|
|
|
if((this.elapsedFrames % this.frameBuffer === 0) && this.frames>1){
|
|
|
|
|
2023-09-26 04:40:05 -05:00
|
|
|
if(this.currentFrame < this.frames-1){
|
|
|
|
this.currentFrame++
|
|
|
|
}
|
|
|
|
else{
|
2024-02-24 07:43:25 -06:00
|
|
|
this.currentFrame=1
|
2023-09-26 04:40:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 04:16:40 -05:00
|
|
|
}
|