Added rotatable laser weapon

This commit is contained in:
2ndbeam 2024-05-12 21:19:44 +03:00
commit 154da5104e
10 changed files with 99 additions and 13 deletions

View file

@ -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")

View file

@ -7,10 +7,8 @@
script = ExtResource("1_ga8vg") script = ExtResource("1_ga8vg")
projectile = ExtResource("2_qo148") projectile = ExtResource("2_qo148")
shoot_timer = NodePath("ShootTimer") shoot_timer = NodePath("ShootTimer")
spawner_points = [NodePath("Spawner")] spawner_points = [NodePath("")]
[node name="ShootTimer" type="Timer" parent="."] [node name="ShootTimer" type="Timer" parent="."]
wait_time = 0.25 wait_time = 0.25
one_shot = true one_shot = true
[node name="Spawner" type="Node2D" parent="."]

View file

@ -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_ship.gd" id="2_oqdd7"]
[ext_resource type="Script" path="res://scripts/Ship/player_input_controller.gd" id="3_0e84a"] [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://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://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="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/player_camera.gd" id="5_rclap"]
[ext_resource type="Script" path="res://scripts/Ship/weapons.gd" id="6_f6fm2"] [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"] [sub_resource type="GDScript" id="GDScript_ry4sc"]
resource_name = "money_counter" resource_name = "money_counter"
@ -101,10 +101,9 @@ contact_monitor = true
[node name="Weapons" type="Node2D" parent="."] [node name="Weapons" type="Node2D" parent="."]
script = ExtResource("6_f6fm2") 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" action_id = "primary"
ammo_type = "Laser Energy"
ammo_consumption = 1.0
[node name="ColorableGUI" type="CanvasLayer" parent="."] [node name="ColorableGUI" type="CanvasLayer" parent="."]

View file

@ -25,4 +25,4 @@ position = Vector2(19, 10)
position = Vector2(7171, -28) position = Vector2(7171, -28)
[node name="KamikazeShip" parent="FactionAggressive" index="0" instance=ExtResource("4_i6rbg")] [node name="KamikazeShip" parent="FactionAggressive" index="0" instance=ExtResource("4_i6rbg")]
position = Vector2(147, 92) position = Vector2(4245, -1247)

View file

@ -3,6 +3,15 @@ extends Node2D
## Shortcut to get_parent() ## Shortcut to get_parent()
@onready var ship: Ship = 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: func _physics_process(_delta) -> void:
ship.engine.acceleration_axis = Input.get_axis("deccelerate", "accelerate") ship.engine.acceleration_axis = Input.get_axis("deccelerate", "accelerate")
ship.engine.rotation_axis = Input.get_axis("rotateleft", "rotateright") 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") weapon.shoot_request = Input.get_action_strength("shootprimary")
"secondary": "secondary":
weapon.shoot_request = Input.get_action_strength("shootsecondary") 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

View file

@ -19,6 +19,13 @@ var destination_degrees: float = 0:
destination_degrees = value destination_degrees = value
## Delta to destination_degrees ## Delta to destination_degrees
var destination_difference: float = 1.0 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) ## available map bounds (use with absolute position)
var available_bounds: Vector2 var available_bounds: Vector2
# for testing purposes only # for testing purposes only
@ -35,11 +42,9 @@ func enter(_message):
func process(_delta): func process(_delta):
# checking if need to apply torque # checking if need to apply torque
var current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees
var other_destination_difference = ship.hull.global_rotation_degrees - destination_degrees if abs(current_destination_difference) > destination_difference:
var min_difference = min(abs(current_destination_difference), abs(other_destination_difference)) ship.engine.rotation_axis = clamp(current_destination_difference / 360 + 0.5 * sign(current_destination_difference), -1.0, 1.0)
if min_difference > destination_difference:
ship.engine.rotation_axis = min_difference / 180
else: else:
ship.engine.rotation_axis = 0.0 ship.engine.rotation_axis = 0.0
# making ship always accelerate # making ship always accelerate

15
scripts/Weapons/laser.gd Normal file
View file

@ -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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 B

After

Width:  |  Height:  |  Size: 371 B

Before After
Before After

BIN
sprites/lasergun.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 205 B

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://1yihkbdosopx"
path="res://.godot/imported/lasergun.png-6b93eff0666d2773975cf9e373c6fac8.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://sprites/lasergun.png"
dest_files=["res://.godot/imported/lasergun.png-6b93eff0666d2773975cf9e373c6fac8.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