51 lines
1.7 KiB
GDScript
51 lines
1.7 KiB
GDScript
extends Entity
|
|
class_name Enemy
|
|
|
|
@onready var sprite_2d: Sprite2D = $Sprite2D
|
|
@onready var nav_agent: NavigationAgent2D = $NavigationAgent2D
|
|
|
|
var vision: int = 100
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if nav_agent.is_navigation_finished():
|
|
call_deferred("new_target_point")
|
|
return
|
|
|
|
var current_agent_position: Vector2 = global_position
|
|
var next_path_position: Vector2 = nav_agent.get_next_path_position()
|
|
|
|
velocity = current_agent_position.direction_to(next_path_position) * speed
|
|
move_and_slide()
|
|
for i in range(get_slide_collision_count()):
|
|
var collision := get_slide_collision(i)
|
|
if collision.get_collider() is Chest:
|
|
var buff: Buff = collision.get_collider().open(self)
|
|
if buff:
|
|
match(buff.color):
|
|
buff.colors.RED:
|
|
lives += 1
|
|
color.r -= 0.25
|
|
buff.colors.GREEN:
|
|
#$Camera2D.zoom -= Vector2(0.4, 0.4)
|
|
color.g -= 0.25
|
|
buff.colors.BLUE:
|
|
speed += 12
|
|
color.b -= 0.25
|
|
sprite_2d.modulate = color
|
|
|
|
func new_target_point():
|
|
# Wait for the first physics frame so the NavigationServer can sync.
|
|
await get_tree().physics_frame
|
|
|
|
var target_point := Vector2.from_angle(randf_range(0.0, TAU)) * vision
|
|
var map := get_world_2d().navigation_map
|
|
var closest_point := NavigationServer2D.map_get_closest_point(map, global_position + target_point)
|
|
var point_delta := closest_point - target_point
|
|
var is_on_map = point_delta.is_zero_approx() # Answer to original question!
|
|
#if not is_on_map:
|
|
## Wasn't on the map, so push in from edge. If you have thin sections on
|
|
## your navmesh, this could push it back off the navmesh!
|
|
#point_delta = point_delta.normalized()
|
|
#closest_point += point_delta * 32
|
|
nav_agent.target_position = closest_point
|