added attacks
This commit is contained in:
@@ -1,23 +1,20 @@
|
||||
extends CharacterBody2D
|
||||
extends Entity
|
||||
class_name Enemy
|
||||
|
||||
@onready var sprite_2d: Sprite2D = $Sprite2D
|
||||
@onready var nav_agent: NavigationAgent2D = $NavigationAgent2D
|
||||
|
||||
var color := Color.WHITE
|
||||
@export var speed = 100
|
||||
var lives: int = 1
|
||||
var vision: int = 100
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
velocity = Vector2.ZERO
|
||||
if Input.is_action_pressed("ui_down"):
|
||||
velocity += Vector2.DOWN
|
||||
if Input.is_action_pressed("ui_up"):
|
||||
velocity += Vector2.UP
|
||||
if Input.is_action_pressed("ui_left"):
|
||||
velocity += Vector2.LEFT
|
||||
if Input.is_action_pressed("ui_right"):
|
||||
velocity += Vector2.RIGHT
|
||||
velocity = velocity.normalized() * speed
|
||||
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)
|
||||
@@ -35,3 +32,19 @@ func _physics_process(delta: float) -> void:
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user