Strip out all that state modifier stuff for a new approach.

This commit is contained in:
Nitsud Yarg 2024-06-25 21:14:30 -07:00
parent 4fda694182
commit 188bf46a09
12 changed files with 139 additions and 189 deletions

View File

@ -69,7 +69,7 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://src/state_machine_animated_actor.gd"
}, {
"base": "Node",
"base": "Object",
"class": "StateModifier",
"language": "GDScript",
"path": "res://src/state_modifier.gd"

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=127 format=2]
[gd_scene load_steps=126 format=2]
[ext_resource path="res://src/templates/Actor/ActorTemplate.tscn" type="PackedScene" id=1]
[ext_resource path="res://src/playerD/movement_component.gd" type="Script" id=2]
@ -8,7 +8,6 @@
[ext_resource path="res://src/playerD/states/fall.gd" type="Script" id=6]
[ext_resource path="res://src/components/Health_Component.tscn" type="PackedScene" id=7]
[ext_resource path="res://src/playerD/states/jump.gd" type="Script" id=8]
[ext_resource path="res://src/state_modifier.gd" type="Script" id=9]
[ext_resource path="res://src/playerD/states/attack.gd" type="Script" id=10]
[ext_resource path="res://src/playerD/states/dash.gd" type="Script" id=11]
[ext_resource path="res://src/playerD/states/hurt.gd" type="Script" id=12]
@ -576,14 +575,7 @@ roll_node = NodePath("../roll")
script = ExtResource( 6 )
move_speed = 90.0
animation_sequence = [ "jump" ]
landing_node = NodePath("landing")
[node name="landing" type="Node" parent="movement_state_machine/fall" index="0"]
script = ExtResource( 9 )
animation_name = "jump"
modifier_type = "Exit Animation"
starting_frame = 6
pre_append_animation = true
landing_node = NodePath("")
[node name="move" parent="movement_state_machine" index="2"]
script = ExtResource( 5 )
@ -599,7 +591,6 @@ animation_sequence = [ "jump" ]
idle_node = NodePath("../idle")
fall_node = NodePath("../fall")
attack_node = NodePath("../attack")
landing_node = NodePath("../fall/landing")
jump_force = 250.0
[node name="attack" type="Node" parent="movement_state_machine" index="4"]
@ -609,14 +600,6 @@ jump_node = NodePath("../jump")
idle_node = NodePath("../idle")
fall_node = NodePath("../fall")
move_node = NodePath("../move")
draw_weapon_node = NodePath("draw_weapon")
[node name="draw_weapon" type="Node" parent="movement_state_machine/attack" index="0"]
script = ExtResource( 9 )
animation_name = "_shoot"
modifier_type = "Animation Suffix"
timeout_seconds = 2.2
animation_suffix = true
[node name="hurt" type="Node" parent="movement_state_machine" index="5"]
script = ExtResource( 12 )

View File

@ -10,7 +10,7 @@ onready var jump_state: State = get_node(jump_node)
onready var idle_state: State = get_node(idle_node)
onready var fall_state: State = get_node(fall_node)
onready var move_state: State = get_node(move_node)
onready var weapon_state_modifier: StateModifier = get_node(draw_weapon_node)
#onready var weapon_state_modifier: StateModifier = get_node(draw_weapon_node)
var can_fire = true
@ -19,7 +19,7 @@ signal do_attack()
func enter() -> void:
#.enter()
##TODO: Turn this to clear only the animation modifiers.
modifier_stack_ref.clear()
# modifier_stack_ref.clear()
mod_animation_sequence.clear()
#mod_animation_sequence = animation_sequence.duplicate(true)
mod_animation_sequence.append_array(animation_sequence)
@ -94,8 +94,8 @@ func process_physics(delta: float) -> State:
func exit() -> void:
# force timer reset
weapon_state_modifier.state_timeout.start()
push_animation_state_modifier(weapon_state_modifier)
# weapon_state_modifier.state_timeout.start()
# push_animation_state_modifier(weapon_state_modifier)
#$"../draw_weapon".state_timeout.start()
return

View File

@ -1,7 +1,7 @@
extends StateAnimatedActor
func enter() -> void:
modifier_stack_ref.clear()
# modifier_stack_ref.clear()
.enter()
move_component.velocity.x = 0

View File

@ -4,13 +4,13 @@ export (NodePath) var idle_node
export (NodePath) var landing_node
onready var idle_state: State = get_node(idle_node)
onready var landing_mod: StateModifier = get_node(landing_node)
#onready var landing_mod: StateModifier = get_node(landing_node)
func enter() -> void:
var landing_mod_index = modifier_stack_ref.rfind(landing_mod)
if landing_mod_index != -1:
#print("You're Supposed to be dead!?: ", landing_mod_index)
modifier_stack_ref.remove(landing_mod_index)
# var landing_mod_index = modifier_stack_ref.rfind(landing_mod)
# if landing_mod_index != -1:
# #print("You're Supposed to be dead!?: ", landing_mod_index)
# modifier_stack_ref.remove(landing_mod_index)
.enter()
# Jump to fall frame
animations.frame = 5
@ -32,7 +32,7 @@ func process_physics(delta: float) -> State:
func exit() -> void:
.exit()
push_animation_state_modifier(landing_mod)
# push_animation_state_modifier(landing_mod)
# $"../landing".enter()
# modifier_stack_ref.push_front($"../landing")
return

View File

@ -7,7 +7,7 @@ onready var idle_state: State = get_node(idle_node)
var flash_color = Color.yellow
func enter() -> void:
modifier_stack_ref.clear()
# modifier_stack_ref.clear()
.enter()
move_component.velocity.x = 0
#parent.set_hurtbox(false)

View File

@ -9,15 +9,15 @@ export (NodePath) var landing_node
onready var idle_state: State = get_node(idle_node)
onready var fall_state: State = get_node(fall_node)
onready var attack_state: State = get_node(attack_node)
onready var landing_mod: StateModifier = get_node(landing_node)
#onready var landing_mod: StateModifier = get_node(landing_node)
export var jump_force: float = 200.0
func enter() -> void:
var landing_mod_index = modifier_stack_ref.rfind(landing_mod)
if landing_mod_index != -1:
#print("You're Supposed to be dead!?: ", landing_mod_index)
modifier_stack_ref.remove(landing_mod_index)
# var landing_mod_index = modifier_stack_ref.rfind(landing_mod)
# if landing_mod_index != -1:
# #print("You're Supposed to be dead!?: ", landing_mod_index)
# modifier_stack_ref.remove(landing_mod_index)
# if modifier_stack_ref.has(landing_mod):
# print("Jump from Landing Mod at: ", modifier_stack_ref.rfind(landing_mod))
.enter()

View File

@ -8,7 +8,7 @@ signal state_entered()
signal state_exited()
# Declare member variables here. Examples:
var modifier_stack_ref: Array # Well this didn't work
#var modifier_stack_ref: Array # Well this didn't work
var state_timeout: Timer
# Called when the node enters the scene tree for the first time.

View File

@ -68,7 +68,6 @@ func _ready():
func enter() -> void:
# animations.connect("animation_finished", self, "_on_AnimatedSprite_animation_finished")
mod_animation_sequence.clear()
#mod_animation_sequence = animation_sequence.duplicate(true)
mod_animation_sequence.append_array(animation_sequence)
@ -86,34 +85,34 @@ func enter() -> void:
#modifier_stack_ref = state_modifiers
# 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:
# 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
@ -222,43 +221,44 @@ func push_animation_state_modifier(modifier: StateModifier):
# 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
# 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)
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
# 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)
else:
modifier_stack_ref.push_front(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)
# 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

@ -4,7 +4,7 @@ export (NodePath) var starting_state
export var debug_state_machine: bool = false
var current_state: State
var state_modifiers: Array
#var state_modifiers: Array
# Initialize the state machine by giving each child state a reference to the
# parent object it belongs to and enter the default starting_state.
@ -13,7 +13,7 @@ func init() -> void:
if child is State:
if debug_state_machine:
print("Initializing State Node: ", child.name)
child.modifier_stack_ref = state_modifiers
# child.modifier_stack_ref = state_modifiers
if debug_state_machine:
child.debug_state = true
@ -27,10 +27,10 @@ func change_state(new_state: State) -> void:
current_state = new_state
current_state.enter()
if(state_modifiers.size() > 0 and debug_state_machine):
print("Active Modifiers:")
for mods in state_modifiers:
print(mods.name)
# if(state_modifiers.size() > 0 and debug_state_machine):
# print("Active Modifiers:")
# for mods in state_modifiers:
# print(mods.name)
# Pass through functions for the Player to call,
# handling state changes as needed.

View File

@ -9,10 +9,10 @@ func change_state(new_state: State) -> void:
current_state = new_state
current_state.enter()
if(state_modifiers.size() > 0 and debug_state_machine):
print("Active Modifiers:")
for mods in state_modifiers:
print(mods.name)
# if(state_modifiers.size() > 0 and debug_state_machine):
# print("Active Modifiers:")
# for mods in state_modifiers:
# print(mods.name)
func set_previous_animation( old_state: StateAnimatedActor, new_state: StateAnimatedActor ) -> void:
# added this to help prevent nul index crashes. (Usually because of signal based state changes.)
@ -35,7 +35,7 @@ func init_animated_actor(parent: KinematicBody2D, animations: AnimatedSprite, mo
child.parent = parent
child.animations = animations
child.move_component = move_component
child.modifier_stack_ref = state_modifiers
# child.modifier_stack_ref = state_modifiers
if debug_state_machine:
child.debug_state = true
@ -49,62 +49,34 @@ func process_frame(delta: float) -> void:
# We could iterate through a whole list of states but I'm not sure i want
# states to be that complex.
var current_anim_state :StateAnimatedActor = current_state
if state_modifiers.empty() == false:
for i in range(state_modifiers.size()):
# if this modifer is a timer based one
if state_modifiers[i].state_timeout.time_left == 0 and state_modifiers[i].timeout_seconds !=0:
# Need to do extra stuff if it's an animation based one
if state_modifiers[i].modifier_type == "Animation Suffix":
if debug_state_machine:
print("State Modifier Timeout: ", state_modifiers[i].name, " -> ", state_modifiers[i].animation_name)
# Reset animation suffix
current_anim_state.animation_suffix = ''
# Get the current frame of animation
var current_frame = current_anim_state.animations.frame
# Set the animation index to the current one
current_anim_state.animation_index = current_anim_state.current_animation_sequence
#current_anim_state.current_animation_sequence
#current_anim_state.animations.play()
current_anim_state.animations.play(
current_anim_state.mod_animation_sequence[current_anim_state.animation_index])
# Try to set the current frame to the same as before.
current_anim_state.animations.frames.frames.size() >= current_frame
current_anim_state.animations.frame = current_frame
##TODO:
# no longer needed this way maybe. way to overengineer man.
# if current_anim_state.animation_sequence.size() >= current_anim_state.mod_animation_sequence.size():
# print("Change sequence!: ", current_anim_state.mod_animation_sequence)
# print("Origin sequence!: ", current_state.mod_animation_sequence)
# if state_modifiers.empty() == false:
# for i in range(state_modifiers.size()):
# # if this modifer is a timer based one
# if state_modifiers[i].state_timeout.time_left == 0 and state_modifiers[i].timeout_seconds !=0:
# # Need to do extra stuff if it's an animation based one
# if state_modifiers[i].modifier_type == "Animation Suffix":
# if debug_state_machine:
# print("State Modifier Timeout: ", state_modifiers[i].name, " -> ", state_modifiers[i].animation_name)
# # Reset animation suffix
# current_anim_state.animation_suffix = ''
# # Get the current frame of animation
# var current_frame = current_anim_state.animations.frame
#
# # Set the animation index to the current one
# current_anim_state.animation_index = current_anim_state.current_animation_sequence
# #current_anim_state.current_animation_sequence
# #current_anim_state.animations.play()
#
# current_anim_state.animations.play(
# current_anim_state.mod_animation_sequence[current_anim_state.animation_index])
# # Try to set the current frame to the same as before.
# current_anim_state.animations.frames.frames.size() >= current_frame
# current_anim_state.animations.frame = current_frame
# else:
# current_anim_state.animations.play(
# current_anim_state.animation_sequence[0])
if debug_state_machine:
print("Pop State Modifier: ", state_modifiers[i].name)
state_modifiers.pop_at(i)
# if state_modifiers[-1].state_timeout.time_left == 0 and state_modifiers[-1].pre_append_animation == false:
#
# if debug_state_machine:
# print("Pop State Modifier: ", state_modifiers[-1].name)
# state_modifiers.pop_back()
# # Reset animation suffix
# current_state.animation_suffix = ''
# var current_frame = current_state.animations.frame
# #current_state.animations.play(current_state.animation_sequence[current_state.animation_index])
# #TODO: Bodge to fix crash
# current_state.animation_index = 0
# current_state.current_animation_sequence = 0
# current_state.animations.play(current_state.animation_sequence[0])
# current_state.animations.frame = current_frame;
# print("Pop State Modifier: ", state_modifiers[i].name)
# state_modifiers.pop_at(i)
var new_state = current_state.process_frame(delta)
if new_state:
change_state(new_state)
@ -114,24 +86,17 @@ func _on_AnimatedSprite_animation_finished():
##TODO:
# It's hard to pop an exit animation off when it's stacked with another kind of animation.
for i in state_modifiers.size():
if state_modifiers[i].modifier_type == "Exit Animation":
#print("Time to pop state modifer: ", state_modifiers[i].animation_name, current_state.animation_suffix, " <-> ", current_state.animations.animation + current_state.animation_suffix)
if (state_modifiers[i].animation_name + current_state.animation_suffix) == current_state.animations.animation:
print("Time to pop state modifer: ", state_modifiers[i].animation_name, current_state.animation_suffix, " <-> ", current_state.animations.animation )
print(current_state.mod_animation_sequence)
state_modifiers.pop_at(i)
current_state.animation_index += 1
break
# for i in state_modifiers.size():
# if state_modifiers[i].modifier_type == "Exit Animation":
# #print("Time to pop state modifer: ", state_modifiers[i].animation_name, current_state.animation_suffix, " <-> ", current_state.animations.animation + current_state.animation_suffix)
# if (state_modifiers[i].animation_name + current_state.animation_suffix) == current_state.animations.animation:
# print("Time to pop state modifer: ", state_modifiers[i].animation_name, current_state.animation_suffix, " <-> ", current_state.animations.animation )
# print(current_state.mod_animation_sequence)
# state_modifiers.pop_at(i)
# current_state.animation_index += 1
# break
# if state_modifiers.empty() == false:
# if state_modifiers[-1].pre_append_animation == true:
# if debug_state_machine:
# print("Pop State Modifier: ", state_modifiers[-1].name)
# state_modifiers.pop_back()
##TODO:
# consider just stopping the animation and index increment here.
if current_state.animations.frames.get_animation_loop(
current_state.animations.animation) == false:
if current_state.animation_sequence.size() > 0:

View File

@ -1,5 +1,5 @@
class_name StateModifier
extends Node
extends Object
## State modification
##
## A state modifier doesn't have any direct control of a game object.
@ -10,6 +10,8 @@ extends Node
##
## @WIP
enum type {NONE,EXIT_ANIMATION}
export var debug_state: bool = false
export var animation_name: String
@ -59,7 +61,7 @@ func _ready():
state_timeout.wait_time = timeout_seconds
state_timeout.one_shot = true
state_timeout.autostart = false
add_child(state_timeout)
#add_child(state_timeout)
func enter() -> void:
if debug_state: