2020-02-29 13:45:56 +01:00
|
|
|
var asdf = require("../../../lib/index");
|
2020-02-29 08:27:07 +01:00
|
|
|
const { TileSprite, Texture, math } = asdf;
|
|
|
|
const texture = new Texture("./res/images/characters.png");
|
|
|
|
|
|
|
|
class Snake extends TileSprite {
|
|
|
|
constructor () {
|
|
|
|
super(texture, 32, 32);
|
|
|
|
this.scale = { x: 2, y: 2 };
|
|
|
|
|
|
|
|
this.rate = 0.1;
|
|
|
|
this.curTime = 0;
|
|
|
|
this.curFrame = 0;
|
|
|
|
this.frames = [
|
|
|
|
{ x: 0, y: 3 },
|
|
|
|
{ x: 1, y: 3 },
|
|
|
|
{ x: 2, y: 3 },
|
|
|
|
{ x: 3, y: 3 }
|
|
|
|
];
|
|
|
|
this.frame = this.frames[this.curFrame];
|
|
|
|
|
|
|
|
this.speed = math.rand(50, 100)
|
|
|
|
this.pos.y = math.rand(0, 320);
|
|
|
|
this.pos.x = math.rand(-32, 320);
|
|
|
|
}
|
|
|
|
update (dt, t) {
|
|
|
|
const { rate, frames } = this;
|
|
|
|
this.curTime += dt;
|
|
|
|
if (this.curTime > rate) {
|
|
|
|
this.frame = frames[this.curFrame++ % frames.length];
|
|
|
|
this.curTime -= rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pos.x += (dt * this.speed);
|
|
|
|
if (this.pos.x > 640 + 32) {
|
|
|
|
this.pos.y = math.rand(-16, 304);
|
|
|
|
this.pos.x = -32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 13:45:56 +01:00
|
|
|
module.exports = Snake;
|