More state modifier fixes. Still getting crashes.

This commit is contained in:
Nitsud Yarg 2024-06-16 12:41:07 -07:00
parent 438edc8e70
commit 87eba39116
4 changed files with 134 additions and 55 deletions

View File

@ -98,6 +98,10 @@ region = Rect2( 1920, 0, 240, 160 )
atlas = SubResource( 90 ) atlas = SubResource( 90 )
region = Rect2( 2160, 0, 240, 160 ) region = Rect2( 2160, 0, 240, 160 )
[sub_resource type="AtlasTexture" id=163]
atlas = SubResource( 90 )
region = Rect2( 480, 160, 240, 160 )
[sub_resource type="AtlasTexture" id=112] [sub_resource type="AtlasTexture" id=112]
atlas = SubResource( 90 ) atlas = SubResource( 90 )
region = Rect2( 1920, 160, 240, 160 ) region = Rect2( 1920, 160, 240, 160 )
@ -302,10 +306,6 @@ region = Rect2( 0, 160, 240, 160 )
atlas = SubResource( 90 ) atlas = SubResource( 90 )
region = Rect2( 240, 160, 240, 160 ) region = Rect2( 240, 160, 240, 160 )
[sub_resource type="AtlasTexture" id=163]
atlas = SubResource( 90 )
region = Rect2( 480, 160, 240, 160 )
[sub_resource type="AtlasTexture" id=164] [sub_resource type="AtlasTexture" id=164]
atlas = SubResource( 90 ) atlas = SubResource( 90 )
region = Rect2( 0, 1280, 240, 160 ) region = Rect2( 0, 1280, 240, 160 )
@ -427,6 +427,11 @@ animations = [ {
"name": "idle", "name": "idle",
"speed": 10.0 "speed": 10.0
}, { }, {
"frames": [ SubResource( 163 ) ],
"loop": true,
"name": "idle_shoot",
"speed": 5.0
}, {
"frames": [ SubResource( 112 ), SubResource( 113 ), SubResource( 114 ), SubResource( 115 ), SubResource( 113 ), SubResource( 116 ), SubResource( 117 ), SubResource( 118 ) ], "frames": [ SubResource( 112 ), SubResource( 113 ), SubResource( 114 ), SubResource( 115 ), SubResource( 113 ), SubResource( 116 ), SubResource( 117 ), SubResource( 118 ) ],
"loop": false, "loop": false,
"name": "jump", "name": "jump",
@ -489,8 +494,8 @@ player_number = 1
[node name="AnimatedSprite" parent="." index="0"] [node name="AnimatedSprite" parent="." index="0"]
position = Vector2( -1, -51 ) position = Vector2( -1, -51 )
frames = SubResource( 190 ) frames = SubResource( 190 )
animation = "idle" animation = "idle_shoot"
frame = 11 frame = 0
flip_h = false flip_h = false
__meta__ = { __meta__ = {
"_aseprite_wizard_config_": { "_aseprite_wizard_config_": {
@ -521,10 +526,12 @@ attack_node = NodePath("../attack")
[node name="fall" parent="movement_state_machine" index="1"] [node name="fall" parent="movement_state_machine" index="1"]
script = ExtResource( 6 ) script = ExtResource( 6 )
animation_sequence = [ "jump" ] animation_sequence = [ "jump" ]
landing_node = NodePath("landing")
[node name="landing" type="Node" parent="movement_state_machine/fall" index="0"] [node name="landing" type="Node" parent="movement_state_machine/fall" index="0"]
script = ExtResource( 9 ) script = ExtResource( 9 )
animation_name = "jump" animation_name = "jump"
modifier_type = "Exit Animation"
starting_frame = 6 starting_frame = 6
pre_append_animation = true pre_append_animation = true
@ -553,6 +560,7 @@ draw_weapon_node = NodePath("draw_weapon")
[node name="draw_weapon" type="Node" parent="movement_state_machine/attack" index="0"] [node name="draw_weapon" type="Node" parent="movement_state_machine/attack" index="0"]
script = ExtResource( 9 ) script = ExtResource( 9 )
animation_name = "_shoot" animation_name = "_shoot"
modifier_type = "Animation Suffix"
timeout_seconds = 2.2 timeout_seconds = 2.2
animation_suffix = true animation_suffix = true

View File

@ -1,8 +1,10 @@
extends StateAnimatedActor extends StateAnimatedActor
export (NodePath) var idle_node export (NodePath) var idle_node
export (NodePath) var landing_node
onready var idle_state: State = get_node(idle_node) onready var idle_state: State = get_node(idle_node)
onready var landing_mod: StateModifier = get_node(landing_node)
func enter() -> void: func enter() -> void:
.enter() .enter()
@ -26,6 +28,7 @@ func process_physics(delta: float) -> State:
func exit() -> void: func exit() -> void:
.exit() .exit()
$"../landing".enter() push_animation_state_modifier(landing_mod)
modifier_stack_ref.push_front($"../landing") # $"../landing".enter()
# modifier_stack_ref.push_front($"../landing")
return return

View File

@ -59,38 +59,79 @@ func _ready():
func enter() -> void: func enter() -> void:
# animations.connect("animation_finished", self, "_on_AnimatedSprite_animation_finished") # animations.connect("animation_finished", self, "_on_AnimatedSprite_animation_finished")
mod_animation_sequence.clear() mod_animation_sequence.clear()
mod_animation_sequence = animation_sequence.duplicate(true) #mod_animation_sequence = animation_sequence.duplicate(true)
mod_animation_sequence.append_array(animation_sequence)
# Reset animation suffix in case there isn't one # Reset animation suffix in case there isn't one
animation_suffix = '' animation_suffix = ''
animation_index = 0
current_animation_sequence = 0
#var enter_animation = ''
var enter_frame = 0
if debug_state: if debug_state:
print(parent.name, " entering State: ", self.name) print(parent.name, " entering State: ", self.name)
move_component.current_movement_state = self.name move_component.current_movement_state = self.name
emit_signal("state_entered") emit_signal("state_entered")
#modifier_stack_ref = state_modifiers #modifier_stack_ref = state_modifiers
if modifier_stack_ref.empty() == false: # a modifier is applied
if modifier_stack_ref[-1].animation_name != '': # the first one has an animation # A new attempt at animation modifiers
if modifier_stack_ref[-1].animation_suffix: # it's a suffix type if modifier_stack_ref.empty() == false:
if mod_animation_sequence.size() > 0: # the current animation is multipart (sequence > 0) print("nope: ")
animation_index = 0 var i = modifier_stack_ref.size() - 1
current_animation_sequence = 0 ##NOTE: i apparently reverse range itteration doesn't work
# Guess I don't need this one anymore #for i in range(modifier_stack_ref.size(), 0): # work in reverse
#if animations.frames.get_animation_names().has(animation_sequence[animation_index] + modifier_stack_ref[-1].animation_name): while (i >= 0 and modifier_stack_ref[i].modifier_type != "None"):
if animations.frames.has_animation(mod_animation_sequence[animation_index] + modifier_stack_ref[-1].animation_name): 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[-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 else:
#animation_sequence.size() print("Warning!: Animation suffix that doesn't exist ",
print ("DEBUG: A prepend animation") mod_animation_sequence[animation_index] + modifier_stack_ref[i].animation_name)
#mod_animation_sequence = animation_sequence "Replace Animation":
mod_animation_sequence.push_front(modifier_stack_ref[-1].animation_name) #animations.play(modifier_stack_ref[i].animation_name)
else: #animations.frame = modifier_stack_ref[i].starting_frame
animations.play(modifier_stack_ref[-1].animation_name) mod_animation_sequence.clear()
animations.frame = modifier_stack_ref[-1].starting_frame mod_animation_sequence.push_back(modifier_stack_ref[i].animation_name)
return 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 mod_animation_sequence.size() > 0:
animation_index = 0
current_animation_sequence = 0
animations.play(mod_animation_sequence[animation_index] + animation_suffix) animations.play(mod_animation_sequence[animation_index] + animation_suffix)
return #TODO: maybe check current animatio has this frame
animations.frame = enter_frame
else:
print("Error! Resolved to empty animation sequence!?")
return
func exit() -> void: func exit() -> void:
emit_signal("state_exited") emit_signal("state_exited")
@ -108,7 +149,8 @@ func process_frame(_delta: float) -> State:
##TODO: Add a safety check here. ##TODO: Add a safety check here.
animations.play(mod_animation_sequence[animation_index] + animation_suffix) animations.play(mod_animation_sequence[animation_index] + animation_suffix)
if debug_state: if debug_state:
print("An animation sequence: ", animations.animation , animation_index) print("An animation sequence: ", mod_animation_sequence[animation_index - 1], " -> ",
animations.animation , " (", animation_index, ")")
current_animation_sequence = animation_index current_animation_sequence = animation_index
# We have no more sequence animations and the current one doesn't loop # We have no more sequence animations and the current one doesn't loop
# we do this to prevent the default idle animation from playing. # we do this to prevent the default idle animation from playing.
@ -181,4 +223,6 @@ func push_animation_state_modifier(modifier: StateModifier):
# if modifier.pre_append_animation == true and modifier_stack_ref[-1].animation_suffix == true: # if modifier.pre_append_animation == true and modifier_stack_ref[-1].animation_suffix == true:
# modifier.animation_suffix # modifier.animation_suffix
# modifier_stack_ref.push_back(modifier) # modifier_stack_ref.push_back(modifier)
else:
modifier_stack_ref.push_front(modifier)

View File

@ -24,38 +24,62 @@ func process_frame(delta: float) -> void:
# If the modifier is no longer valid, pop it from the stack. # If the modifier is no longer valid, pop it from the stack.
# We could iterate through a whole list of states but I'm not sure i want # We could iterate through a whole list of states but I'm not sure i want
# states to be that complex. # states to be that complex.
var current_anim_state :StateAnimatedActor = current_state
if state_modifiers.empty() == false: 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":
# Reset animation suffix
current_anim_state.animation_suffix = ''
var current_frame = current_anim_state.animations.frame
current_anim_state.animation_index = current_anim_state.current_animation_sequence
#current_anim_state.current_animation_sequence
#current_anim_state.animations.play()
if current_anim_state.animation_sequence.size() >= current_anim_state.current_animation_sequence:
current_anim_state.animations.play(
current_state.animation_sequence[current_anim_state.current_animation_sequence])
current_anim_state.animations.frames.frames.size() >= current_frame
current_anim_state.animations.frame = current_frame
else:
current_anim_state.animations.play(
current_state.animation_sequence[0])
if debug_state_machine:
print("Pop State Modifier: ", state_modifiers[i].name)
state_modifiers.pop_at(i)
# for i in state_modifiers: # if state_modifiers[-1].state_timeout.time_left == 0 and state_modifiers[-1].pre_append_animation == false:
# # if this modifer is a timer based one # if debug_state_machine:
# if i.state_timeout.time_left == 0 and i.timeout_seconds !=0: # print("Pop State Modifier: ", state_modifiers[-1].name)
# if debug_state_machine: # state_modifiers.pop_back()
# print("Pop State Modifier: ", i.name) # # Reset animation suffix
# current_state.animation_suffix = ''
if state_modifiers[-1].state_timeout.time_left == 0 and state_modifiers[-1].pre_append_animation == false: # var current_frame = current_state.animations.frame
if debug_state_machine: # #current_state.animations.play(current_state.animation_sequence[current_state.animation_index])
print("Pop State Modifier: ", state_modifiers[-1].name) # #TODO: Bodge to fix crash
state_modifiers.pop_back() # current_state.animation_index = 0
# Reset animation suffix # current_state.current_animation_sequence = 0
current_state.animation_suffix = '' # current_state.animations.play(current_state.animation_sequence[0])
var current_frame = current_state.animations.frame # current_state.animations.frame = current_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;
var new_state = current_state.process_frame(delta) var new_state = current_state.process_frame(delta)
if new_state: if new_state:
change_state(new_state) change_state(new_state)
func _on_AnimatedSprite_animation_finished(): func _on_AnimatedSprite_animation_finished():
if state_modifiers.empty() == false: for i in state_modifiers.size():
if state_modifiers[-1].pre_append_animation == true: if state_modifiers[i].animation_name == current_state.animations.animation:
if debug_state_machine: state_modifiers.pop_at(i)
print("Pop State Modifier: ", state_modifiers[-1].name)
state_modifiers.pop_back() # 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()
if current_state.animations.frames.get_animation_loop( if current_state.animations.frames.get_animation_loop(
current_state.animations.animation) == false: current_state.animations.animation) == false:
if current_state.animation_sequence.size() > 0: if current_state.animation_sequence.size() > 0: