98 lines
3 KiB
GDScript
98 lines
3 KiB
GDScript
extends "res://scenes/game/entities/entity.gd"
|
|
|
|
@export_range(1,10) var damage = 1
|
|
@export_range(0.1,1) var attackCooldown = 1
|
|
|
|
@onready var navigationAgent = $NavigationAgent
|
|
@onready var targetRaycast = $Radius/RayCast2D
|
|
@onready var attackCooldownTimer = $AttackCooldown
|
|
|
|
@onready var damageArea = $DamageArea
|
|
|
|
@onready var focusedPlayer = null
|
|
|
|
func focusPlayer(player):
|
|
focusedPlayer = player
|
|
|
|
func unfocusPlayer():
|
|
focusedPlayer = null
|
|
|
|
func _ready() -> void:
|
|
navigationAgent.velocity_computed.connect(Callable(_on_navigation_agent_velocity_computed))
|
|
|
|
func set_movement_target(movement_target: Vector2):
|
|
navigationAgent.set_target_position(movement_target)
|
|
|
|
func moveTowardsPlayer():
|
|
if $Radius.get_overlapping_bodies() != [] and not targetRaycast.is_colliding():
|
|
navigationAgent.target_position = focusedPlayer.global_position
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if focusedPlayer != null:
|
|
#targetRaycast.target_position = focusedPlayer.global_position - global_position
|
|
if $Radius.get_overlapping_bodies() != [] and not targetRaycast.is_colliding():
|
|
navigationAgent.target_position = focusedPlayer.global_position
|
|
if focusedPlayer != null:
|
|
targetRaycast.target_position = focusedPlayer.global_position - global_position
|
|
else:
|
|
targetRaycast.target_position = Vector2(global_position.x-1000,global_position.y-1000)
|
|
#if $Radius.get_overlapping_bodies() != [] and not targetRaycast.is_colliding():
|
|
# navigationAgent.target_position = focusedPlayer.global_position
|
|
#if global_position.distance_squared_to(focusedPlayer.global_position) > speed *10:
|
|
|
|
#moveTowardsPlayer()
|
|
var currentAgentPosition = global_position
|
|
|
|
|
|
if NavigationServer2D.map_get_iteration_id(navigationAgent.get_navigation_map()) == 0:
|
|
return
|
|
if navigationAgent.is_navigation_finished():
|
|
return
|
|
|
|
var nextPathPosition = navigationAgent.get_next_path_position()
|
|
var newVelocity = currentAgentPosition.direction_to(nextPathPosition)
|
|
if navigationAgent.avoidance_enabled:
|
|
navigationAgent.set_velocity(newVelocity * speed)
|
|
else:
|
|
_on_navigation_agent_velocity_computed(newVelocity)
|
|
|
|
|
|
func _on_navigation_agent_velocity_computed(safe_velocity: Vector2) -> void:
|
|
|
|
velocity = safe_velocity
|
|
move_and_slide()
|
|
|
|
|
|
func _on_radius_body_entered(body: Node2D) -> void:
|
|
if body.is_in_group("Player"):
|
|
focusPlayer(body)
|
|
|
|
|
|
func _on_radius_body_exited(body: Node2D) -> void:
|
|
|
|
if body.is_in_group("Player"):
|
|
unfocusPlayer()
|
|
|
|
func attack():
|
|
damageArea.attack(damage)
|
|
|
|
|
|
func _on_damage_area_area_entered(area: Area2D) -> void:
|
|
attackCooldownTimer.start(attackCooldown)
|
|
|
|
|
|
|
|
func _on_attack_cooldown_timeout() -> void:
|
|
attack()
|
|
attackCooldownTimer.start(attackCooldown)
|
|
|
|
|
|
func _on_damage_area_area_exited(area: Area2D) -> void:
|
|
#attackCooldownTimer.time_left = attackCooldown
|
|
attackCooldownTimer.stop()
|
|
|
|
#func _on_ray_cast_2d_draw() -> void:
|
|
#if focusedPlayer != null:
|
|
##targetRaycast.target_position = focusedPlayer.global_position - global_position
|
|
#if $Radius.get_overlapping_bodies() != [] and not targetRaycast.is_colliding():
|
|
#navigationAgent.target_position = focusedPlayer.global_position
|