diff --git a/behaviours/counter.gd b/behaviours/counter.gd new file mode 100644 index 0000000..a94cf9e --- /dev/null +++ b/behaviours/counter.gd @@ -0,0 +1,29 @@ +extends Node +class_name Counter + + +signal changed(old: int, new: int) + + +@export var starting_value: int + +var value: int + + +func _ready(): + value = starting_value + +func change(amount: int): + var old = value + value = amount + changed.emit(old, value) + +func increase(amount: int = 1): + if amount < 0: + Log.w(self, "Increasing a counter by a negative value.") + change(value + amount) + +func decrease(amount: int = 1): + if amount < 0: + Log.w(self, "Decreasing a counter by a negative value.") + change(value - amount) diff --git a/behaviours/counter.tscn b/behaviours/counter.tscn new file mode 100644 index 0000000..d522baf --- /dev/null +++ b/behaviours/counter.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=2 format=3 uid="uid://brvbtvt4em32"] + +[sub_resource type="GDScript" id="GDScript_tks6b"] +script/source = "extends Node + + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + pass +" + +[node name="Counter" type="Node"] +script = SubResource("GDScript_tks6b")