Some code optimization
This commit is contained in:
@@ -13,12 +13,3 @@ func _get_colliding_player(ray_cast: RayCast2D) -> Player:
|
||||
if collider is Player:
|
||||
return collider
|
||||
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
|
||||
|
||||
|
||||
var state_by_ray_cast : Dictionary[RayCast2D, 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
|
||||
cultist._set_ray_cast_enable(cultist.left_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:
|
||||
if not cultist.is_on_floor():
|
||||
switch_state.emit(fall_state)
|
||||
return
|
||||
|
||||
if _chase_if_player_in_ray_cast(cultist.left_player_close_ray, chase_left_state):
|
||||
return
|
||||
|
||||
if _chase_if_player_in_ray_cast(cultist.right_player_close_ray, chase_right_state):
|
||||
return
|
||||
else:
|
||||
for ray_cast in state_by_ray_cast:
|
||||
var player := _get_colliding_player(ray_cast)
|
||||
if player != null:
|
||||
switch_state.emit(state_by_ray_cast[ray_cast])
|
||||
break
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
|
||||
@@ -11,7 +11,10 @@ func physics_process(delta: float) -> void:
|
||||
if not cultist.is_on_floor():
|
||||
switch_state.emit(fall_state)
|
||||
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():
|
||||
var stream := SoundManager.sfx_stream_player_lost
|
||||
SoundManager.play_sfx_stream(stream, cultist.global_position)
|
||||
|
||||
Reference in New Issue
Block a user