Lots of work on state modifiers in prep for movement.

This commit is contained in:
Dustin 2025-04-30 23:09:43 -07:00
parent 6d51eaffb0
commit d5818f1c17
8 changed files with 152 additions and 69 deletions

View File

@ -39,7 +39,8 @@ func _ready():
# processed
current_state = get_node(callable_state_machine).current_state
#state_machine_modifiers = get_node(callable_state_machine).state_modifiers
modifier = get_node(callable_state_machine).merged_animation_state_modifiers
## Not doing it this way anymore modifier is applied when one arrives by signal.
#modifier = get_node(callable_state_machine).merged_animation_state_modifiers
# Sprites should only have a process function
# but if I have this will the sprite still animate?
@ -89,6 +90,7 @@ func change_animation(enter_frame := 0, index := 0, suffix := ''):
animation_index = index
current_animation_sequence = animation_index
## TODO: this runs everytime since the modifier will apply once and stay referenced.
if modifier:
##TODO: Apply the animation starting frame
#modifier.animation_starting_frame
@ -240,9 +242,14 @@ func _on_state_change(old_state_name:String, new_state :State):
else:
push_warning("Received non animated Actor state.")
func _on_modifiers_updated(_modifier_type :int, _modifier_action :int):
func _on_modifiers_updated(_modifier_type :int, _modifier_action :int, _merged_modifier :StateModifier):
##TODO: There's so many things I could maybe do here but lets target the
## current use case.
# current use case.
##
## If the signalled merged modifier is a Animation one set it
if _merged_modifier is StateModifierAnimatedActor:
modifier = _merged_modifier
print("<<--- >> is active? ", modifier.is_active)
if _modifier_type == StateModifierAnimatedActor.TYPE.ANIMATION_SUFFIX:
print("A: ", animation, " F: ", frame, " -Modifier Changed Signal")
# Wait for frame to finish before doing anything.

View File

@ -32,7 +32,7 @@ var acceleration = Vector2(0,0)
var physics_delta:float
var process_delta:float
var modifier: StateModifierAnimatedActor
var modifier: StateModifierMovement
#Removing Probably not used
#var attack_function: FuncRef
@ -50,7 +50,7 @@ func _ready():
## A reference to the current state machine state
current_state = get_node(callable_state_machine).current_state
## A reference to the state machine merged modifiers
modifier = get_node(callable_state_machine).merged_animation_state_modifiers
#modifier = get_node(callable_state_machine).merged_animation_state_modifiers
############
## These get called by the parent

View File

@ -3,12 +3,13 @@ class_name StateMachine extends Node
export var debug_state_machine: bool = false
signal state_changed(old_state_name, new_state)
signal modifiers_updated(modifier_type, modifier_eventid)
signal modifiers_updated(modifier_type, modifier_eventid, _merged_modifier)
var current_state: State
var state_modifiers: Array
onready var merged_animation_state_modifiers :StateModifierAnimatedActor = StateModifierAnimatedActor.new()
var merged_modifiers : Dictionary
#onready var merged_animation_state_modifiers :StateModifierAnimatedActor = StateModifierAnimatedActor.new()
export var starting_state_name = 'default'
export(Array,Resource) var states
@ -48,19 +49,25 @@ func _ready():
push_error("State machine cannot find starting state: " + starting_state_name)
func _physics_process(delta):
if(state_modifiers.size() > 0):
merged_animation_state_modifiers.reset()
merged_animation_state_modifiers.animation_name = 'floopy doo,'
merge_modifiers()
#func _physics_process(delta):
# if(state_modifiers.size() > 0):
# merged_animation_state_modifiers.reset()
# merged_animation_state_modifiers.animation_name = 'floopy doo,'
# #merge_modifiers()
func merge_modifiers():
merged_animation_state_modifiers.reset()
#merged_animation_state_modifiers.reset()
for modgroup in merged_modifiers.keys():
merged_modifiers["StateModifierAnimatedActor"].reset()
for m in state_modifiers:
if m is StateModifierAnimatedActor:
if merged_modifiers.has("StateModifierAnimatedActor") == false:
merged_modifiers["StateModifierAnimatedActor"] = StateModifierAnimatedActor.new()
#print(m.name)
var this_mod_type :StateModifierAnimatedActor = merged_modifiers["StateModifierAnimatedActor"]
if m.is_active:
merged_animation_state_modifiers.merge_StateModifierAnimatedActor(m)
#merged_animation_state_modifiers.merge(m)
this_mod_type.merge(m)
# Change to the new state by first calling any exit logic on the current state.
func change_state(new_state: State) -> void:
@ -113,13 +120,16 @@ func push_state_modifier(_state_modifier: StateModifier) -> void:
if debug_state_machine:
print("Add State Modifier: ", _state_modifier.name, " now size ", state_modifiers.size())
func handle_modifier_events(eventID :int):
func handle_modifier_events(eventID :int, modifier_class_name :String):
##TODO: why did I care about the events? they both do the same thing
if eventID == StateModifier.EVENT_ID.ACTIVATED:
if debug_state_machine:
print ("A mod activated! Which one though?")
print ("A mod activated! Which one though?", modifier_class_name)
merge_modifiers()
emit_signal("modifiers_updated",merged_animation_state_modifiers.modifier_type, eventID)
emit_signal("modifiers_updated",merged_modifiers[modifier_class_name].modifier_type,
eventID,
merged_modifiers[modifier_class_name] )
elif eventID == StateModifier.EVENT_ID.DEACTIVATED:
merge_modifiers()
emit_signal("modifiers_updated",merged_animation_state_modifiers.modifier_type, eventID)
emit_signal("modifiers_updated",merged_modifiers[modifier_class_name].modifier_type, eventID, merged_modifiers[modifier_class_name])

View File

@ -86,6 +86,10 @@ var state_call_function: FuncRef
# mp.directional_modifier = modifier_properties.directional_modifier
# return mp
## Because I've tried a few ways to identify the object without a CR error
func _to_string():
return "StateModifier"
func setup(modifier_name:String, type = TYPE.NONE,_timeout_seconds : float = 0):
name = modifier_name
modifier_type = type
@ -102,8 +106,11 @@ func reset():
modifier_type = TYPE.NONE
func copy(_copy_state: StateModifier):
_copy_state.name = name
_copy_state.modifier_type = modifier_type
## This should really be the other way.
# _copy_state.name = name
# _copy_state.modifier_type = modifier_type
name = _copy_state.name
modifier_type = _copy_state.modifier_type
func merge(_merge_from_modifier: StateModifier):
@ -113,12 +120,12 @@ func activate():
is_active = true
if timeout != null:
timeout.start()
emit_signal("modifier_event",EVENT_ID.ACTIVATED)
emit_signal("modifier_event",EVENT_ID.ACTIVATED, to_string())
func deactivate():
is_active = false
print(name," <- Got call to deactivate.")
emit_signal("modifier_event",EVENT_ID.DEACTIVATED)
emit_signal("modifier_event",EVENT_ID.DEACTIVATED, to_string())
#func ready(modifier_name:String, anim_name:String = '', type = TYPE.NONE, timeout : float = 0, parent:Node = null, function_name : String = "") -> Timer:
# name = modifier_name

View File

@ -8,15 +8,8 @@ var animation_suffix: String = ''
var animation_starting_frame: int = 0
var animation_speed: float
var horizontal_speed: float = 0
var horizontal_acceleration: float = 0
## Movement Speed Offsets (Positive or Negative)
var horizontal_speed_offset: float = 0
## Applies only if offset applies
var horizontal_speed_offset_acceleration: float = 0
var jerk_factor: float = 0
var gravity: int = 0
func _to_string():
return "StateModifierAnimatedActor"
func reset():
.reset()
@ -24,26 +17,30 @@ func reset():
animation_suffix = ''
animation_starting_frame = 0
horizontal_speed = 0
horizontal_acceleration = 0
## Movement Speed Offsets (Positive or Negative)
horizontal_speed_offset = 0
## Applies only if offset applies
horizontal_speed_offset_acceleration = 0
jerk_factor = 0
func copy_StateModifierAnimatedActor(_copy_state: StateModifierAnimatedActor):
func copy(_copy_state: StateModifier):
.copy(_copy_state)
## Using is gives me cyclic redundancy problems
#if _copy_state is StateModifierAnimatedActor:
#if _copy_state.is_class("StateModifierAnimatedActor"):
if _copy_state.to_string() == "StateModifierAnimatedActor":
_copy_state.animation_name = animation_name
_copy_state.animation_starting_frame = animation_starting_frame
_copy_state.animation_speed = animation_speed
else:
print_debug("Non AnimatedActor modifier passed into this modifier")
##TODO: finish these
_copy_state.horizontal_speed = horizontal_speed
_copy_state.horizontal_acceleration = horizontal_acceleration
##DEPR: Need to remove this
#func copy_StateModifierAnimatedActor(_copy_state: StateModifierAnimatedActor):
# .copy(_copy_state)
# _copy_state.animation_name = animation_name
# _copy_state.animation_starting_frame = animation_starting_frame
# _copy_state.animation_speed = animation_speed
func merge_StateModifierAnimatedActor(_merge_from_modifier: StateModifierAnimatedActor):
func merge(_merge_from_modifier: StateModifier):
.merge(_merge_from_modifier)
#print(_merge_from_modifier.to_string(), " I'm a mod named: ", _merge_from_modifier.get_class())
#if _merge_from_modifier is StateModifierAnimatedActor:
if _merge_from_modifier.to_string() == "StateModifierAnimatedActor":
if modifier_type == TYPE.NONE and _merge_from_modifier.modifier_type != TYPE.NONE:
modifier_type = _merge_from_modifier.modifier_type
@ -56,13 +53,28 @@ func merge_StateModifierAnimatedActor(_merge_from_modifier: StateModifierAnimate
TYPE.REPLACE_ANIMATION:
if _merge_from_modifier.animation_name != '':
animation_name = _merge_from_modifier.animation_name
_: # None
# _: # None
# print ('physics?')
horizontal_speed += _merge_from_modifier.horizontal_speed
horizontal_acceleration += _merge_from_modifier.horizontal_acceleration
horizontal_speed_offset += _merge_from_modifier.horizontal_speed_offset
horizontal_speed_offset_acceleration += _merge_from_modifier.horizontal_speed_offset_acceleration
##DEPR: Need to remove this
#func merge_StateModifierAnimatedActor(_merge_from_modifier: StateModifierAnimatedActor):
# .merge(_merge_from_modifier)
# if modifier_type == TYPE.NONE and _merge_from_modifier.modifier_type != TYPE.NONE:
# modifier_type = _merge_from_modifier.modifier_type
#
# #_copy_state.animation_name += animation_name
# match _merge_from_modifier.modifier_type:
# TYPE.ANIMATION_SUFFIX:
# # Attempt to seemlessly transition animations.
# if _merge_from_modifier.animation_suffix != '':
# animation_suffix = _merge_from_modifier.animation_suffix
# TYPE.REPLACE_ANIMATION:
# if _merge_from_modifier.animation_name != '':
# animation_name = _merge_from_modifier.animation_name
# _: # None
# print ('physics?')
#
#
#_copy_state.animation_starting_frame = animation_starting_frame
#_copy_state.animation_speed = animation_speed

View File

@ -0,0 +1,41 @@
class_name StateModifierMovement
extends StateModifier
## Movement Modifiers
var horizontal_speed: float = 0
var horizontal_acceleration: float = 0
## Movement Speed Offsets (Positive or Negative)
var horizontal_speed_offset: float = 0
## Applies only if offset applies
var horizontal_speed_offset_acceleration: float = 0
var jerk_factor: float = 0
var gravity: int = 0
func _to_string():
return "StateModifierMovement"
func reset():
.reset()
horizontal_speed = 0
horizontal_acceleration = 0
## Movement Speed Offsets (Positive or Negative)
horizontal_speed_offset = 0
## Applies only if offset applies
horizontal_speed_offset_acceleration = 0
jerk_factor = 0
func copy(_copy_state: StateModifier):
.copy(_copy_state)
if _copy_state.to_string() == "StateModifierMovement":
_copy_state.horizontal_speed = horizontal_speed
_copy_state.horizontal_acceleration = horizontal_acceleration
func merge(_merge_from_modifier: StateModifier):
.merge(_merge_from_modifier)
if _merge_from_modifier.to_string() == "StateModifierMovement":
horizontal_speed += _merge_from_modifier.horizontal_speed
horizontal_acceleration += _merge_from_modifier.horizontal_acceleration
horizontal_speed_offset += _merge_from_modifier.horizontal_speed_offset
horizontal_speed_offset_acceleration += _merge_from_modifier.horizontal_speed_offset_acceleration

View File

@ -44,7 +44,7 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://src/Interactables/HealthPickup.gd"
}, {
"base": "Area2D",
"base": "Reference",
"class": "Interactable",
"language": "GDScript",
"path": "res://lib/templates/Interactable/Interactable.gd"
@ -154,6 +154,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://lib/classes/state_modifier_animation.gd"
}, {
"base": "StateModifier",
"class": "StateModifierMovement",
"language": "GDScript",
"path": "res://lib/classes/state_modifier_movement.gd"
}, {
"base": "CanvasLayer",
"class": "UIComponent",
"language": "GDScript",
@ -189,6 +194,7 @@ _global_script_class_icons={
"StateMachineAnimatedActor": "",
"StateModifier": "",
"StateModifierAnimatedActor": "",
"StateModifierMovement": "",
"UIComponent": ""
}

View File

@ -76,5 +76,5 @@ func _on_state_exited_attack_sword():
print("you just swung your sword, you should get a modifier.")
#attack_sword.timeout.start()
attack_sword.activate()
print ("you has merged as? :" ,modifier.animation_name)
print ("you has merged as? :" ,modifier.animation_suffix)