mr-crocs-adventures/js/Movable.js

117 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2020-01-22 22:50:45 +01:00
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;
2020-01-25 13:11:25 +01:00
this.jumpHeight = 35;
2020-01-23 23:09:03 +01:00
this.fallSpeed = 0;
2020-01-22 22:50:45 +01:00
}
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;
}
2020-01-23 23:09:03 +01:00
getRect()
{
return this.animations[this.currentAnimation].getRect();
}
2020-02-09 22:06:59 +01:00
getCenter()
{
return new GeometryPoint(
this.position.x + this.getWidth() * 0.5,
this.position.y + this.getHeight() * 0.5
);
}
2020-01-25 23:30:36 +01:00
getWidth()
{
return this.getRect().width;
}
getHeight()
{
return this.getRect().height;
}
2020-01-25 13:11:25 +01:00
getPositionFootLeft()
{
return new GeometryPoint(
this.position.x - this.animations[this.currentAnimation].getWidth() * 0.5, this.position.y
);
}
getPositionFootRight()
{
return new GeometryPoint(
2020-01-25 23:30:36 +01:00
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()
2020-01-25 13:11:25 +01:00
);
}
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();
}
2020-02-09 22:06:59 +01:00
draw(context, camera)
2020-01-22 22:50:45 +01:00
{
if (camera.isMovableInsideView(this)) {
this.animations[this.currentAnimation].setFootPosition(this.position.x, this.position.y);
this.animations[this.currentAnimation].draw(context, camera);
}
2020-01-22 22:50:45 +01:00
}
}