# 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(component_access_st: mut ComponentAccessState) -> void { machine component_access_st.hero_state { state Idle { print(100); become Swinging } state Rolling { print(101); become Idle } state Swinging { print(102); become Rolling } } } handler Tick(component_access_st: mut ComponentAccessState) phase Update { component_access_st.frames += 1 if component_access_st.frames == 1 { component_access_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(component_access_st.player).roll_frames) # 9 Hero.of(component_access_st.player).iframes = 20 Hero.of(component_access_st.player).iframes -= 5 print(Hero.of(component_access_st.player).iframes) # 15 let hero = Hero.of(component_access_st.player) hero.roll_frames += 1 print(Hero.of(component_access_st.player).roll_frames) # 10 print(Position.of(component_access_st.player).x + Position.of(1).x) # 33 print(Hero.has(component_access_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 component_access_st.frames == 2 { step(component_access_st); step(component_access_st); step(component_access_st) # 101 100 102 print(component_access_st.hero_state) # 1 print(Position.count()) # 2: the player and the thing spawned after } if component_access_st.frames == 3 { print(Hero.of(component_access_st.player).flash) # ticked twice: 1 Position.despawn_all() print(Position.count()) # 0 quit() } } }