To know if there’s a wall between two sprites, you have to cast a ray from sprite A to sprite B, so here’s the function that tells you if there’s a wall between two sprites.
function wallBetween(a: Sprite, b: Sprite): boolean {
const locA = a.tilemapLocation()
const locB = b.tilemapLocation()
if (locA.col == locB.col && locA.row == locB.row)
return false
let dirX = b.x - a.x
let dirY = b.y - a.y
let dirDist = Math.sqrt(Math.pow(dirX, 2) + Math.pow(dirY, 2))
if (dirDist < 8.0)
return false
dirX /= dirDist
dirY /= dirDist
let _loc = locA
let curX = a.x
let curY = a.y
while (true) {
curX += dirX *8.0
curY += dirY *8.0
const loc = tiles.getTileLocation(Math.floor(curX /16.0), Math.floor(curY /16.0))
if (loc.col == _loc.col && loc.row == _loc.row)
continue
if (loc.col == locB.col && loc.row == locB.row) {
console.log(`no walls between (${locA.col}, ${locA.row}) and (${locB.col}, ${locB.row})`)
return false
}
if (tiles.tileAtLocationIsWall(loc)) {
console.log(`hit wall at (${loc.col}, ${loc.row}) between (${locA.col}, ${locA.row}) and (${locB.col}, ${locB.row})`)
return true
}
_loc = loc
}
}
This function returns false if there’s no wall between two sprites, and true if otherwise.
If you instead want to know what kind of wall is between sprite A and B, change return false to return null, return true to return tiles.getTileAt(loc.col, loc.row), and : boolean { to : Image {.
but if you instead want to know where the wall between sprite A and B is, change return false to return null, return true to return loc, and : boolean { to : tiles.Location.
but if you instead want to know how this works at all… maybe later I’ll post an explanation, meanwhile, enjoy the free code snippet!
Ok bye!!! :3