59 lines
2.9 KiB
Text
59 lines
2.9 KiB
Text
# component_access.ludic — typed component access from an entity handle:
|
|
# `Prop.of(e)` reads and writes fields, `Prop.has(e)` is safe for -1 and dead
|
|
# handles, `Prop.count()` counts, `Prop.despawn_all()` clears; `countdown` fields
|
|
# tick toward 0 each Update; `Enum.Variant` typed vars and machines. Prints:
|
|
# 9 15 10 33 1 0 0 1 0 1 101 100 102 1 2 1 0
|
|
program ComponentAccess {
|
|
import "ludic.core/components.ludic"
|
|
property Hero { roll_frames: int = 0, iframes: int = 0, flash: countdown = 0 }
|
|
property Tag { n: int = 0 }
|
|
model Player { Position, Hero }
|
|
model Thing { Position, Tag }
|
|
enum HeroState { Idle, Rolling, Swinging }
|
|
state ComponentaccessState {
|
|
hero_state: HeroState = HeroState.Rolling
|
|
player: int = -1
|
|
frames: int = 0
|
|
}
|
|
function step(componentaccess_st: mut ComponentaccessState) -> void {
|
|
machine componentaccess_st.hero_state {
|
|
state Idle { print(100); become Swinging }
|
|
state Rolling { print(101); become Idle }
|
|
state Swinging { print(102); become Rolling }
|
|
}
|
|
}
|
|
handler Tick(componentaccess_st: mut ComponentaccessState) phase Update {
|
|
componentaccess_st.frames += 1
|
|
if componentaccess_st.frames == 1 {
|
|
componentaccess_st.player = spawn Player { Position { x: 3, y: 4 }, Hero { roll_frames: 9, flash: 3 } }
|
|
spawn Thing { Position { x: 30, y: 40 }, Tag { n: 7 } }
|
|
print(Hero.of(componentaccess_st.player).roll_frames) # 9
|
|
Hero.of(componentaccess_st.player).iframes = 20
|
|
Hero.of(componentaccess_st.player).iframes -= 5
|
|
print(Hero.of(componentaccess_st.player).iframes) # 15
|
|
let hero = Hero.of(componentaccess_st.player)
|
|
hero.roll_frames += 1
|
|
print(Hero.of(componentaccess_st.player).roll_frames) # 10
|
|
print(Position.of(componentaccess_st.player).x + Position.of(1).x) # 33
|
|
print(Hero.has(componentaccess_st.player)) # 1
|
|
print(Hero.has(1)) # 0
|
|
print(Hero.has(-1)) # 0
|
|
print(Tag.has(1)) # 1
|
|
despawn 1
|
|
print(Tag.has(1)) # 0
|
|
print(Position.count()) # 1: the player
|
|
spawn Thing { Position { x: 1 } }
|
|
}
|
|
if componentaccess_st.frames == 2 {
|
|
step(componentaccess_st); step(componentaccess_st); step(componentaccess_st) # 101 100 102
|
|
print(componentaccess_st.hero_state) # 1
|
|
print(Position.count()) # 2: the player and the thing spawned after
|
|
}
|
|
if componentaccess_st.frames == 3 {
|
|
print(Hero.of(componentaccess_st.player).flash) # ticked twice: 1
|
|
Position.despawn_all()
|
|
print(Position.count()) # 0
|
|
quit()
|
|
}
|
|
}
|
|
}
|