Last active
June 1, 2020 12:56
-
-
Save jonchun/f8aa65c0b94a602ac903642a9b620d2b to your computer and use it in GitHub Desktop.
EffectManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name AnimatedSpriteEffect | |
extends AnimatedSprite | |
signal effect_animation_finished(animated_sprite_effect) | |
var loop = false | |
var _owner = null | |
func _ready() -> void: | |
connect("animation_finished", self, "_on_Animation_finished") | |
func _physics_process(delta: float) -> void: | |
if _owner: | |
global_position = _owner.global_position | |
func _on_Animation_finished(): | |
emit_signal("effect_animation_finished", self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name EffectManager | |
extends Node2D | |
signal effect_started | |
signal effect_ended | |
# Dictionary mapping effect names to their spriteframe resource | |
var effect_map: = {} | |
func _init() -> void: | |
# need to first load and create a map of all of our effects | |
var spriteframes_dir = "res://assets/Effect/SpriteFrames/" | |
var spriteframes_list = Utils.list_dir(spriteframes_dir, "tres") | |
for spriteframes in spriteframes_list: | |
var resource: SpriteFrames = load("%s%s" % [spriteframes_dir, spriteframes]) | |
var animations: PoolStringArray = resource.get_animation_names() | |
for animation in animations: | |
effect_map[animation] = resource | |
func play(effect_name: String, _position: Vector2, _owner = null) -> void: | |
if effect_map.has(effect_name): | |
var animated_sprite = AnimatedSpriteEffect.new() | |
animated_sprite.frames = effect_map[effect_name] | |
animated_sprite._owner = _owner | |
animated_sprite.connect("effect_animation_finished", self, "_on_Animation_finished") | |
animated_sprite.global_position = _position | |
add_child(animated_sprite) | |
animated_sprite.play(effect_name) | |
emit_signal("effect_started", effect_name) | |
else: | |
print("[EffectManager] Unable to find effect: %s" % effect_name) | |
func _on_Animation_finished(animated_sprite) -> void: | |
if not animated_sprite.loop: | |
emit_signal("effect_ended", animated_sprite.animation) | |
animated_sprite.visible = false | |
animated_sprite.queue_free() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# returns an array of files in a path and filters on an optional suffix | |
static func list_dir(path: String, suffix: = "") -> Array: | |
var dir = Directory.new() | |
var files = [] | |
if dir.open(path) == OK: | |
dir.list_dir_begin(true, true) | |
var file_name = dir.get_next() | |
while file_name != "": | |
if suffix != "": | |
if file_name.ends_with(suffix): | |
files.append(file_name) | |
else: | |
files.append(file_name) | |
file_name = dir.get_next() | |
else: | |
print("An error occurred when trying to access the path.") | |
return files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment