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
+27 -1
View File
@@ -4,7 +4,6 @@ import Projectile from './projectile.js'
export default class Ability {
static skillshot({ range, radius, speed, onCollide, after }) {
return function(x, y) {
console.log(this)
const projectile = new Projectile()
const destination = this.position.clone().add(new Vector2(x, y).sub(this.position).normalize().multiplyScalar(range))
projectile.owner = this.id
@@ -17,4 +16,31 @@ export default class Ability {
this.game?.spawnProjectile(projectile)
}
}
static homingProjectile({ range, radius, speed, onCollide, after }) {
return function(x, y) {
const cursor = new Vector2(x, y)
let closest = null
let distance = Infinity
this.game?.entities.filter((e) => e.id != this.id && e.position.clone().sub(this.position).length() < range).forEach((e) => {
const newDistance = e.position.clone().sub(cursor).length() < distance
if (newDistance < distance) {
closest = e
distance = newDistance
}
})
if (closest == null) { return } // TODO: refund
const projectile = new Projectile()
projectile.owner = this.id
projectile.position.copy(this.position)
projectile.homingTarget = closest
projectile.radius = radius
projectile.speed = speed
projectile.after = after
projectile.onCollide = onCollide
this.game?.spawnProjectile(projectile)
}
}
}