mirror of
https://git.leinelab.org/Mal/mr-crocs-adventures.git
synced 2025-09-11 02:57:36 +02:00
Basic implementations
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import GeometryPoint from "../geometry/GeometryPoint.js";
|
||||
import GeometryRect from "../geometry/GeometryRect.js";
|
||||
|
||||
export default class RetroSprite
|
||||
{
|
||||
constructor(image, scale = 1)
|
||||
{
|
||||
this.image = image;
|
||||
this.image = new Image();
|
||||
this.image.src = image;
|
||||
this.canvas = document.createElement('canvas');
|
||||
this.context = this.canvas.getContext('2d');
|
||||
this.position = new GeometryPoint();
|
||||
@@ -13,6 +15,37 @@ export default class RetroSprite
|
||||
this.render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the position of the sprite's center
|
||||
*
|
||||
* @returns {GeometryPoint}
|
||||
*/
|
||||
getCenter()
|
||||
{
|
||||
this.getRect().getCenter();
|
||||
}
|
||||
|
||||
setFootPosition(x, y)
|
||||
{
|
||||
this.position.x = x - this.canvas.width * 0.5;
|
||||
this.position.y = y - this.canvas.height;
|
||||
}
|
||||
|
||||
getRect()
|
||||
{
|
||||
return new GeometryRect(this.position.x, this.position.y, this.canvas.width, this.canvas.height);
|
||||
}
|
||||
|
||||
getWidth()
|
||||
{
|
||||
return this.canvas.width;
|
||||
}
|
||||
|
||||
getHeight()
|
||||
{
|
||||
return this.canvas.height;
|
||||
}
|
||||
|
||||
draw(context)
|
||||
{
|
||||
context.drawImage(this.canvas, this.position.x, this.position.y);
|
||||
@@ -20,7 +53,7 @@ export default class RetroSprite
|
||||
|
||||
hasRectCollisionWith(sprite)
|
||||
{
|
||||
|
||||
return this.getRect().getRectIntersection(sprite.getRect()) !== null;
|
||||
}
|
||||
|
||||
hasCollisionWith(sprite)
|
||||
@@ -31,6 +64,9 @@ export default class RetroSprite
|
||||
render()
|
||||
{
|
||||
let canvasTemp = document.createElement('canvas');
|
||||
canvasTemp.width = this.image.width * this.scale;
|
||||
canvasTemp.height = this.image.height * this.scale;
|
||||
|
||||
let contextTemp = canvasTemp.getContext('2d');
|
||||
contextTemp.drawImage(this.image, 0, 0);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user