Reworked idle, lookaround and death to the states
This commit is contained in:
@@ -42,6 +42,8 @@ var _is_switching_needed := false
|
||||
@onready var coyote_time_timer : Timer = $CoyoteTimeTimer
|
||||
@onready var center_area : Area2D = $CenterArea2D
|
||||
|
||||
@onready var state_machine : PlayerStateMachine = $PlayerStateMachine
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
collision_switcher.material = sprite.material
|
||||
@@ -106,13 +108,8 @@ func _switch() -> void:
|
||||
|
||||
|
||||
func kill() -> void:
|
||||
if not _is_alive: return
|
||||
|
||||
SoundManager.play_sfx_stream(SoundManager.sfx_stream_death, global_position)
|
||||
_is_alive = false
|
||||
get_tree().paused = true
|
||||
process_mode = Node.PROCESS_MODE_ALWAYS
|
||||
sprite.play(ANIMATION_DEATH)
|
||||
state_machine.kill_player()
|
||||
|
||||
|
||||
func add_pickup(pickup: AbstractPickup) -> void:
|
||||
@@ -140,7 +137,7 @@ func _is_killing_collider(collider: Object) -> bool:
|
||||
|
||||
func _update_animation() -> void:
|
||||
var animation := _get_animation()
|
||||
if sprite.animation != animation:
|
||||
if sprite.animation != animation and acceleration not in [ANIMATION_IDLE, ANIMATION_LOOK_AROUND_1, ANIMATION_LOOK_AROUND_2]:
|
||||
sprite.play(animation)
|
||||
|
||||
|
||||
@@ -185,20 +182,22 @@ func _rearrange_pickups() -> void:
|
||||
|
||||
|
||||
func _play_look_around_animation() -> void:
|
||||
sprite.play(ANIMATION_LOOK_AROUND_1 if randi_range(1, 2) == 1 else ANIMATION_LOOK_AROUND_2)
|
||||
pass
|
||||
#sprite.play(ANIMATION_LOOK_AROUND_1 if randi_range(1, 2) == 1 else ANIMATION_LOOK_AROUND_2)
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
match sprite.animation:
|
||||
ANIMATION_LOOK_AROUND_1, ANIMATION_LOOK_AROUND_2:
|
||||
sprite.play(ANIMATION_IDLE)
|
||||
#ANIMATION_LOOK_AROUND_1, ANIMATION_LOOK_AROUND_2:
|
||||
#sprite.play(ANIMATION_IDLE)
|
||||
ANIMATION_DEATH:
|
||||
dead.emit()
|
||||
queue_free()
|
||||
|
||||
|
||||
func _on_animation_looped() -> void:
|
||||
match sprite.animation:
|
||||
ANIMATION_IDLE:
|
||||
if randi_range(1, 100) <= LOOK_AROUND_CHANCE:
|
||||
_play_look_around_animation()
|
||||
pass
|
||||
#match sprite.animation:
|
||||
#ANIMATION_IDLE:
|
||||
#if randi_range(1, 100) <= LOOK_AROUND_CHANCE:
|
||||
#_play_look_around_animation()
|
||||
|
||||
@@ -3,9 +3,13 @@
|
||||
[ext_resource type="Script" uid="uid://bb8kc32sggrn2" path="res://game/characters/player/player.gd" id="1_xln5q"]
|
||||
[ext_resource type="PackedScene" uid="uid://5qlocc0yu8ug" path="res://game/collision_switcher.tscn" id="2_j06tb"]
|
||||
[ext_resource type="PackedScene" uid="uid://cvx6akiplg21s" path="res://game/characters/player/player_sprite.tscn" id="2_n6ad3"]
|
||||
[ext_resource type="PackedScene" uid="uid://3ll3i1fgktg2" path="res://game/state_machine/state_machine.tscn" id="4_65114"]
|
||||
[ext_resource type="PackedScene" uid="uid://bdk23k2bypj8w" path="res://game/characters/player/state_machine/player_state_machine.tscn" id="4_ybe51"]
|
||||
[ext_resource type="PackedScene" uid="uid://c0d4wfdg7xjti" path="res://game/characters/player/state_machine/player_state.tscn" id="5_65114"]
|
||||
[ext_resource type="Script" uid="uid://bnxccvt265hlu" path="res://game/characters/player/state_machine/idle_state.gd" id="6_3qf5w"]
|
||||
[ext_resource type="Script" uid="uid://nc62375vpnsw" path="res://game/characters/player/state_machine/walk_state.gd" id="7_btbyc"]
|
||||
[ext_resource type="Script" uid="uid://cbv84l1duwfep" path="res://game/characters/player/state_machine/look_around_state.gd" id="7_ybe51"]
|
||||
[ext_resource type="Script" uid="uid://cxa3hf7tqpeoa" path="res://game/characters/player/state_machine/jump_state.gd" id="8_ebhjm"]
|
||||
[ext_resource type="Script" uid="uid://d2uiegcx4jeuk" path="res://game/characters/player/state_machine/dead_state.gd" id="9_gdxv1"]
|
||||
|
||||
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_xln5q"]
|
||||
radius = 4.0
|
||||
@@ -51,10 +55,31 @@ collision_mask = 35
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="CenterArea2D" unique_id=1350886064]
|
||||
shape = SubResource("CapsuleShape2D_t6gq4")
|
||||
|
||||
[node name="StateMachine" parent="." unique_id=633169960 instance=ExtResource("4_65114")]
|
||||
[node name="PlayerStateMachine" parent="." unique_id=633169960 node_paths=PackedStringArray("player", "dead_state", "initial_state") instance=ExtResource("4_ybe51")]
|
||||
player = NodePath("..")
|
||||
dead_state = NodePath("DeadState")
|
||||
initial_state = NodePath("IdleState")
|
||||
|
||||
[node name="IdleState" parent="StateMachine" unique_id=739226464 node_paths=PackedStringArray("player") instance=ExtResource("5_65114")]
|
||||
[node name="IdleState" parent="PlayerStateMachine" unique_id=739226464 node_paths=PackedStringArray("look_around_state", "player") instance=ExtResource("5_65114")]
|
||||
script = ExtResource("6_3qf5w")
|
||||
look_around_state = NodePath("../LookAroundState")
|
||||
player = NodePath("../..")
|
||||
|
||||
[node name="LookAroundState" parent="PlayerStateMachine" unique_id=946166563 node_paths=PackedStringArray("idle_state", "player") instance=ExtResource("5_65114")]
|
||||
script = ExtResource("7_ybe51")
|
||||
idle_state = NodePath("../IdleState")
|
||||
player = NodePath("../..")
|
||||
|
||||
[node name="WalkState" parent="PlayerStateMachine" unique_id=195513070 node_paths=PackedStringArray("player") instance=ExtResource("5_65114")]
|
||||
script = ExtResource("7_btbyc")
|
||||
player = NodePath("../..")
|
||||
|
||||
[node name="JumpState" parent="PlayerStateMachine" unique_id=1433721843 node_paths=PackedStringArray("player") instance=ExtResource("5_65114")]
|
||||
script = ExtResource("8_ebhjm")
|
||||
player = NodePath("../..")
|
||||
|
||||
[node name="DeadState" parent="PlayerStateMachine" unique_id=2135733235 node_paths=PackedStringArray("player") instance=ExtResource("5_65114")]
|
||||
script = ExtResource("9_gdxv1")
|
||||
player = NodePath("../..")
|
||||
|
||||
[connection signal="animation_finished" from="AnimatedSprite2D" to="." method="_on_animation_finished"]
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
extends PlayerState
|
||||
|
||||
|
||||
const ANIMATION_DEATH = "death"
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
SoundManager.play_sfx_stream(SoundManager.sfx_stream_death, player.global_position)
|
||||
player.sprite.animation_finished.connect(_on_aniimation_finished, CONNECT_ONE_SHOT)
|
||||
player.process_mode = Node.PROCESS_MODE_ALWAYS
|
||||
process_mode = Node.PROCESS_MODE_ALWAYS
|
||||
player.sprite.play(ANIMATION_DEATH)
|
||||
get_tree().paused = true
|
||||
|
||||
|
||||
func _on_aniimation_finished() -> void:
|
||||
player.dead.emit()
|
||||
player.queue_free()
|
||||
@@ -0,0 +1 @@
|
||||
uid://d2uiegcx4jeuk
|
||||
@@ -1 +1,23 @@
|
||||
extends PlayerState
|
||||
|
||||
|
||||
const ANIMATION_IDLE = "idle"
|
||||
|
||||
const LOOK_AROUND_CHANCE = 25
|
||||
|
||||
|
||||
@export var look_around_state: PlayerState
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
player.sprite.animation_looped.connect(_on_animation_looped)
|
||||
player.sprite.play(ANIMATION_IDLE)
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
player.sprite.animation_looped.disconnect(_on_animation_looped)
|
||||
|
||||
|
||||
func _on_animation_looped() -> void:
|
||||
if randi_range(1, 100) <= LOOK_AROUND_CHANCE:
|
||||
switch_state.emit(look_around_state)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
extends PlayerState
|
||||
@@ -0,0 +1 @@
|
||||
uid://cxa3hf7tqpeoa
|
||||
@@ -0,0 +1,21 @@
|
||||
extends PlayerState
|
||||
|
||||
|
||||
const ANIMATION_LOOK_AROUND_1 = "look_around_1"
|
||||
const ANIMATION_LOOK_AROUND_2 = "look_around_2"
|
||||
|
||||
|
||||
@export var idle_state: PlayerState
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
player.sprite.animation_finished.connect(_on_animation_finished, CONNECT_ONE_SHOT)
|
||||
player.sprite.play(_get_animation())
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
switch_state.emit(idle_state)
|
||||
|
||||
|
||||
func _get_animation() -> String:
|
||||
return ANIMATION_LOOK_AROUND_1 if randi_range(1, 2) == 1 else ANIMATION_LOOK_AROUND_2
|
||||
@@ -0,0 +1 @@
|
||||
uid://cbv84l1duwfep
|
||||
@@ -0,0 +1,18 @@
|
||||
class_name PlayerStateMachine
|
||||
extends StateMachine
|
||||
|
||||
|
||||
@export var player: Player
|
||||
|
||||
@export var dead_state: PlayerState
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
if not player.is_node_ready():
|
||||
await player.ready
|
||||
|
||||
super._ready()
|
||||
|
||||
|
||||
func kill_player() -> void:
|
||||
_change_state(dead_state)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dk3t8s0lt2r8n
|
||||
@@ -0,0 +1,7 @@
|
||||
[gd_scene format=3 uid="uid://bdk23k2bypj8w"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://3ll3i1fgktg2" path="res://game/state_machine/state_machine.tscn" id="1_7tog0"]
|
||||
[ext_resource type="Script" uid="uid://dk3t8s0lt2r8n" path="res://game/characters/player/state_machine/player_state_machine.gd" id="2_ydgfo"]
|
||||
|
||||
[node name="PlayerStateMachine" unique_id=633169960 instance=ExtResource("1_7tog0")]
|
||||
script = ExtResource("2_ydgfo")
|
||||
@@ -0,0 +1 @@
|
||||
extends PlayerState
|
||||
@@ -0,0 +1 @@
|
||||
uid://nc62375vpnsw
|
||||
@@ -3,7 +3,7 @@ extends Node
|
||||
|
||||
|
||||
@warning_ignore("unused_signal")
|
||||
signal new_state(state: AbstractState)
|
||||
signal switch_state(state: AbstractState)
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
|
||||
@@ -2,9 +2,18 @@ class_name StateMachine
|
||||
extends Node
|
||||
|
||||
|
||||
@export var initial_state: AbstractState
|
||||
|
||||
|
||||
var current_state : AbstractState
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_connect_states()
|
||||
if initial_state:
|
||||
_change_state(initial_state)
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if current_state:
|
||||
current_state.process(delta)
|
||||
@@ -15,8 +24,16 @@ func _physics_process(delta: float) -> void:
|
||||
current_state.physics_process(delta)
|
||||
|
||||
|
||||
func change_state(new_state: AbstractState) -> void:
|
||||
func _change_state(new_state: AbstractState) -> void:
|
||||
if current_state:
|
||||
current_state.exit()
|
||||
current_state = new_state
|
||||
current_state.enter()
|
||||
|
||||
|
||||
func _connect_states() -> void:
|
||||
for child in get_children():
|
||||
if not child is AbstractState: continue
|
||||
var state := child as AbstractState
|
||||
state.switch_state.connect(_change_state)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user