Created
February 11, 2023 06:45
-
-
Save boruok/e1e5280801910c6bcb85783fb5c68eca to your computer and use it in GitHub Desktop.
base class for composite object
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 Component extends Object | |
signal added_component(component) | |
signal removed_component(component) | |
var id := -1 | |
var name := "" | |
var parent : Object | |
var groups := [] | |
var _components : Array | |
func add_component(s: Script) -> Component: | |
var c = s.new() # TODO add static typing | |
c.parent = self | |
_components.append(c) | |
emit_signal("added_component", c) | |
return c | |
func get_component(s: Script) -> Component: | |
var idx := _find_c_index(s) | |
return null if idx == -1 else _components[idx] | |
func remove_component(s: Script) -> void: | |
var idx := _find_c_index(s) | |
if idx == -1: | |
return # not found | |
_components[idx].queue_free() | |
emit_signal("removed_component", _components[idx]) | |
func has_component(s: Script) -> bool: | |
return _find_c_index(s) != -1 | |
func get_components() -> Array: | |
return _components | |
func get_components_size() -> int: | |
return _components.size() | |
func update(delta: float) -> void: | |
for c in _components: | |
c.update(delta) | |
func queue_free() -> void: | |
for c in _components: | |
c.queue_free() | |
call_deferred("free") | |
func get_class() -> String: | |
return "Component" | |
func _find_c_index(s: Script) -> int: | |
for i in _components.size(): | |
if _components[i].get_script() == s: | |
return i | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment