add terrain collision

This commit is contained in:
2024-12-23 18:30:59 +09:00
parent ae6f4c2847
commit 37a77e902c
5 changed files with 51 additions and 24 deletions
+11 -4
View File
@@ -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)