Skip to content

Commit

Permalink
more about melee enemy attack logic
Browse files Browse the repository at this point in the history
  • Loading branch information
guszille committed Jan 28, 2024
1 parent 5644f40 commit 13dbf74
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 85 deletions.
51 changes: 35 additions & 16 deletions Enemies/Enemy.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ func _physics_process(delta) -> void:
States.RUNNING:
if type == 'ranged':
if target:
check_end_move_height()
check_manhattan_distance_to_attack()
move_to_target_height()
elif type == 'melee':
if target:
check_manhattan_distance_to_attack(true) # Check X and Z distances.
move_to_target()

States.PATROL:
Expand Down Expand Up @@ -66,11 +67,17 @@ func set_state(new_state: States) -> void:
if patrol_direction == Vector3.ZERO:
set_state(States.IDLE)
else:
$AnimationPlayer.play('P_Aiming_Walk')
if type == 'ranged':
$AnimationPlayer.play('P_Aiming_Walk')
elif type == 'melee':
$AnimationPlayer.play('P2_Walk')
$PatrolTimer.start()

States.ATTACKING:
$AnimationPlayer.play('P_Throw_Pie')
if type == 'ranged':
$AnimationPlayer.play('P_Throw_Pie')
elif type == 'melee':
$AnimationPlayer.play('P2_Melee_Attack')

States.HURT:
$AnimationPlayer.stop()
Expand All @@ -91,11 +98,11 @@ func set_state(new_state: States) -> void:
elif type == 'melee':
$AnimationPlayer.play('P2_Slow_Diyng')

func move_to_direction(patrol_direction):
func move_to_direction(patrol_direction) -> void:
var direction = Vector3.ZERO

direction = patrol_direction.normalized()

if direction.x:
look_at(global_position + Vector3(-direction.x, 0, 0))

Expand All @@ -105,11 +112,11 @@ func move_to_direction(patrol_direction):

move_and_slide()

func move_to_target():
func move_to_target() -> void:
var direction = Vector3.ZERO

direction = (target.global_position - global_position).normalized()

if direction.x:
look_at(global_position + Vector3(-direction.x, 0, 0))

Expand All @@ -127,7 +134,7 @@ func move_to_target_height() -> void:
if direction.x:
look_at(global_position + Vector3(-direction.x, 0, 0))

velocity.x = 0 #direction.x * 2
velocity.x = 0 # direction.x * 2
velocity.z = 5 * 2
velocity.y = 0

Expand All @@ -136,10 +143,15 @@ func move_to_target_height() -> void:

move_and_slide()

func check_end_move_height() -> void:
if abs(global_position.z - target.global_position.z) <= 3:
$AnimationPlayer.stop()
set_state(States.ATTACKING)
func check_manhattan_distance_to_attack(check_x_axis = false) -> void:
if !check_x_axis:
if abs(global_position.z - target.global_position.z) <= 3:
$AnimationPlayer.stop()
set_state(States.ATTACKING)
else:
if abs(global_position.z - target.global_position.z) <= 3 and abs(global_position.x - target.global_position.x) <= 3:
$AnimationPlayer.stop()
set_state(States.ATTACKING)

func choose_patrol_direction() -> Vector3:
return Vector3(randi_range(-1, 1), 0, randi_range(-1, 1)).normalized()
Expand All @@ -148,21 +160,22 @@ func _on_patrol_timer_timeout():
if state == States.PATROL:
set_state(States.RUNNING)

# Called by the "P_Throw_Pie" animation.
func begin_attack():
$RangedAttackComponent.begin_attack(target)

func setStateHurt():
func set_state_hurt():
if state != States.BIGHURT:
set_state(States.HURT)

func setStateBigHurt():
func set_state_big_hurt():
set_state(States.BIGHURT)

func setStateDead():
func set_state_dead():
set_state(States.DEAD)

func _on_animation_player_animation_finished(anim_name):
if anim_name == 'P_Throw_Pie':
if anim_name == 'P_Throw_Pie' or anim_name == 'P2_Melee_Attack':
set_state(States.PATROL)
elif anim_name == 'P_Idle' or anim_name == 'P2_Idle':
set_state(States.RUNNING)
Expand All @@ -172,3 +185,9 @@ func _on_animation_player_animation_finished(anim_name):
set_state(States.RUNNING)
elif anim_name == 'P_Slow_Diyng' or anim_name == 'P2_Slow_Diyng':
queue_free()

func _on_area_3d_area_entered(area):
if area is HitboxComponent:
if area.get_parent().is_in_group('player'):
var hitbox:HitboxComponent = area
hitbox.take_hit(10, false)
Loading

0 comments on commit 13dbf74

Please sign in to comment.