mr-crocs-adventures/js/Movable.js

117 lines
2.6 KiB
JavaScript

import GeometryPoint from "./geometry/GeometryPoint.js";
export default class Movable
{
constructor(defaultAnimation, speed = 1)
{
this.currentAnimation = 'DEFAULT';
this.animations = {
DEFAULT: defaultAnimation,
};
this.position = new GeometryPoint();
this.speed = speed;
this.jumpHeight = 35;
this.fallSpeed = 0;
}
playAnimation(animation, timestamp)
{
this.currentAnimation = animation;
this.animations[animation].play(timestamp);
}
addAnimation(name, animation)
{
this.animations[name] = animation;
}
moveLeft(delta = 1)
{
this.position.x -= this.speed * delta;
}
moveRight(delta = 1)
{
this.position.x += this.speed * delta;
}
getRect()
{
return this.animations[this.currentAnimation].getRect();
}
getCenter()
{
return new GeometryPoint(
this.position.x + this.getWidth() * 0.5,
this.position.y + this.getHeight() * 0.5
);
}
getWidth()
{
return this.getRect().width;
}
getHeight()
{
return this.getRect().height;
}
getPositionFootLeft()
{
return new GeometryPoint(
this.position.x - this.animations[this.currentAnimation].getWidth() * 0.5, this.position.y
);
}
getPositionFootRight()
{
return new GeometryPoint(
this.position.x + this.getWidth() * 0.5, this.position.y
);
}
getPositionHeadLeft()
{
return new GeometryPoint(
this.position.x - this.animations[this.currentAnimation].getWidth() * 0.5,
this.position.y - this.animations[this.currentAnimation].getHeight()
);
}
getPositionHeadRight()
{
return new GeometryPoint(
this.position.x + this.getWidth() * 0.5,
this.position.y - this.animations[this.currentAnimation].getHeight()
);
}
jump()
{
this.fallSpeed -= this.jumpHeight;
this.isJumping = true;
}
getFootHeight()
{
return new GeometryPoint(
this.position.x,
this.position.y + this.animations[this.currentAnimation].getHeight()
);
}
setFootHeight(height)
{
this.position.y = height - this.animations[this.currentAnimation].getHeight();
}
draw(context, camera)
{
if (camera.isMovableInsideView(this)) {
this.animations[this.currentAnimation].setFootPosition(this.position.x, this.position.y);
this.animations[this.currentAnimation].draw(context, camera);
}
}
}