Fixed chase states
This commit is contained in:
@@ -6,4 +6,5 @@ extends AbstractCultist
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
super._ready()
|
||||
collision_switcher.material = sprite.material
|
||||
|
||||
@@ -26,8 +26,18 @@ var target_x := 0.0
|
||||
@onready var chase_right_state : CultistState = $CultistStateMachine/ChaseRightState
|
||||
|
||||
|
||||
func _physics_process(_delta: float) -> void:
|
||||
func _ready() -> void:
|
||||
_set_ray_cast_enable(left_player_close_ray, false)
|
||||
_set_ray_cast_enable(right_player_close_ray, false)
|
||||
_set_ray_cast_enable(left_player_distant_ray, false)
|
||||
_set_ray_cast_enable(right_player_distant_ray, false)
|
||||
|
||||
state_machine.init()
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
move_and_slide()
|
||||
state_machine.physics_process(delta)
|
||||
|
||||
|
||||
func update_x_velocity(direction: int, max_speed: float, delta: float) -> void:
|
||||
@@ -42,3 +52,7 @@ func _on_player_touch_area_entered(body: Node2D) -> void:
|
||||
state_machine._change_state(chase_left_state)
|
||||
else:
|
||||
state_machine._change_state(chase_right_state)
|
||||
|
||||
|
||||
func _set_ray_cast_enable(ray_cast: RayCast2D, enabled: bool) -> void:
|
||||
ray_cast.process_mode = Node.PROCESS_MODE_INHERIT if enabled else Node.PROCESS_MODE_DISABLED
|
||||
|
||||
@@ -90,7 +90,7 @@ shape = SubResource("CapsuleShape2D_tjvyk")
|
||||
|
||||
[node name="CultistStateMachine" parent="." unique_id=633169960 node_paths=PackedStringArray("cultist", "initial_state") instance=ExtResource("4_yu28k")]
|
||||
cultist = NodePath("..")
|
||||
initial_state = NodePath("IdleState")
|
||||
initial_state = NodePath("WalkLeftState")
|
||||
|
||||
[node name="IdleState" parent="CultistStateMachine" unique_id=739226464 node_paths=PackedStringArray("fall_state", "cultist") instance=ExtResource("5_tu2pi")]
|
||||
script = ExtResource("6_e5j7k")
|
||||
@@ -114,9 +114,11 @@ fall_state = NodePath("../FallState")
|
||||
look_around_state = NodePath("../LookAroundState")
|
||||
cultist = NodePath("../..")
|
||||
|
||||
[node name="LookAroundState" parent="CultistStateMachine" unique_id=2025927882 node_paths=PackedStringArray("fall_state", "chase_left_state", "chase_right_state", "cultist") instance=ExtResource("5_tu2pi")]
|
||||
[node name="LookAroundState" parent="CultistStateMachine" unique_id=2025927882 node_paths=PackedStringArray("fall_state", "walk_left_state", "walk_right_state", "chase_left_state", "chase_right_state", "cultist") instance=ExtResource("5_tu2pi")]
|
||||
script = ExtResource("10_lu83c")
|
||||
fall_state = NodePath("../FallState")
|
||||
walk_left_state = NodePath("../WalkLeftState")
|
||||
walk_right_state = NodePath("../WalkRightState")
|
||||
chase_left_state = NodePath("../ChaseLeftState")
|
||||
chase_right_state = NodePath("../ChaseRightState")
|
||||
cultist = NodePath("../..")
|
||||
@@ -147,6 +149,4 @@ look_around_state = NodePath("../LookAroundState")
|
||||
fall_state = NodePath("../FallState")
|
||||
cultist = NodePath("../..")
|
||||
|
||||
[connection signal="animation_finished" from="CultistSprite" to="." method="_on_animation_finished"]
|
||||
[connection signal="animation_looped" from="CultistSprite" to="." method="_on_animation_looped"]
|
||||
[connection signal="body_entered" from="PlayerTouchArea" to="." method="_on_player_touch_area_entered"]
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
[gd_scene format=3 uid="uid://bx0luh4vdob3q"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://but5aeh7y1s0f" path="res://game/characters/enemies/abstract_cultist.gd" id="1_2wrno"]
|
||||
[ext_resource type="PackedScene" uid="uid://dtxiercm8dsfm" path="res://game/characters/enemies/cultist_sprite.tscn" id="2_tjvyk"]
|
||||
[ext_resource type="PackedScene" uid="uid://dike8xgbqdut2" path="res://game/killing_area.tscn" id="3_tjvyk"]
|
||||
[ext_resource type="PackedScene" uid="uid://bg7k6ovbk1xgm" path="res://game/characters/enemies/state_machine/cultist_state_machine.tscn" id="4_yu28k"]
|
||||
[ext_resource type="PackedScene" uid="uid://bq1v55jv4c6qe" path="res://game/characters/enemies/state_machine/cultist_state.tscn" id="5_tu2pi"]
|
||||
[ext_resource type="Script" uid="uid://cdw73paln1rg8" path="res://game/characters/enemies/state_machine/idle_state.gd" id="6_e5j7k"]
|
||||
[ext_resource type="Script" uid="uid://bqnq6m0s4etqh" path="res://game/characters/enemies/state_machine/fall_state.gd" id="7_7tkj8"]
|
||||
[ext_resource type="Script" uid="uid://b8o0lvuy6a5q1" path="res://game/characters/enemies/state_machine/fall_left_state.gd" id="8_2opx4"]
|
||||
[ext_resource type="Script" uid="uid://cxg54u14yb6go" path="res://game/characters/enemies/state_machine/fall_right_state.gd" id="9_lu83c"]
|
||||
[ext_resource type="Script" uid="uid://bfp8gywqqkju7" path="res://game/characters/enemies/state_machine/look_around_state.gd" id="10_lu83c"]
|
||||
[ext_resource type="Script" uid="uid://hgn4yll571hp" path="res://game/characters/enemies/state_machine/move_left_state.gd" id="11_lh753"]
|
||||
[ext_resource type="Script" uid="uid://bwwhmjxhg2adp" path="res://game/characters/enemies/state_machine/move_right_state.gd" id="12_lh753"]
|
||||
|
||||
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_mocsw"]
|
||||
radius = 4.0
|
||||
height = 28.0
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_vecdo"]
|
||||
size = Vector2(32, 28)
|
||||
|
||||
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_tjvyk"]
|
||||
radius = 4.0
|
||||
height = 28.0
|
||||
|
||||
[node name="AbstractCultist" type="CharacterBody2D" unique_id=1676614852]
|
||||
collision_layer = 0
|
||||
script = ExtResource("1_2wrno")
|
||||
|
||||
[node name="CultistSprite" parent="." unique_id=1997264413 instance=ExtResource("2_tjvyk")]
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=185651284]
|
||||
position = Vector2(0, 2)
|
||||
shape = SubResource("CapsuleShape2D_mocsw")
|
||||
|
||||
[node name="WallCheck" type="Node2D" parent="." unique_id=782156094]
|
||||
position = Vector2(0, 9)
|
||||
|
||||
[node name="LeftWallRay" type="RayCast2D" parent="WallCheck" unique_id=521416854]
|
||||
unique_name_in_owner = true
|
||||
target_position = Vector2(-16, 0)
|
||||
|
||||
[node name="RightWallRay" type="RayCast2D" parent="WallCheck" unique_id=1457669135]
|
||||
unique_name_in_owner = true
|
||||
target_position = Vector2(16, 0)
|
||||
|
||||
[node name="PlayerCloseCheck" type="Node2D" parent="." unique_id=1593775088]
|
||||
position = Vector2(0, 9)
|
||||
|
||||
[node name="LeftPlayerCloseRay" type="RayCast2D" parent="PlayerCloseCheck" unique_id=947570027]
|
||||
unique_name_in_owner = true
|
||||
target_position = Vector2(-48, 0)
|
||||
collision_mask = 0
|
||||
|
||||
[node name="RightPlayerCloseRay" type="RayCast2D" parent="PlayerCloseCheck" unique_id=1559118218]
|
||||
unique_name_in_owner = true
|
||||
target_position = Vector2(48, 0)
|
||||
collision_mask = 0
|
||||
|
||||
[node name="PlayerDistantCheck" type="Node2D" parent="." unique_id=1597489123]
|
||||
position = Vector2(0, 9)
|
||||
|
||||
[node name="LeftPlayerDistantRay" type="RayCast2D" parent="PlayerDistantCheck" unique_id=1491085518]
|
||||
unique_name_in_owner = true
|
||||
target_position = Vector2(-320, 0)
|
||||
collision_mask = 0
|
||||
|
||||
[node name="RightPlayerDistantRay" type="RayCast2D" parent="PlayerDistantCheck" unique_id=1377654831]
|
||||
unique_name_in_owner = true
|
||||
target_position = Vector2(320, 0)
|
||||
collision_mask = 0
|
||||
|
||||
[node name="PlayerTouchArea" type="Area2D" parent="." unique_id=2147163970]
|
||||
collision_layer = 0
|
||||
collision_mask = 0
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="PlayerTouchArea" unique_id=262478897]
|
||||
position = Vector2(0, 2)
|
||||
shape = SubResource("RectangleShape2D_vecdo")
|
||||
|
||||
[node name="KillingArea" parent="." unique_id=1891922549 instance=ExtResource("3_tjvyk")]
|
||||
collision_mask = 0
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="KillingArea" unique_id=1710758452]
|
||||
position = Vector2(0, 2)
|
||||
shape = SubResource("CapsuleShape2D_tjvyk")
|
||||
|
||||
[node name="CultistStateMachine" parent="." unique_id=633169960 node_paths=PackedStringArray("cultist", "initial_state") instance=ExtResource("4_yu28k")]
|
||||
cultist = NodePath("..")
|
||||
initial_state = NodePath("IdleState")
|
||||
|
||||
[node name="IdleState" parent="CultistStateMachine" unique_id=739226464 node_paths=PackedStringArray("fall_state", "cultist") instance=ExtResource("5_tu2pi")]
|
||||
script = ExtResource("6_e5j7k")
|
||||
fall_state = NodePath("../FallState")
|
||||
cultist = NodePath("../..")
|
||||
|
||||
[node name="FallState" parent="CultistStateMachine" unique_id=1764777802 node_paths=PackedStringArray("look_around_state", "cultist") instance=ExtResource("5_tu2pi")]
|
||||
script = ExtResource("7_7tkj8")
|
||||
look_around_state = NodePath("../LookAroundState")
|
||||
cultist = NodePath("../..")
|
||||
|
||||
[node name="FallLeftState" parent="CultistStateMachine" unique_id=1011952297 node_paths=PackedStringArray("fall_state", "look_around_state", "cultist") instance=ExtResource("5_tu2pi")]
|
||||
script = ExtResource("8_2opx4")
|
||||
fall_state = NodePath("../FallState")
|
||||
look_around_state = NodePath("../LookAroundState")
|
||||
cultist = NodePath("../..")
|
||||
|
||||
[node name="FallRightState" parent="CultistStateMachine" unique_id=1932915544 node_paths=PackedStringArray("fall_state", "look_around_state", "cultist") instance=ExtResource("5_tu2pi")]
|
||||
script = ExtResource("9_lu83c")
|
||||
fall_state = NodePath("../FallState")
|
||||
look_around_state = NodePath("../LookAroundState")
|
||||
cultist = NodePath("../..")
|
||||
|
||||
[node name="LookAroundState" parent="CultistStateMachine" unique_id=2025927882 node_paths=PackedStringArray("fall_state", "cultist") instance=ExtResource("5_tu2pi")]
|
||||
script = ExtResource("10_lu83c")
|
||||
fall_state = NodePath("../FallState")
|
||||
cultist = NodePath("../..")
|
||||
|
||||
[node name="MoveLeftState" parent="CultistStateMachine" unique_id=276543931 instance=ExtResource("5_tu2pi")]
|
||||
script = ExtResource("11_lh753")
|
||||
|
||||
[node name="MoveRightState" parent="CultistStateMachine" unique_id=1671686910 instance=ExtResource("5_tu2pi")]
|
||||
script = ExtResource("12_lh753")
|
||||
|
||||
[connection signal="animation_finished" from="CultistSprite" to="." method="_on_animation_finished"]
|
||||
[connection signal="animation_looped" from="CultistSprite" to="." method="_on_animation_looped"]
|
||||
[connection signal="body_entered" from="PlayerTouchArea" to="." method="_on_player_touch_area_entered"]
|
||||
@@ -1,9 +1,15 @@
|
||||
extends CultistChaseState
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
super._ready()
|
||||
|
||||
func init() -> void:
|
||||
_direction = DIRECTION_LEFT
|
||||
_wall_ray_cast = cultist.left_wall_ray
|
||||
_player_ray_cast = cultist.left_player_distant_ray
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
cultist.sprite.play(CultistSprite.ANIMATION_CHASE_LEFT)
|
||||
|
||||
|
||||
func _is_target_reached() -> bool:
|
||||
return cultist.position.x <= cultist.target_x
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
extends CultistChaseState
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
super._ready()
|
||||
|
||||
func init() -> void:
|
||||
_direction = DIRECTION_RIGHT
|
||||
_wall_ray_cast = cultist.right_wall_ray
|
||||
_player_ray_cast = cultist.right_player_distant_ray
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
cultist.sprite.play(CultistSprite.ANIMATION_CHASE_RIGHT)
|
||||
|
||||
|
||||
func _is_target_reached() -> bool:
|
||||
return cultist.position.x >= cultist.target_x
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
@abstract
|
||||
class_name CultistChaseState
|
||||
extends CultistMoveState
|
||||
|
||||
@@ -13,7 +14,11 @@ func physics_process(delta: float) -> void:
|
||||
if player != null:
|
||||
cultist.target_x = player.position.x
|
||||
|
||||
if is_equal_approx(cultist.position.x, cultist.target_x):
|
||||
if _is_target_reached() or _wall_ray_cast.is_colliding():
|
||||
switch_state.emit(look_around_state)
|
||||
else:
|
||||
cultist.update_x_velocity(_direction, cultist.MAX_CHASE_SPEED, delta)
|
||||
|
||||
|
||||
@abstract
|
||||
func _is_target_reached() -> bool
|
||||
|
||||
@@ -5,16 +5,6 @@ extends AbstractState
|
||||
@export var cultist: AbstractCultist
|
||||
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
if not cultist.is_node_ready():
|
||||
await cultist.ready
|
||||
|
||||
|
||||
func _set_ray_cast_enable(ray_cast: RayCast2D, enabled: bool) -> void:
|
||||
ray_cast.process_mode = Node.PROCESS_MODE_INHERIT if enabled else Node.PROCESS_MODE_DISABLED
|
||||
|
||||
|
||||
func _get_colliding_player(ray_cast: RayCast2D) -> Player:
|
||||
ray_cast.force_raycast_update()
|
||||
|
||||
|
||||
@@ -3,11 +3,3 @@ extends StateMachine
|
||||
|
||||
|
||||
@export var cultist: AbstractCultist
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
if not cultist.is_node_ready():
|
||||
await cultist.ready
|
||||
|
||||
super._ready()
|
||||
|
||||
|
||||
@@ -2,15 +2,18 @@ extends CultistState
|
||||
|
||||
|
||||
@export var fall_state: CultistState
|
||||
@export var walk_left_state: CultistState
|
||||
@export var walk_right_state: CultistState
|
||||
@export var chase_left_state: CultistState
|
||||
@export var chase_right_state: CultistState
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
cultist.sprite.animation_finished.connect(_on_animation_finished, CONNECT_ONE_SHOT)
|
||||
cultist.sprite.play(CultistSprite.LOOK_AROUND_ANIMATIONS.pick_random())
|
||||
cultist.velocity = Vector2.ZERO
|
||||
_set_ray_cast_enable(cultist.left_player_close_ray, true)
|
||||
_set_ray_cast_enable(cultist.right_player_close_ray, true)
|
||||
cultist._set_ray_cast_enable(cultist.left_player_close_ray, true)
|
||||
cultist._set_ray_cast_enable(cultist.right_player_close_ray, true)
|
||||
|
||||
|
||||
func physics_process(_delta: float) -> void:
|
||||
@@ -26,5 +29,9 @@ func physics_process(_delta: float) -> void:
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
_set_ray_cast_enable(cultist.left_player_close_ray, false)
|
||||
_set_ray_cast_enable(cultist.right_player_close_ray, false)
|
||||
cultist._set_ray_cast_enable(cultist.left_player_close_ray, false)
|
||||
cultist._set_ray_cast_enable(cultist.right_player_close_ray, false)
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
switch_state.emit([walk_left_state, walk_right_state].pick_random())
|
||||
|
||||
@@ -18,8 +18,8 @@ var _player_ray_cast : RayCast2D
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
_set_ray_cast_enable(_player_ray_cast, true)
|
||||
cultist._set_ray_cast_enable(_player_ray_cast, true)
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
_set_ray_cast_enable(_player_ray_cast, false)
|
||||
cultist._set_ray_cast_enable(_player_ray_cast, false)
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
extends CultistWalkState
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
super._ready()
|
||||
|
||||
print(cultist.left_player_distant_ray)
|
||||
|
||||
func init() -> void:
|
||||
_direction = DIRECTION_LEFT
|
||||
_wall_ray_cast = cultist.left_wall_ray
|
||||
_player_ray_cast = cultist.left_player_distant_ray
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
cultist.sprite.play(CultistSprite.ANIMATION_WALK_LEFT)
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
extends CultistWalkState
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
super._ready()
|
||||
|
||||
func init() -> void:
|
||||
_direction = DIRECTION_RIGHT
|
||||
_wall_ray_cast = cultist.right_wall_ray
|
||||
_player_ray_cast = cultist.right_player_distant_ray
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
cultist.sprite.play(CultistSprite.ANIMATION_WALK_RIGHT)
|
||||
|
||||
@@ -27,10 +27,13 @@ var _is_switching_needed := false
|
||||
@onready var jump_buffer_timer : Timer = $JumpBufferTimer
|
||||
@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
|
||||
|
||||
state_machine.init()
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
@@ -70,6 +73,7 @@ func _physics_process(delta: float) -> void:
|
||||
_switch()
|
||||
|
||||
move_and_slide()
|
||||
state_machine.physics_process(delta)
|
||||
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
|
||||
@@ -3,10 +3,3 @@ extends StateMachine
|
||||
|
||||
|
||||
@export var player: Player
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
if not player.is_node_ready():
|
||||
await player.ready
|
||||
|
||||
super._ready()
|
||||
|
||||
@@ -6,6 +6,10 @@ extends Node
|
||||
signal switch_state(state: AbstractState)
|
||||
|
||||
|
||||
func init() -> void:
|
||||
pass
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
pass
|
||||
|
||||
|
||||
@@ -8,18 +8,18 @@ extends Node
|
||||
var current_state : AbstractState
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_connect_states()
|
||||
func init() -> void:
|
||||
_init_states()
|
||||
if initial_state:
|
||||
_change_state(initial_state)
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
func process(delta: float) -> void:
|
||||
if current_state:
|
||||
current_state.process(delta)
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
func physics_process(delta: float) -> void:
|
||||
if current_state:
|
||||
current_state.physics_process(delta)
|
||||
|
||||
@@ -34,9 +34,10 @@ func _change_state(new_state: AbstractState) -> void:
|
||||
current_state.enter()
|
||||
|
||||
|
||||
func _connect_states() -> void:
|
||||
func _init_states() -> void:
|
||||
for child in get_children():
|
||||
if not child is AbstractState: continue
|
||||
var state := child as AbstractState
|
||||
state.init()
|
||||
state.switch_state.connect(_change_state)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user