Skip to content

Commit

Permalink
feat: Optimized animation handler and crashing (#463)
Browse files Browse the repository at this point in the history
Co-authored-by: Dennis Ploeger <[email protected]>
  • Loading branch information
dploeger and dploeger authored Nov 25, 2021
1 parent df88bd6 commit 75c00b4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
64 changes: 35 additions & 29 deletions addons/escoria-core/game/core-scripts/esc_animation_player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extends Node
class_name ESCAnimationPlayer


# Emitted when the animation finsihed playing
signal animation_finished(name)


Expand All @@ -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
#
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
4 changes: 4 additions & 0 deletions addons/escoria-core/game/core-scripts/log/esc_logger.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions addons/escoria-core/game/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 75c00b4

Please sign in to comment.