Start of animation player state mashine

This commit is contained in:
Paul Norberger
2020-04-19 10:19:32 +02:00
parent d71b728e94
commit 917aff815a
15 changed files with 88 additions and 63 deletions

View File

@@ -7,25 +7,3 @@ func _ready():
"split_up": $SplitUp,
"stagger": $Stagger
}
func _change_state(state_name):
# The base state_machine interface this node extends does most of the work
if not _active:
return
if state_name in ["stagger"]:
states_stack.push_front(states_map[state_name])
if state_name == "jump" and current_state == $Move:
$Jump.initialize($Move.speed, $Move.velocity)
._change_state(state_name)
func _input(event):
"""
Here we only handle input that can interrupt states, attacking in this case
otherwise we let the state node handle it
"""
if event.is_action_pressed("attack"):
if current_state == $Attack:
return
_change_state("attack")
return
current_state.handle_input(event)

View File

@@ -1,4 +1,4 @@
extends "res://Boss/SlimeBoss/States/Motion/MotionState.gd"
extends "res://Overlap/StateMachine/MotionState.gd"
func enter():
owner.get_node("AnimationPlayer").play("MoveDown") # TODO: Replace animation
@@ -9,4 +9,5 @@ func handle_input(event):
func update(delta):
var input_direction = get_input_direction()
if input_direction:
emit_signal("finished", "move")
# emit_signal("finished", "move")
pass

View File

@@ -1,7 +0,0 @@
extends "res://Overlap/StateMachine/State.gd"
func get_input_direction():
var input_direction = Vector2()
input_direction.x = int(Input.is_action_pressed("move_right")) - int(Input.is_action_pressed("move_left"))
input_direction.y = int(Input.is_action_pressed("move_down")) - int(Input.is_action_pressed("move_up"))
return input_direction

View File

@@ -0,0 +1,8 @@
extends "res://Overlap/StateMachine/State.gd"
func get_input_direction():
var input_direction = Vector2()
input_direction.x = int(Input.is_action_pressed("right")) - int(Input.is_action_pressed("left"))
input_direction.y = int(Input.is_action_pressed("down")) - int(Input.is_action_pressed("up"))
input_direction = input_direction.normalized()
return input_direction

View File

@@ -11,7 +11,7 @@ var _active = false setget set_active
func _ready():
for child in get_children():
child.connect("finished", self, "_change_state")
child.connect("finished", self, "change_state")
initialize(START_STATE)
func initialize(start_state):
@@ -39,7 +39,7 @@ func _on_animation_finished(anim_name):
return
current_state._on_animation_finished(anim_name)
func _change_state(state_name):
func change_state(state_name):
if not _active:
return
current_state.exit()

View File

@@ -9,7 +9,6 @@ onready var speed := max_speed setget set_speed
signal no_health
func set_health(value):
if value > max_health:
health = max_health
else:

View File

@@ -14,9 +14,7 @@ export(int, 0, 500) var ACCELERATION := 450
onready var player_stats := $Stats
onready var debug_label := $DebugLabel
onready var animation_player := $AnimationPlayer
onready var animation_tree := $AnimationTree
onready var animation_state = animation_tree.get("parameters/playback")
onready var animation_state := $AnimationStates
enum moveState{
MOVE,
@@ -65,6 +63,7 @@ func move():
move_and_slide(velocity)
func movement_move(delta):
var input_vector = Vector2.ZERO
# This is a clever way to handle directional input
# Input.get_action_strength(...) returns a value between 0 and 1 depending
@@ -76,15 +75,12 @@ func movement_move(delta):
input_vector = input_vector.normalized()
if input_vector == Vector2.ZERO:
animation_state.travel("idle")
animation_state.change_state("idle")
velocity = Vector2.ZERO
else:
rollvector = input_vector
animation_tree.set("parameters/idle/blend_position", input_vector)
animation_tree.set("parameters/hit/blend_position", input_vector)
animation_tree.set("parameters/roll/blend_position", input_vector)
animation_tree.set("parameters/run/blend_position", input_vector)
animation_state.travel("run")
animation_state.change_state("run")
velocity = velocity.move_toward(player_stats.speed * input_vector, ACCELERATION * delta)
if Input.is_action_just_pressed("roll"):
movementState = moveState.ROLL
@@ -93,14 +89,14 @@ func movement_move(delta):
func movement_hit():
velocity = Vector2.ZERO
animation_state.travel("hit")
animation_state.change_state("attack")
func hit_finished():
movementState = moveState.MOVE
func movement_roll():
velocity = rollvector * ROLL_SPEED
animation_state.travel("roll")
animation_state.change_state("roll")
"""
# Roll.gd

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=56 format=2]
[gd_scene load_steps=61 format=2]
[ext_resource path="res://Player/Player.gd" type="Script" id=1]
[ext_resource path="res://Player/Player.png" type="Texture" id=2]
@@ -6,6 +6,11 @@
[ext_resource path="res://Overlap/HurtHit_Box/Hitbox.tscn" type="PackedScene" id=4]
[ext_resource path="res://Overlap/Stats/Stats.tscn" type="PackedScene" id=5]
[ext_resource path="res://Fonts/Harmonic/Harmonic.ttf" type="DynamicFontData" id=6]
[ext_resource path="res://Player/States/Idle.gd" type="Script" id=7]
[ext_resource path="res://Player/States/Attack.gd" type="Script" id=8]
[ext_resource path="res://Player/States/Roll.gd" type="Script" id=9]
[ext_resource path="res://Player/States/Run.gd" type="Script" id=10]
[ext_resource path="res://Player/PlayerStateMachine.gd" type="Script" id=11]
[sub_resource type="CapsuleShape2D" id=1]
radius = 2.15976
@@ -597,6 +602,7 @@ states/run/node = SubResource( 37 )
states/run/position = Vector2( 511, 271 )
transitions = [ "idle", "hit", SubResource( 38 ), "hit", "idle", SubResource( 39 ), "idle", "run", SubResource( 40 ), "run", "idle", SubResource( 41 ), "idle", "roll", SubResource( 42 ), "roll", "idle", SubResource( 43 ) ]
start_node = "idle"
graph_offset = Vector2( 12, 66 )
[sub_resource type="AnimationNodeStateMachinePlayback" id=45]
@@ -708,7 +714,21 @@ __meta__ = {
[node name="Stats" parent="." instance=ExtResource( 5 )]
max_health = 5
[node name="States" type="Node" parent="."]
[node name="AnimationStates" type="Node" parent="."]
script = ExtResource( 11 )
START_STATE = NodePath("Idle")
[node name="Run" type="Node" parent="AnimationStates"]
script = ExtResource( 10 )
[node name="Idle" type="Node" parent="AnimationStates"]
script = ExtResource( 7 )
[node name="Attack" type="Node" parent="AnimationStates"]
script = ExtResource( 8 )
[node name="Roll" type="Node" parent="AnimationStates"]
script = ExtResource( 9 )
[connection signal="area_entered" from="Hitbox" to="." method="_on_Hitbox_area_entered"]
[connection signal="area_entered" from="Hurtbox" to="." method="_on_Hurtbox_area_entered"]
[connection signal="area_exited" from="Hurtbox" to="." method="_on_Hurtbox_area_exited"]

View File

@@ -0,0 +1,10 @@
extends "res://Overlap/StateMachine/StateMachine.gd"
func _ready():
states_map = {
"idle": $Idle,
"run": $Run,
"attack": $Attack,
"roll": $Roll
}

View File

@@ -0,0 +1,7 @@
extends "res://Player/States/PlayerAnimationState.gd"
func enter():
animation_playback.travel("hit")
func update(delta):
animation_tree.set("parameters/hit/blend_position", owner.rollvector)

View File

@@ -0,0 +1,8 @@
extends "res://Player/States/PlayerAnimationState.gd"
func enter():
animation_playback.travel("idle")
func update(delta):
var input_vector = get_input_direction()
animation_tree.set("parameters/idle/blend_position", input_vector)

View File

@@ -1,16 +0,0 @@
extends Node
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass

View File

@@ -0,0 +1,5 @@
extends "res://Overlap/StateMachine/MotionState.gd"
onready var animation_player := get_node("../../AnimationPlayer")
onready var animation_tree := get_node("../../AnimationTree")
onready var animation_playback = animation_tree.get("parameters/playback")

View File

@@ -0,0 +1,8 @@
extends "res://Player/States/PlayerAnimationState.gd"
func enter():
animation_playback.travel("roll")
func update(delta):
var input_vector = get_input_direction()
animation_tree.set("parameters/roll/blend_position", owner.rollvector)

8
src/Player/States/Run.gd Normal file
View File

@@ -0,0 +1,8 @@
extends "res://Player/States/PlayerAnimationState.gd"
func enter():
animation_playback.travel("run")
func update(delta):
var input_vector = get_input_direction()
animation_tree.set("parameters/run/blend_position", input_vector)