Some code optimization

This commit is contained in:
2026-02-12 21:08:35 +03:00
parent 530ea16f11
commit 80d8785a71
8 changed files with 23 additions and 53 deletions
+2 -11
View File
@@ -24,6 +24,7 @@ var target_x := 0.0
@onready var state_machine : CultistStateMachine = $CultistStateMachine @onready var state_machine : CultistStateMachine = $CultistStateMachine
@onready var chase_left_state : CultistState = $CultistStateMachine/ChaseLeftState @onready var chase_left_state : CultistState = $CultistStateMachine/ChaseLeftState
@onready var chase_right_state : CultistState = $CultistStateMachine/ChaseRightState @onready var chase_right_state : CultistState = $CultistStateMachine/ChaseRightState
@onready var look_around_state : CultistState = $CultistStateMachine/LookAroundState
func _ready() -> void: func _ready() -> void:
@@ -36,23 +37,13 @@ func _ready() -> void:
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
move_and_slide()
state_machine.physics_process(delta) state_machine.physics_process(delta)
move_and_slide()
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) velocity.x = move_toward(velocity.x, direction * max_speed, ACCELERATION * delta)
#TODO Rework
func _on_player_touch_area_entered(body: Node2D) -> void:
if body is Player:
target_x = body.position.x
if body.position.x < position.x:
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: 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 ray_cast.process_mode = Node.PROCESS_MODE_INHERIT if enabled else Node.PROCESS_MODE_DISABLED
@@ -18,9 +18,6 @@
radius = 4.0 radius = 4.0
height = 28.0 height = 28.0
[sub_resource type="RectangleShape2D" id="RectangleShape2D_vecdo"]
size = Vector2(32, 28)
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_tjvyk"] [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_tjvyk"]
radius = 4.0 radius = 4.0
height = 28.0 height = 28.0
@@ -72,14 +69,6 @@ unique_name_in_owner = true
target_position = Vector2(320, 0) target_position = Vector2(320, 0)
collision_mask = 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")] [node name="KillingArea" parent="." unique_id=1891922549 instance=ExtResource("3_tjvyk")]
collision_mask = 0 collision_mask = 0
@@ -142,5 +131,3 @@ script = ExtResource("14_cqp7f")
look_around_state = NodePath("../LookAroundState") look_around_state = NodePath("../LookAroundState")
fall_state = NodePath("../FallState") fall_state = NodePath("../FallState")
cultist = NodePath("../..") cultist = NodePath("../..")
[connection signal="body_entered" from="PlayerTouchArea" to="." method="_on_player_touch_area_entered"]
+1 -4
View File
@@ -46,8 +46,5 @@ collision_mask = 11
[node name="RightPlayerDistantRay" parent="PlayerDistantCheck" parent_id_path=PackedInt32Array(1597489123) index="1" unique_id=1377654831] [node name="RightPlayerDistantRay" parent="PlayerDistantCheck" parent_id_path=PackedInt32Array(1597489123) index="1" unique_id=1377654831]
collision_mask = 11 collision_mask = 11
[node name="PlayerTouchArea" parent="." index="6" unique_id=2147163970] [node name="KillingArea" parent="." index="6" unique_id=1891922549]
collision_mask = 8
[node name="KillingArea" parent="." index="7" unique_id=1891922549]
collision_mask = 8 collision_mask = 8
+1 -4
View File
@@ -30,8 +30,5 @@ collision_mask = 171
[node name="RightPlayerDistantRay" parent="PlayerDistantCheck" parent_id_path=PackedInt32Array(1597489123) index="1" unique_id=1377654831] [node name="RightPlayerDistantRay" parent="PlayerDistantCheck" parent_id_path=PackedInt32Array(1597489123) index="1" unique_id=1377654831]
collision_mask = 171 collision_mask = 171
[node name="PlayerTouchArea" parent="." index="5" unique_id=2147163970] [node name="KillingArea" parent="." index="5" unique_id=1891922549]
collision_mask = 136
[node name="KillingArea" parent="." index="6" unique_id=1891922549]
collision_mask = 136 collision_mask = 136
@@ -13,12 +13,3 @@ func _get_colliding_player(ray_cast: RayCast2D) -> Player:
if collider is Player: if collider is Player:
return collider return collider
return null return null
func _chase_if_player_in_ray_cast(ray_cast: RayCast2D, chase_state: CultistState) -> bool:
var player := _get_colliding_player(ray_cast)
if player != null:
cultist.target_x = player.position.x
switch_state.emit(chase_state)
return true
return false
@@ -8,24 +8,31 @@ extends CultistState
@export var chase_right_state: CultistState @export var chase_right_state: CultistState
var state_by_ray_cast : Dictionary[RayCast2D, CultistState]
func enter() -> void: func enter() -> void:
cultist.sprite.animation_finished.connect(_on_animation_finished, CONNECT_ONE_SHOT) cultist.sprite.animation_finished.connect(_on_animation_finished, CONNECT_ONE_SHOT)
cultist.sprite.play(CultistSprite.LOOK_AROUND_ANIMATIONS.pick_random()) cultist.sprite.play(CultistSprite.LOOK_AROUND_ANIMATIONS.pick_random())
cultist.velocity = Vector2.ZERO cultist.velocity = Vector2.ZERO
cultist._set_ray_cast_enable(cultist.left_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) cultist._set_ray_cast_enable(cultist.right_player_close_ray, true)
state_by_ray_cast = {
cultist.left_player_close_ray: chase_left_state,
cultist.right_player_close_ray: chase_right_state,
}
func physics_process(_delta: float) -> void: func physics_process(_delta: float) -> void:
if not cultist.is_on_floor(): if not cultist.is_on_floor():
switch_state.emit(fall_state) switch_state.emit(fall_state)
return else:
for ray_cast in state_by_ray_cast:
if _chase_if_player_in_ray_cast(cultist.left_player_close_ray, chase_left_state): var player := _get_colliding_player(ray_cast)
return if player != null:
switch_state.emit(state_by_ray_cast[ray_cast])
if _chase_if_player_in_ray_cast(cultist.right_player_close_ray, chase_right_state): break
return
func exit() -> void: func exit() -> void:
@@ -11,7 +11,10 @@ func physics_process(delta: float) -> void:
if not cultist.is_on_floor(): if not cultist.is_on_floor():
switch_state.emit(fall_state) switch_state.emit(fall_state)
else: else:
if not _chase_if_player_in_ray_cast(_player_ray_cast, chase_state): var player := _get_colliding_player(_player_ray_cast)
if player != null:
switch_state.emit(chase_state)
else:
if _wall_ray_cast.is_colliding(): if _wall_ray_cast.is_colliding():
var stream := SoundManager.sfx_stream_player_lost var stream := SoundManager.sfx_stream_player_lost
SoundManager.play_sfx_stream(stream, cultist.global_position) SoundManager.play_sfx_stream(stream, cultist.global_position)
+1 -4
View File
@@ -43,8 +43,5 @@ collision_mask = 161
[node name="RightPlayerDistantRay" parent="PlayerDistantCheck" parent_id_path=PackedInt32Array(1597489123) index="1" unique_id=1377654831] [node name="RightPlayerDistantRay" parent="PlayerDistantCheck" parent_id_path=PackedInt32Array(1597489123) index="1" unique_id=1377654831]
collision_mask = 161 collision_mask = 161
[node name="PlayerTouchArea" parent="." index="6" unique_id=2147163970] [node name="KillingArea" parent="." index="6" unique_id=1891922549]
collision_mask = 128
[node name="KillingArea" parent="." index="7" unique_id=1891922549]
collision_mask = 128 collision_mask = 128