From 154da5104ed5233136eeace1e5c788b4af733599 Mon Sep 17 00:00:00 2001 From: 2ndbeam <166764593+2ndbeam@users.noreply.github.com> Date: Sun, 12 May 2024 21:19:44 +0300 Subject: [PATCH] Added rotatable laser weapon --- scenes/Ships/Modules/Weapons/laser.tscn | 22 ++++++++++++ scenes/Ships/Modules/Weapons/weapon.tscn | 4 +-- scenes/Ships/player_ship.tscn | 7 ++-- scenes/Star Systems/star_system_debug.tscn | 2 +- scripts/Ship/player_input_controller.gd | 13 +++++++ .../State Machine/Kamikaze/WanderingState.gd | 15 +++++--- scripts/Weapons/laser.gd | 15 ++++++++ sprites/laserbox.png | Bin 130 -> 371 bytes sprites/lasergun.png | Bin 0 -> 205 bytes sprites/lasergun.png.import | 34 ++++++++++++++++++ 10 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 scenes/Ships/Modules/Weapons/laser.tscn create mode 100644 scripts/Weapons/laser.gd create mode 100644 sprites/lasergun.png create mode 100644 sprites/lasergun.png.import diff --git a/scenes/Ships/Modules/Weapons/laser.tscn b/scenes/Ships/Modules/Weapons/laser.tscn new file mode 100644 index 0000000..f821cb3 --- /dev/null +++ b/scenes/Ships/Modules/Weapons/laser.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=5 format=3 uid="uid://bf10g066l8grd"] + +[ext_resource type="PackedScene" uid="uid://ryy1tdrxmjav" path="res://scenes/Ships/Modules/Weapons/weapon.tscn" id="1_l10lx"] +[ext_resource type="Texture2D" uid="uid://1yihkbdosopx" path="res://sprites/lasergun.png" id="2_5ysch"] +[ext_resource type="Script" path="res://scripts/Weapons/laser.gd" id="2_qqcrf"] +[ext_resource type="Texture2D" uid="uid://c3gei46k8muk3" path="res://sprites/laserbox.png" id="3_65sll"] + +[node name="Laser" node_paths=PackedStringArray("spawner_points") instance=ExtResource("1_l10lx")] +script = ExtResource("2_qqcrf") +max_gun_rotation = 30.0 +id = "laser" +ammo_type = "Laser Energy" +ammo_consumption = 1.0 +spawner_points = [NodePath("Gun")] + +[node name="Gun" type="Sprite2D" parent="." index="1"] +texture = ExtResource("2_5ysch") +centered = false +offset = Vector2(0, -6.5) + +[node name="Frame" type="Sprite2D" parent="." index="2"] +texture = ExtResource("3_65sll") diff --git a/scenes/Ships/Modules/Weapons/weapon.tscn b/scenes/Ships/Modules/Weapons/weapon.tscn index 5b5fddd..e01a58b 100644 --- a/scenes/Ships/Modules/Weapons/weapon.tscn +++ b/scenes/Ships/Modules/Weapons/weapon.tscn @@ -7,10 +7,8 @@ script = ExtResource("1_ga8vg") projectile = ExtResource("2_qo148") shoot_timer = NodePath("ShootTimer") -spawner_points = [NodePath("Spawner")] +spawner_points = [NodePath("")] [node name="ShootTimer" type="Timer" parent="."] wait_time = 0.25 one_shot = true - -[node name="Spawner" type="Node2D" parent="."] diff --git a/scenes/Ships/player_ship.tscn b/scenes/Ships/player_ship.tscn index b79bfac..9a9deb6 100644 --- a/scenes/Ships/player_ship.tscn +++ b/scenes/Ships/player_ship.tscn @@ -3,11 +3,11 @@ [ext_resource type="Script" path="res://scripts/Ship/player_ship.gd" id="2_oqdd7"] [ext_resource type="Script" path="res://scripts/Ship/player_input_controller.gd" id="3_0e84a"] [ext_resource type="PackedScene" uid="uid://bsu4eqwdfewwi" path="res://scenes/Ships/Modules/Hulls/hull.tscn" id="3_ku5af"] -[ext_resource type="PackedScene" uid="uid://ryy1tdrxmjav" path="res://scenes/Ships/Modules/Weapons/weapon.tscn" id="4_fy1be"] [ext_resource type="PackedScene" uid="uid://mw4kwxoeqch3" path="res://scenes/Ships/Modules/Engines/engine.tscn" id="4_pmbbn"] [ext_resource type="PackedScene" uid="uid://bunboi5ouscw8" path="res://scenes/Ships/Modules/Shields/shield.tscn" id="5_7fjpq"] [ext_resource type="Script" path="res://scripts/Ship/player_camera.gd" id="5_rclap"] [ext_resource type="Script" path="res://scripts/Ship/weapons.gd" id="6_f6fm2"] +[ext_resource type="PackedScene" uid="uid://bf10g066l8grd" path="res://scenes/Ships/Modules/Weapons/laser.tscn" id="7_0ss0b"] [sub_resource type="GDScript" id="GDScript_ry4sc"] resource_name = "money_counter" @@ -101,10 +101,9 @@ contact_monitor = true [node name="Weapons" type="Node2D" parent="."] script = ExtResource("6_f6fm2") -[node name="Weapon" parent="Weapons" instance=ExtResource("4_fy1be")] +[node name="Laser" parent="Weapons" instance=ExtResource("7_0ss0b")] +position = Vector2(75, 0) action_id = "primary" -ammo_type = "Laser Energy" -ammo_consumption = 1.0 [node name="ColorableGUI" type="CanvasLayer" parent="."] diff --git a/scenes/Star Systems/star_system_debug.tscn b/scenes/Star Systems/star_system_debug.tscn index 07ebc1b..6883116 100644 --- a/scenes/Star Systems/star_system_debug.tscn +++ b/scenes/Star Systems/star_system_debug.tscn @@ -25,4 +25,4 @@ position = Vector2(19, 10) position = Vector2(7171, -28) [node name="KamikazeShip" parent="FactionAggressive" index="0" instance=ExtResource("4_i6rbg")] -position = Vector2(147, 92) +position = Vector2(4245, -1247) diff --git a/scripts/Ship/player_input_controller.gd b/scripts/Ship/player_input_controller.gd index 9fc452b..9603f9c 100644 --- a/scripts/Ship/player_input_controller.gd +++ b/scripts/Ship/player_input_controller.gd @@ -3,6 +3,15 @@ extends Node2D ## Shortcut to get_parent() @onready var ship: Ship = get_parent() +var expected_rotation: float = 0.0: + set(value): + if value > 180: + expected_rotation = value - 360 + elif value < -180: + expected_rotation = 360 + value + else: + expected_rotation = value + func _physics_process(_delta) -> void: ship.engine.acceleration_axis = Input.get_axis("deccelerate", "accelerate") ship.engine.rotation_axis = Input.get_axis("rotateleft", "rotateright") @@ -12,4 +21,8 @@ func _physics_process(_delta) -> void: weapon.shoot_request = Input.get_action_strength("shootprimary") "secondary": weapon.shoot_request = Input.get_action_strength("shootsecondary") + if "gun_rotation" in weapon: + var angle_to_mouse = ship.hull.position.angle_to_point(get_global_mouse_position()) + expected_rotation = rad_to_deg(angle_to_mouse - ship.hull.rotation) + weapon.gun_rotation = expected_rotation diff --git a/scripts/State Machine/Kamikaze/WanderingState.gd b/scripts/State Machine/Kamikaze/WanderingState.gd index 458bf55..2c8afc8 100644 --- a/scripts/State Machine/Kamikaze/WanderingState.gd +++ b/scripts/State Machine/Kamikaze/WanderingState.gd @@ -19,6 +19,13 @@ var destination_degrees: float = 0: destination_degrees = value ## Delta to destination_degrees var destination_difference: float = 1.0 + +var current_destination_difference: float: + set(value): + if value > 180: + current_destination_difference = value - 360 + else: + current_destination_difference = value ## available map bounds (use with absolute position) var available_bounds: Vector2 # for testing purposes only @@ -35,11 +42,9 @@ func enter(_message): func process(_delta): # checking if need to apply torque - var current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees - var other_destination_difference = ship.hull.global_rotation_degrees - destination_degrees - var min_difference = min(abs(current_destination_difference), abs(other_destination_difference)) - if min_difference > destination_difference: - ship.engine.rotation_axis = min_difference / 180 + 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: ship.engine.rotation_axis = 0.0 # making ship always accelerate diff --git a/scripts/Weapons/laser.gd b/scripts/Weapons/laser.gd new file mode 100644 index 0000000..473bc51 --- /dev/null +++ b/scripts/Weapons/laser.gd @@ -0,0 +1,15 @@ +extends Weapon + +## Reference to Gun node +@onready var gun = $Gun +## Maximum gun rotation angle +@export var max_gun_rotation: float = 30.0 +## Current gun rotation angle. Clamps with max_gun_rotation +var gun_rotation: float = 0.0: + set(value): + gun_rotation = clamp(value, -max_gun_rotation / 2, max_gun_rotation / 2) + update_gun() + +## Updates gun sprite (and projectile spawner) rotation +func update_gun(): + gun.rotation_degrees = gun_rotation diff --git a/sprites/laserbox.png b/sprites/laserbox.png index 15245cbd1a7e5496c2485bb80681d235ef8f42bf..60615249b66dd364ac7ed6e60d0e66d715ccf767 100644 GIT binary patch literal 371 zcmV-(0gV2MP)+zsNOd^dU|J!6vX`5lG{q@8J`qNE-ryB+qnq)*dOuJi#4@h#=<+ z{8mcXn6Oz69GGH$I0e?g^^|B15a0}W1k9KqkffhOnx=GJ zHxMOBLZ0XS(g$!0ybQ#Blj1lQ@MmUOW_$WB>B{!d83?E-3LE++>Czhrs4Pnxx|ej} z4FptGl?~lVI`;+wYMRD|ZY3?ffk6lpinR(g8$%zH2dih1^v)|cB0To4gx;Tbd^uC?4k(a@cgL!WF zvJ-4_O