Added some utilites for collision detection
This commit is contained in:
parent
fb3aa86e1d
commit
c31cd2b30a
@ -54,6 +54,20 @@ class TileMap extends Container {
|
||||
setFrameAtPixelPos(pos, frame) {
|
||||
return this.setFrameAtMapPos(this.pixelToMapPos(pos), frame);
|
||||
}
|
||||
|
||||
tilesAtCorners(bounds, xo = 0, yo = 0) {
|
||||
return [
|
||||
[bounds.x, bounds.y], // Top-left
|
||||
[bounds.x + bounds.w, bounds.y], // Top-right
|
||||
[bounds.x, bounds.y + bounds.h], // Bottom-left
|
||||
[bounds.x + bounds.w, bounds.y + bounds.h] // Bottom-right
|
||||
].map(([x, y]) =>
|
||||
this.tileAtPixelPos({
|
||||
x: x + xo,
|
||||
y: y + yo
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TileMap;
|
16
lib/movement/deadInTracks.js
Normal file
16
lib/movement/deadInTracks.js
Normal file
@ -0,0 +1,16 @@
|
||||
const entity = require("../utilities/entity");
|
||||
|
||||
function deadInTracks(ent, map, x = 0, y = 0) {
|
||||
const bounds = entity.bounds(ent);
|
||||
const tiles = map.tilesAtCorners(bounds, x, y);
|
||||
const walks = tiles.map(t => t && t.frame.walkable);
|
||||
const blocked = walks.some(w => !w);
|
||||
if (blocked) {
|
||||
x = 0,
|
||||
y = 0;
|
||||
}
|
||||
return { x, y };
|
||||
}
|
||||
module.exports = {
|
||||
deadInTracks
|
||||
};
|
51
lib/movement/wallslide.js
Normal file
51
lib/movement/wallslide.js
Normal file
@ -0,0 +1,51 @@
|
||||
const entity = require("../utils/entity.js");
|
||||
|
||||
function wallslide(ent, map, x = 0, y = 0) {
|
||||
let tiles;
|
||||
let tileEdge;
|
||||
const bounds = entity.bounds(ent);
|
||||
|
||||
// Final amounts of movement to allow
|
||||
let xo = x;
|
||||
let yo = y;
|
||||
|
||||
// Check vertical movement
|
||||
if (y !== 0) {
|
||||
tiles = map.tilesAtCorners(bounds, 0, yo);
|
||||
const [tl, tr, bl, br] = tiles.map(t => t && t.frame.walkable);
|
||||
|
||||
// Hit your head
|
||||
if (y < 0 && !(tl && tr)) {
|
||||
tileEdge = tiles[0].pos.y + tiles[0].h;
|
||||
yo = tileEdge - bounds.y;
|
||||
}
|
||||
// Hit your feet
|
||||
if (y > 0 && !(bl && br)) {
|
||||
tileEdge = tiles[2].pos.y - 1;
|
||||
yo = tileEdge - (bounds.y + bounds.h);
|
||||
}
|
||||
}
|
||||
|
||||
// Check horizontal movement
|
||||
if (x !== 0) {
|
||||
tiles = map.tilesAtCorners(bounds, xo, yo);
|
||||
const [tl, tr, bl, br] = tiles.map(t => t && t.frame.walkable);
|
||||
|
||||
// Hit left edge
|
||||
if (x < 0 && !(tl && bl)) {
|
||||
tileEdge = tiles[0].pos.x + tiles[0].w;
|
||||
xo = tileEdge - bounds.x;
|
||||
}
|
||||
// Hit right edge
|
||||
if (x > 0 && !(tr && br)) {
|
||||
tileEdge = tiles[1].pos.x - 1;
|
||||
xo = tileEdge - (bounds.x + bounds.w);
|
||||
}
|
||||
}
|
||||
// xo & yo contain the amount we're allowed to move by.
|
||||
return { x: xo, y: yo };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
wallslide
|
||||
};
|
Loading…
Reference in New Issue
Block a user