Terrain collision.

This commit is contained in:
Mal
2020-01-25 23:30:36 +01:00
parent 0cd532f45a
commit b7f669224d
3 changed files with 146 additions and 44 deletions

View File

@@ -1,6 +1,7 @@
import RetroSprite from "./RetroSprite.js";
import RetroArchitectureTile from "./RetroArchitectureTile.js";
import GeometryRectCollection from "../geometry/GeometryRectCollection.js";
import GeometryPoint from "../geometry/GeometryPoint.js";
export default class RetroArchitecture
{
@@ -67,10 +68,10 @@ export default class RetroArchitecture
return false;
}
getTileForPosition(position)
getTileForPosition(position, offsetX = 0, offsetY = 0)
{
let x = parseInt(position.x / this.tileWidth);
let y = parseInt(position.y / this.tileHeight);
let x = parseInt(position.x / this.tileWidth) + offsetX;
let y = parseInt(position.y / this.tileHeight) + offsetY;
if (x < 0 || x >= this.columns || y < 0 || y >= this.rows) {
return null;
@@ -79,13 +80,28 @@ export default class RetroArchitecture
return {x: x, y: y};
}
getCeilingHeight(position)
{
let tilePosition = this.getTileForPosition(position, 0);
while (tilePosition !== null && tilePosition.y > 0) {
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
return tilePosition.y * this.tileHeight + this.tileHeight;
}
tilePosition.y--;
}
return 0;
}
getGroundHeight(position)
{
let tilePosition = this.getTileForPosition(position);
while (tilePosition !== null && tilePosition.y < this.rows) {
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
return tilePosition.y * this.tileHeight
return tilePosition.y * this.tileHeight;
}
tilePosition.y++;
@@ -94,6 +110,36 @@ export default class RetroArchitecture
return this.tileHeight * this.rows;
}
getWallRight(position)
{
let tilePosition = this.getTileForPosition(new GeometryPoint(position.x, position.y), 1, -1);
while (tilePosition !== null && tilePosition.x < this.columns) {
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
return tilePosition.x * this.tileWidth - this.tileWidth * 0.5;
}
tilePosition.x++;
}
return this.tileWidth * this.columns;
}
getWallLeft(position)
{
let tilePosition = this.getTileForPosition(new GeometryPoint(position.x, position.y), -1,-1);
while (tilePosition !== null && tilePosition.x > 0) {
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
return tilePosition.x * this.tileWidth + this.tileWidth * 1.5;
}
tilePosition.x--;
}
return 0;
}
draw(context)
{
for (let y = 0; y < this.rows; y++) {