Cleanup related to modifier feature.

This commit is contained in:
Nitsud Yarg 2024-06-29 08:56:49 -07:00
parent bc5c67ca82
commit 47bdf1728c
6 changed files with 36 additions and 188 deletions

View File

@ -20,16 +20,20 @@ var draw_weapon_modifier: StateModifier
func _ready():
state_timeout.connect("timeout", self, "disable_cooldown")
draw_weapon_modifier = StateModifier.new()
var mod_timer :Timer = draw_weapon_modifier.ready( "draw_weapon", "_shoot" , draw_weapon_modifier.TYPE.ANIMATION_SUFFIX, 2.0)
add_child(mod_timer)
func disable_cooldown():
print("It's butt kicking time!")
if debug_state:
print("Attack state cooldown timeout. Can attack again.")
state_ready = true
func enter() -> void:
if modifier:
if modifier.get_instance_id() == draw_weapon_modifier.get_instance_id():
print ("They the same cannot apply modifier: ", draw_weapon_modifier.get_instance_id())
#print ("They the same cannot apply modifier: ", draw_weapon_modifier.get_instance_id())
modifier = null
.enter()
##TODO: Turn this to clear only the animation modifiers.
@ -72,10 +76,6 @@ func enter() -> void:
_:
print("Warning!: Attack state encountered unhandled prev animation: ", previous_animation_name)
draw_weapon_modifier = StateModifier.new()
var mod_timer :Timer = draw_weapon_modifier.ready( "_shoot" , draw_weapon_modifier.TYPE.ANIMATION_SUFFIX, 2.0)
add_child(mod_timer)
# An example where the modifier is left to alert on a timeout
#mod_timer.connect("timeout", draw_weapon_modifier, "_on_Timer_timeout")
# An example where the current node subscribes

View File

@ -14,7 +14,7 @@ var speed_decay_rate := 1.0
var adjusted_move_speed: float
func enter():
remove_animation_state_modifiers()
#remove_animation_state_modifiers()
.enter()
adjusted_move_speed = move_speed_modifier
speed_multiplier = 5

View File

@ -12,7 +12,7 @@ func _ready():
landing_modifier= StateModifier.new()
#add_child(modifier)
#modifier.init_ref()
landing_modifier.ready( "jump" , landing_modifier.TYPE.EXIT_ANIMATION)
landing_modifier.ready( "landing", "jump" , landing_modifier.TYPE.EXIT_ANIMATION)
landing_modifier.starting_frame = 6
#print("ready! MOD")

View File

@ -9,7 +9,7 @@ onready var idle_state: State = get_node(idle_node)
var speed_decay_rate := 1.0
func enter():
remove_animation_state_modifiers()
#remove_animation_state_modifiers()
.enter()

View File

@ -151,17 +151,7 @@ func update_animation(enter_frame := 0, index := 0, suffix := ''):
return
func enter() -> void:
# mod_animation_sequence.clear()
# #mod_animation_sequence = animation_sequence.duplicate(true)
# mod_animation_sequence.append_array(animation_sequence)
# # Reset animation suffix in case there isn't one
# animation_suffix = ''
# animation_index = 0
# current_animation_sequence = 0
# #var enter_animation = ''
# var enter_frame = 0
func enter() -> void:
if debug_state:
print(parent.name, " entering State: ", self.name)
move_component.current_movement_state = self.name
@ -169,101 +159,6 @@ func enter() -> void:
#modifier_stack_ref = state_modifiers
update_animation()
# if modifier != null:
## if modifier.modifier_type == modifier.TYPE.EXIT_ANIMATION:
## #print("OHHH Modifier Applies! ", modifier.animation_name)
## mod_animation_sequence.push_front(modifier.animation_name)
## enter_frame = modifier.starting_frame
# match modifier.modifier_type:
# modifier.TYPE.EXIT_ANIMATION:
# mod_animation_sequence.push_front(modifier.animation_name)
# enter_frame = modifier.starting_frame
#
# modifier.TYPE.ANIMATION_SUFFIX:
# if animations.frames.has_animation(mod_animation_sequence[animation_index] + modifier.animation_name):
# animation_suffix = modifier.animation_name
# else:
# print("Warning!: Modifier attempting to apply suffix that doesn't exist ",
# mod_animation_sequence[animation_index] + modifier.animation_name)
#
# modifier.TYPE.REPLACE_ANIMATION:
# #animations.play(modifier_stack_ref[i].animation_name)
# #animations.frame = modifier_stack_ref[i].starting_frame
# mod_animation_sequence.clear()
# mod_animation_sequence.push_back(modifier.animation_name)
# enter_frame = modifier.starting_frame
#
# if mod_animation_sequence.size() > 0:
# if debug_state:
# print("Starting Animation: " , mod_animation_sequence[animation_index] + animation_suffix ,
# " frame count ", animations.frames.get_frame_count(mod_animation_sequence[animation_index] + animation_suffix))
# animations.play(mod_animation_sequence[animation_index] + animation_suffix)
# #TODO: maybe check current animatio has this frame
# animations.frame = enter_frame
# else:
# print("Error! Resolved to empty animation sequence!?")
# return
# A new attempt at animation modifiers
# if modifier_stack_ref.empty() == false:
# print("nope: ")
# var i = modifier_stack_ref.size() - 1
# ##NOTE: i apparently reverse range itteration doesn't work
# #for i in range(modifier_stack_ref.size(), 0): # work in reverse
# while (i >= 0 and modifier_stack_ref[i].modifier_type != "None"):
# print("Animation Modifier!: ",modifier_stack_ref[i].modifier_type)
# match modifier_stack_ref[i].modifier_type:
# "Exit Animation":
# mod_animation_sequence.push_front(modifier_stack_ref[i].animation_name)
# enter_frame = modifier_stack_ref[i].starting_frame
#
# # Gonna ty and avoid this for now
# #modifier_stack_ref.pop_at(i) # we're done with this modifier
#
# "Animation Suffix":
# if animations.frames.has_animation(mod_animation_sequence[animation_index] + modifier_stack_ref[i].animation_name):
# animation_suffix = modifier_stack_ref[i].animation_name
# else:
# print("Warning!: Animation suffix that doesn't exist ",
# mod_animation_sequence[animation_index] + modifier_stack_ref[i].animation_name)
# "Replace Animation":
# #animations.play(modifier_stack_ref[i].animation_name)
# #animations.frame = modifier_stack_ref[i].starting_frame
# mod_animation_sequence.clear()
# mod_animation_sequence.push_back(modifier_stack_ref[i].animation_name)
# enter_frame = modifier_stack_ref[i].starting_frame
# i -= 1
# if modifier_stack_ref.empty() == false: # a modifier is applied
# if modifier_stack_ref[-1].animation_name != '': # the first one has an animation
# if modifier_stack_ref[-1].animation_suffix: # it's a suffix type
# if mod_animation_sequence.size() > 0: # the current animation is multipart (sequence > 0)
# animation_index = 0
# current_animation_sequence = 0
# # Guess I don't need this one anymore
# #if animations.frames.get_animation_names().has(animation_sequence[animation_index] + modifier_stack_ref[-1].animation_name):
# if animations.frames.has_animation(mod_animation_sequence[animation_index] + modifier_stack_ref[-1].animation_name):
# animation_suffix = modifier_stack_ref[-1].animation_name
# elif modifier_stack_ref[-1].pre_append_animation == true: # it's a pre-append type
# #animation_sequence.size()
# print ("DEBUG: A prepend animation")
# #mod_animation_sequence = animation_sequence
# mod_animation_sequence.push_front(modifier_stack_ref[-1].animation_name)
# else:
# animations.play(modifier_stack_ref[-1].animation_name)
# animations.frame = modifier_stack_ref[-1].starting_frame
# return
# if mod_animation_sequence.size() > 0:
# if debug_state:
# print("Starting Animation: " , mod_animation_sequence[animation_index] + animation_suffix)
# animations.play(mod_animation_sequence[animation_index] + animation_suffix)
# #TODO: maybe check current animatio has this frame
# animations.frame = enter_frame
# else:
# print("Error! Resolved to empty animation sequence!?")
return
func exit() -> void:
@ -276,10 +171,12 @@ func transfer_modifiers(exiting_state_modifier : StateModifier):
if modifier == null: # We have no existing modifier applied.
match exiting_state_modifier.modifier_type:
exiting_state_modifier.TYPE.ANIMATION_SUFFIX:
print("Transferring Animation Suffix")
if debug_state:
print("Transferring Animation Suffix")
modifier = exiting_state_modifier
exiting_state_modifier.TYPE.EXIT_ANIMATION:
print("Exit Animation: well this was useless.")
if debug_state:
print("Exit Animation: well this was useless.")
else:
print("Insert modifier merge function here...")
@ -371,54 +268,3 @@ func move_actor_as_desired(delta: float):
move_component.velocity.x = move_component.desired_movement_vector.x * new_speed
move_component.velocity = parent.move_and_slide(move_component.velocity, Vector2(0, -1))
func push_animation_state_modifier(modifier: StateModifier):
## This function attempt to optimally place multiple modifiers in an array
# in such a way where the first elements are modifiers without animation changes
# the last elements are animation changes or animation suffixes.
# there is some potential for performance issues at scale with this kinds of operations.
# This function will grow in complexity as needed but the idea is that here is
# where different modifier logic can be tested and developed.
# First assure this modifer isn't already there. If it is, maybe I should reset it?
# if modifier_stack_ref.has(modifier) == false:
# modifier.enter() # call enter function of the modifier
# if modifier_stack_ref.size() > 0: # only bother with this if multiple modifers applied
# # Get the last modifier in the stack
# var last_modifer: StateModifier = modifier_stack_ref[-1]
# # If the modifier at the back and the new one are the same type, we have a problem.
# if last_modifer.modifier_type == modifier.modifier_type and last_modifer.modifier_type != 'None':
# print("WARNING: Multiple same type modifiers applying.")
# # Replace the modifier
# modifier_stack_ref.pop_back()
# modifier_stack_ref.push_back(modifier)
# else:
# match last_modifer.modifier_type:
# "None":
# # no special processing, push it to the front of a list
# modifier_stack_ref.push_front(modifier)
# "Exit Animation":
# if modifier.modifier_type == "Animation Suffix":
# # We place this at the back so we can still modify it maybe?
# modifier_stack_ref.push_back(modifier)
# "Animation Suffix":
# if modifier.modifier_type == "Exit Animation":
# # We have to place this one right before maybe?
# # Whoops! Forgot to actually do this
# #modifier_stack_ref.push_back(modifier)
# modifier_stack_ref.insert(modifier_stack_ref.size() - 1, modifier)
## if last_modifer.animation_name != '': # and if an animation applies
## if last_modifer.pre_append_animation == modifier.pre_append_animation: # They're the same type
## if modifier.pre_append_animation == true and modifier_stack_ref[-1].animation_suffix == true:
## modifier.animation_suffix
## modifier_stack_ref.push_back(modifier)
# else:
# modifier_stack_ref.push_front(modifier)
pass
func remove_animation_state_modifiers():
# for i in modifier_stack_ref.size():
# if modifier_stack_ref[i].modifier_type != "None":
# #print("Time to pop state modifer: ", state_modifiers[i].animation_name, current_state.animation_suffix, " <-> ", current_state.animations.animation + current_state.animation_suffix)
# modifier_stack_ref.pop_at(i)
pass

View File

@ -12,16 +12,19 @@ extends Reference
enum TYPE {NONE,EXIT_ANIMATION, ANIMATION_SUFFIX, REPLACE_ANIMATION}
export var debug_state: bool = false
var debug_state: bool = false
export var animation_name: String
## Animation modifier variables
## The name of the animation that could be applied and starting frame
var animation_name: String
var starting_frame: int = 0
## 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
var name :String = ''
# Move Speed in Pixels Per Second
# These should only apply if Modification Type Set to None.
@ -30,7 +33,8 @@ var move_speed: float
var move_speed_modifier: float = 0
var move_speed_modifier_decay: float = 0
var timeout_seconds: float = 0.0
var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity")
# Attempting to use this as an animation suffix that can linger after
# state transition like 'run-fire' to 'idle-fire' 'jump-fire' etc.
@ -43,23 +47,31 @@ var timeout_seconds: float = 0.0
# 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
var timeout_seconds: float = 0.0
# 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
## Can't do it this way.
#func copy_modifier() -> StateModifier:
# var new_modifier = StateModifier.new()
# new_modifier.ready(animation_name, modifier_type, timeout_seconds)
#
# return StateModifier.new()
#
#func merge_modifier(merging_mod: StateModifier) -> 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:
func ready(modifier_name:String, 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
@ -76,12 +88,15 @@ func ready(anim_name:String = '', type = TYPE.NONE, timeout : float = 0, parent:
#add_child(state_timeout)
return null
## Transfer and callback related methods. These sort of worked but I didn't end up using them
## They're an interesting idea though.
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")
#print("Oh Crap! I can't beleive it worked")
if state_call_function:
if state_call_function.is_valid():
state_call_function.call_func()
@ -89,16 +104,3 @@ func _on_Timer_timeout():
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