Rewriting ships: Added weapon base and changed hull parent to Node

This commit is contained in:
2ndbeam 2024-04-29 12:08:13 +03:00
commit 6957169ba5
13 changed files with 121 additions and 37 deletions

View file

@ -2,8 +2,8 @@ extends RigidBody2D
class_name Hull
## Shortcut to get_parent()
@onready var ship: Ship = get_parent()
## Shortcut to get_parent().get_parent()
@onready var ship: Ship = get_parent().get_parent()
## Hull ID to use with the Game.get_module func
@export var id: String = "hull"
@ -36,3 +36,8 @@ func add_ammunition(which: String, value: float) -> bool:
if ammunition[which] > max_ammunition[which]:
ammunition[which] = max_ammunition[which]
return true
## Update ship's position and rotation
func _physics_process(_delta):
ship.position = position
ship.rotation = rotation

View file

@ -6,3 +6,9 @@ extends Node2D
func _physics_process(_delta) -> void:
ship.engine.acceleration_axis = Input.get_axis("deccelerate", "accelerate")
ship.engine.rotation_axis = Input.get_axis("rotateleft", "rotateright")
for weapon in ship.weapons.list:
match weapon.action_id:
"primary":
weapon.shoot_request = Input.get_action_strength("shootprimary")
"secondary":
weapon.shoot_request = Input.get_action_strength("shootsecondary")

View file

@ -10,7 +10,7 @@ signal destroyed
## Reference to the engine module
@onready var engine: ShipEngine = $Engine
## Reference to the hull module
@onready var hull: Hull = $Hull
@onready var hull: Hull = $HullHolder/Hull
## Reference to the shield module
@onready var shield: Shield = $Shield
## Reference to weapons node
@ -20,6 +20,7 @@ signal destroyed
var faction : Game.Faction = Game.Faction.Player
func _ready() -> void:
hull.global_position += global_position
destroyed.connect(destroy)
## Reset all required variables

51
scripts/Ship/weapon.gd Normal file
View file

@ -0,0 +1,51 @@
extends Node2D
class_name Weapon
## Weapon ID to be used with Game.get_module()
@export var id: String = "weapon"
## Variable to use with input controller to determine which weapon to shoot
@export var action_id: String = "null"
## Projectile scene which will be created on shoot()
@export var projectile: PackedScene
## Applies additional angle (in degrees) while shooting
@export var spread: float = 0
## Ammo type that will be consumed
@export var ammo_type: String = "n/a"
## How much ammo will be consumet each shoot() call
@export var ammo_consumption: float = 0
## Timer to make cooldown between shots
@export var shoot_timer: Timer
## Points where projectiles will spawn
@export var spawner_points: Array[Node2D]
## Shortcut to get_parent().get_parent()
@onready var ship: Ship = get_parent().get_parent()
## Spread, converted to radians
var deviation: float = deg_to_rad(spread)
## Input variable to determine will the ship shoot
var shoot_request: bool = false
## Emits when shoot() is called
signal weapon_shooted
func _ready() -> void:
randomize()
func _process(_delta) -> void:
var can_shoot = ship.hull.ammunition[ammo_type] >= ammo_consumption and shoot_timer.is_stopped()
if can_shoot and shoot_request:
shoot()
ship.hull.add_ammunition(ammo_type, -ammo_consumption)
shoot_timer.start()
if ammo_type == "Laser Energy":
ship.shield.laser_recharge_timer.start()
func shoot() -> void:
for spawner in spawner_points:
var projectile_instance = projectile.instantiate()
ProjectileContainer.instance.add_child(projectile_instance)
projectile_instance.global_position = spawner.global_position
projectile_instance.global_rotation = spawner.global_rotation + randf_range(-deviation/2, deviation/2)
projectile_instance.faction = ship.faction
projectile_instance.modulate = ship.modulate
weapon_shooted.emit()

View file

@ -3,23 +3,30 @@ extends Node2D
# TODO: implement add_weapon
## Shortcut to get_parent()
@onready var ship = get_parent()
@onready var ship: Ship = get_parent()
## List of weapons
var list: Array[Weapon] = get_children() as Array[Weapon]
@onready var list: Array[Node] = get_children() as Array[Node]
## Updates list with actual children of this node
func update_weapon_list() -> void:
list = get_children() as Array[Weapon]
list = get_children() as Array[Node]
## Removes weapon with given ID and returns true if successful
func remove_weapon(id: String) -> bool:
## Removes weapon with given ID
func remove_weapon(id: String) -> void:
for weapon in list:
if weapon.id == id:
list.erase(weapon)
weapon.free()
return true
return false
break
update_weapon_list()
## Adds weapon with given ID to list
func add_weapon(id: String) -> void:
func add_weapon(_id: String) -> void:
pass
## Returns a reference to weapon with given id if it exists, otherwise returns null
func get_weapon(id: String) -> Weapon:
for weapon in list:
if weapon.id == id:
return weapon as Weapon
return null

View file

@ -4,7 +4,7 @@ class_name Game
enum ITEM_TYPE {VALUABLE, WEAPON, HULL, SHIELD, ENGINE, AMMUNITION}
enum AMMO_TYPE {NULL, LASER_ENERGY, ROCKETS}
enum BASE_TYPE {POWER, MINING, FOOD, TRADING, MODULE}
enum Faction {Player, Peaceful, Neutral, Aggressive}
enum Faction {None, Player, Peaceful, Neutral, Aggressive}
const DEFAULT_ITEM = preload("res://items/test_item.tres")
const DEFAULT_WEAPON = preload("res://scenes/weapons/presets/SingleLaserMk1.tscn")

View file

@ -8,7 +8,7 @@ class_name Projectile
@export var collider : Area2D
@export var lifetime : float = 10
var faction = "none"
var faction: Game.Faction = Game.Faction.None
var destination_angle : float
var target : Node2D = self
@ -34,10 +34,10 @@ func _physics_process(delta):
func _on_collision(body):
match body.collision_layer:
1:
if body.faction != faction:
if body.ship.faction != faction:
if target != self:
target.queue_free()
body.shield.deal_damage(damage,global_position)
body.ship.shield.deal_damage(damage)
queue_free()
2:
if target != self:

View file

@ -1,6 +1,6 @@
extends Node2D
class_name Weapon
# class_name Weapon
@export var projectile : PackedScene
@export var spread : float = 0