diff --git a/game/characters/enemies/abstract_cultist.gd b/game/characters/enemies/abstract_cultist.gd index 104100a..4c825d6 100644 --- a/game/characters/enemies/abstract_cultist.gd +++ b/game/characters/enemies/abstract_cultist.gd @@ -32,7 +32,7 @@ const DIRECTION_RIGHT = 1 @export var initial_state : State = State.Idle -var _target_x := 0.0 +var target_x := 0.0 var _target_found := false @@ -59,40 +59,40 @@ func _physics_process(delta: float) -> void: match _state: State.ChasingLeft: _process_player_ray(left_player_distant_ray) - if position.x < _target_x: + if position.x < target_x: SoundManager.play_sfx_stream(SoundManager.sfx_stream_player_lost, global_position) _state = State.LookAround else: - _update_x_velocity(DIRECTION_LEFT, MAX_CHASE_SPEED, delta) + update_x_velocity(DIRECTION_LEFT, MAX_CHASE_SPEED, delta) _check_wall_collision_and_switch_state(DIRECTION_LEFT) _update_animation() move_and_slide() State.ChasingRight: _process_player_ray(right_player_distant_ray) - if position.x > _target_x: + if position.x > target_x: SoundManager.play_sfx_stream(SoundManager.sfx_stream_player_lost, global_position) _state = State.LookAround else: - _update_x_velocity(DIRECTION_RIGHT, MAX_CHASE_SPEED, delta) + update_x_velocity(DIRECTION_RIGHT, MAX_CHASE_SPEED, delta) _check_wall_collision_and_switch_state(DIRECTION_RIGHT) _update_animation() move_and_slide() State.WalkLeft: if _process_player_ray(left_player_distant_ray): _set_chase_state() - _update_x_velocity(DIRECTION_LEFT, MAX_WALK_SPEED, delta) + update_x_velocity(DIRECTION_LEFT, MAX_WALK_SPEED, delta) _check_wall_collision_and_switch_state(DIRECTION_LEFT) _update_animation() move_and_slide() State.WalkRight: if _process_player_ray(right_player_distant_ray): _set_chase_state() - _update_x_velocity(DIRECTION_RIGHT, MAX_WALK_SPEED, delta) + update_x_velocity(DIRECTION_RIGHT, MAX_WALK_SPEED, delta) _check_wall_collision_and_switch_state(DIRECTION_RIGHT) _update_animation() move_and_slide() State.LookAround: - _update_x_velocity(0, MAX_WALK_SPEED * 2, delta) + update_x_velocity(0, MAX_WALK_SPEED * 2, delta) if not _target_found: var close_rays : Array[RayCast2D] = [left_player_close_ray, right_player_close_ray] _target_found = _process_player_rays(close_rays) @@ -131,7 +131,7 @@ func _process_player_ray(ray: RayCast2D) -> bool: ray.force_raycast_update() var collider := ray.get_collider() if collider is Player: - _target_x = collider.position.x + target_x = collider.position.x return true return false @@ -143,7 +143,7 @@ func _process_player_rays(rays: Array[RayCast2D]) -> bool: return false -func _update_x_velocity(direction: int, max_speed: float, delta: float) -> void: +func update_x_velocity(direction: int, max_speed: float, delta: float) -> void: velocity.x = move_toward(velocity.x, direction * max_speed, ACCELERATION * delta) @@ -226,7 +226,7 @@ func _set_walking_state() -> void: func _set_chase_state() -> void: SoundManager.play_sfx_stream(SoundManager.sfx_stream_player_spoted, global_position) - if _target_x < position.x: + if target_x < position.x: _state = State.ChasingLeft else: _state = State.ChasingRight @@ -262,5 +262,5 @@ func _on_animation_looped() -> void: func _on_player_touch_area_entered(body: Node2D) -> void: if body is Player: - _target_x = body.position.x + target_x = body.position.x _set_chase_state() diff --git a/game/characters/enemies/abstract_cultist.tscn b/game/characters/enemies/abstract_cultist.tscn index dafc5d6..df110eb 100644 --- a/game/characters/enemies/abstract_cultist.tscn +++ b/game/characters/enemies/abstract_cultist.tscn @@ -10,6 +10,8 @@ [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 @@ -115,6 +117,16 @@ script = ExtResource("10_lu83c") fall_state = NodePath("../FallState") cultist = NodePath("../..") +[node name="MoveLeftState" parent="CultistStateMachine" unique_id=276543931 node_paths=PackedStringArray("another_direction_move_state", "cultist") instance=ExtResource("5_tu2pi")] +script = ExtResource("11_lh753") +another_direction_move_state = NodePath("../MoveRightState") +cultist = NodePath("../..") + +[node name="MoveRightState" parent="CultistStateMachine" unique_id=1671686910 node_paths=PackedStringArray("another_direction_move_state", "cultist") instance=ExtResource("5_tu2pi")] +script = ExtResource("12_lh753") +another_direction_move_state = NodePath("../MoveLeftState") +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"] diff --git a/game/characters/enemies/abstract_cultist.tscn1734506821.tmp b/game/characters/enemies/abstract_cultist.tscn1734506821.tmp new file mode 100644 index 0000000..3a18027 --- /dev/null +++ b/game/characters/enemies/abstract_cultist.tscn1734506821.tmp @@ -0,0 +1,128 @@ +[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"] diff --git a/game/characters/enemies/state_machine/idle_state.gd b/game/characters/enemies/state_machine/idle_state.gd index 77c42ca..bd18af5 100644 --- a/game/characters/enemies/state_machine/idle_state.gd +++ b/game/characters/enemies/state_machine/idle_state.gd @@ -6,6 +6,7 @@ extends CultistState func enter() -> void: cultist.sprite.play(CultistSprite.ANIMATION_IDLE) + cultist.velocity = Vector2.ZERO func physics_process(_delta: float) -> void: diff --git a/game/characters/enemies/state_machine/look_around_state.gd b/game/characters/enemies/state_machine/look_around_state.gd index bb04ea1..87e4ae2 100644 --- a/game/characters/enemies/state_machine/look_around_state.gd +++ b/game/characters/enemies/state_machine/look_around_state.gd @@ -6,6 +6,7 @@ extends CultistState func enter() -> void: 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) diff --git a/game/characters/enemies/state_machine/move_left_state.gd b/game/characters/enemies/state_machine/move_left_state.gd new file mode 100644 index 0000000..d1f2cf7 --- /dev/null +++ b/game/characters/enemies/state_machine/move_left_state.gd @@ -0,0 +1,7 @@ +extends CultistMoveState + + +func _ready() -> void: + _direction = DIRECTION_LEFT + _wall_ray_cast = cultist.left_wall_ray + _player_ray_cast = cultist.left_player_distant_ray diff --git a/game/characters/enemies/state_machine/move_left_state.gd.uid b/game/characters/enemies/state_machine/move_left_state.gd.uid new file mode 100644 index 0000000..10c99c1 --- /dev/null +++ b/game/characters/enemies/state_machine/move_left_state.gd.uid @@ -0,0 +1 @@ +uid://hgn4yll571hp diff --git a/game/characters/enemies/state_machine/move_right_state.gd b/game/characters/enemies/state_machine/move_right_state.gd new file mode 100644 index 0000000..5dd5225 --- /dev/null +++ b/game/characters/enemies/state_machine/move_right_state.gd @@ -0,0 +1,7 @@ +extends CultistMoveState + + +func _ready() -> void: + _direction = DIRECTION_RIGHT + _wall_ray_cast = cultist.right_wall_ray + _player_ray_cast = cultist.right_player_distant_ray diff --git a/game/characters/enemies/state_machine/move_right_state.gd.uid b/game/characters/enemies/state_machine/move_right_state.gd.uid new file mode 100644 index 0000000..23f82b0 --- /dev/null +++ b/game/characters/enemies/state_machine/move_right_state.gd.uid @@ -0,0 +1 @@ +uid://bwwhmjxhg2adp diff --git a/game/characters/enemies/state_machine/move_state.gd b/game/characters/enemies/state_machine/move_state.gd new file mode 100644 index 0000000..3d942b0 --- /dev/null +++ b/game/characters/enemies/state_machine/move_state.gd @@ -0,0 +1,51 @@ +@abstract +class_name CultistMoveState +extends CultistState + + +const MAX_WALK_SPEED = 85 + +const DIRECTION_LEFT = -1 +const DIRECTION_RIGHT = 1 + + +@export var fall_state: CultistState +@export var chase_state: CultistState +@export var another_direction_move_state: CultistState + + +var _direction := 0 +var _wall_ray_cast : RayCast2D +var _player_ray_cast : RayCast2D + + +func enter() -> void: + _set_ray_cast_enable(_player_ray_cast, true) + + +func exit() -> void: + _set_ray_cast_enable(_player_ray_cast, false) + + +func physics_process(delta: float) -> void: + if not cultist.is_on_floor(): + switch_state.emit(fall_state) + else: + var player := _get_colliding_player(_player_ray_cast) + if player != null: + cultist.target_x = player.position.x + switch_state.emit(chase_state) + elif _wall_ray_cast.is_colliding(): + switch_state.emit(another_direction_move_state) + else: + cultist.update_x_velocity(_direction, MAX_WALK_SPEED, delta) + + +func _get_colliding_player(ray_cast: RayCast2D) -> Player: + ray_cast.force_raycast_update() + + if ray_cast.is_colliding(): + var collider := ray_cast.get_collider() + if collider is Player: + return collider + return null diff --git a/game/characters/enemies/state_machine/move_state.gd.uid b/game/characters/enemies/state_machine/move_state.gd.uid new file mode 100644 index 0000000..4b257a5 --- /dev/null +++ b/game/characters/enemies/state_machine/move_state.gd.uid @@ -0,0 +1 @@ +uid://lplif1gcjbai