using { /Verse.org/SceneGraph } using { /Verse.org/SceneGraph/KeyframedMovement } using { /Verse.org/Simulation } using { /Verse.org/SpatialMath } using { /UnrealEngine.com/Temporary/Diagnostics } # Bolt this onto an entity with a Keyframed Movement component to make it # PATROL: walk out to a far point, then back, forever — like a guard pacing # a hallway or a security drone sweeping a vault. patrol_component := class(component): # How far the patrol walks before turning back, in units. @editable var PatrolDistance:float = 600.0 # Seconds for ONE leg (out OR back). @editable var LegSeconds:float = 3.0 OnBeginSimulation():void = (super:)OnBeginSimulation() if (Mover := Entity.GetComponent[keyframed_movement_component]): # The single outbound leg: glide PatrolDistance to the left, # easing in and out so each turn feels like a real pause-and-go. Outbound := keyframed_movement_delta: Duration := LegSeconds Easing := ease_in_out_cubic_bezier_easing_function{} Transform := transform: Translation := vector3: Left := PatrolDistance Forward := 0.0 Up := 0.0 # Ping-pong: play the leg forward, then reverse it, forever. Mover.SetKeyframes(array{Outbound}, pingpong_keyframed_movement_playback_mode{}) Mover.Play() # Listen for the turn-around so we could react (sound, score, etc.). Mover.KeyframeReachedEvent.Subscribe(OnReachedWaypoint) # Fires each time a keyframe is hit. Keyframe is the index reached; # IsReversed is true when we're on the way back. OnReachedWaypoint(Reached:tuple(int, logic)):void = # A real game might play a footstep here or flip a patrol flag. Print("Patrol reached a waypoint")