From ab3ac4af669d301a7b0d09f37036fa68aaaa1d5b Mon Sep 17 00:00:00 2001 From: 2ndbeam <166764593+2ndbeam@users.noreply.github.com> Date: Mon, 13 May 2024 21:04:36 +0300 Subject: [PATCH] Added shooter ship --- scenes/Ships/Modules/Hulls/shooter_hull.tscn | 14 +++ scenes/Ships/NPC Ships/kamikaze_ship.tscn | 35 +++++++- scenes/Ships/NPC Ships/shooter_ship.tscn | 80 ++++++++++++++++++ scenes/Star Systems/star_system_debug.tscn | 8 +- scenes/projectiles/Laser.tscn | 2 +- scripts/State Machine/Kamikaze/AttackState.gd | 2 + .../State Machine/Kamikaze/WanderingState.gd | 1 + scripts/State Machine/Shooter/AttackState.gd | 60 +++++++++++++ sprites/shooter.png | Bin 0 -> 3527 bytes sprites/shooter.png.import | 34 ++++++++ 10 files changed, 232 insertions(+), 4 deletions(-) create mode 100644 scenes/Ships/Modules/Hulls/shooter_hull.tscn create mode 100644 scenes/Ships/NPC Ships/shooter_ship.tscn create mode 100644 scripts/State Machine/Shooter/AttackState.gd create mode 100644 sprites/shooter.png create mode 100644 sprites/shooter.png.import diff --git a/scenes/Ships/Modules/Hulls/shooter_hull.tscn b/scenes/Ships/Modules/Hulls/shooter_hull.tscn new file mode 100644 index 0000000..f2d4598 --- /dev/null +++ b/scenes/Ships/Modules/Hulls/shooter_hull.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://bckgb423wsdrw"] + +[ext_resource type="PackedScene" uid="uid://bsu4eqwdfewwi" path="res://scenes/Ships/Modules/Hulls/hull.tscn" id="1_7i1ve"] +[ext_resource type="Texture2D" uid="uid://bxtd2ij1dc1d3" path="res://sprites/shooter.png" id="2_gvwov"] + +[node name="Hull" instance=ExtResource("1_7i1ve")] +max_hp = 10.0 + +[node name="Sprite" parent="." index="0"] +position = Vector2(0, 0.5) +texture = ExtResource("2_gvwov") + +[node name="Collision" parent="." index="1"] +polygon = PackedVector2Array(-52.5, 15, -52.5, 51, -69.5, 61, -63.5, 71, -62.5, 71, 76.5, 1, 76.5, 0, -62.5, -70, -63.5, -70, -69.5, -60, -52.5, -50, -52.5, -14, -62.5, -14, -75.5, -28, -76.5, -28, -76.5, 29, -75.5, 29, -61.5, 15) diff --git a/scenes/Ships/NPC Ships/kamikaze_ship.tscn b/scenes/Ships/NPC Ships/kamikaze_ship.tscn index 9cc990c..5c01cb3 100644 --- a/scenes/Ships/NPC Ships/kamikaze_ship.tscn +++ b/scenes/Ships/NPC Ships/kamikaze_ship.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=9 format=3 uid="uid://pev6k21vqaem"] +[gd_scene load_steps=10 format=3 uid="uid://pev6k21vqaem"] [ext_resource type="Script" path="res://scripts/Ship/ship.gd" id="1_82bba"] [ext_resource type="PackedScene" uid="uid://bkd4gyhlota7l" path="res://scenes/Ships/Modules/Hulls/kamikaze_hull.tscn" id="2_165nu"] @@ -9,6 +9,19 @@ [ext_resource type="Script" path="res://scripts/State Machine/Kamikaze/WanderingState.gd" id="6_emcj1"] [ext_resource type="Script" path="res://scripts/State Machine/Kamikaze/AttackState.gd" id="7_ilmkp"] +[sub_resource type="GDScript" id="GDScript_nif6i"] +resource_name = "health_shield_counter" +script/source = "extends Label + +## Text with placeholder +@onready var unformatted_text: String = text +## Shortcut to get_parent().get_parent().get_parent() +@onready var ship: Ship = get_parent().get_parent().get_parent() + +func _process(_delta): + text = unformatted_text.format([ship.hull.hp, ship.hull.max_hp, ship.shield.capacity, ship.shield.max_capacity]) +" + [node name="KamikazeShip" type="Node2D"] script = ExtResource("1_82bba") @@ -38,4 +51,24 @@ wait_time = 10.0 [node name="AttackState" type="Node" parent="StateTree"] script = ExtResource("7_ilmkp") +[node name="StatHolder" type="Node" parent="."] + +[node name="StatCarry" type="Node2D" parent="StatHolder"] + +[node name="HealthShield" type="Label" parent="StatHolder/StatCarry"] +offset_left = -49.0 +offset_top = -133.0 +offset_right = 50.0 +offset_bottom = -84.0 +text = "{0}/{1} HP +{2}/{3} SP" +horizontal_alignment = 1 +vertical_alignment = 1 +script = SubResource("GDScript_nif6i") + +[node name="RemoteTransform2D" type="RemoteTransform2D" parent="."] +remote_path = NodePath("../StatHolder/StatCarry") +update_rotation = false +update_scale = false + [connection signal="timeout" from="StateTree/WanderingState/UpdateDestination" to="StateTree/WanderingState" method="update_destination"] diff --git a/scenes/Ships/NPC Ships/shooter_ship.tscn b/scenes/Ships/NPC Ships/shooter_ship.tscn new file mode 100644 index 0000000..2d1b113 --- /dev/null +++ b/scenes/Ships/NPC Ships/shooter_ship.tscn @@ -0,0 +1,80 @@ +[gd_scene load_steps=11 format=3 uid="uid://yso8f0y5kg50"] + +[ext_resource type="Script" path="res://scripts/Ship/ship.gd" id="1_o387g"] +[ext_resource type="PackedScene" uid="uid://bckgb423wsdrw" path="res://scenes/Ships/Modules/Hulls/shooter_hull.tscn" id="2_kthut"] +[ext_resource type="PackedScene" uid="uid://mw4kwxoeqch3" path="res://scenes/Ships/Modules/Engines/engine.tscn" id="3_ysnrn"] +[ext_resource type="PackedScene" uid="uid://bunboi5ouscw8" path="res://scenes/Ships/Modules/Shields/shield.tscn" id="4_5rkla"] +[ext_resource type="Script" path="res://scripts/Ship/weapons.gd" id="5_f305p"] +[ext_resource type="PackedScene" uid="uid://bf10g066l8grd" path="res://scenes/Ships/Modules/Weapons/laser.tscn" id="6_hun72"] +[ext_resource type="Script" path="res://scripts/State Machine/tree.gd" id="7_5uvtf"] +[ext_resource type="Script" path="res://scripts/State Machine/Kamikaze/WanderingState.gd" id="8_i8jwt"] +[ext_resource type="Script" path="res://scripts/State Machine/Shooter/AttackState.gd" id="9_0eak7"] + +[sub_resource type="GDScript" id="GDScript_r7j0p"] +resource_name = "health_shield_counter" +script/source = "extends Label + +## Text with placeholder +@onready var unformatted_text: String = text +## Shortcut to get_parent().get_parent().get_parent() +@onready var ship: Ship = get_parent().get_parent().get_parent() + +func _process(_delta): + text = unformatted_text.format([ship.hull.hp, ship.hull.max_hp, ship.shield.capacity, ship.shield.max_capacity]) +" + +[node name="ShooterShip" type="Node2D"] +process_mode = 1 +script = ExtResource("1_o387g") + +[node name="HullHolder" type="Node" parent="."] + +[node name="Hull" parent="HullHolder" instance=ExtResource("2_kthut")] + +[node name="Engine" parent="." instance=ExtResource("3_ysnrn")] + +[node name="Shield" parent="." instance=ExtResource("4_5rkla")] +max_capacity = 3 + +[node name="Weapons" type="Node2D" parent="."] +script = ExtResource("5_f305p") + +[node name="Laser" parent="Weapons" instance=ExtResource("6_hun72")] +position = Vector2(67, 0.5) +max_gun_rotation = 15.0 +action_id = "shoot" +ammo_type = "n/a" +ammo_consumption = 0.0 + +[node name="StateTree" type="Node" parent="." node_paths=PackedStringArray("state")] +script = ExtResource("7_5uvtf") +state = NodePath("WanderingState") + +[node name="WanderingState" type="Node" parent="StateTree"] +script = ExtResource("8_i8jwt") + +[node name="UpdateDestination" type="Timer" parent="StateTree/WanderingState"] +wait_time = 10.0 + +[node name="AttackState" type="Node" parent="StateTree"] +script = ExtResource("9_0eak7") + +[node name="StatHolder" type="Node" parent="."] + +[node name="StatCarry" type="Node2D" parent="StatHolder"] + +[node name="HealthShield" type="Label" parent="StatHolder/StatCarry"] +offset_left = -49.0 +offset_top = -133.0 +offset_right = 50.0 +offset_bottom = -84.0 +text = "{0}/{1} HP +{2}/{3} SP" +horizontal_alignment = 1 +vertical_alignment = 1 +script = SubResource("GDScript_r7j0p") + +[node name="RemoteTransform2D" type="RemoteTransform2D" parent="."] +remote_path = NodePath("../StatHolder/StatCarry") +update_rotation = false +update_scale = false diff --git a/scenes/Star Systems/star_system_debug.tscn b/scenes/Star Systems/star_system_debug.tscn index 6883116..3a4432a 100644 --- a/scenes/Star Systems/star_system_debug.tscn +++ b/scenes/Star Systems/star_system_debug.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=5 format=3 uid="uid://crneq2enhxsw5"] +[gd_scene load_steps=6 format=3 uid="uid://crneq2enhxsw5"] [ext_resource type="PackedScene" uid="uid://bsnrcw64qr2hr" path="res://scenes/Star Systems/star_system_template.tscn" id="1_2ai1l"] [ext_resource type="PackedScene" uid="uid://dk3nvl8f0v24e" path="res://scenes/base_template.tscn" id="3_m5ica"] [ext_resource type="PackedScene" uid="uid://pev6k21vqaem" path="res://scenes/Ships/NPC Ships/kamikaze_ship.tscn" id="4_i6rbg"] +[ext_resource type="PackedScene" uid="uid://yso8f0y5kg50" path="res://scenes/Ships/NPC Ships/shooter_ship.tscn" id="5_o3ny8"] [ext_resource type="PackedScene" uid="uid://dok3i8u5t1ka4" path="res://scenes/Ships/player_ship.tscn" id="7_jyplv"] [node name="StarSystem" instance=ExtResource("1_2ai1l")] @@ -25,4 +26,7 @@ position = Vector2(19, 10) position = Vector2(7171, -28) [node name="KamikazeShip" parent="FactionAggressive" index="0" instance=ExtResource("4_i6rbg")] -position = Vector2(4245, -1247) +position = Vector2(-444, -228) + +[node name="ShooterShip" parent="FactionAggressive" index="1" instance=ExtResource("5_o3ny8")] +position = Vector2(204, 491) diff --git a/scenes/projectiles/Laser.tscn b/scenes/projectiles/Laser.tscn index aaddd25..772e33d 100644 --- a/scenes/projectiles/Laser.tscn +++ b/scenes/projectiles/Laser.tscn @@ -7,7 +7,7 @@ size = Vector2(4, 32) [node name="Laser" type="Node2D" node_paths=PackedStringArray("collider")] script = ExtResource("1_6nacn") -speed = 600.0 +speed = 1000.0 collider = NodePath("Collision") [node name="LaserBody" type="Line2D" parent="."] diff --git a/scripts/State Machine/Kamikaze/AttackState.gd b/scripts/State Machine/Kamikaze/AttackState.gd index 017677f..97b36c2 100644 --- a/scripts/State Machine/Kamikaze/AttackState.gd +++ b/scripts/State Machine/Kamikaze/AttackState.gd @@ -37,6 +37,7 @@ func process(_delta): # checking if need to apply torque destination_degrees = rad_to_deg(ship.global_position.angle_to_point(target.global_position)) current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees + if abs(current_destination_difference) > destination_difference: ship.engine.rotation_axis = clamp(current_destination_difference / 360 + 0.5 * sign(current_destination_difference), -1.0, 1.0) else: @@ -44,6 +45,7 @@ func process(_delta): ship.hull.apply_central_force(Vector2.from_angle(ship.hull.rotation) * additional_stab_force) # making ship always accelerate ship.engine.acceleration_axis = 1.0 + var distance_to_target = ship.global_position.distance_to(target.global_position) if distance_to_target > target_loss_distance: get_parent().transit("WanderingState") diff --git a/scripts/State Machine/Kamikaze/WanderingState.gd b/scripts/State Machine/Kamikaze/WanderingState.gd index 2c8afc8..fdebaac 100644 --- a/scripts/State Machine/Kamikaze/WanderingState.gd +++ b/scripts/State Machine/Kamikaze/WanderingState.gd @@ -43,6 +43,7 @@ func enter(_message): func process(_delta): # checking if need to apply torque current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees + if abs(current_destination_difference) > destination_difference: ship.engine.rotation_axis = clamp(current_destination_difference / 360 + 0.5 * sign(current_destination_difference), -1.0, 1.0) else: diff --git a/scripts/State Machine/Shooter/AttackState.gd b/scripts/State Machine/Shooter/AttackState.gd new file mode 100644 index 0000000..4190b1e --- /dev/null +++ b/scripts/State Machine/Shooter/AttackState.gd @@ -0,0 +1,60 @@ +extends State + +## If target is further than this distance, transit to WanderingState +@export var target_loss_distance: float = 1536.0 +## Node which is being targeted by this ship +var target: Node2D +## Shortcut to get_parent().get_parent() +@onready var ship = get_parent().get_parent() +## Ship will strive to achieve this angle +var destination_degrees: float = 0: + set(value): + if value > 180: + destination_degrees = value - 360 + elif value < -180: + destination_degrees = 360 + value + else: + destination_degrees = value +## Delta to destination_degrees +var destination_difference: float + +var current_destination_difference: float: + set(value): + if value > 180: + current_destination_difference = value - 360 + else: + current_destination_difference = value + +## Gun node to change its behavior +var gun: Node2D + +func enter(message: Dictionary): + if message.has("target"): + target = message["target"] + gun = ship.weapons.list[0] + destination_difference = gun.max_gun_rotation / 2 + else: + get_parent().transit("WanderingState") + +func process(_delta): + # checking if need to apply torque + destination_degrees = rad_to_deg(ship.global_position.angle_to_point(target.global_position)) + current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees + + if abs(current_destination_difference) > destination_difference: + ship.engine.rotation_axis = clamp(current_destination_difference / 360 + 0.5 * sign(current_destination_difference), -1.0, 1.0) + gun.shoot_request = 0.0 + else: + ship.engine.rotation_axis = 0.0 + gun.shoot_request = 1.0 + + var distance_to_target = ship.global_position.distance_to(target.global_position) + if distance_to_target > target_loss_distance: + get_parent().transit("WanderingState") + gun.gun_rotation = 0.0 + gun.shoot_request = 0.0 + return + + ship.engine.acceleration_axis = clamp((distance_to_target - 256) / 256 + 0.3, -1.0, 1.0) + gun.gun_rotation = current_destination_difference + diff --git a/sprites/shooter.png b/sprites/shooter.png new file mode 100644 index 0000000000000000000000000000000000000000..ef72ad69841e02af8e698658510c2efbfb7e3a7a GIT binary patch literal 3527 zcmWkxc|4R|8-9>|Plm`g#?H``En76hOk}JfRCq|Qks4cB2bHyDhM}>O5*3;G8VO~| zGALrgPvVL=hme#Af^IyxG8`ASH5fZx?XWEk;M z!J4@ow4)miZS8iwaJA?%;n2K7r&7xgpCbX&4?ou(Wvg4Zg#II4C3@d~+f_<3XF3Ik zePO@MlhQi3LH>@S4h|2Xg%+t8at4nz6;qM%?#d%uwNQ7Au-tuS~Mxt%r9o1>E@Dd{_0@sj#3b;370e(Ov-}tzSi$DZ^2lbiPHE+|Lga=kW zU6v^?Jc-yBBdu2@Mnpw@9eWv4v&sU*tT{s#K|KpMyGa$~clUp$?JlmbU-I{dD~R!X zdwbtlpRiaOz|gy=w$vn#JX8o-Y@2&uAUNx-#${k^oTs3m!1kk5WP^{t=xEpJ+n&%< zHhs4r5gpwqIG2B>z-aSxLpa;-0jR+8Z1nK-kI#yTp(1mr)EiV0Ai&?>)7`yed8F1g zHGyJ=?c_9nJ-Ga}_LL<{@U;O7)yBgwW}u?dT1lv?sxpuJVbImpRn-o#l$^lKj33Je z1v5qzk0)jI^?BY*!lsrSFgH89`F(L{Ebr;hJWv|LOuPH>E)L<1n|O4aajBpFzyGmo z2^}3BD+>!#Y<5nRB*GL+PXd#_$fNyBz<+X`_LvKuZ1$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&3C!rRK|qK%TUd_W~& zKKE!f(Mhp?Q0&oQsWKqoLq7so06z2Ecxzf@yaa0b+(7Fz%+<<=M2uzh@B?oIFPfnZ uT7{F=SU?NcR6md)ICXPT^&tH2A0DB%zvMD!juk?mFaU>fMAz6{O#VM+yP!G% literal 0 HcmV?d00001 diff --git a/sprites/shooter.png.import b/sprites/shooter.png.import new file mode 100644 index 0000000..08b359f --- /dev/null +++ b/sprites/shooter.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://bxtd2ij1dc1d3" +path="res://.godot/imported/shooter.png-dc69c2835aa11bc6878a2e4130c257c1.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://sprites/shooter.png" +dest_files=["res://.godot/imported/shooter.png-dc69c2835aa11bc6878a2e4130c257c1.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