GodotWIP/src/state_modifier.gd

105 lines
3.2 KiB
GDScript

class_name StateModifier
extends Reference
## State modification
##
## A state modifier doesn't have any direct control of a game object.
## They are transfered from the enter and exit of other states and
## influence the movement or animation of their parent states.
## Rather than going full into paralell state machines I hope that this
## modifier will be suitable for a basic state machine.
##
## @WIP
enum TYPE {NONE,EXIT_ANIMATION, ANIMATION_SUFFIX, REPLACE_ANIMATION}
export var debug_state: bool = false
export var animation_name: String
## Modification Type
## if an animation is specified, the default behavior will be just to override
## the state animation. Or perform a 'Replace Animation'
var modifier_type = TYPE.NONE
var starting_frame: int = 0
# Move Speed in Pixels Per Second
# These should only apply if Modification Type Set to None.
# (But I'm not sure if I like that model.)
var move_speed: float
var move_speed_modifier: float = 0
var move_speed_modifier_decay: float = 0
var timeout_seconds: float = 0.0
# Attempting to use this as an animation suffix that can linger after
# state transition like 'run-fire' to 'idle-fire' 'jump-fire' etc.
#var animation_suffix: bool = false
#var pre_append_animation: bool = false
# not sure if I can do the array thing from state change. Maybe?
# disabling this sequencing for now.
# export(Array, String) var animation_sequence
# var animation_index: int = 0
# var current_animation_sequence: int = 0
var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity")
#var animation_sequence_timer: Timer
var state_timeout: Timer
# Meant to be called on timeout or other counters this modifier may have.
# Should be updated whenever modifier owner transfers
var state_call_function: FuncRef
#func copy_modifier() -> StateModifier:
# var new_modifier = StateModifier.new()
# new_modifier.ready(animation_name, modifier_type, timeout_seconds)
#
# return StateModifier.new()
func ready(anim_name:String = '', type = TYPE.NONE, timeout : float = 0, parent:Node = null, function_name : String = "") -> Timer:
# If somebody forgot to specify the type
if anim_name != '' and type == TYPE.NONE:
modifier_type = TYPE.REPLACE_ANIMATION
animation_name = anim_name
modifier_type = type
# if parent != null and function_name != "":
# state_call_function = funcref(parent, function_name)
if timeout > 0:
state_timeout = Timer.new()
state_timeout.wait_time = timeout
state_timeout.one_shot = true
state_timeout.autostart = false
return state_timeout
#add_child(state_timeout)
return null
func transfer_owner(parent:Node):
print("Modifier owner will now be: ", parent.name)
state_call_function = funcref(parent, 'update_animation')
func _on_Timer_timeout():
print("Oh Crap! I can't beleive it worked")
if state_call_function:
if state_call_function.is_valid():
state_call_function.call_func()
else:
print("Warning! Modifier timed out with no callback to alert.")
else:
print("Warning! no function applies.")
func enter() -> void:
if debug_state:
print("Apply State Modifier: ", self.name)
# state_timeout.start()
#modifier_stack_ref = state_modifiers
return
func exit() -> void:
pass
func _update():
pass