78 lines
3 KiB
GDScript
78 lines
3 KiB
GDScript
extends CharacterBody2D
|
|
|
|
class_name NPCShip
|
|
|
|
@export var DestinationTimer : Timer
|
|
@export var Fraction = "Enemy"
|
|
@export var BountyMin : int = 20
|
|
@export var BountyMax : int = 30
|
|
@export var Bounty : PackedScene
|
|
|
|
var State = "idle"
|
|
var Shooting = false
|
|
var AllowShooting = true
|
|
|
|
@onready var PlayerShip = get_tree().current_scene.get_node("MainShip")
|
|
@onready var NPCEngine = $"Engine"
|
|
@onready var Hull = $"Hull"
|
|
@onready var DebugLabel = $DebugLabel
|
|
@onready var TargetSnap = $TargetSnap
|
|
@onready var Healthbar = $Zalupa/ZalupaTwo/Health
|
|
@onready var Shield = $Shield
|
|
|
|
func _ready():
|
|
DestinationTimer.timeout.connect(switchdestination)
|
|
TargetSnap.mouse_entered.connect(get_tree().current_scene.addtargetlist.bind(self))
|
|
TargetSnap.mouse_exited.connect(get_tree().current_scene.removetargetlist.bind(self))
|
|
|
|
func _physics_process(_delta):
|
|
match State:
|
|
"idle":
|
|
idlestate()
|
|
"chase":
|
|
chasestate()
|
|
"maintaindistance":
|
|
maintaindistancestate()
|
|
"runaway":
|
|
runawaystate()
|
|
Healthbar.text = "{HP} HS + {SC} SC".format({"HP" : "%0.2f" % Hull.HP, "SC" : "%0.2f" % Shield.Capacity})
|
|
if Vector2.ZERO.distance_to(global_position) > 5800 or Hull.HP <= 0:
|
|
destroy()
|
|
|
|
func switchdestination():
|
|
NPCEngine.DestinationAngle = randi_range(0, 360)
|
|
|
|
func idlestate():
|
|
Shooting = false
|
|
if global_position.distance_to(PlayerShip.global_position) <= 512 and PlayerShip.AllowShooting: State = "chase"
|
|
if Vector2.ZERO.distance_to(global_position) > 5800: NPCEngine.DestinationAngle = rad_to_deg(global_position.angle_to(Vector2.ZERO))
|
|
|
|
func chasestate():
|
|
NPCEngine.DestinationAngle = rad_to_deg(global_position.angle_to_point(PlayerShip.global_position))
|
|
Shooting = true if NPCEngine.DestinationDifference == clamp(NPCEngine.DestinationDifference, -5, 5) else false
|
|
if global_position.distance_to(PlayerShip.global_position) > 512 or !PlayerShip.AllowShooting: State = "idle"
|
|
if Hull.HP < Hull.MaxHP * 0.2: State = "runaway"
|
|
if global_position.distance_to(PlayerShip.global_position) <= 128: State = "maintaindistance"
|
|
|
|
func maintaindistancestate():
|
|
NPCEngine.DestinationAngle += 1
|
|
Shooting = true if NPCEngine.DestinationDifference == clamp(NPCEngine.DestinationDifference, -5, 5) else false
|
|
if global_position.distance_to(PlayerShip.global_position) > 128: State = "chase"
|
|
if !PlayerShip.AllowShooting: State = "idle"
|
|
|
|
func runawaystate():
|
|
Shooting = false
|
|
NPCEngine.DestinationAngle = rad_to_deg(global_position.angle_to_point(PlayerShip.global_position)) - 180
|
|
if global_position.distance_to(PlayerShip.global_position) > 1024 or !PlayerShip.AllowShooting: State = "idle"
|
|
|
|
func destroy():
|
|
Hull.HP = Hull.MaxHP
|
|
Hull.Fuel = Hull.MaxFuel
|
|
Shield.Capacity = Shield.MaxShieldCapacity
|
|
State = "idle"
|
|
var BountyInst = Bounty.instantiate()
|
|
get_tree().current_scene.add_child(BountyInst)
|
|
BountyInst.global_position = global_position
|
|
BountyInst.Amount = randi_range(BountyMin, BountyMax)
|
|
BountyInst.Text.text = str(BountyInst.Amount) + " MU"
|
|
global_position = Vector2(randi_range(-4096, 4096), randi_range(-4096, 4096))
|