add homing projectiles

This commit is contained in:
2025-01-12 00:29:11 +09:00
parent 51b61ab449
commit 2eb914a680
3 changed files with 43 additions and 4 deletions
+15 -2
View File
@@ -13,12 +13,25 @@ export default class Projectile {
#position = new Vector2()
#dest = null
#homingTarget = null
#game = null
get collider() {
return new SAT.Circle(new SAT.Vector(this.x, this.y), this.radius)
}
get homing() {
return !!this.#homingTarget
}
get destination() {
return this.#dest ?? this.#homingTarget?.position
}
set homingTarget(value) {
this.#homingTarget = value
}
constructor(...options) {
Object.entries(options).forEach((value, key) => this[key] = value)
}
@@ -43,7 +56,7 @@ export default class Projectile {
}
checkIfArrived() {
if (!this.#position.equals(this.#dest)) { return }
if (!this.#position.equals(this.destination)) { return }
if (this.after != null) {
this.after(this)
@@ -68,7 +81,7 @@ export default class Projectile {
takeStep() {
const speed = (this.speed / (this.game?.tickBudget ?? 1000))
const destination = this.#dest
const destination = this.destination
const difference = destination.clone().sub(this.position)
const distance = difference.length()
const direction = difference.clone().normalize()