feat: Optimized animation handler and crashing (#463)

Co-authored-by: Dennis Ploeger <develop@dieploegers.de>
This commit is contained in:
Dennis Ploeger
2021-11-25 23:05:17 +01:00
committed by GitHub
parent df88bd6cb6
commit 75c00b4993
3 changed files with 42 additions and 31 deletions

View File

@@ -4,6 +4,7 @@ extends Node
class_name ESCAnimationPlayer
# Emitted when the animation finsihed playing
signal animation_finished(name)
@@ -19,6 +20,9 @@ var _animated_sprite: AnimatedSprite
# Wether the player node is of type AnimationPlayer (just for convenience)
var _is_animation_player: bool = false
# Currently running animation
var _current_animation: String = ""
# Create a new animation player
#
@@ -32,6 +36,23 @@ func _init(node: Node):
_animation_player = node
else:
_animated_sprite = node
node.add_child(self)
# Connect animation signals
func _ready() -> void:
if _is_animation_player:
_player_node.connect(
"animation_finished",
self,
"_on_animation_finished"
)
else:
_player_node.connect(
"animation_finished",
self,
"_on_animation_finished_animated_sprite"
)
# Return the currently playing animation
@@ -70,29 +91,11 @@ func stop():
# - name: The animation name to play
# - backwards: Play backwards
func play(name: String, backwards: bool = false):
if _player_node.is_connected(
"animation_finished",
self,
"_on_animation_finished"
):
_player_node.disconnect(
"animation_finished",
self,
"_on_animation_finished"
)
if _is_animation_player:
_player_node.connect(
"animation_finished",
self,
"_on_animation_finished"
)
_animation_player.seek(0)
else:
_player_node.connect(
"animation_finished",
self,
"_on_animation_finished",
[name]
)
_animated_sprite.frame = 0
_current_animation = name
if backwards and _is_animation_player:
_animation_player.play_backwards(name)
elif backwards:
@@ -151,6 +154,14 @@ func get_length(name: String) -> float:
_animated_sprite.frames.get_animation_speed(name)
# Return true if the ESCAnimationPlayer node is valid, ie. it has a valid player
# node.
# **Returns: true if the ESCAnimationPlayer has a valid player node,
# else false**
func is_valid() -> bool:
return _player_node != null and _player_node is Node
# Transport the animation_finished signal
#
# #### Parameters
@@ -159,16 +170,11 @@ func get_length(name: String) -> float:
func _on_animation_finished(name: String):
if _is_animation_player and not _animation_player.get_animation(name).loop:
_animation_player.stop()
_animation_player.seek(0)
elif not _animated_sprite.frames.get_animation_loop(name):
_animated_sprite.stop()
_animated_sprite.frame = 0
emit_signal("animation_finished", name)
# Return true if the ESCAnimationPlayer node is valid, ie. it has a valid player
# node.
# **Returns: true if the ESCAnimationPlayer has a valid player node,
# else false**
func is_valid() -> bool:
return _player_node != null and _player_node is Node
# Special signal handler for animated sprites
func _on_animation_finished_animated_sprite():
_on_animation_finished(_current_animation)

View File

@@ -208,6 +208,10 @@ func report_errors(p_path: String, errors: Array) -> void:
# * message: Message to log
# * err: if true, write in stderr
func _log(message:String, err: bool = false):
var info = OS.get_datetime()
info["message"] = message
message = "{year}-{month}-{day}T{hour}{minute}{second} {message}" \
.format(info)
if err:
printerr(message)
else:

View File

@@ -61,9 +61,10 @@ func clear_scene() -> void:
if escoria.game_scene.get_parent() == current_scene:
current_scene.remove_child(escoria.game_scene)
current_scene.get_parent().remove_child(current_scene)
current_scene.free()
current_scene.queue_free()
current_scene = null