add buffs
This commit is contained in:
+50
-2
@@ -3,17 +3,19 @@ import Pathfind from './pathfind.js'
|
||||
import SAT from 'sat'
|
||||
import SATX from './satx.js'
|
||||
import Team from './team.js'
|
||||
import Buff from './buff.js'
|
||||
|
||||
export default class Entity {
|
||||
id = crypto.randomUUID()
|
||||
abilities = {}
|
||||
buffs = []
|
||||
casting = null
|
||||
cooldowns = {}
|
||||
dead = false
|
||||
health = null
|
||||
height = 40
|
||||
maxHealth = 1
|
||||
memory = {}
|
||||
memory = {} // TODO: hide from reports but keep public
|
||||
position = null
|
||||
radius = 0
|
||||
speed = 400
|
||||
@@ -151,6 +153,17 @@ export default class Entity {
|
||||
)
|
||||
}
|
||||
|
||||
applyBuff(id) {
|
||||
const index = this.buffs.findIndex((it) => it.id == id)
|
||||
const timestamp = this.game?.currentTick ?? 0
|
||||
if (index > -1) {
|
||||
this.buffs[index].timestamp = timestamp
|
||||
}
|
||||
else {
|
||||
this.buffs.push({ id, timestamp })
|
||||
}
|
||||
}
|
||||
|
||||
collidables() {
|
||||
const entityColliders = (this.game?.entities ?? []).filter((e) => e.id != this.id).map((e) => e.collider())
|
||||
const terrainColliders = (this.game?.terrains ?? []).map((t) => t.colliders()).flat()
|
||||
@@ -179,7 +192,13 @@ export default class Entity {
|
||||
}
|
||||
|
||||
damage(amount) {
|
||||
this.health = Math.min(Math.max(0, this.health - amount), this.maxHealth)
|
||||
let damage = amount
|
||||
if (this.hasBuff(Buff.exposed.id)) {
|
||||
damage *= 3 // TODO: move to buff, make generic
|
||||
this.removeBuff(Buff.exposed.id)
|
||||
}
|
||||
|
||||
this.health = Math.min(Math.max(0, this.health - damage), this.maxHealth)
|
||||
}
|
||||
|
||||
despawn() {
|
||||
@@ -190,6 +209,10 @@ export default class Entity {
|
||||
return this.position.distanceTo(cursor)
|
||||
}
|
||||
|
||||
hasBuff(id) {
|
||||
return this.buffs.some((it) => it.id == id)
|
||||
}
|
||||
|
||||
heal(amount) {
|
||||
this.health = Math.min(Math.max(0, this.health + amount), this.maxHealth)
|
||||
}
|
||||
@@ -202,6 +225,10 @@ export default class Entity {
|
||||
return SATX.collideObjects(this.collider(), colliders)
|
||||
}
|
||||
|
||||
removeBuff(id) {
|
||||
this.buffs = this.buffs.filter((it) => it.id != id)
|
||||
}
|
||||
|
||||
respawn() {
|
||||
this.position = this.#spawnPosition.clone()
|
||||
this.health = this.maxHealth
|
||||
@@ -221,6 +248,7 @@ export default class Entity {
|
||||
this.#cast()
|
||||
this.#checkHealth()
|
||||
this.#move()
|
||||
this.#tickBuffs()
|
||||
this.fixPosition()
|
||||
}
|
||||
|
||||
@@ -375,4 +403,24 @@ export default class Entity {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#tickBuff(index) {
|
||||
const entityBuff = this.buffs[index]
|
||||
if (entityBuff == null) { return }
|
||||
|
||||
const buffDefinition = this.game?.buffs.find((it) => it.id == entityBuff.id)
|
||||
if (buffDefinition == null) { return }
|
||||
|
||||
const buff = { ...buffDefinition, ...entityBuff }
|
||||
const duration = this.game?.secToTick(buff.duration) ?? 0
|
||||
const currentTick = this.game?.currentTick ?? 0
|
||||
|
||||
if (buff.timestamp + duration < currentTick) {
|
||||
this.removeBuff(buff.id)
|
||||
}
|
||||
}
|
||||
|
||||
#tickBuffs() {
|
||||
this.buffs.forEach((_v, i) => this.#tickBuff(i))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user