add terrain collision
This commit is contained in:
+11
-4
@@ -28,15 +28,22 @@ export default class Entity {
|
||||
set y(value) { this.position.y = value }
|
||||
|
||||
get collidables() {
|
||||
return this.game?.entities.filter((e) => e.id != this.id).map((e) => e.collider)
|
||||
const entityColliders = (this.game?.entities ?? []).filter((e) => e.id != this.id).map((e) => e.collider)
|
||||
const terrainColliders = (this.game?.terrains ?? []).map((t) => t.colliders).flat()
|
||||
|
||||
return entityColliders.concat(terrainColliders)
|
||||
}
|
||||
|
||||
get collider() {
|
||||
return new SAT.Circle(new SAT.Vector(this.x, this.y), this.radius)
|
||||
}
|
||||
|
||||
isColliding(collider) {
|
||||
return SATX.colliding(this.collider, collider)
|
||||
get colliders() {
|
||||
return [this.collider]
|
||||
}
|
||||
|
||||
isColliding(...colliders) {
|
||||
return SATX.collideObjects(this.collider, colliders)
|
||||
}
|
||||
|
||||
moveAction(x, y) {
|
||||
@@ -71,7 +78,7 @@ export default class Entity {
|
||||
const position = distance <= speed ? fixedDest : stepTaken
|
||||
|
||||
const collider = Entity.collider(position.x, position.y, this.radius)
|
||||
const isColliding = this.collidables.some((c) => SATX.colliding(collider, c))
|
||||
const isColliding = SATX.collideObjects(collider, this.collidables)
|
||||
|
||||
if (!isColliding) {
|
||||
this.position.copy(position)
|
||||
|
||||
Reference in New Issue
Block a user