The_Dark_Side_of_Earth/statusses/status.gd

58 lines
2.2 KiB
GDScript

# Status inherits from Timer: The time until the status expires.
class_name Status extends Timer
var params = {}
const default_parameters = {
# Vulnerable: Player Status. Increases Damage taken by 1 per Hit
"Vulnerable": {},
# Slow: Player Status. Decreases movement speed by speed_factor.
# Can theoretically also be used to increase speed.
"Slow": {
"slow_factor": 0.6
}
}
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.
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
child.params = get_default_parameters(status_name).duplicate(true)
child.params.merge(params_in.duplicate(true))
target.add_child(child)
child.start(duration)
else:
var child : Status = target.get_node(status_name)
child.reapply(duration)
# Checks whether the target is affected by the given status.
static func affects(status_name : String, target : Node) -> bool:
return target.has_node(status_name)
# Returns the status on the target, or null if the status is not present.
static func get_status(status_name : String, target : Node) -> Status:
if affects(status_name, target):
return target.get_node(status_name)
else:
return null
# By default, reapplying a status resets the duration and merges the new parameters into the given ones.
func reapply(duration_new : float, params_new : Dictionary = {}):
self.start(max(duration_new, time_left))
merge_params(params_new)
# Controls the override behavior of each status
func merge_params(params_new):
if name == "Slow" and params.has("slow_factor") and params_new.has("slow_factor"):
params.slow_factor = min(params.slow_factor, params_new.slow_factor)
func expire():
queue_free()
static func get_default_parameters(status_name : String) -> Dictionary:
return default_parameters.get(status_name, {})