add cast times and cooldowns

This commit is contained in:
2025-01-12 03:30:52 +09:00
parent 2eb914a680
commit e0dd7dcaf3
7 changed files with 142 additions and 47 deletions
+51 -4
View File
@@ -9,15 +9,19 @@ export default class Entity {
id = crypto.randomUUID()
speed = 400
radius = 0
health = 1
health = 1 // TODO: health can go into negatives and can go over maxHealth
maxHealth = 1
abilities = [
Ability.homingProjectile({ range: 600, radius: 3, speed: 500, onCollide: Effect.damage({ despawn: true }) }),
Ability.skillshot({ range: 800, radius: 5, speed: 3000, onCollide: Effect.damage({ despawn: true }) }),
Ability.basicAttack(600, 5, 600),
Ability.straightShot(800, 7, 3000),
() => {},
() => {},
() => {},
]
casting = null
// TODO: teams
cooldowns = {}
#position = new Vector2()
#dest = null
@@ -68,8 +72,42 @@ export default class Entity {
])
}
cast() {
if (this.casting == null) {
return false
}
const castTime = this.game?.secToTick(this.casting.ability.castTime) ?? 0
const castStart = this.casting.timestamp
const timestamp = this.game?.currentTick ?? 0
if (castStart + castTime < timestamp) {
return false
}
this.casting.ability.effect.bind(this)(this.casting.cursor)
if (this.casting.ability.cooldown != null) {
this.cooldowns[this.casting.ability.id] = timestamp
}
this.casting = null
return true
}
castAction(slot, x, y) {
this.abilities[slot].bind(this)(x, y)
const ability = this.abilities[slot]
const cursor = new Vector2(x, y)
const cooldown = this.game?.secToTick(ability.cooldown) ?? 0
const lastCast = this.cooldowns[ability.id]
const timestamp = this.game?.currentTick ?? 0
if (lastCast != null && lastCast + cooldown > timestamp) {
return false
}
this.#dest = null
this.casting = { ability, cursor, timestamp }
return true
}
collidables() {
@@ -87,10 +125,18 @@ export default class Entity {
return entityColliders.concat(terrainColliders)
}
damage(amount, source = null) {
this.health = Math.min(Math.max(0, this.health - amount), this.maxHealth)
}
despawn() {
this.game?.despawn(this)
}
heal(amount, source = null) {
this.health = Math.min(Math.max(0, this.health + amount), this.maxHealth)
}
fixPosition() {
this.#position = SATX.fixCollisions(this.#position, this.collidables(), this.radius, this.game?.width, this.game?.height)
}
@@ -166,6 +212,7 @@ export default class Entity {
}
update() {
this.cast()
this.takeStep()
this.fixPosition()
}