diff --git a/scenes/Ships/Modules/Engines/engine.tscn b/scenes/Ships/Modules/Engines/engine.tscn new file mode 100644 index 0000000..863ab30 --- /dev/null +++ b/scenes/Ships/Modules/Engines/engine.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://mw4kwxoeqch3"] + +[ext_resource type="Script" path="res://scripts/Ship/engine.gd" id="1_e4bt7"] + +[node name="Engine" type="Node2D"] +script = ExtResource("1_e4bt7") diff --git a/scenes/Ships/Modules/Hulls/hull.tscn b/scenes/Ships/Modules/Hulls/hull.tscn new file mode 100644 index 0000000..15d7292 --- /dev/null +++ b/scenes/Ships/Modules/Hulls/hull.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=3 format=3 uid="uid://bsu4eqwdfewwi"] + +[ext_resource type="Script" path="res://scripts/Ship/hull.gd" id="1_7nfg8"] +[ext_resource type="Texture2D" uid="uid://beb76cssb3knp" path="res://sprites/ship.png" id="2_vhl4r"] + +[node name="Hull" type="RigidBody2D"] +position = Vector2(0.5, 0) +gravity_scale = 0.0 +angular_damp = 1.0 +script = ExtResource("1_7nfg8") + +[node name="Sprite" type="Sprite2D" parent="."] +texture = ExtResource("2_vhl4r") + +[node name="Collision" type="CollisionPolygon2D" parent="."] +polygon = PackedVector2Array(-45.5, 14, -45.5, 64, -44.5, 64, 81.5, 1, 81.5, -1, -44.5, -64, -45.5, -64, -45.5, -14, -67.5, -14, -81.5, -28, -82.5, -28, -82.5, 28, -81.5, 28, -67.5, 14) diff --git a/scenes/Ships/player_ship.tscn b/scenes/Ships/player_ship.tscn new file mode 100644 index 0000000..535b61d --- /dev/null +++ b/scenes/Ships/player_ship.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=5 format=3 uid="uid://dok3i8u5t1ka4"] + +[ext_resource type="PackedScene" uid="uid://bjkshql8ut6hk" path="res://scenes/Ships/ship.tscn" id="1_6x7bu"] +[ext_resource type="Shader" path="res://shaders/ship.gdshader" id="2_dbojl"] +[ext_resource type="Script" path="res://scripts/Ship/player_input_controller.gd" id="3_0e84a"] + +[sub_resource type="ShaderMaterial" id="ShaderMaterial_u1mh3"] +resource_local_to_scene = true +resource_name = "Shield Material" +shader = ExtResource("2_dbojl") +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="PlayerShip" instance=ExtResource("1_6x7bu")] +material = SubResource("ShaderMaterial_u1mh3") + +[node name="InputController" type="Node2D" parent="." index="2"] +script = ExtResource("3_0e84a") diff --git a/scenes/Ships/ship.tscn b/scenes/Ships/ship.tscn new file mode 100644 index 0000000..0356f00 --- /dev/null +++ b/scenes/Ships/ship.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=5 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"] + +[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")] diff --git a/scripts/Ship/engine.gd b/scripts/Ship/engine.gd new file mode 100644 index 0000000..735c452 --- /dev/null +++ b/scripts/Ship/engine.gd @@ -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) diff --git a/scripts/Ship/hull.gd b/scripts/Ship/hull.gd new file mode 100644 index 0000000..ae3eea6 --- /dev/null +++ b/scripts/Ship/hull.gd @@ -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() diff --git a/scripts/Ship/player_input_controller.gd b/scripts/Ship/player_input_controller.gd new file mode 100644 index 0000000..34ef1e4 --- /dev/null +++ b/scripts/Ship/player_input_controller.gd @@ -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") diff --git a/scripts/Ship/ship.gd b/scripts/Ship/ship.gd new file mode 100644 index 0000000..c785d21 --- /dev/null +++ b/scripts/Ship/ship.gd @@ -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) diff --git a/sprites/ship.png b/sprites/ship.png new file mode 100644 index 0000000..0f9fab9 Binary files /dev/null and b/sprites/ship.png differ diff --git a/sprites/ship.png.import b/sprites/ship.png.import new file mode 100644 index 0000000..cee68f9 --- /dev/null +++ b/sprites/ship.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://beb76cssb3knp" +path="res://.godot/imported/ship.png-4d5e64b785005468aa74f7ca14df1341.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://sprites/ship.png" +dest_files=["res://.godot/imported/ship.png-4d5e64b785005468aa74f7ca14df1341.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1