make projectiles use bounding boxes too
This commit is contained in:
+30
-17
@@ -36,17 +36,6 @@ export default class Projectile {
|
||||
}
|
||||
}
|
||||
|
||||
checkCollisions(collider) {
|
||||
(this.game?.entities ?? []).filter((e) => e.id != this.id).forEach((e) => {
|
||||
if (this.game == null) { return }
|
||||
if (e.id == this.owner?.id) { return }
|
||||
|
||||
if (SATX.collideObject(collider, e.collider())) {
|
||||
if (this.onCollide != null) { this.onCollide(this, e) }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
collider() {
|
||||
return new SAT.Circle(new SAT.Vector(this.position.x, this.position.y), this.radius)
|
||||
}
|
||||
@@ -55,10 +44,14 @@ export default class Projectile {
|
||||
this.game?.despawn(this)
|
||||
}
|
||||
|
||||
setPosition(vector) {
|
||||
this.position.copy(vector)
|
||||
this.#calculateBbox()
|
||||
}
|
||||
|
||||
update() {
|
||||
this.#move()
|
||||
this.#calculateBbox()
|
||||
if (this.onCollide != null) { this.checkCollisions(this.collider()) }
|
||||
this.#checkStationaryCollisions()
|
||||
this.#checkIfArrived()
|
||||
}
|
||||
|
||||
@@ -80,21 +73,41 @@ export default class Projectile {
|
||||
this.despawn()
|
||||
}
|
||||
|
||||
#checkStationaryCollisions() {
|
||||
if (this.onCollide == null) { return }
|
||||
|
||||
const bbox = this.bbox
|
||||
const entitiesAndTerrains = this.game?.entities ?? []
|
||||
const bboxCheckedObstacles = entitiesAndTerrains.filter((it) => SATX.bboxCheck(bbox, it.bbox))
|
||||
if (bboxCheckedObstacles.length > 0) {
|
||||
const colliders = bboxCheckedObstacles.map((it) => it.colliders()).flat()
|
||||
const collider = this.collider()
|
||||
colliders.filter((it) => SATX.collideObject(collider, it)).forEach((it) => this.onCollide(this, it))
|
||||
}
|
||||
}
|
||||
|
||||
#move() {
|
||||
if (this.destination == null) { return }
|
||||
|
||||
const speed = (this.speed / (this.game?.tickBudget ?? 1000))
|
||||
const prevPos = this.position.clone()
|
||||
if (this.position.distanceTo(this.destination) < speed) {
|
||||
this.position.copy(this.destination)
|
||||
this.setPosition(this.destination)
|
||||
}
|
||||
else {
|
||||
const step = this.destination.clone().sub(this.position).normalize().multiplyScalar(speed)
|
||||
this.position.add(step)
|
||||
}
|
||||
|
||||
// TODO: decouple from entity's tunnel collider
|
||||
const tunnel = Entity.tunnelCollider(prevPos.x, prevPos.y, this.position.x, this.position.y, this.radius)
|
||||
if (this.onCollide != null) { this.checkCollisions(tunnel) }
|
||||
if (this.onCollide != null) {
|
||||
const bbox = Entity.tunnelBbox(position.x, position.y, destination.x, destination.y, this.radius)
|
||||
const entitiesAndTerrains = this.game?.entities ?? []
|
||||
const bboxCheckedObstacles = entitiesAndTerrains.filter((it) => SATX.bboxCheck(bbox, it.bbox))
|
||||
if (bboxCheckedObstacles.length > 0) {
|
||||
const colliders = bboxCheckedObstacles.map((it) => it.colliders()).flat()
|
||||
const collider = Entity.tunnelCollider(prevPos.x, prevPos.y, this.position.x, this.position.y, this.radius)
|
||||
colliders.filter((it) => SATX.collideObject(collider, it)).forEach((it) => this.onCollide(this, it))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user