Rewriting ships: Movement system

This commit is contained in:
2ndbeam 2024-04-28 18:53:09 +03:00
commit e2b9fa6c69
10 changed files with 208 additions and 0 deletions

26
scripts/Ship/engine.gd Normal file
View file

@ -0,0 +1,26 @@
extends Node2D
class_name ShipEngine
# TODO: implement dashes
# TODO: make particles and turbo
## Engine ID to use with Game.get_module
@export var id: String = "engine"
## Acceleration speed of the engine
@export var acceleration_speed: float = 50.0
## Rotation speed of the engine
@export var rotation_speed: float = 5000.0
## Shortcut to get_parent()
@onready var ship: Ship = get_parent()
## Acceleration control variable
var acceleration_axis: float = 0.0
## Rotation control variable
var rotation_axis: float = 0.0
func _physics_process(_delta):
# apply movement and rotation
ship.hull.apply_central_force(Vector2.from_angle(ship.hull.rotation) * acceleration_speed * acceleration_axis)
ship.hull.apply_torque(rotation_speed * rotation_axis)

27
scripts/Ship/hull.gd Normal file
View file

@ -0,0 +1,27 @@
extends RigidBody2D
class_name Hull
## Shortcut to get_parent()
@onready var ship: Ship = get_parent()
## Hull ID to use with the Game.get_module func
@export var id: String = "hull"
## Max HP of the hull
@export var max_hp: float = 30.0
## Maximum amount of ammunition to be carried
@export var max_ammunition: Dictionary = {
"n/a": 0, # don't change this k thx
"Laser Energy": 100,
"Rockets": 10,
}
## Current HP of the hull. If it reaches zero, it emits parent's destroyed signal
var hp: float = max_hp:
set(new_hp):
if new_hp > 0:
hp = new_hp
else:
ship.destroyed.emit()

View file

@ -0,0 +1,8 @@
extends Node2D
## Shortcut to get_parent()
@onready var ship: Ship = get_parent()
func _physics_process(_delta):
ship.engine.acceleration_axis = Input.get_axis("deccelerate", "accelerate")
ship.engine.rotation_axis = Input.get_axis("rotateleft", "rotateright")

56
scripts/Ship/ship.gd Normal file
View file

@ -0,0 +1,56 @@
extends Node2D
class_name Ship
# TODO: add weapons
## Emits when hull hp reaches zero.
signal destroyed
## Reference to the engine module
@onready var engine: ShipEngine = $Engine
## Reference to the hull module
@onready var hull: Hull = $Hull
## Reference to the shield module
@onready var shield: Shield = $Shield
## Reference to primary weapon
#@onready var primary_weapon: Weapon = $PrimaryWeapon
## Reference to secondary weapon
#@onready var secondary_weapon: Weapon = $SecondaryWeapon
## Faction which this ship belongs to
var faction : Game.Faction = Game.Faction.Player
func _ready():
#shield.material = material
destroyed.connect(destroy)
## Reset all required variables
func destroy():
hull.hp = hull.max_hp
shield.capacity = shield.max_capacity
## Swaps old hull with the new one
func change_hull(new_hull_id: String):
var new_hull: Hull = Game.get_module(new_hull_id, 'hull').instantiate()
var old_hull: Hull = hull
add_child(new_hull)
hull = new_hull
hull.hp = old_hull.hp
get_tree().create_timer(0.05).timeout.connect(old_hull.queue_free)
## Swaps old engine with the new one
func change_engine(new_engine_id: String):
var new_engine: ShipEngine = Game.get_module(new_engine_id, 'engine').instantiate()
var old_engine: ShipEngine = engine
add_child(new_engine)
engine = new_engine
get_tree().create_timer(0.05).timeout.connect(old_engine.queue_free)
## Swaps old shield with the new one
func change_shield(new_shield_id: String):
var new_shield: Shield = Game.get_module(new_shield_id, 'shield').instantiate()
var old_shield: Shield = shield
add_child(new_shield)
shield = new_shield
get_tree().create_timer(0.05).timeout.connect(old_shield.queue_free)