inflate ranges by entity radii

This commit is contained in:
2025-01-13 16:17:34 +09:00
parent 16429a6e1b
commit ffbc4d9803
4 changed files with 110 additions and 141 deletions
+19 -15
View File
@@ -159,6 +159,14 @@ export default class Entity {
this.cooldowns[id] = this.game?.currentTick ?? 0
}
closestTargetTo(cursor, range) {
return this
.game
?.entities
.filter((e) => this.team != e.team && e.distanceTo(cursor) <= range + this.radius + e.radius)
.reduce((e1, e2) => (e1?.distanceTo(cursor) ?? Infinity) < e2.distanceTo(cursor) ? e1 : e2, null)
}
damage(amount) {
this.health = Math.min(Math.max(0, this.health - amount), this.maxHealth)
}
@@ -167,6 +175,10 @@ export default class Entity {
this.game?.despawn(this)
}
distanceTo(vector) {
return this.position.distanceTo(vector)
}
heal(amount) {
this.health = Math.min(Math.max(0, this.health + amount), this.maxHealth)
}
@@ -260,24 +272,16 @@ export default class Entity {
if (this.casting != null) { return false }
if (this.#attacking) {
const attackCursor = this.#dest ?? this.position
const targets = this.game?.entities.filter((e) => e.team != this.team && e.position.clone().sub(attackCursor).length() < this.abilities[0].range)
const target = targets.reduce((prev, e) => {
if (prev == null || e.position.clone().sub(attackCursor).length() > prev.position.clone().sub(attackCursor).length()) {
return e
}
else {
return prev
}
}, null)
if (target != null && target.position.clone().sub(this.position).length() < this.abilities[0].range) {
const cooldown = this.game?.secToTick(this.abilities[0].cooldown) ?? 0
const lastCast = this.cooldowns[this.abilities[0].id]
const cursor = this.#dest ?? this.position
const basicAttack = this.abilities[0]
const target = this.closestTargetTo(cursor, basicAttack.range)
if (target != null && this.distanceTo(target.position) < basicAttack.range) {
const cooldown = this.game?.secToTick(basicAttack.cooldown) ?? 0
const lastCast = this.cooldowns[basicAttack.id]
const timestamp = this.game?.currentTick ?? 0
if (lastCast != null && lastCast + cooldown > timestamp) { return false }
this.castAction(0, attackCursor.x, attackCursor.y, false)
this.castAction(0, cursor.x, cursor.y, false)
return true
}
}