The_Dark_Side_of_Earth/statusses/status.gd

59 lines
2.2 KiB
GDScript3
Raw Normal View History

2025-10-14 15:57:23 +02:00
# Status inherits from Timer: The time until the status expires.
2025-10-11 16:37:14 +02:00
class_name Status extends Timer
var params = {}
const default_parameters = {
2025-10-14 15:57:23 +02:00
# Vulnerable: Player Status. Increases Damage taken by 1 per Hit
2025-10-11 16:37:14 +02:00
"Vulnerable": {},
2025-10-14 15:57:23 +02:00
# Slow: Player Status. Decreases movement speed by speed_factor.
# Can theoretically also be used to increase speed.
2025-10-11 16:37:14 +02:00
"Slow": {
"slow_factor": 0.6
}
}
2025-10-14 15:57:23 +02:00
func _ready() -> void:
timeout.connect(expire)
one_shot = true
# Applies the given Status as a child to the target node for the given duration.
# Was intended to work through statusses inheriting from Status, but Godot inheritance interacts terribly with static things.
# If that status is already present on the node, duration is reset and reapply is called.
2025-10-11 16:37:14 +02:00
static func apply(status_name : String, target : Node, duration : float, params_in : Dictionary = {}):
if not target.has_node(status_name):
var child = Status.new()
child.name = status_name
2025-10-11 19:06:46 +02:00
child.params = get_default_parameters(status_name).duplicate(true)
child.params.merge(params_in.duplicate(true))
2025-10-11 16:37:14 +02:00
target.add_child(child)
child.start(duration)
else:
var child : Status = target.get_node(status_name)
child.reapply(duration)
2025-10-14 15:57:23 +02:00
# Checks whether the target is affected by the given status.
2025-10-11 16:37:14 +02:00
static func affects(status_name : String, target : Node) -> bool:
return target.has_node(status_name)
2025-10-14 15:57:23 +02:00
# Returns the status on the target, or null if the status is not present.
2025-10-11 16:37:14 +02:00
static func get_status(status_name : String, target : Node) -> Status:
if affects(status_name, target):
return target.get_node(status_name)
else:
return null
2025-10-14 15:57:23 +02:00
# By default, reapplying a status resets the duration and merges the new parameters into the given ones.
2025-10-11 18:22:15 +02:00
func reapply(duration_new : float, params_new : Dictionary = {}):
2025-10-11 16:37:14 +02:00
self.start(max(duration_new, time_left))
2025-10-11 18:22:15 +02:00
merge_params(params_new)
2025-10-14 15:57:23 +02:00
# Controls the override behavior of each status
2025-10-11 18:22:15 +02:00
func merge_params(params_new):
2025-10-11 19:06:46 +02:00
if name == "Slow" and params.has("slow_factor") and params_new.has("slow_factor"):
2025-10-11 18:22:15 +02:00
params.slow_factor = min(params.slow_factor, params_new.slow_factor)
2025-10-11 16:37:14 +02:00
func expire():
queue_free()
static func get_default_parameters(status_name : String) -> Dictionary:
2025-10-11 19:06:46 +02:00
return default_parameters.get(status_name, {})