From b510927667e7c64a01fecabaf2a6e6ff037829ed Mon Sep 17 00:00:00 2001 From: Nitsud Yarg Date: Fri, 14 Jun 2024 22:04:11 -0700 Subject: [PATCH] Continue adding Player based on inherited actor template. --- src/enemyC/EnemyC.tscn | 2 +- src/playerD/Player.gd | 4 ++ src/playerD/Player.tscn | 47 ++++++++++++++++++++++-- src/playerD/states/attack.gd | 57 +++++++++++++++++++++++++++++ src/playerD/states/fall.gd | 22 ++++++++--- src/playerD/states/idle.gd | 13 +++++-- src/playerD/states/jump.gd | 32 ++++++++++++++++ src/playerD/states/move.gd | 7 +++- src/state_animated_actor.gd | 12 +++++- src/state_machine_animated_actor.gd | 6 ++- src/state_modifier.gd | 5 ++- 11 files changed, 188 insertions(+), 19 deletions(-) create mode 100644 src/playerD/states/attack.gd create mode 100644 src/playerD/states/jump.gd diff --git a/src/enemyC/EnemyC.tscn b/src/enemyC/EnemyC.tscn index 0cd1f42..9a96e52 100644 --- a/src/enemyC/EnemyC.tscn +++ b/src/enemyC/EnemyC.tscn @@ -130,7 +130,7 @@ actor_type = "Enemy" position = Vector2( 11, -7 ) frames = SubResource( 20 ) animation = "idle" -frame = 2 +frame = 0 __meta__ = { "_aseprite_wizard_config_": { "layer": "", diff --git a/src/playerD/Player.gd b/src/playerD/Player.gd index 3c2af6c..9ae6dbc 100644 --- a/src/playerD/Player.gd +++ b/src/playerD/Player.gd @@ -17,3 +17,7 @@ func hit_Receiver(damage): PlayerInfo.player_health = $Health_Component.health yield( get_tree().create_timer(2 + $movement_state_machine/hurt.timeout_seconds), "timeout") $Hurtbox_Component.set_hurtbox(true) + + +func _on_attack_do_attack(): + pass # Replace with function body. diff --git a/src/playerD/Player.tscn b/src/playerD/Player.tscn index 39a0036..aa9a633 100644 --- a/src/playerD/Player.tscn +++ b/src/playerD/Player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=97 format=2] +[gd_scene load_steps=100 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] @@ -7,6 +7,9 @@ [ext_resource path="res://src/playerD/states/move.gd" type="Script" id=5] [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] [sub_resource type="StreamTexture" id=1] load_path = "res://.import/Mega.png-93dfe0790902b932f7b46f7b23c5d4e7.stex" @@ -388,7 +391,7 @@ animations = [ { "speed": 10.0 }, { "frames": [ SubResource( 59 ), SubResource( 60 ), SubResource( 61 ), SubResource( 62 ) ], -"loop": true, +"loop": false, "name": "shoot", "speed": 10.0 }, { @@ -420,7 +423,7 @@ player_number = 1 position = Vector2( -1, -51 ) frames = SubResource( 88 ) animation = "idle" -frame = 22 +frame = 0 flip_h = false __meta__ = { "_aseprite_wizard_config_": { @@ -439,9 +442,14 @@ __meta__ = { script = ExtResource( 2 ) player_number = 1 +[node name="movement_state_machine" parent="." index="3"] +debug_state_machine = true + [node name="idle" parent="movement_state_machine" index="0"] script = ExtResource( 4 ) animation_sequence = [ "idle" ] +jump_node = NodePath("../jump") +attack_node = NodePath("../attack") [node name="fall" parent="movement_state_machine" index="1"] script = ExtResource( 6 ) @@ -449,7 +457,36 @@ animation_sequence = [ "jump" ] [node name="move" parent="movement_state_machine" index="2"] script = ExtResource( 5 ) -animation_sequence = [ "run" ] +animation_sequence = [ "step", "run" ] +jump_node = NodePath("../jump") + +[node name="jump" type="Node" parent="movement_state_machine" index="3"] +script = ExtResource( 8 ) +animation_sequence = [ "jump" ] +idle_node = NodePath("../idle") +fall_node = NodePath("../fall") +jump_force = 300.0 + +[node name="attack" type="Node" parent="movement_state_machine" index="4"] +script = ExtResource( 10 ) +timeout_seconds = 2.0 +animation_sequence = [ "shoot" ] +jump_node = NodePath("../jump") +idle_node = NodePath("../idle") +fall_node = NodePath("../fall") +move_node = NodePath("../move") + +[node name="landing" type="Node" parent="movement_state_machine" index="5"] +script = ExtResource( 9 ) +animation_name = "jump" +starting_frame = 6 +timeout_seconds = 0.1 + +[node name="draw_weapon" type="Node" parent="movement_state_machine" index="6"] +script = ExtResource( 9 ) +animation_name = "_shoot" +timeout_seconds = 2.2 +animation_suffix = true [node name="Hurtbox_Component" parent="." index="4"] collision_layer = 16 @@ -462,3 +499,5 @@ shape = SubResource( 89 ) [node name="Health_Component" parent="." index="6" instance=ExtResource( 7 )] max_health = 100 + +[connection signal="do_attack" from="movement_state_machine/attack" to="." method="_on_attack_do_attack"] diff --git a/src/playerD/states/attack.gd b/src/playerD/states/attack.gd new file mode 100644 index 0000000..78f2af7 --- /dev/null +++ b/src/playerD/states/attack.gd @@ -0,0 +1,57 @@ +extends StateAnimatedActor + +export (NodePath) var jump_node +export (NodePath) var idle_node +export (NodePath) var fall_node +export (NodePath) var move_node + +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) + +var can_fire = true +signal do_attack() + +func enter() -> void: + .enter() + state_timeout.start() + can_fire = true + if modifier_stack_ref.has($"../draw_weapon") == false: + $"../draw_weapon".enter() + modifier_stack_ref.push_front($"../draw_weapon") + +func process_frame(delta: float) -> State: + if animations.frame == 2: + animations.stop() + if state_timeout.time_left == 0: + return idle_state + return null + + +func process_physics(delta: float) -> State: + + if can_fire: # trying to do this in the physics because irrecular behavior + emit_signal("do_attack") + can_fire = false + + if move_component.wants_jump() and parent.is_on_floor(): + return jump_state + + move_actor_as_desired(delta) + + if move_component.velocity.x != 0.0: + return move_state + + if !parent.is_on_floor(): + return fall_state + + + return null + +func exit() -> void: + # force timer reset + $"../draw_weapon".state_timeout.start() + + return + diff --git a/src/playerD/states/fall.gd b/src/playerD/states/fall.gd index b6750ad..63bc907 100644 --- a/src/playerD/states/fall.gd +++ b/src/playerD/states/fall.gd @@ -4,16 +4,28 @@ export (NodePath) var idle_node onready var idle_state: State = get_node(idle_node) +func enter() -> void: + .enter() + # Jump to fall frame + animations.frame = 5 + animations.stop() + func process_physics(delta: float) -> State: - #parent.velocity.y += gravity * delta - move_component.velocity.y += gravity * delta - - move_component.velocity.x = move_component.desired_movement_vector.x * move_speed - move_component.velocity = parent.move_and_slide(move_component.velocity, Vector2(0, -1)) + + # First allow horizontal movement. + move_actor_as_desired(delta) + #Flip the character before tha actual move maybe. if move_component.get_movement_direction() != 0.0: parent.transform.x.x = move_component.get_movement_direction() if parent.is_on_floor(): return idle_state + return null + +func exit() -> void: + .exit() + $"../landing".enter() + modifier_stack_ref.push_front($"../landing") + return diff --git a/src/playerD/states/idle.gd b/src/playerD/states/idle.gd index 250c59c..7f86d05 100644 --- a/src/playerD/states/idle.gd +++ b/src/playerD/states/idle.gd @@ -2,9 +2,13 @@ extends StateAnimatedActor export (NodePath) var fall_node export (NodePath) var move_node +export (NodePath) var jump_node +export (NodePath) var attack_node onready var fall_state: State = get_node(fall_node) onready var move_state: State = get_node(move_node) +onready var jump_state: State = get_node(jump_node) +onready var attack_state: State = get_node(attack_node) func enter() -> void: .enter() @@ -15,10 +19,11 @@ func enter() -> void: func process_physics(delta: float) -> State: -# parent.velocity.y += gravity * delta -# #parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1)) -# parent.velocity.x = move_component.desired_movement_vector.x * move_speed -# parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1)) + if move_component.wants_jump() and parent.is_on_floor(): + return jump_state + + if move_component.wants_shoot(): + return attack_state move_actor_as_desired(delta) diff --git a/src/playerD/states/jump.gd b/src/playerD/states/jump.gd new file mode 100644 index 0000000..1515319 --- /dev/null +++ b/src/playerD/states/jump.gd @@ -0,0 +1,32 @@ +extends StateAnimatedActor + + +export (NodePath) var idle_node +export (NodePath) var fall_node + +onready var idle_state: State = get_node(idle_node) +onready var fall_state: State = get_node(fall_node) + +export var jump_force: float = 200.0 + +func enter() -> void: + .enter() + # Apply initial jump velocity on state enter + move_component.velocity.y = -jump_force + +func process_physics(delta: float) -> State: + + if move_component.velocity.y > 0: + return fall_state + + # First allow horizontal movement. + move_actor_as_desired(delta) + + #Flip the character before tha actual move maybe. + if move_component.get_movement_direction() != 0.0: + parent.transform.x.x = move_component.get_movement_direction() + + if parent.is_on_floor(): + return idle_state + + return null diff --git a/src/playerD/states/move.gd b/src/playerD/states/move.gd index f93dab5..5497006 100644 --- a/src/playerD/states/move.gd +++ b/src/playerD/states/move.gd @@ -2,11 +2,15 @@ extends StateAnimatedActor export (NodePath) var fall_node export (NodePath) var idle_node +export (NodePath) var jump_node onready var fall_state: State = get_node(fall_node) onready var idle_state: State = get_node(idle_node) +onready var jump_state: State = get_node(jump_node) func process_physics(delta: float) -> State: + if move_component.wants_jump() and parent.is_on_floor(): + return jump_state move_actor_as_desired(delta) @@ -14,7 +18,8 @@ func process_physics(delta: float) -> State: return idle_state #Flip the character before tha actual move maybe. - parent.transform.x.x = move_component.get_movement_direction() + if move_component.get_movement_direction() != 0.0: + parent.transform.x.x = move_component.get_movement_direction() if !parent.is_on_floor(): return fall_state diff --git a/src/state_animated_actor.gd b/src/state_animated_actor.gd index 0bc1c23..1ed5da2 100644 --- a/src/state_animated_actor.gd +++ b/src/state_animated_actor.gd @@ -76,6 +76,7 @@ func enter() -> void: animation_suffix = 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 animation_sequence.size() > 0: animation_index = 0 @@ -92,13 +93,20 @@ func process_input(_event: InputEvent) -> State: return null func process_frame(_delta: float) -> State: - if animation_sequence.size() > 0 and current_animation_sequence != animation_index: + #if animation_sequence.size() > 0 and current_animation_sequence != animation_index: + if animation_sequence.size() > animation_index and current_animation_sequence != animation_index: if animation_index >= animation_sequence.size(): animation_index = 0 ##TODO: Add a safety check here. animations.play(animation_sequence[animation_index] + animation_suffix) - print("An animation sequence: ", animations.animation , animation_index) + if debug_state: + print("An animation sequence: ", animations.animation , animation_index) current_animation_sequence = animation_index + # We have no more sequence animations and the current one doesn't loop + # we do this to prevent the default idle animation from playing. + if animation_index >= animation_sequence.size() and animations.frames.get_animation_loop( + animations.animation) == false: + animations.stop() # Singal based frame call. if emitter_frame_subscriptions.has(animations.frame): diff --git a/src/state_machine_animated_actor.gd b/src/state_machine_animated_actor.gd index 98bb4d2..f7bbf79 100644 --- a/src/state_machine_animated_actor.gd +++ b/src/state_machine_animated_actor.gd @@ -32,7 +32,11 @@ func process_frame(delta: float) -> void: # 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]) + #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) if new_state: diff --git a/src/state_modifier.gd b/src/state_modifier.gd index 6faf39b..a978e32 100644 --- a/src/state_modifier.gd +++ b/src/state_modifier.gd @@ -10,8 +10,10 @@ extends Node ## ## @WIP +export var debug_state: bool = false export var animation_name: String +export var starting_frame: int = 0 export var move_speed: float = 60 export var timeout_seconds: float = 0.0 @@ -39,7 +41,8 @@ func _ready(): add_child(state_timeout) func enter() -> void: - print("Apply State Modifier: ", self.name) + if debug_state: + print("Apply State Modifier: ", self.name) state_timeout.start() #modifier_stack_ref = state_modifiers return