@@ -7,32 +7,14 @@ export type Axis = typeof Axis.Type
77const JumpKind = Schema . Literals ( [ "Ground" , "Double" , "Wall" ] )
88
99const State = Schema . TaggedUnion ( {
10- Character : { } ,
11- Locomotion : { } ,
12- Playing : { } ,
1310 Paused : { pausedAt : Schema . Number } ,
14- Grounded : { } ,
15- Standing : { } ,
1611 Running : { startedAt : Schema . Number } ,
1712 Ducking : { startedAt : Schema . Number } ,
1813 Landing : { impact : Schema . Number , resumeAxis : Axis , landedAt : Schema . Number } ,
1914 Airborne : { originY : Schema . Number } ,
20- Motion : { } ,
2115 Jumping : { startedAt : Schema . Number , push : Axis , kind : JumpKind } ,
2216 Falling : { apexY : Schema . Number } ,
23- Diving : { startedAt : Schema . Number } ,
24- AirJump : { } ,
25- AirJumpGroundLock : { } ,
26- AirJumpWallLock : { } ,
27- AirJumpReady : { } ,
28- AirJumpSpent : { } ,
29- WallContact : { } ,
30- NoWall : { } ,
31- LeftWall : { } ,
32- RightWall : { } ,
33- Facing : { } ,
34- Left : { } ,
35- Right : { }
17+ Diving : { startedAt : Schema . Number }
3618} )
3719
3820// Inputs and physics facts are one runtime-decoded, statically typed protocol.
@@ -61,22 +43,18 @@ const awayFrom = (wall: Axis): Axis => (wall === -1 ? 1 : wall === 1 ? -1 : 0)
6143
6244export const CharacterStates = Machine . defineStates ( {
6345 Character : {
64- schema : State . cases . Character ,
6546 type : "parallel" ,
6647 states : {
6748 locomotion : {
68- schema : State . cases . Locomotion ,
6949 initial : "Playing" ,
7050 states : {
7151 Playing : {
72- schema : State . cases . Playing ,
7352 initial : "Grounded" ,
7453 states : {
7554 Grounded : {
76- schema : State . cases . Grounded ,
7755 initial : "Standing" ,
7856 states : {
79- Standing : State . cases . Standing ,
57+ Standing : { } ,
8058 Running : State . cases . Running ,
8159 Ducking : State . cases . Ducking ,
8260 Landing : State . cases . Landing
@@ -87,7 +65,6 @@ export const CharacterStates = Machine.defineStates({
8765 type : "parallel" ,
8866 states : {
8967 motion : {
90- schema : State . cases . Motion ,
9168 initial : "Jumping" ,
9269 states : {
9370 Jumping : State . cases . Jumping ,
@@ -96,13 +73,12 @@ export const CharacterStates = Machine.defineStates({
9673 }
9774 } ,
9875 airJump : {
99- schema : State . cases . AirJump ,
10076 initial : "AirJumpGroundLock" ,
10177 states : {
102- AirJumpGroundLock : State . cases . AirJumpGroundLock ,
103- AirJumpWallLock : State . cases . AirJumpWallLock ,
104- AirJumpReady : State . cases . AirJumpReady ,
105- AirJumpSpent : State . cases . AirJumpSpent
78+ AirJumpGroundLock : { } ,
79+ AirJumpWallLock : { } ,
80+ AirJumpReady : { } ,
81+ AirJumpSpent : { }
10682 }
10783 }
10884 }
@@ -117,20 +93,18 @@ export const CharacterStates = Machine.defineStates({
11793 }
11894 } ,
11995 facing : {
120- schema : State . cases . Facing ,
12196 initial : "Right" ,
12297 states : {
123- Left : State . cases . Left ,
124- Right : State . cases . Right
98+ Left : { } ,
99+ Right : { }
125100 }
126101 } ,
127102 contact : {
128- schema : State . cases . WallContact ,
129103 initial : "NoWall" ,
130104 states : {
131- NoWall : State . cases . NoWall ,
132- LeftWall : State . cases . LeftWall ,
133- RightWall : State . cases . RightWall
105+ NoWall : { } ,
106+ LeftWall : { } ,
107+ RightWall : { }
134108 }
135109 }
136110 }
@@ -457,9 +431,12 @@ export const locomotionState = (snapshot: CharacterSnapshot) => {
457431 }
458432
459433 const playing = locomotion . state
460- return playing . path === "Character.locomotion.Playing.Grounded"
461- ? playing . state . value
462- : playing . states . motion . state . value
434+ if ( playing . path === "Character.locomotion.Playing.Airborne" ) {
435+ return playing . states . motion . state . value
436+ }
437+ return playing . state . path === "Character.locomotion.Playing.Grounded.Standing"
438+ ? { _tag : "Standing" as const }
439+ : playing . state . value
463440}
464441
465442export type LocomotionMode = ReturnType < typeof locomotionState > [ "_tag" ]
@@ -468,22 +445,32 @@ export const locomotionMode = (snapshot: CharacterSnapshot): LocomotionMode => l
468445
469446export const locomotionBranch = ( snapshot : CharacterSnapshot ) => {
470447 const locomotion = snapshot . states . locomotion . state
471- return locomotion . path === "Character.locomotion.Paused" ? locomotion . value . _tag : locomotion . state . value . _tag
448+ if ( locomotion . path === "Character.locomotion.Paused" ) return "Paused" as const
449+ return locomotion . state . path === "Character.locomotion.Playing.Grounded" ? "Grounded" as const : "Airborne" as const
472450}
473451
474452export const airJumpMode = ( snapshot : CharacterSnapshot ) => {
475453 const locomotion = snapshot . states . locomotion . state
476- return locomotion . path === "Character.locomotion.Playing" &&
477- locomotion . state . path === "Character.locomotion.Playing.Airborne"
478- ? locomotion . state . states . airJump . state . value . _tag
479- : undefined
454+ if (
455+ locomotion . path !== "Character.locomotion.Playing" ||
456+ locomotion . state . path !== "Character.locomotion.Playing.Airborne"
457+ ) return undefined
458+
459+ const path = locomotion . state . states . airJump . state . path
460+ if ( path === "Character.locomotion.Playing.Airborne.airJump.AirJumpGroundLock" ) return "AirJumpGroundLock" as const
461+ if ( path === "Character.locomotion.Playing.Airborne.airJump.AirJumpWallLock" ) return "AirJumpWallLock" as const
462+ if ( path === "Character.locomotion.Playing.Airborne.airJump.AirJumpReady" ) return "AirJumpReady" as const
463+ return "AirJumpSpent" as const
480464}
481465
482466export const wallContact = ( snapshot : CharacterSnapshot ) => {
483- return snapshot . states . contact . state . value . _tag
467+ const path = snapshot . states . contact . state . path
468+ if ( path === "Character.contact.NoWall" ) return "NoWall" as const
469+ return path === "Character.contact.LeftWall" ? "LeftWall" as const : "RightWall" as const
484470}
485471
486- export const facingDirection = ( snapshot : CharacterSnapshot ) => snapshot . states . facing . state . value . _tag
472+ export const facingDirection = ( snapshot : CharacterSnapshot ) =>
473+ snapshot . states . facing . state . path === "Character.facing.Left" ? "Left" as const : "Right" as const
487474
488475export const activeStateData = ( snapshot : CharacterSnapshot ) => {
489476 const locomotion = snapshot . states . locomotion . state
@@ -493,15 +480,16 @@ export const activeStateData = (snapshot: CharacterSnapshot) => {
493480 }
494481
495482 const playing = locomotion . state
496- const { _tag : _branch , ...branchData } = playing . value
497483 if ( playing . path === "Character.locomotion.Playing.Grounded" ) {
484+ if ( playing . state . path === "Character.locomotion.Playing.Grounded.Standing" ) return { }
498485 const { _tag : _leaf , ...leafData } = playing . state . value
499- return { ... branchData , ... leafData }
486+ return leafData
500487 }
488+ const { _tag : _branch , ...branchData } = playing . value
501489 const { _tag : _motion , ...motionData } = playing . states . motion . state . value
502490 return {
503491 ...branchData ,
504492 ...motionData ,
505- airJump : playing . states . airJump . state . value . _tag
493+ airJump : airJumpMode ( snapshot )
506494 }
507495}
0 commit comments