Gravity and ground height.

This commit is contained in:
Mal
2020-01-25 13:11:25 +01:00
parent 388799c97e
commit 0cd532f45a
8 changed files with 316 additions and 27 deletions

View File

@@ -1,6 +1,6 @@
import RetroSprite from "./RetroSprite.js";
import GeometryRect from "../geometry/GeometryRect.js";
import RetroArchitectureTile from "./RetroArchitectureTile.js";
import GeometryRectCollection from "../geometry/GeometryRectCollection.js";
export default class RetroArchitecture
{
@@ -24,6 +24,29 @@ export default class RetroArchitecture
}
}
getCollisionRects(rect)
{
let posX = Math.floor(rect.position.x / this.tileWidth) - 2;
let posY = Math.floor(rect.position.y / this.tileHeight) - 2;
let rangeX = parseInt( posX + rect.width / this.tileWidth) + 4;
let rangeY = parseInt(posY + rect.height / this.tileHeight) + 4;
let collection = new GeometryRectCollection();
for (let y = Math.max(0, posY); y < rangeY; y++) {
for (let x = Math.max(0, posX); x < rangeX; x++) {
if (y < this.matrix.length && x < this.matrix[y].length && this.matrix[y][x] !== null) {
let intersection = this.matrix[y][x].rect.getRectIntersection(rect);
if (intersection !== null) {
collection.addRect(intersection);
}
}
}
}
return collection;
}
hasRectCollision(rect)
{
let posX = Math.floor(rect.position.x / this.tileWidth) - 2;

View File

@@ -46,6 +46,21 @@ export default class RetroSprite
return this.canvas.height;
}
getFootHeight()
{
return new GeometryPoint(this.position.x, this.position.y + this.getHeight());
}
getPositionLeftFoot()
{
return new GeometryPoint(this.position.x - this.getWidth() * 0.5, this.position.y);
}
getPositionRightFoot()
{
return new GeometryPoint(this.position.x + this.getWidth() * 0.5, this.position.y);
}
draw(context)
{
context.drawImage(this.canvas, this.position.x, this.position.y);