Done refactoring
This commit is contained in:
parent
3a136ff215
commit
2176e9d798
88 changed files with 821 additions and 880 deletions
82
scripts/objects/npcship.gd
Normal file
82
scripts/objects/npcship.gd
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
extends CharacterBody2D
|
||||
|
||||
class_name NPCShip
|
||||
|
||||
@export var destination_timer : Timer
|
||||
@export var faction = "Enemy"
|
||||
@export var bounty_min : int = 20
|
||||
@export var bounty_max : int = 30
|
||||
@export var bounty : PackedScene
|
||||
@onready var ship = get_tree().current_scene.get_node("MainShip")
|
||||
@onready var engine = $"Engine"
|
||||
@onready var hull = $"Hull"
|
||||
@onready var debug_label = $DebugLabel
|
||||
@onready var target_snap = $TargetSnap
|
||||
@onready var healthbar = $Zalupa/ZalupaTwo/Health
|
||||
@onready var shield = $Shield
|
||||
var state = "idle"
|
||||
var shooting = false
|
||||
var allow_shooting = true
|
||||
|
||||
func _ready():
|
||||
destination_timer.timeout.connect(switchdestination)
|
||||
target_snap.mouse_entered.connect(get_tree().current_scene.addtargetlist.bind(self))
|
||||
target_snap.mouse_exited.connect(get_tree().current_scene.removetargetlist.bind(self))
|
||||
|
||||
func _physics_process(_delta):
|
||||
match state:
|
||||
"idle":
|
||||
idlestate()
|
||||
"chase":
|
||||
chasestate()
|
||||
"maintaindistance":
|
||||
maintaindistancestate()
|
||||
"runaway":
|
||||
runawaystate()
|
||||
var format = {"HP" : "%0.2f" % hull.hp, "SC" : "%0.2f" % shield.capacity}
|
||||
healthbar.text = "{HP} HS + {SC} SC".format(format)
|
||||
if Vector2.ZERO.distance_to(global_position) > 5800 or hull.hp <= 0:
|
||||
destroy()
|
||||
|
||||
func switchdestination():
|
||||
engine.destination_angle = randi_range(0, 360)
|
||||
|
||||
func idlestate():
|
||||
shooting = false
|
||||
if global_position.distance_to(ship.global_position) <= 512 and ship.allow_shooting:
|
||||
state = "chase"
|
||||
|
||||
func chasestate():
|
||||
engine.destination_angle = rad_to_deg(global_position.angle_to_point(ship.global_position))
|
||||
shooting = true if abs(engine.destination_difference) <= 5 else false
|
||||
if global_position.distance_to(ship.global_position) > 512 or !ship.allow_shooting:
|
||||
state = "idle"
|
||||
if hull.hp < hull.max_hp * 0.2: state = "runaway"
|
||||
if global_position.distance_to(ship.global_position) <= 128:
|
||||
state = "maintaindistance"
|
||||
|
||||
func maintaindistancestate():
|
||||
engine.destination_angle += 1
|
||||
shooting = true if abs(engine.destination_difference) <= 5 else false
|
||||
if global_position.distance_to(ship.global_position) > 128:
|
||||
state = "chase"
|
||||
if !ship.allow_shooting:
|
||||
state = "idle"
|
||||
|
||||
func runawaystate():
|
||||
shooting = false
|
||||
engine.destination_angle = rad_to_deg(global_position.angle_to_point(ship.global_position)) - 180
|
||||
if global_position.distance_to(ship.global_position) > 1024 or !ship.allow_shooting:
|
||||
state = "idle"
|
||||
|
||||
func destroy():
|
||||
hull.hp = hull.max_hp
|
||||
hull.fuel = hull.max_fuel
|
||||
shield.capacity = shield.max_capacity
|
||||
state = "idle"
|
||||
var bounty_inst = bounty.instantiate()
|
||||
get_tree().current_scene.add_child(bounty_inst)
|
||||
bounty_inst.global_position = global_position
|
||||
bounty_inst.amount = randi_range(bounty_min, bounty_max)
|
||||
bounty_inst.text.text = str(bounty_inst.amount) + " MU"
|
||||
global_position = Vector2(randi_range(-4096, 4096), randi_range(-4096, 4096))
|
||||
Loading…
Add table
Add a link
Reference in a new issue