Refactor player animation and collision based entrances

This commit is contained in:
2026-04-02 16:11:29 -05:00
parent eabfeab91a
commit 7ad56fb876
28 changed files with 187 additions and 118 deletions

View File

@@ -486,6 +486,6 @@ animations = [{
[node name="Sprite" type="AnimatedSprite2D" unique_id=1296959783]
sprite_frames = SubResource("SpriteFrames_jqxwg")
animation = &"opening-chest-down"
animation = &"idle-down"
autoplay = "idle-down"
script = ExtResource("1_jqxwg")

View File

@@ -15,7 +15,6 @@ func _ready() -> void:
func Enter(_extra_parameters: Dictionary) -> void:
print("Walking State Entered")
_queued_dv = _extra_parameters["dv"]
StartedWalking.emit()

View File

@@ -1,38 +1,3 @@
extends AnimatedSprite2D
class_name PlayerSprite
# Signals
signal DrawingBowAnimationFinished
signal FiringArrowAnimationFinished
signal AnimationFinished(animation_name: String)
# Private Variables
var _current_animation := "idle-down"
var _is_flipped := false
# Public Methods
func OnAnimationFinished() -> void:
if _current_animation.begins_with("drawing-bow-"):
DrawingBowAnimationFinished.emit()
elif _current_animation.begins_with("firing-arrow-"):
FiringArrowAnimationFinished.emit()
AnimationFinished.emit(_current_animation)
func PlaySpecifiedAnimation(animation_name: String) -> void:
_current_animation = animation_name
animation = animation_name
play()
func _build_animation_name(state: BaseState, direction: Enums.Directions) -> String:
# e.g. "idle-down", "walking-up"
var state_str := state.GetAnimationBaseName().replace("_", "-")
var direction_str := _direction_to_animation_state(direction)
return "%s-%s" % [state_str, direction_str]
func _direction_to_animation_state(direction: Enums.Directions) -> String:
if direction == Enums.Directions.LEFT or direction == Enums.Directions.RIGHT:
return "side"
return Enums.Directions.keys()[direction].to_lower()
# sprite.gd

View File

@@ -0,0 +1,64 @@
extends Node
# sprite_animation_changer.gd
# When the player actually changes state or direction, this component will decide
# which sprite animation should play
@export var sprite: AnimatedSprite2D
var _cached_dv: Vector2
var _cached_state: PlayerStateMachine.States
var _walking_animation_data = {
Vector2.LEFT: ["walking-side", true],
Vector2.RIGHT: ["walking-side", false],
Vector2.UP: ["walking-up", false],
Vector2.DOWN: ["walking-down", false]
}
var _idle_animation_data = {
Vector2.LEFT: ["idle-side", true],
Vector2.RIGHT: ["idle-side", false],
Vector2.UP: ["idle-up", false],
Vector2.DOWN: ["idle-down", false]
}
func OnStateChanged(to_state: PlayerStateMachine.States, _from_state: PlayerStateMachine.States) -> void:
_cached_state = to_state
if to_state == PlayerStateMachine.States.WALKING:
_to_walking_state()
return
if to_state == PlayerStateMachine.States.IDLE:
_to_idle_state()
return
func OnMovementQueued(dv: Vector2) -> void:
_cached_dv = Vector2Utils.GetClosestDirectionVector(dv)
if _cached_state == PlayerStateMachine.States.WALKING:
# Direction changed while walking, update animation, just run _to_walking_state again
_to_walking_state()
func _to_walking_state() -> void:
if _walking_animation_data.has(_cached_dv):
var animation_data = _walking_animation_data[_cached_dv]
var animation_name = animation_data[0] as String
var is_flipped = animation_data[1] as bool
sprite.animation = animation_name
sprite.flip_h = is_flipped
sprite.play()
func _to_idle_state() -> void:
if _idle_animation_data.has(_cached_dv):
var animation_data = _idle_animation_data[_cached_dv]
var animation_name = animation_data[0] as String
var is_flipped = animation_data[1] as bool
sprite.animation = animation_name
sprite.flip_h = is_flipped
sprite.play()

View File

@@ -0,0 +1 @@
uid://by3g7ne2b3lgi

View File

@@ -1,7 +1,7 @@
extends Node
class_name PlayerStateMachine
signal StateChanged
signal StateChanged(to_state: States, from_state: States)
enum States {
UNSET, IDLE, WALKING, USING_ITEM_A, DRAWING_BOW, FIRING_ARROW,
@@ -52,6 +52,9 @@ func _process(delta: float) -> void:
func _swap_state() -> void:
var from_state := current_state.GetStateEnum()
var to_state := queued_state
if current_state:
current_state.Exit()
@@ -60,4 +63,4 @@ func _swap_state() -> void:
queued_state = PlayerStateMachine.States.UNSET
queued_parameters = {}
StateChanged.emit()
StateChanged.emit(to_state, from_state)

View File

@@ -7,6 +7,7 @@
[ext_resource type="PackedScene" uid="uid://nbkisxm2oekn" path="res://Entities/Characters/Utility/Components/movement_component.tscn" id="5_h314u"]
[ext_resource type="Script" uid="uid://bwmmah30t3m0u" path="res://Entities/Characters/Player/Scripts/States/walking_state.gd" id="5_rg3km"]
[ext_resource type="Script" uid="uid://ctoxjn2rvtjs6" path="res://Entities/Characters/Player/Scripts/state_event_connector.gd" id="6_18fwg"]
[ext_resource type="Script" uid="uid://by3g7ne2b3lgi" path="res://Entities/Characters/Player/Scripts/sprite_animation_changer.gd" id="8_bm64c"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_62n52"]
size = Vector2(13, 9)
@@ -15,6 +16,7 @@ size = Vector2(13, 9)
y_sort_enabled = true
[node name="Player Body" parent="." unique_id=1502234578 instance=ExtResource("1_62n52")]
collision_layer = 17
[node name="Sprite" parent="Player Body" unique_id=1169131604 instance=ExtResource("2_1uhri")]
position = Vector2(0, 7)
@@ -39,11 +41,17 @@ state_machine = NodePath("..")
[node name="Movement Component" parent="." unique_id=737644583 instance=ExtResource("5_h314u")]
[node name="Internal (Code Here)" type="Node" parent="." unique_id=95043416]
[node name="Internal" type="Node" parent="." unique_id=95043416]
[node name="State Event Connector" type="Node" parent="Internal (Code Here)" unique_id=186266500 node_paths=PackedStringArray("state_machine")]
[node name="State Event Connector" type="Node" parent="Internal" unique_id=186266500 node_paths=PackedStringArray("state_machine")]
script = ExtResource("6_18fwg")
state_machine = NodePath("../../State Machine")
[connection signal="MovementQueued" from="Movement Component" to="Internal (Code Here)/State Event Connector" method="OnMovementQueued"]
[connection signal="NoMovementQueued" from="Movement Component" to="Internal (Code Here)/State Event Connector" method="OnNoMovementQueued"]
[node name="Sprite Animation Changer" type="Node" parent="Internal" unique_id=281485983 node_paths=PackedStringArray("sprite")]
script = ExtResource("8_bm64c")
sprite = NodePath("../../Player Body/Sprite")
[connection signal="StateChanged" from="State Machine" to="Internal/Sprite Animation Changer" method="OnStateChanged"]
[connection signal="MovementQueued" from="Movement Component" to="Internal/State Event Connector" method="OnMovementQueued"]
[connection signal="MovementQueued" from="Movement Component" to="Internal/Sprite Animation Changer" method="OnMovementQueued"]
[connection signal="NoMovementQueued" from="Movement Component" to="Internal/State Event Connector" method="OnNoMovementQueued"]

View File

@@ -1 +0,0 @@
uid://ckfuj0lm6jv3i

View File

@@ -1,5 +1,5 @@
extends Node
class_name LoadingZoneTransporter
class_name EntranceTransporter
signal MapTransitionQueued(map_id: Enums.MapIds, marker_name: String)

View File

@@ -0,0 +1 @@
uid://bqedrioybnvi5

View File

@@ -1,8 +1,8 @@
extends Area2D
class_name InteractiveLoadingZone
class_name InteractiveEntrance
@export var loading_zone_transporter: LoadingZoneTransporter
@export var entrance_transporter: EntranceTransporter
# Public Methods
func Activate() -> void:
loading_zone_transporter.Activate()
entrance_transporter.Activate()

View File

@@ -1 +0,0 @@
uid://bvl1vdqd5cjkc

View File

@@ -1,9 +1,6 @@
[gd_scene format=3 uid="uid://ca75b65eh7vv8"]
[ext_resource type="Script" uid="uid://ckfuj0lm6jv3i" path="res://Entities/Map Objects/Loading Zone/Scripts/collision_loading_zone.gd" id="1_pb5hg"]
[gd_scene format=3 uid="uid://dq6ifketavnfu"]
[node name="Collision Loading Zone" type="Area2D" unique_id=1043800735 groups=["Collision Loading Zone Group"]]
collision_layer = 0
script = ExtResource("1_pb5hg")
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

View File

@@ -0,0 +1,6 @@
[gd_scene format=3 uid="uid://bbules4o3xayc"]
[ext_resource type="Script" uid="uid://bqedrioybnvi5" path="res://Entities/Map Objects/Loading Zone/Scripts/entrance_transporter.gd" id="1_bkamm"]
[node name="Entrance Transporter" type="Node" unique_id=1690817663 groups=["Loading Zone Transporter Group"]]
script = ExtResource("1_bkamm")

View File

@@ -1,6 +0,0 @@
[gd_scene format=3 uid="uid://bbules4o3xayc"]
[ext_resource type="Script" uid="uid://bvl1vdqd5cjkc" path="res://Entities/Map Objects/Loading Zone/Scripts/loading_zone_transporter.gd" id="1_p8o2m"]
[node name="Loading Zone Transporter" type="Node" unique_id=1690817663 groups=["Loading Zone Transporter Group"]]
script = ExtResource("1_p8o2m")

View File

@@ -0,0 +1,4 @@
[gd_scene format=3 uid="uid://vc0dkptkr1ow"]
[node name="On Collision Trigger" type="Area2D" unique_id=895298999]
collision_layer = 0