Skip to content

Instantly share code, notes, and snippets.

@AndreaCatania
Created July 6, 2021 09:24
Show Gist options
  • Save AndreaCatania/a29d963648b4beafb8b5ceee2d38a53c to your computer and use it in GitHub Desktop.
Save AndreaCatania/a29d963648b4beafb8b5ceee2d38a53c to your computer and use it in GitHub Desktop.
extends Node3D
var player_query: DynamicQuery
var animation_tree: AnimationTree
var root_state_machine: AnimationNodeStateMachinePlayback
func _ready():
# Build the player query used to fetch the player status.
player_query = DynamicQuery.new()
player_query.with_component(ECS.LocalPlayer, System.IMMUTABLE)
player_query.with_component(ECS.BtPawn, System.IMMUTABLE)
player_query.with_component(ECS.CharacterMotion, System.IMMUTABLE)
player_query.with_component(ECS.CharacterMotionAttributes, System.IMMUTABLE)
# Fetch motion mode
player_query.maybe_component(ECS.MotionModeGround, System.IMMUTABLE)
player_query.maybe_component(ECS.MotionModeSliding, System.IMMUTABLE)
player_query.maybe_component(ECS.MotionModeFalling, System.IMMUTABLE)
player_query.maybe_component(ECS.MotionModeOvertake, System.IMMUTABLE)
# Fetch the on ground extra info
player_query.maybe_component(ECS.IsCrouched, System.IMMUTABLE)
player_query.maybe_component(ECS.IsRunning, System.IMMUTABLE)
player_query.maybe_component(ECS.IsRushing, System.IMMUTABLE)
player_query.build()
assert(player_query.is_valid())
# Take the animation tree nodes
animation_tree = $"AnimationTree"
root_state_machine = animation_tree.get("parameters/playback")
func _process(delta):
player_query.begin(ECS.get_active_world())
while player_query.is_not_done():
update_animation_state(
player_query.get_component(1), # Pawn component
player_query.get_component(2), # Character Motion component
player_query.get_component(3), # Character Motion Attributes component
player_query.get_component(4), # Motion Mode Ground component
player_query.get_component(5), # Motion Mode Sliding component
player_query.get_component(6), # Motion Mode Falling component
player_query.get_component(7), # Motion Mode Overtake component
player_query.get_component(8), # Is Crouched component
player_query.get_component(9), # Is Running component
player_query.get_component(10) # Is Rushing component
)
break # We expect only one local player, no need to use `player_query.next()`
player_query.end()
func update_animation_state(
pawn,
character_motion,
character_motion_attr,
mm_ground,
mm_sliding,
mm_falling,
mm_overtake,
is_crouched,
is_running,
is_rushing):
if mm_ground.is_valid():
# The pawn is on ground
if is_rushing.is_valid():
root_state_machine.travel("StandingRushingBH")
else:
if is_crouched.is_valid():
root_state_machine.travel("CrouchMotionBH")
var amount = pawn.velocity.length() / character_motion_attr.max_speed_crouch_run
animation_tree["parameters/CrouchMotionBH/blend_position"] = amount
else:
root_state_machine.travel("StandingMotionBH")
var amount = pawn.velocity.length() / character_motion_attr.max_speed_run
animation_tree["parameters/StandingMotionBH/blend_position"] = amount
elif mm_sliding.is_valid():
# The pawn is sliding
if mm_sliding.is_induced_sliding:
root_state_machine.travel("Sliding")
else:
# TODO use proper animation for not induced sliding
root_state_machine.travel("Sliding")
elif mm_falling.is_valid():
# TODO play falling?
print("Falling")
elif mm_overtake.is_valid():
# The pawn is overtaking
print("Overtake")
else:
print("[FATAL] The pawn status is not defined.")
if character_motion.is_jump_just_pressed:
# The pawn jump
root_state_machine.travel("ActionJump")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment