Done refactoring
This commit is contained in:
parent
3a136ff215
commit
2176e9d798
88 changed files with 821 additions and 880 deletions
10
scripts/objects/Bounty.gd
Normal file
10
scripts/objects/Bounty.gd
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
extends Area2D
|
||||
|
||||
@export var amount: float = 20
|
||||
|
||||
@onready var text = $Label
|
||||
|
||||
func _on_body_entered(body):
|
||||
if body is MainShip:
|
||||
body.money += amount
|
||||
queue_free()
|
||||
21
scripts/objects/Hull.gd
Normal file
21
scripts/objects/Hull.gd
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
extends Node2D
|
||||
|
||||
class_name Hull
|
||||
|
||||
@export var max_hp : int = 30
|
||||
@export var max_fuel : int = 1000
|
||||
|
||||
@onready var hp = max_hp
|
||||
@onready var fuel : float = max_fuel
|
||||
|
||||
var ammunition = {
|
||||
"n/a" : 0,
|
||||
"Laser Energy" : 100,
|
||||
"Rockets" : 10
|
||||
}
|
||||
|
||||
var max_ammunition = {
|
||||
"n/a" : 0,
|
||||
"Laser Energy" : 100,
|
||||
"Rockets" : 20
|
||||
}
|
||||
27
scripts/objects/MainShip.gd
Normal file
27
scripts/objects/MainShip.gd
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
extends CharacterBody2D
|
||||
|
||||
class_name MainShip
|
||||
|
||||
@onready var engine = $Engine
|
||||
@onready var hull = $Hull
|
||||
@onready var shield = $Shield
|
||||
@onready var pause_controller = $GUI/Interface/PauseController
|
||||
@onready var minimap = $CanvasLayer/Control/Minimap
|
||||
@onready var camera = $Camera
|
||||
|
||||
var allow_shooting = true
|
||||
var faction = "player"
|
||||
var money : float = 1000
|
||||
|
||||
func _process(_delta):
|
||||
if hull.hp < 0: destroy()
|
||||
|
||||
func changeinterfacecolor():
|
||||
$GUI/Interface.modulate = modulate
|
||||
|
||||
func destroy():
|
||||
hull.hp = hull.max_hp
|
||||
shield.capacity = shield.max_capacity
|
||||
global_position = Vector2.ZERO
|
||||
engine.speed = 0
|
||||
engine.turbo_enabled = false
|
||||
18
scripts/objects/Minimap.gd
Normal file
18
scripts/objects/Minimap.gd
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
extends Control
|
||||
|
||||
@export var marker : PackedScene
|
||||
|
||||
@onready var ship = $"../../.."
|
||||
|
||||
var markers = []
|
||||
|
||||
func _process(_delta):
|
||||
$Sprite.self_modulate = ship.modulate
|
||||
|
||||
func add_marker(target : Node, type : String):
|
||||
var marker_inst = marker.instantiate()
|
||||
markers.append(marker_inst)
|
||||
marker_inst.target = target
|
||||
marker_inst.type = type
|
||||
marker_inst.position = Vector2(96, 96)
|
||||
add_child(marker_inst)
|
||||
21
scripts/objects/MinimapMarker.gd
Normal file
21
scripts/objects/MinimapMarker.gd
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
extends Node2D
|
||||
|
||||
@onready var marker = $MarkerSprite
|
||||
@onready var ship = $"../../../.."
|
||||
|
||||
var target : Node2D
|
||||
var type = "hostile"
|
||||
var tmi = {
|
||||
"hostile": 0,
|
||||
"base": 1,
|
||||
"loot": 2
|
||||
}
|
||||
|
||||
func _ready():
|
||||
marker.frame = tmi[type]
|
||||
|
||||
func _process(_delta):
|
||||
rotation = ship.global_position.angle_to_point(target.global_position)
|
||||
var sp_scale = 1024 / clamp(ship.global_position.distance_to(target.global_position), 512, 2048)
|
||||
marker.scale = Vector2(sp_scale, sp_scale)
|
||||
modulate = target.modulate
|
||||
10
scripts/objects/Rocket.gd
Normal file
10
scripts/objects/Rocket.gd
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
extends Projectile
|
||||
|
||||
func areyouready():
|
||||
target = Node2D.new()
|
||||
if len(get_tree().current_scene.can_target) > 0:
|
||||
get_tree().current_scene.can_target[0].add_child(target)
|
||||
else:
|
||||
get_parent().add_child(target)
|
||||
target.global_position = get_global_mouse_position()
|
||||
|
||||
44
scripts/objects/Shield.gd
Normal file
44
scripts/objects/Shield.gd
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
extends Node2D
|
||||
|
||||
class_name Shield
|
||||
|
||||
@export var max_capacity : int = 8
|
||||
@export var shield_charge_rate : float = 1
|
||||
@export var recharge_timer : Timer
|
||||
@export var laser_timer : Timer
|
||||
@export var laser_charge_rate : float = 20
|
||||
@onready var ship = get_parent()
|
||||
@onready var capacity : float = max_capacity
|
||||
var can_recharge : bool = false
|
||||
var laser_recharge : bool = true
|
||||
|
||||
func _ready():
|
||||
recharge_timer.timeout.connect(recharging_timer_out)
|
||||
laser_timer.timeout.connect(laser_timer_out)
|
||||
|
||||
func deal_damage(damage : float):
|
||||
capacity -= damage
|
||||
if capacity < 0:
|
||||
ship.hull.hp += capacity
|
||||
capacity = 0
|
||||
can_recharge = false
|
||||
recharge_timer.start()
|
||||
laser_timer.start()
|
||||
|
||||
func recharging_timer_out():
|
||||
can_recharge = true
|
||||
|
||||
func _physics_process(delta):
|
||||
if can_recharge:
|
||||
capacity += shield_charge_rate * delta
|
||||
if capacity > max_capacity:
|
||||
capacity = max_capacity
|
||||
can_recharge = false
|
||||
if laser_recharge:
|
||||
ship.hull.ammunition["Laser Energy"] += laser_charge_rate * delta
|
||||
if ship.hull.ammunition["Laser Energy"] > ship.hull.max_ammunition["Laser Energy"]:
|
||||
ship.hull.ammunition["Laser Energy"] = ship.hull.max_ammunition["Laser Energy"]
|
||||
laser_recharge = false
|
||||
|
||||
func laser_timer_out():
|
||||
laser_recharge = true
|
||||
57
scripts/objects/ShipEngine.gd
Normal file
57
scripts/objects/ShipEngine.gd
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
extends Node2D
|
||||
|
||||
class_name ShipEngine
|
||||
|
||||
@export var max_speed : float = 200
|
||||
@export var max_turbo_speed : float = 300
|
||||
@export var acceleration : float = 50
|
||||
@export var fuel_consumption : float = 100
|
||||
@export var rotation_speed : int = 90
|
||||
@onready var ship = get_parent()
|
||||
@onready var hull = $"../Hull"
|
||||
var speed = 0
|
||||
var min_speed = max_speed / -4
|
||||
var turbo_enabled = false
|
||||
var alternative_movement = false
|
||||
var destination_angle : float
|
||||
var destination_difference : float
|
||||
|
||||
|
||||
func _physics_process(delta):
|
||||
var turbo_input = Input.get_action_raw_strength("turbo")
|
||||
var acceleration_input = Input.get_axis("deccelerate", "accelerate") if ship is MainShip else 1.0
|
||||
var rotation_input = Input.get_axis("rotateleft","rotateright")
|
||||
if destination_angle - ship.rotation_degrees == clamp(destination_angle - ship.rotation_degrees, -180, 180):
|
||||
destination_difference = destination_angle - ship.rotation_degrees
|
||||
else:
|
||||
destination_difference = ship.rotation_degrees - destination_angle
|
||||
if destination_difference != clamp(destination_difference, -1, 1):
|
||||
ship.rotation_degrees += sign(destination_difference) * rotation_speed * delta
|
||||
else:
|
||||
ship.rotation_degrees = destination_angle
|
||||
if alternative_movement:
|
||||
destination_angle += rotation_input * rotation_speed * delta
|
||||
if destination_angle > 180: destination_angle = -180
|
||||
if destination_angle < -180: destination_angle = 180
|
||||
if Vector2.ZERO.distance_to(global_position) >= 5800:
|
||||
destination_angle = rad_to_deg(global_position.angle_to_point(Vector2.ZERO))
|
||||
elif ship is MainShip:
|
||||
destination_angle = rad_to_deg(ship.global_position.angle_to_point(get_global_mouse_position()))
|
||||
turbo_enabled = clamp(turbo_input * hull.fuel, 0, 1) if ship is MainShip else (ship.state == "runaway" and hull.fuel > 0)
|
||||
if !turbo_enabled:
|
||||
speed = clamp(speed + acceleration_input * acceleration * delta, min_speed, max(max_speed, speed))
|
||||
if speed > max_speed:
|
||||
speed -= acceleration * delta if acceleration_input != -1 else 0
|
||||
else:
|
||||
if hull.fuel > 0:
|
||||
speed = clamp(speed + acceleration * delta, min_speed, max_turbo_speed)
|
||||
if speed > max_speed:
|
||||
hull.fuel -= fuel_consumption * delta
|
||||
if hull.fuel < 0:
|
||||
hull.fuel = 0
|
||||
|
||||
ship.velocity = Vector2.from_angle(ship.rotation) * speed
|
||||
ship.move_and_slide()
|
||||
|
||||
if Input.is_action_just_released("alternatemovement") and ship is MainShip:
|
||||
alternative_movement = !alternative_movement
|
||||
10
scripts/objects/Star.gd
Normal file
10
scripts/objects/Star.gd
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
extends AnimatedSprite2D
|
||||
|
||||
func _ready():
|
||||
randomize()
|
||||
var Size : float = randf_range(0.5, 2)
|
||||
frame = randi_range(0, 7)
|
||||
scale = Vector2(Size, Size)
|
||||
speed_scale = randf_range(0, 2)
|
||||
var Colors = [Color.LIGHT_BLUE, Color.WHITE, Color.LIGHT_GOLDENROD, Color.YELLOW, Color.ORANGE, Color.ORANGE_RED, Color.RED]
|
||||
modulate = Colors[randi_range(0, 6)]
|
||||
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))
|
||||
48
scripts/objects/projectile.gd
Normal file
48
scripts/objects/projectile.gd
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
extends Node2D
|
||||
|
||||
class_name Projectile
|
||||
|
||||
@export var speed : float = 300
|
||||
@export var rotation_speed : float = 0
|
||||
@export var damage : float = 1
|
||||
@export var collider : Area2D
|
||||
@export var lifetime : float = 10
|
||||
|
||||
var faction = "none"
|
||||
var destination_angle : float
|
||||
var target : Node2D = self
|
||||
|
||||
func _ready():
|
||||
get_tree().create_timer(lifetime).timeout.connect(queue_free)
|
||||
collider.body_entered.connect(_on_collision)
|
||||
areyouready()
|
||||
|
||||
func _physics_process(delta):
|
||||
if rotation_speed == 0: destination_angle = global_rotation_degrees
|
||||
else: destination_angle = rad_to_deg(global_position.angle_to_point(target.global_position))
|
||||
global_position += speed * delta * global_transform.x
|
||||
var destination_difference : float
|
||||
if abs(destination_angle - global_rotation_degrees) <= 180:
|
||||
destination_difference = destination_angle - global_rotation_degrees
|
||||
else:
|
||||
destination_difference = global_rotation_degrees - destination_angle
|
||||
if destination_difference != clamp(destination_difference, -1, 1):
|
||||
global_rotation_degrees += sign(destination_difference) * rotation_speed * delta
|
||||
else:
|
||||
global_rotation_degrees = destination_angle
|
||||
|
||||
func _on_collision(body):
|
||||
match body.collision_layer:
|
||||
1:
|
||||
if body.faction != faction:
|
||||
if target != self:
|
||||
target.queue_free()
|
||||
body.shield.deal_damage(damage)
|
||||
queue_free()
|
||||
2:
|
||||
if target != self:
|
||||
target.queue_free()
|
||||
queue_free()
|
||||
|
||||
func areyouready():
|
||||
pass
|
||||
45
scripts/objects/weapon.gd
Normal file
45
scripts/objects/weapon.gd
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
extends Node2D
|
||||
|
||||
class_name Weapon
|
||||
|
||||
@export var projectile : PackedScene
|
||||
@export var spread : float = 0
|
||||
@export var ammo_type : String = "n/a"
|
||||
@export var ammo_consumption : float = 0
|
||||
@export var shoot_timer : Timer
|
||||
@export var shoot_action : String = ""
|
||||
@export var spawner_points : Array[Node2D]
|
||||
|
||||
@onready var ship = $"../.."
|
||||
@onready var slot = $".."
|
||||
|
||||
var deviation : float = deg_to_rad(spread)
|
||||
|
||||
func _ready():
|
||||
randomize()
|
||||
if ship is MainShip:
|
||||
match slot.name:
|
||||
"PrimaryWeapon":
|
||||
shoot_action = "shootprimary"
|
||||
"SecondaryWeapon":
|
||||
shoot_action = "shootsecondary"
|
||||
elif ship is NPCShip:
|
||||
shoot_action = "npc"
|
||||
|
||||
func _process(_delta):
|
||||
var can_shoot = ship.hull.ammunition[ammo_type] >= ammo_consumption and shoot_timer.is_stopped() and ship.allow_shooting
|
||||
if can_shoot and (Input.get_action_strength(shoot_action) and shoot_action != "npc" or shoot_action == "npc" and ship.shooting):
|
||||
shoot()
|
||||
ship.hull.ammunition[ammo_type] -= ammo_consumption
|
||||
shoot_timer.start()
|
||||
if ammo_type == "Laser Energy":
|
||||
ship.shield.laser_timer.start()
|
||||
|
||||
func shoot():
|
||||
for spawner in spawner_points:
|
||||
var proj_inst = projectile.instantiate()
|
||||
ProjectileContainer.instance.add_child(proj_inst)
|
||||
proj_inst.global_position = spawner.global_position
|
||||
proj_inst.global_rotation = spawner.global_rotation + randf_range(-deviation/2, deviation/2)
|
||||
proj_inst.faction = ship.faction
|
||||
proj_inst.modulate = ship.modulate
|
||||
Loading…
Add table
Add a link
Reference in a new issue