Project Files
This commit is contained in:
42
Scripts/Characters/Player/sprite.gd
Normal file
42
Scripts/Characters/Player/sprite.gd
Normal file
@@ -0,0 +1,42 @@
|
||||
extends Node2D
|
||||
|
||||
@export var state_machine: PlayerStateMachine
|
||||
@export var direction_component: FacingDirectionComponent
|
||||
|
||||
@onready var base: AnimatedSprite2D = $Base
|
||||
@onready var hair: AnimatedSprite2D = $Hair
|
||||
@onready var pants: AnimatedSprite2D = $Pants
|
||||
@onready var hands: AnimatedSprite2D = $Hands
|
||||
@onready var shoes: AnimatedSprite2D = $Shoes
|
||||
|
||||
var all_parts: Array[AnimatedSprite2D]
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
all_parts = [
|
||||
base, hair, pants, hands, shoes
|
||||
]
|
||||
|
||||
var animation := _build_animation_name(state_machine.GetCurrentState(), direction_component.GetCurrentDirection())
|
||||
var should_flip := _should_flip_horizontal()
|
||||
for part in all_parts:
|
||||
part.animation = animation
|
||||
part.play()
|
||||
part.flip_h = should_flip
|
||||
|
||||
|
||||
func _build_animation_name(state: PlayerStateMachine.States, direction: Enums.Directions) -> String:
|
||||
# e.g. "idle-down", "walking-up"
|
||||
var state_str = PlayerStateMachine.States.keys()[state].to_lower()
|
||||
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()
|
||||
|
||||
|
||||
func _should_flip_horizontal() -> bool:
|
||||
return direction_component.GetCurrentDirection() == Enums.Directions.LEFT
|
||||
1
Scripts/Characters/Player/sprite.gd.uid
Normal file
1
Scripts/Characters/Player/sprite.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cuar23q48cbja
|
||||
19
Scripts/Characters/Player/state_machine.gd
Normal file
19
Scripts/Characters/Player/state_machine.gd
Normal file
@@ -0,0 +1,19 @@
|
||||
extends Node
|
||||
class_name PlayerStateMachine
|
||||
|
||||
enum States {
|
||||
IDLE, WALKING
|
||||
}
|
||||
|
||||
# Public Methods
|
||||
func GetCurrentState() -> States:
|
||||
return States.IDLE
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
1
Scripts/Characters/Player/state_machine.gd.uid
Normal file
1
Scripts/Characters/Player/state_machine.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://c74mhfemxuuco
|
||||
Reference in New Issue
Block a user