sunder/scenes/entities/player/scripts/energy.gd
2026-02-18 18:33:17 -05:00

44 lines
1.6 KiB
GDScript

# This file is part of sunder.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
## This class defines an Energy component for entities.
@icon("res://assets/icons/energy.svg")
class_name Energy extends Node
## Emitted when [param value] is changed ([param value] > [param 0])
signal changed(new_value:int)
## Emitted when [param value] is exhausted ([param value] == [param 0])
signal exhausted
## The maximum energy value
@export var max_value:float = 100.
## The amount of energy left to consume
@export var value:float = 100.:
set(new_value):
value = clampf(new_value, 0, max_value)
if value > 0:
changed.emit(value)
else:
exhausted.emit()
## Wether the energy [param value] should recharge over time based on [param charge_rate]
@export var charging:bool = true
## The energy charge rate per second, expressed as a percentage of [param max_value]
@export_range(0,100, 1., "suffix:%") var charge_rate:float = 10
func _process(delta: float) -> void:
if charging:
value += charge_rate / 100 * max_value * delta