From e2b9fa6c69afcd9feaeee629c941a985d13497b0 Mon Sep 17 00:00:00 2001 From: 2ndbeam <166764593+2ndbeam@users.noreply.github.com> Date: Sun, 28 Apr 2024 18:53:09 +0300 Subject: [PATCH] Rewriting ships: Movement system --- scenes/Ships/Modules/Engines/engine.tscn | 6 +++ scenes/Ships/Modules/Hulls/hull.tscn | 16 +++++++ scenes/Ships/player_ship.tscn | 21 +++++++++ scenes/Ships/ship.tscn | 14 ++++++ scripts/Ship/engine.gd | 26 +++++++++++ scripts/Ship/hull.gd | 27 +++++++++++ scripts/Ship/player_input_controller.gd | 8 ++++ scripts/Ship/ship.gd | 56 +++++++++++++++++++++++ sprites/ship.png | Bin 0 -> 1828 bytes sprites/ship.png.import | 34 ++++++++++++++ 10 files changed, 208 insertions(+) create mode 100644 scenes/Ships/Modules/Engines/engine.tscn create mode 100644 scenes/Ships/Modules/Hulls/hull.tscn create mode 100644 scenes/Ships/player_ship.tscn create mode 100644 scenes/Ships/ship.tscn create mode 100644 scripts/Ship/engine.gd create mode 100644 scripts/Ship/hull.gd create mode 100644 scripts/Ship/player_input_controller.gd create mode 100644 scripts/Ship/ship.gd create mode 100644 sprites/ship.png create mode 100644 sprites/ship.png.import 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 0000000000000000000000000000000000000000..0f9fab912e582085e7bf7f7e25cf109d6aa03dae GIT binary patch literal 1828 zcmV+<2iy3GP)(}9j)RFUd9N;`}VqGl1tVg-L zyo{44PvY9OYk2kQRrskePM+2al=Ub9;LV#iSXo)Y`1p7nPT=6Q{tU!fh@3#FB2GVY z3Z*I~&JVROkPVfSC{+n@J_T_)mD4Cy#96Egfl@`B#i&pyRmACbg+!?$PB$wwN|nM{ ztyYQCfxJMedi(Zm7tWV2Uq&|}P7Cr1rApycr*-1AD=$&1QsVqb`vN`9Ym};lI5$9? zS|Z9&#A!!F8541ujVSv>oMs}*1QDm+h%!aQsVAZwpp-bLv@g){h;oP$;(P+)OchaP zP!#9x?rwAw;!Kl>*fuo@H8wUT0Gygir6Tk5^RlzEBRzT`B2yOpU0XTE7Z5Yfqa;qE zw0?+_D6JjhBuZ;1AJMwt@lj0O8f8_H4Bi|`#K zalVVPBO<3vFN7$&an8=p%J%lQmpF0wCOcveOz%@QX}NP8#2NMIYO6CPJxTuB@!|T6AtrO-&i!X}*lYxwW;`Yj)Xd z$oBU3u(q~_vuDqu(P*IEZbvuy7{EgSj{sa8_VtL8B~BMkqGYMd8z)h+fy)agQL>@S z8YfY*%!=aN+S-b4k_abJvdmLB&z(Cr5Kf|GxfRAaH#dh)rxV>o;rs=_1@$0NvfSI- z+r#SWD$blagR57s#^Ia+u#Q8VM9H+azrT3}bxJ@1dSOV}<_(7s%nKvj#6;ZOxO8Bd8Ju#|? zk_}x}7*#~c1}M^1cjj32S}V@!X< zmKP)7Sc6ebqSJS&RKnf6cQH3N7k>C(0DmX>^C>F>7*k*L`OkZnQR+0^YPC?S)#5hE z=QQ1`@IQlI7r4@V5mjE`O7oG?mTL&9>5J0PReg!lQ;bKV>;v*t&Wj>SPcimIX%Hk) zn!{-C<)saSBubAl#-i+V%Mx86iPB??u_$?SC%4CSjIk&qy%638&xt7tMm4=qb_{_e z$`s?%4MTZrWy{C6TPyP>^cIhgH=*x{xl=Y#65~54Z2?J?9%Brnva<3KR+)!J3C%t45PGdyhBNh-NO%#8LU#N;L4RNXf~S|9UYC^S_G1}TiK zRtqOioQT_m7*j-)Lqv@EM3e)R5aR-Xr+QabjEFKt#Hc5tOb{{ZjVSv>jAkOrn26DA zL>Y=0?T9ErjJD-9%7hrd0`N@l%KXktl*DLvUZGSNmzI_=GBOgk2{Brb7buS%JBCK1 zL5z-6Xq3e0W`#sajBZybl*Cw!3W1UsiyMO? zc$)U0lIaB`NC12X;5>lOK#+yc<|Cy5_z?%EX+rcU8&L{?`#8Y(r{T3Q1N{f}D;PCe ST=*dX0000