fix projectiles phasing through stuff

This commit is contained in:
2025-01-17 14:01:18 +09:00
parent 20f8a2f1fe
commit 787b48a4df
4 changed files with 41 additions and 25 deletions
+14 -7
View File
@@ -6,6 +6,7 @@ export default class Projectile {
id = crypto.randomUUID()
after = null
height = 50
memory = {} // TODO: WARNING: currently only used for hit detection (code smell?)
onCollide = null
owner = null
position = new Vector2()
@@ -29,18 +30,18 @@ export default class Projectile {
Object.entries(options).forEach(([key, value]) => this[key] = value)
}
checkCollisions() {
checkCollisions(collider) {
(this.game?.entities ?? []).filter((e) => e.id != this.id).forEach((e) => {
if (e.id == this.owner?.id) { return }
if (SATX.collideObject(this.collider(), e.collider())) {
this.onCollide(this, e)
if (SATX.collideObject(collider, e.collider())) {
if (this.onCollide != null) { this.onCollide(this, e) }
}
})
}
collider() {
return new SAT.Circle(new SAT.Vector(this.x, this.y), this.radius)
return new SAT.Circle(new SAT.Vector(this.position.x, this.position.y), this.radius)
}
despawn() {
@@ -55,11 +56,12 @@ export default class Projectile {
update() {
this.#move()
if (this.onCollide != null) { this.checkCollisions() }
if (this.onCollide != null) { this.checkCollisions(this.collider()) }
this.#checkIfArrived()
}
#checkIfArrived() {
if (this.destination == null) { return }
if (!this.position.equals(this.destination)) { return }
if (this.after != null) {
@@ -71,11 +73,16 @@ export default class Projectile {
#move() {
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)
}
else {
const step = this.destination.clone().sub(this.position).normalize().multiplyScalar(speed)
this.position.add(step)
}
const step = this.destination.clone().sub(this.position).normalize().multiplyScalar(speed)
this.position.add(step)
const tunnel = SATX.entityTunnel(prevPos.x, prevPos.y, this.position.x, this.position.y, this.radius)
if (this.onCollide != null) { this.checkCollisions(tunnel) }
}
}