From 3d412d62540f4215edfa95f51319e936b5846b53 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 14 Apr 2024 00:51:05 +0200 Subject: [PATCH] Create `Counter` behaviour Look, I can count! 1, 2, 4, 3, 5... --- behaviours/counter.gd | 29 +++++++++++++++++++++++++++++ behaviours/counter.tscn | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 behaviours/counter.gd create mode 100644 behaviours/counter.tscn 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")