feat(character): add animation state API (set/get_animation_state)
This commit is contained in:
@@ -12,6 +12,7 @@ signal state_changed(new_state: CharacterData.State)
|
|||||||
@export var data: CharacterData
|
@export var data: CharacterData
|
||||||
|
|
||||||
var _is_held: bool = false
|
var _is_held: bool = false
|
||||||
|
var _current_anim: String = "idle"
|
||||||
|
|
||||||
const _STATE_COLORS: Dictionary = {
|
const _STATE_COLORS: Dictionary = {
|
||||||
CharacterData.State.HEALTHY: Color(0.6, 0.8, 1.0),
|
CharacterData.State.HEALTHY: Color(0.6, 0.8, 1.0),
|
||||||
@@ -40,6 +41,19 @@ func set_state(new_state: CharacterData.State) -> void:
|
|||||||
state_changed.emit(new_state)
|
state_changed.emit(new_state)
|
||||||
|
|
||||||
|
|
||||||
|
func set_animation_state(anim: String) -> void:
|
||||||
|
_current_anim = anim
|
||||||
|
var sprite: AnimatedSprite2D = get_node_or_null("AnimatedSprite2D") as AnimatedSprite2D
|
||||||
|
if sprite == null or sprite.sprite_frames == null:
|
||||||
|
return
|
||||||
|
if sprite.sprite_frames.has_animation(anim):
|
||||||
|
sprite.play(anim)
|
||||||
|
|
||||||
|
|
||||||
|
func get_animation_state() -> String:
|
||||||
|
return _current_anim
|
||||||
|
|
||||||
|
|
||||||
func _update_visual_state() -> void:
|
func _update_visual_state() -> void:
|
||||||
if data == null:
|
if data == null:
|
||||||
return
|
return
|
||||||
@@ -58,11 +72,13 @@ func _update_visual_state() -> void:
|
|||||||
|
|
||||||
func _on_drag_picked_up(_pos: Vector2) -> void:
|
func _on_drag_picked_up(_pos: Vector2) -> void:
|
||||||
_is_held = true
|
_is_held = true
|
||||||
|
set_animation_state("held")
|
||||||
character_picked_up.emit(self)
|
character_picked_up.emit(self)
|
||||||
|
|
||||||
|
|
||||||
func _on_drag_released(pos: Vector2) -> void:
|
func _on_drag_released(pos: Vector2) -> void:
|
||||||
_is_held = false
|
_is_held = false
|
||||||
|
set_animation_state("idle")
|
||||||
if data == null or data.id.is_empty():
|
if data == null or data.id.is_empty():
|
||||||
return
|
return
|
||||||
GameState.set_character_position(character_id, global_position)
|
GameState.set_character_position(character_id, global_position)
|
||||||
|
|||||||
@@ -44,3 +44,32 @@ func test_character_data_outfit_has_three_empty_slots() -> void:
|
|||||||
assert_eq(_char.data.outfit[0], "")
|
assert_eq(_char.data.outfit[0], "")
|
||||||
assert_eq(_char.data.outfit[1], "")
|
assert_eq(_char.data.outfit[1], "")
|
||||||
assert_eq(_char.data.outfit[2], "")
|
assert_eq(_char.data.outfit[2], "")
|
||||||
|
|
||||||
|
|
||||||
|
func test_default_animation_state_is_idle() -> void:
|
||||||
|
assert_eq(_char.get_animation_state(), "idle")
|
||||||
|
|
||||||
|
|
||||||
|
func test_set_animation_state_sitting() -> void:
|
||||||
|
_char.set_animation_state("sitting")
|
||||||
|
assert_eq(_char.get_animation_state(), "sitting")
|
||||||
|
|
||||||
|
|
||||||
|
func test_set_animation_state_lying() -> void:
|
||||||
|
_char.set_animation_state("lying")
|
||||||
|
assert_eq(_char.get_animation_state(), "lying")
|
||||||
|
|
||||||
|
|
||||||
|
func test_set_animation_state_held() -> void:
|
||||||
|
_char.set_animation_state("held")
|
||||||
|
assert_eq(_char.get_animation_state(), "held")
|
||||||
|
|
||||||
|
|
||||||
|
func test_set_animation_state_happy() -> void:
|
||||||
|
_char.set_animation_state("happy")
|
||||||
|
assert_eq(_char.get_animation_state(), "happy")
|
||||||
|
|
||||||
|
|
||||||
|
func test_set_animation_state_sleeping() -> void:
|
||||||
|
_char.set_animation_state("sleeping")
|
||||||
|
assert_eq(_char.get_animation_state(), "sleeping")
|
||||||
|
|||||||
Reference in New Issue
Block a user