Rewriting ships: Added shields and weapons holder

This commit is contained in:
2ndbeam 2024-04-28 20:53:44 +03:00
commit 95274d0a5b
17 changed files with 396 additions and 24 deletions

View file

@ -0,0 +1,16 @@
[gd_scene load_steps=2 format=3 uid="uid://bunboi5ouscw8"]
[ext_resource type="Script" path="res://scripts/Ship/shield.gd" id="1_sexol"]
[node name="Shield" type="Node2D" node_paths=PackedStringArray("shield_recharge_timer", "laser_recharge_timer")]
script = ExtResource("1_sexol")
shield_recharge_timer = NodePath("")
laser_recharge_timer = NodePath("")
[node name="ShieldTimer" type="Timer" parent="."]
wait_time = 5.0
one_shot = true
[node name="LaserTimer" type="Timer" parent="."]
wait_time = 5.0
one_shot = true

View file

@ -17,5 +17,5 @@ shader_parameter/bublic_size = 20.0
[node name="PlayerShip" instance=ExtResource("1_6x7bu")]
material = SubResource("ShaderMaterial_u1mh3")
[node name="InputController" type="Node2D" parent="." index="2"]
[node name="InputController" type="Node2D" parent="." index="3"]
script = ExtResource("3_0e84a")

View file

@ -1,14 +1,19 @@
[gd_scene load_steps=5 format=3 uid="uid://bjkshql8ut6hk"]
[gd_scene load_steps=6 format=3 uid="uid://bjkshql8ut6hk"]
[ext_resource type="Material" uid="uid://cw0827vwv7pc1" path="res://shaders/materials/ship_material.tres" id="1_ibfis"]
[ext_resource type="Script" path="res://scripts/Ship/ship.gd" id="2_n14ml"]
[ext_resource type="PackedScene" uid="uid://bsu4eqwdfewwi" path="res://scenes/Ships/Modules/Hulls/hull.tscn" id="3_upgeg"]
[ext_resource type="PackedScene" uid="uid://mw4kwxoeqch3" path="res://scenes/Ships/Modules/Engines/engine.tscn" id="4_adr14"]
[ext_resource type="PackedScene" uid="uid://bunboi5ouscw8" path="res://scenes/Ships/Modules/Shields/shield.tscn" id="4_ntnqw"]
[ext_resource type="Script" path="res://scripts/Ship/weapons.gd" id="5_ooyny"]
[node name="Ship" type="Node2D"]
material = ExtResource("1_ibfis")
script = ExtResource("2_n14ml")
[node name="Hull" parent="." instance=ExtResource("3_upgeg")]
[node name="Engine" parent="." instance=ExtResource("4_adr14")]
[node name="Shield" parent="." instance=ExtResource("4_ntnqw")]
[node name="Weapons" type="Node2D" parent="."]
script = ExtResource("5_ooyny")

122
scenes/deb338.tmp Normal file
View file

@ -0,0 +1,122 @@
[gd_scene load_steps=5 format=3 uid="uid://ckdttdkoo1t0s"]
[ext_resource type="PackedScene" uid="uid://dok3i8u5t1ka4" path="res://scenes/Ships/player_ship.tscn" id="1_b1l1c"]
[ext_resource type="Shader" path="res://shaders/ship.gdshader" id="2_15op6"]
[sub_resource type="GDScript" id="GDScript_6w8qb"]
resource_name = "gogo"
script/source = "extends Node2D
@onready var items = $Items
@onready var verified = $\"Verified Items\"
# Called when the node enters the scene tree for the first time.
func _ready():
var dir = DirAccess.open(\"res://items\")
var files = dir.get_files()
var text = \"Items:\\n\"
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Hulls:\\n\"
dir = DirAccess.open(\"res://scenes/hulls\")
files = dir.get_files()
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Engines:\\n\"
dir = DirAccess.open(\"res://scenes/engines\")
files = dir.get_files()
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Shields:\\n\"
dir = DirAccess.open(\"res://scenes/shields\")
files = dir.get_files()
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Weapons:\\n\"
dir = DirAccess.open(\"res://scenes/weapons/presets\")
files = dir.get_files()
for file in files:
text += \"{file}\\n\".format({'file': file})
items.text = text
fetch_modules()
func fetch_modules():
var dir = DirAccess.open(\"res://items\")
var files = dir.get_files()
var text = \"Items:\\n\"
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Verified Hulls:\\n\"
dir = DirAccess.open(\"res://scenes/hulls\")
var modules = dir.get_files()
files = check_for_item(modules)
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Verified Engines:\\n\"
dir = DirAccess.open(\"res://scenes/engines\")
modules = dir.get_files()
files = check_for_item(modules)
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Verified Shields:\\n\"
dir = DirAccess.open(\"res://scenes/shields\")
modules = dir.get_files()
files = check_for_item(modules)
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Verified Weapons:\\n\"
dir = DirAccess.open(\"res://scenes/weapons/presets\")
modules = dir.get_files()
files = check_for_item(modules)
for file in files:
text += \"{file}\\n\".format({'file': file})
verified.text = text
func check_for_item(modules : Array[String]) -> Array[String]:
var returnable : Array[String] = []
for module in modules:
var itm = module.split('.', true, 1)[0]
if Game.get_item(itm).name != Game.DEFAULT_ITEM.name:
returnable.append(itm)
return returnable
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if Input.is_action_just_released(\"pause\"):
get_tree().change_scene_to_file(\"res://scenes/MainMenu.tscn\")
"
[sub_resource type="ShaderMaterial" id="ShaderMaterial_634ti"]
resource_local_to_scene = true
resource_name = "Shield Material"
shader = ExtResource("2_15op6")
shader_parameter/wave_centers = PackedVector2Array()
shader_parameter/wave_distances = PackedFloat32Array(-1)
shader_parameter/color = Color(1, 0, 0, 1)
shader_parameter/max_distance = 20.0
shader_parameter/bublic_size = 20.0
[node name="Debug" type="Node2D"]
script = SubResource("GDScript_6w8qb")
[node name="Items" type="Label" parent="."]
offset_right = 640.0
offset_bottom = 720.0
horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 2
[node name="Verified Items" type="Label" parent="."]
offset_left = 640.0
offset_right = 1280.0
offset_bottom = 720.0
horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 2
[node name="PlayerShip" parent="." instance=ExtResource("1_b1l1c")]
material = SubResource("ShaderMaterial_634ti")
position = Vector2(608, 352)
scale = Vector2(0.5, 0.5)

122
scenes/deb435B.tmp Normal file
View file

@ -0,0 +1,122 @@
[gd_scene load_steps=5 format=3 uid="uid://ckdttdkoo1t0s"]
[ext_resource type="PackedScene" uid="uid://dok3i8u5t1ka4" path="res://scenes/Ships/player_ship.tscn" id="1_b1l1c"]
[ext_resource type="Shader" path="res://shaders/ship.gdshader" id="2_15op6"]
[sub_resource type="GDScript" id="GDScript_6w8qb"]
resource_name = "gogo"
script/source = "extends Node2D
@onready var items = $Items
@onready var verified = $\"Verified Items\"
# Called when the node enters the scene tree for the first time.
func _ready():
var dir = DirAccess.open(\"res://items\")
var files = dir.get_files()
var text = \"Items:\\n\"
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Hulls:\\n\"
dir = DirAccess.open(\"res://scenes/hulls\")
files = dir.get_files()
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Engines:\\n\"
dir = DirAccess.open(\"res://scenes/engines\")
files = dir.get_files()
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Shields:\\n\"
dir = DirAccess.open(\"res://scenes/shields\")
files = dir.get_files()
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Weapons:\\n\"
dir = DirAccess.open(\"res://scenes/weapons/presets\")
files = dir.get_files()
for file in files:
text += \"{file}\\n\".format({'file': file})
items.text = text
fetch_modules()
func fetch_modules():
var dir = DirAccess.open(\"res://items\")
var files = dir.get_files()
var text = \"Items:\\n\"
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Verified Hulls:\\n\"
dir = DirAccess.open(\"res://scenes/hulls\")
var modules = dir.get_files()
files = check_for_item(modules)
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Verified Engines:\\n\"
dir = DirAccess.open(\"res://scenes/engines\")
modules = dir.get_files()
files = check_for_item(modules)
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Verified Shields:\\n\"
dir = DirAccess.open(\"res://scenes/shields\")
modules = dir.get_files()
files = check_for_item(modules)
for file in files:
text += \"{file}\\n\".format({'file': file})
text += \"Verified Weapons:\\n\"
dir = DirAccess.open(\"res://scenes/weapons/presets\")
modules = dir.get_files()
files = check_for_item(modules)
for file in files:
text += \"{file}\\n\".format({'file': file})
verified.text = text
func check_for_item(modules : Array[String]) -> Array[String]:
var returnable : Array[String] = []
for module in modules:
var itm = module.split('.', true, 1)[0]
if Game.get_item(itm).name != Game.DEFAULT_ITEM.name:
returnable.append(itm)
return returnable
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if Input.is_action_just_released(\"pause\"):
get_tree().change_scene_to_file(\"res://scenes/MainMenu.tscn\")
"
[sub_resource type="ShaderMaterial" id="ShaderMaterial_634ti"]
resource_local_to_scene = true
resource_name = "Shield Material"
shader = ExtResource("2_15op6")
shader_parameter/wave_centers = PackedVector2Array()
shader_parameter/wave_distances = PackedFloat32Array(-1)
shader_parameter/color = Color(1, 0, 0, 1)
shader_parameter/max_distance = 20.0
shader_parameter/bublic_size = 20.0
[node name="Debug" type="Node2D"]
script = SubResource("GDScript_6w8qb")
[node name="Items" type="Label" parent="."]
offset_right = 640.0
offset_bottom = 720.0
horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 2
[node name="Verified Items" type="Label" parent="."]
offset_left = 640.0
offset_right = 1280.0
offset_bottom = 720.0
horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 2
[node name="PlayerShip" parent="." instance=ExtResource("1_b1l1c")]
material = SubResource("ShaderMaterial_634ti")
position = Vector2(608, 352)
scale = Vector2(0.5, 0.5)

View file

@ -1,4 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://ckdttdkoo1t0s"]
[gd_scene load_steps=5 format=3 uid="uid://ckdttdkoo1t0s"]
[ext_resource type="PackedScene" uid="uid://dok3i8u5t1ka4" path="res://scenes/Ships/player_ship.tscn" id="1_b1l1c"]
[ext_resource type="Shader" path="res://shaders/ship.gdshader" id="2_15op6"]
[sub_resource type="GDScript" id="GDScript_6w8qb"]
resource_name = "gogo"
@ -85,6 +88,16 @@ func _process(delta):
get_tree().change_scene_to_file(\"res://scenes/MainMenu.tscn\")
"
[sub_resource type="ShaderMaterial" id="ShaderMaterial_634ti"]
resource_local_to_scene = true
resource_name = "Shield Material"
shader = ExtResource("2_15op6")
shader_parameter/wave_centers = PackedVector2Array()
shader_parameter/wave_distances = PackedFloat32Array(-1)
shader_parameter/color = Color(1, 0, 0, 1)
shader_parameter/max_distance = 20.0
shader_parameter/bublic_size = 20.0
[node name="Debug" type="Node2D"]
script = SubResource("GDScript_6w8qb")
@ -102,3 +115,8 @@ offset_bottom = 720.0
horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 2
[node name="PlayerShip" parent="." instance=ExtResource("1_b1l1c")]
material = SubResource("ShaderMaterial_634ti")
position = Vector2(608, 352)
scale = Vector2(0.5, 0.5)

View file

@ -20,7 +20,7 @@ var acceleration_axis: float = 0.0
## Rotation control variable
var rotation_axis: float = 0.0
func _physics_process(_delta):
func _physics_process(_delta) -> void:
# 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)

View file

@ -14,14 +14,25 @@ class_name Hull
## Maximum amount of ammunition to be carried
@export var max_ammunition: Dictionary = {
"n/a": 0, # don't change this k thx
"Laser Energy": 100,
"Laser Energy": 100.0,
"Rockets": 10,
}
## Current ammunition. Change this with set_ammunition
@onready var ammunition: Dictionary = max_ammunition
## Current HP of the hull. If it reaches zero, it emits parent's destroyed signal
var hp: float = max_hp:
@onready var hp: float = max_hp:
set(new_hp):
if new_hp > 0:
hp = new_hp
else:
ship.destroyed.emit()
## Adds amount to ammunition, returns true if it was successful
func add_ammunition(which: String, value: float) -> bool:
if ammunition[which] + value < 0:
return false
ammunition[which] += value;
if ammunition[which] > max_ammunition[which]:
ammunition[which] = max_ammunition[which]
return true

View file

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

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

@ -0,0 +1,56 @@
extends Node2D
class_name Shield
## Shield ID to be used with Game.get_module()
@export var id: String = "shield"
## Maximum damage shield can take before running out of energy
@export var max_capacity: int = 8
## Shield is charged by this amount every second
@export var shield_charge_rate: float = 1.0
## One-shot timer which holds shield recharge cooldown
@export var shield_recharge_timer: Timer
## Laser energy is charged by this amount every second
@export var laser_charge_rate: float = 20.0
## One-shot timer which hold laser recharge cooldown
@export var laser_recharge_timer: Timer
## Shortcut to get_parent()
@onready var ship = get_parent()
## Current shield capacity
@onready var capacity: float = max_capacity:
set(new_capacity):
capacity = new_capacity
if capacity < 0:
ship.hull.hp += capacity
capacity = 0
if capacity > max_capacity:
capacity = max_capacity
## Indicates if shield will charge
var can_recharge_shield: bool = false
## Indicates if laser will charge
var can_recharge_laser: bool = true
func _ready() -> void:
shield_recharge_timer.timeout.connect(shield_timer_out)
laser_recharge_timer.timeout.connect(laser_timer_out)
## This function deals damage to the shield
func deal_damage(damage: float) -> void:
capacity -= damage
can_recharge_shield = false
shield_recharge_timer.start()
laser_recharge_timer.start()
## Allows shield to charge
func shield_timer_out() -> void:
can_recharge_shield = true
## Allows laser to charge
func laser_timer_out() -> void:
can_recharge_laser = true
func _physics_process(delta) -> void:
if can_recharge_shield:
capacity += shield_charge_rate * delta
if can_recharge_laser:
ship.hull.add_ammunition("Laser Energy", laser_charge_rate * delta)

View file

@ -13,25 +13,22 @@ signal destroyed
@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
## Reference to weapons node
@onready var weapons: Node2D = $Weapons
## Faction which this ship belongs to
var faction : Game.Faction = Game.Faction.Player
func _ready():
#shield.material = material
func _ready() -> void:
destroyed.connect(destroy)
## Reset all required variables
func destroy():
func destroy() -> void:
hull.hp = hull.max_hp
shield.capacity = shield.max_capacity
## Swaps old hull with the new one
func change_hull(new_hull_id: String):
func change_hull(new_hull_id: String) -> void:
var new_hull: Hull = Game.get_module(new_hull_id, 'hull').instantiate()
var old_hull: Hull = hull
add_child(new_hull)
@ -40,7 +37,7 @@ func change_hull(new_hull_id: String):
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):
func change_engine(new_engine_id: String) -> void:
var new_engine: ShipEngine = Game.get_module(new_engine_id, 'engine').instantiate()
var old_engine: ShipEngine = engine
add_child(new_engine)
@ -48,7 +45,7 @@ func change_engine(new_engine_id: String):
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):
func change_shield(new_shield_id: String) -> void:
var new_shield: Shield = Game.get_module(new_shield_id, 'shield').instantiate()
var old_shield: Shield = shield
add_child(new_shield)

25
scripts/Ship/weapons.gd Normal file
View file

@ -0,0 +1,25 @@
extends Node2D
# TODO: implement add_weapon
## Shortcut to get_parent()
@onready var ship = get_parent()
## List of weapons
var list: Array[Weapon] = get_children() as Array[Weapon]
## Updates list with actual children of this node
func update_weapon_list() -> void:
list = get_children() as Array[Weapon]
## Removes weapon with given ID and returns true if successful
func remove_weapon(id: String) -> bool:
for weapon in list:
if weapon.id == id:
list.erase(weapon)
weapon.free()
return true
return false
## Adds weapon with given ID to list
func add_weapon(id: String) -> void:
pass

View file

@ -4,6 +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}
const DEFAULT_ITEM = preload("res://items/test_item.tres")
const DEFAULT_WEAPON = preload("res://scenes/weapons/presets/SingleLaserMk1.tscn")
@ -12,7 +13,7 @@ const DEFAULT_ENGINE = preload("res://scenes/engines/starterengine.tscn")
const DEFAULT_SHIELD = preload("res://scenes/shields/startershield.tscn")
const salt = "2ndbeam"
const gameversion = "Ictar 1.1"
const gameversion = "Ifre 1.0"
const beta = false
static var profile : Profile = Profile.new()

View file

@ -1,6 +1,6 @@
extends Node2D
class_name Hull
#class_name Hull
@export var max_hp : float = 30
@export var max_fuel : float = 1000

View file

@ -1,6 +1,6 @@
extends Node2D
class_name Shield
#class_name Shield
@export var max_capacity : int = 8
@export var shield_charge_rate : float = 1

View file

@ -2,7 +2,7 @@ extends Node2D
# TODO: rewrite movement system to be impulse based
class_name ShipEngine
# class_name ShipEngine
@export var max_speed : float = 200
@export var max_turbo_speed : float = 300

View file

@ -26,7 +26,6 @@ func create(type : TYPE, progress_max : int, reward_money : float, restrictions
func do_progress() -> void:
progress += 1
print("sheesh")
if progress >= progress_max:
quest_ended.emit(true)