1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-25 01:24:23 +00:00

Create Counter behaviour

Look, I can count!
1, 2, 4, 3, 5...
This commit is contained in:
Steffo 2024-04-14 00:51:05 +02:00
parent a5b2394aa5
commit 3d412d6254
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
2 changed files with 47 additions and 0 deletions

29
behaviours/counter.gd Normal file
View file

@ -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)

18
behaviours/counter.tscn Normal file
View file

@ -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")