2024-04-15 21:45:42 +00:00
|
|
|
@icon("res://behaviours/counter.svg")
|
2024-04-13 22:51:05 +00:00
|
|
|
extends Node
|
|
|
|
class_name Counter
|
|
|
|
|
2024-04-15 20:39:43 +00:00
|
|
|
## Counts up or down from a starting value.
|
|
|
|
|
2024-04-13 22:51:05 +00:00
|
|
|
|
2024-04-24 02:43:27 +00:00
|
|
|
signal changed(new: int, old: int)
|
2024-04-13 22:51:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@export var starting_value: int
|
|
|
|
|
2024-04-15 20:39:43 +00:00
|
|
|
var value: int = 0
|
2024-04-13 22:51:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
func change(amount: int):
|
|
|
|
var old = value
|
|
|
|
value = amount
|
2024-04-26 00:15:39 +00:00
|
|
|
changed.emit(value, old)
|
2024-04-13 22:51:05 +00:00
|
|
|
|
|
|
|
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)
|
2024-04-15 20:39:43 +00:00
|
|
|
|
|
|
|
func _ready():
|
|
|
|
value = starting_value
|