67 lines
2.1 KiB
GDScript
67 lines
2.1 KiB
GDScript
class_name Enemy extends "res://scenes/spaceShip/spaceShip.gd"
|
|
|
|
@onready var visionArea: VisionArea = $VisionAreas/VisionArea
|
|
@onready var visionAreaStop: VisionArea = $VisionAreas/VisionAreaStop
|
|
@onready var visionAreaEnemy: VisionArea = $VisionAreas/VisionAreaEnemy
|
|
@onready var raycast: RayCast2D = $RayCast2D
|
|
|
|
@onready var hit = $hit
|
|
|
|
@export var range: int = 800
|
|
|
|
func _ready() -> void:
|
|
setVision()
|
|
|
|
func setVision():
|
|
visionArea.changeViewSize(range)
|
|
visionAreaStop.changeViewSize(range-50)
|
|
visionAreaEnemy.changeViewSize(200)
|
|
raycast.target_position.y = -range
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
#print(name, " velocity: ", velocity," velocityMedian: ", getVelocityMedian(), " radius: ", $VisionAreas/VisionAreaStop/CollisionShape2D.shape.radius)
|
|
#if Globals.getPlayer() != null:
|
|
#if getVelocityMedian()*8 > range:
|
|
#visionAreaStop.changeViewSize(getVelocityMedian()*8)
|
|
#global_position.distance_to(Globals.getPlayer().global_position)
|
|
|
|
if Globals.getPlayer() != null:
|
|
look_at(Globals.getPlayer().global_position)
|
|
var direction = Vector2.RIGHT.rotated(rotation)
|
|
if Globals.getPlayer() != null:
|
|
rotation += PI / 2
|
|
|
|
if visionArea.has_overlapping_bodies():
|
|
if not raycast.is_colliding():
|
|
attack()
|
|
#if getEnemyList() != []:
|
|
#for i in getEnemyList():
|
|
##print(global_position.distance_to(i.global_position))
|
|
#direction -= Vector2.UP.rotated(rad_to_deg(global_position.angle_to(i.global_position))) * -10
|
|
#break
|
|
#print(self)
|
|
#print(visionAreaEnemy.get_overlapping_bodies())
|
|
#print(getEnemyList())
|
|
if not visionAreaStop.has_overlapping_bodies():
|
|
velocity = velocity.lerp(direction * speed, acceleration * delta)
|
|
else:
|
|
velocity = velocity.lerp(Vector2.ZERO, acceleration * delta)
|
|
|
|
move_and_slide()
|
|
|
|
func _on_hurt_area_hurt(amount: int) -> void:
|
|
hit.play()
|
|
damage(amount)
|
|
|
|
func getVelocityMedian() -> float:
|
|
var _velocity = velocity
|
|
if _velocity.x < 0:
|
|
_velocity.x *= -1
|
|
if _velocity.y < 0:
|
|
_velocity.y *= -1
|
|
return (_velocity.x + _velocity.y) / 2
|
|
|
|
func getEnemyList() -> Array:
|
|
var enemies = visionAreaEnemy.get_overlapping_bodies()
|
|
enemies.erase(self)
|
|
return enemies
|