Added in editor warnings when weapons are missing components (Audioplayer, Shot spawn location, projectile PackedScene)

Added functionality for weapons to fire projectiles.
This commit is contained in:
KingGurke 2025-04-03 12:26:41 +02:00
parent 9621323983
commit 40ffbb84cd
8 changed files with 135 additions and 17 deletions

View file

@ -1,17 +1,32 @@
[gd_scene load_steps=6 format=3 uid="uid://c0688rpnga7bt"]
[gd_scene load_steps=12 format=3 uid="uid://c0688rpnga7bt"]
[ext_resource type="Script" uid="uid://b5qkxnurvqbi2" path="res://scripts/flyer.gd" id="1_ben2c"]
[ext_resource type="Texture2D" uid="uid://djduujf1cj53r" path="res://sprites/pilotnorkt.png" id="2_ddpln"]
[ext_resource type="Script" uid="uid://cu7q3fqvjoyos" path="res://scripts/player_controller.gd" id="3_ddpln"]
[ext_resource type="Script" uid="uid://cr8aonshxxpqf" path="res://scripts/weapon.gd" id="4_isfd2"]
[ext_resource type="PackedScene" uid="uid://b2ly0rd4mua6i" path="res://scenes/shot.tscn" id="5_unik1"]
[ext_resource type="AudioStream" uid="uid://bkguu5u88kjlk" path="res://audio/gun1.wav" id="6_0tbcv"]
[ext_resource type="AudioStream" uid="uid://cbpofkydw3sa5" path="res://audio/gun2.wav" id="7_aueel"]
[ext_resource type="AudioStream" uid="uid://clg0ybr0jlcwc" path="res://audio/gun3.wav" id="8_v0x65"]
[ext_resource type="AudioStream" uid="uid://brc0i5e6x7v4h" path="res://audio/gun4.wav" id="9_h3pf6"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_isfd2"]
size = Vector2(74.5, 29)
[sub_resource type="AudioStreamRandomizer" id="AudioStreamRandomizer_3kq13"]
random_pitch = 1.01
random_volume_offset_db = 0.01
streams_count = 4
stream_0/stream = ExtResource("6_0tbcv")
stream_1/stream = ExtResource("7_aueel")
stream_2/stream = ExtResource("8_v0x65")
stream_3/stream = ExtResource("9_h3pf6")
[node name="Node2D" type="Node2D"]
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
[node name="CharacterBody2D" type="CharacterBody2D" parent="." node_paths=PackedStringArray("weapons")]
script = ExtResource("1_ben2c")
weapons = [NodePath("Weapon")]
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
position = Vector2(3.75, 1.5)
@ -26,6 +41,17 @@ script = ExtResource("3_ddpln")
controlled_flyer = NodePath("..")
metadata/_custom_type_script = "uid://cu7q3fqvjoyos"
[node name="Weapon" type="Node2D" parent="CharacterBody2D"]
[node name="Weapon" type="Node2D" parent="CharacterBody2D" node_paths=PackedStringArray("shot_spawn", "audio")]
script = ExtResource("4_isfd2")
max_ammo = 100
shot = ExtResource("5_unik1")
shot_spawn = NodePath("ShotSpawn")
audio = NodePath("AudioStreamPlayer2D")
metadata/_custom_type_script = "uid://cr8aonshxxpqf"
[node name="ShotSpawn" type="Node2D" parent="CharacterBody2D/Weapon"]
position = Vector2(46, 10)
[node name="AudioStreamPlayer2D" type="AudioStreamPlayer2D" parent="CharacterBody2D/Weapon"]
stream = SubResource("AudioStreamRandomizer_3kq13")
volume_db = -2.59

19
scenes/shot.tscn Normal file
View file

@ -0,0 +1,19 @@
[gd_scene load_steps=4 format=3 uid="uid://b2ly0rd4mua6i"]
[ext_resource type="Script" uid="uid://8y2wqva60nom" path="res://scripts/projectile.gd" id="1_cupcd"]
[ext_resource type="Texture2D" uid="uid://dau0wpg7gfg3y" path="res://sprites/pew.png" id="2_r6d4o"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ctx4p"]
size = Vector2(12, 1)
[node name="Projectile" type="Area2D"]
input_pickable = false
script = ExtResource("1_cupcd")
metadata/_custom_type_script = "uid://8y2wqva60nom"
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(3, 0)
shape = SubResource("RectangleShape2D_ctx4p")
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("2_r6d4o")

View file

@ -59,7 +59,8 @@ func _physics_process(delta: float) -> void:
rotation = tilt
func shoot(index):
if weapons.size() > index:
#print_debug("trying to shoot weapon ", index, " out of ", )
if index < weapons.size():
weapons[index].shoot()
func deal_damage(amount):

View file

@ -5,7 +5,7 @@ extends FlyerController
func _physics_process(delta: float) -> void:
if not Engine.is_editor_hint():
controlled_flyer.directionInput = Vector2(Input.get_axis("plane_left","plane_right"), Input.get_axis("plane_up","plane_down"))
if InputMap.has_action("shoot_primary") and Input.is_action_just_pressed("shoot_primary"):
controlled_flyer.shoot(1)
if InputMap.has_action("shoot_secondary") and Input.is_action_just_pressed("shoot_secondary"):
controlled_flyer.shoot(2)
if InputMap.has_action("shoot_primary") and Input.is_action_pressed("shoot_primary"):
controlled_flyer.shoot(0)
if InputMap.has_action("shoot_secondary") and Input.is_action_pressed("shoot_secondary"):
controlled_flyer.shoot(1)

View file

@ -1,5 +1,5 @@
class_name Projectile
extends CharacterBody2D
extends Area2D
# constants
@export var speed = 10
@ -8,11 +8,13 @@ extends CharacterBody2D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
body_entered.connect(_on_projectile_body_entered)
func _physics_process(delta: float) -> void:
var target = move_and_collide(velocity * delta).get_collider()
position += transform.x * speed * delta
func _on_projectile_body_entered(target):
if target is Flyer && friendly != target.friendly:
target.deal_damage(damage)
queue_free()

View file

@ -1,13 +1,17 @@
@tool
class_name Weapon
extends Node2D
# constants
@export var max_ammo = 1
## Number of seconds until the weapon can be used again.
@export var cooldown = 1
@export var shot: Projectile
@export var cooldown = 0.1
@export var shot: PackedScene:
set(new_shot):
shot = new_shot
update_configuration_warnings()
@export var shot_spawn: Node2D
@export var audio: AudioStreamRandomizer
@export var audio: AudioStreamPlayer2D
# gameplay variables
var ammo
@ -15,20 +19,52 @@ var timer = 0.0
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
ammo = max_ammo
if !Engine.is_editor_hint():
ammo = max_ammo
else:
child_entered_tree.connect(_on_child_entered_tree)
child_exiting_tree.connect(_on_child_exited_tree)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
timer += delta
if !Engine.is_editor_hint():
timer += delta
func _get_configuration_warnings():
var warnings = []
if shot == null:
warnings.append("Please select a projectile scene for the shot variable.")
if shot_spawn == null:
warnings.append("Please add a Node2D as the muzzle/projectile spawning location for the shot_spawn variable.")
if audio == null:
warnings.append("Please add an AudioStreamPlayer2D which plays the weapon's audio.")
return warnings
func _on_child_entered_tree(node):
print_debug("saw a child entering tree: ", node)
if node.get_class() == "Node2D":
shot_spawn = node
if node is AudioStreamPlayer2D:
audio = node
update_configuration_warnings()
func _on_child_exited_tree(node):
if node == shot_spawn:
shot_spawn = null
if node == audio:
audio = null
update_configuration_warnings()
# Creates and returns an instance of the weapon's projectile.
func shoot() -> Projectile:
print_debug("trying to shoot, timer at ", timer, " out of ", cooldown, " ammo at ", ammo, " out of ", max_ammo)
if timer > cooldown && ammo > 0:
timer = 0
var new_shot = shot.instantiate()
owner.add_child(new_shot)
new_shot.transform = shot_spawn.transform
new_shot.transform = shot_spawn.global_transform
ammo -= 1
audio.play()
return new_shot

BIN
sprites/pew.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 133 B

34
sprites/pew.png.import Normal file
View file

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