Health and damage
Health holds hit points. Hitboxes, kill zones and scripts damage it with Damage(amount, source), Kill(source) and Heal(amount); at zero it dies once and stays dead until Revive(hp).
hp := node.Get<Health>()
if hp.Damage(2, other) {
audio.PlayEvent(HitSound)
}
After a hit the node ignores damage for Invuln Time seconds of game time. That time starts after the step, so all hits of one step count: two swords landing together both hurt, and Died still fires once. SetInvulnerable(seconds) makes the node untouchable on purpose.
At death a node with Respawn comes back; otherwise Destroy On Death removes it and Despawn On Death returns it to its pool. Team keeps hitboxes of the same team away from it unless they allow friendly fire.
In the scene press J next to the dummy to strike it; it shows its hit points and comes back a second after it dies.
Try it
Change the example parameters and compare the result. Reset restores the initial scene.
Scene7 nodes
Scriptmain.aes
package demo
import (
"aego/debug"
"aego/input"
)
// Player walks with A/D, jumps with Space and strikes with J.
script Player {
func Update(dt float) {
m := node.Get<Motion2D>()
x := 0.0
if input.Down(input.KeyA) || input.Down(input.KeyLeft) {
x -= 1
}
if input.Down(input.KeyD) || input.Down(input.KeyRight) {
x += 1
}
m.Move(x)
if input.KeyPressed(input.KeySpace) || input.KeyPressed(input.KeyW) || input.KeyPressed(input.KeyUp) {
m.Jump()
}
if input.KeyReleased(input.KeySpace) || input.KeyReleased(input.KeyW) || input.KeyReleased(input.KeyUp) {
m.JumpReleased()
}
}
}
// Sword strikes once per J press with its Hitbox 2D.
script Sword {
func Update(dt float) {
if input.KeyPressed(input.KeyJ) {
node.Get<Hitbox2D>().Pulse()
}
p := node.WorldPosition()
debug.Rect(Rect{p.X - 0.3, p.Y - 0.2, 0.6, 0.4}, Color{1, 1, 1, 0.5})
debug.Text(Vec2{p.X - 1.6, p.Y - 1.6}, "J: strike", Color{0.8, 0.8, 0.8, 1})
}
}
// Dummy shows its hit points, the last damage, and comes back a second after it dies.
script Dummy {
var wait float
var flash float
var shown int
var died Connection
var hurt Connection
func Ready() {
hp := node.Get<Health>()
died = hp.Died.Connect(OnDied)
hurt = hp.Damaged.Connect(OnDamaged)
}
func OnDied(source NodeRef) {
wait = 1
}
func OnDamaged(amount int, source NodeRef) {
shown = amount
flash = 1
}
func Update(dt float) {
hp := node.Get<Health>()
if wait > 0 {
wait -= dt
if wait <= 0 {
hp.Revive(0)
}
}
p := node.WorldPosition()
debug.Rect(Rect{p.X - 0.6, p.Y - 1.3, 1.2, 0.15}, Color{0.3, 0.3, 0.3, 1})
debug.Rect(Rect{p.X - 0.6, p.Y - 1.3, 1.2 * float(hp.Current) / float(hp.Max), 0.15}, Color{0.2, 0.85, 0.3, 1})
if flash > 0 {
flash -= dt
debug.Text(Vec2{p.X + 0.6, p.Y - 1.6 - (1 - flash) * 0.5}, "-" + string(shown), Color{1, 0.45, 0.3, 1})
}
}
}