Implement weapon switching.

pull/42/head
tokc 2021-03-31 11:50:53 +02:00
parent 8a7cacafd0
commit 9d3b70f76f
4 changed files with 167 additions and 11 deletions

View File

@ -0,0 +1,101 @@
[gd_scene load_steps=15 format=2]
[ext_resource path="res://Assets/Weapons/Weapon.gd" type="Script" id=1]
[ext_resource path="res://Assets/Weapons/Handgun/Handgun.gltf" type="PackedScene" id=2]
[ext_resource path="res://Assets/Audio/SFX/Weapons-Handgun-Reload.wav" type="AudioStream" id=3]
[ext_resource path="res://Assets/Audio/SFX/Weapons_Handun_Shot.wav" type="AudioStream" id=4]
[ext_resource path="res://Assets/Audio/SFX/Weapons-Empty.wav" type="AudioStream" id=5]
[ext_resource path="res://Assets/Effects/CircleSoft.svg" type="Texture" id=6]
[sub_resource type="Gradient" id=1]
offsets = PoolRealArray( 0, 0.0511628, 0.0883721, 0.302326, 1 )
colors = PoolColorArray( 0, 0, 0, 1, 2, 2, 2, 1, 0.901961, 0.866667, 0.576471, 1, 0.909804, 0.592157, 0.168627, 1, 0, 0, 0, 1 )
[sub_resource type="GradientTexture" id=2]
gradient = SubResource( 1 )
[sub_resource type="Curve" id=3]
_data = [ Vector2( 0, 0.986364 ), 0.0, -0.872434, 0, 0, Vector2( 0.092437, 0.752585 ), 0.0, 0.0, 0, 0, Vector2( 1, 0.198039 ), -0.85103, 0.0, 0, 0 ]
[sub_resource type="CurveTexture" id=4]
curve = SubResource( 3 )
[sub_resource type="ParticlesMaterial" id=5]
direction = Vector3( 0, 0, -5 )
spread = 15.0
gravity = Vector3( 0, 0, 0 )
initial_velocity = 8.0
initial_velocity_random = 0.75
damping = 100.0
damping_random = 0.5
scale_random = 1.0
scale_curve = SubResource( 4 )
color_ramp = SubResource( 2 )
[sub_resource type="SpatialMaterial" id=6]
flags_transparent = true
flags_unshaded = true
vertex_color_use_as_albedo = true
params_blend_mode = 1
params_billboard_mode = 3
particles_anim_h_frames = 1
particles_anim_v_frames = 1
particles_anim_loop = false
albedo_texture = ExtResource( 6 )
[sub_resource type="QuadMesh" id=7]
material = SubResource( 6 )
size = Vector2( 0.3, 0.3 )
[sub_resource type="SphereMesh" id=8]
radius = 0.05
height = 0.1
[node name="OtherHandgun" type="Spatial"]
script = ExtResource( 1 )
[node name="MuzzleFlash" type="Particles" parent="."]
transform = Transform( -4.37113e-08, 0, 0.999999, 0, 0.999999, 0, -1, 0, -4.37114e-08, -0.3053, 0.124057, 0 )
cast_shadow = 0
emitting = false
amount = 16
lifetime = 0.1
one_shot = true
explosiveness = 1.0
visibility_aabb = AABB( -0.176241, -0.185755, -0.556571, 0.352482, 0.37151, 0.655134 )
process_material = SubResource( 5 )
draw_pass_1 = SubResource( 7 )
[node name="MeshInstance" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.270753, 0.207034, 0 )
mesh = SubResource( 8 )
material/0 = null
[node name="Handgun" parent="." instance=ExtResource( 2 )]
[node name="Muzzle" parent="Handgun" index="0"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.331868, 0.125291, 0 )
[node name="Ejector" parent="Handgun" index="1"]
transform = Transform( -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0, 0, 1, -0.0373496, 0.126382, -0.00737098 )
[node name="Sounds" type="Spatial" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.295478, 0.724833, 0.584699 )
[node name="SoundShoot" type="AudioStreamPlayer3D" parent="Sounds"]
transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, 0, 0 )
stream = ExtResource( 4 )
attenuation_model = 3
unit_db = 3.0
unit_size = 25.0
emission_angle_enabled = true
emission_angle_degrees = 60.0
attenuation_filter_cutoff_hz = 15000.0
[node name="SoundEmpty" type="AudioStreamPlayer3D" parent="Sounds"]
stream = ExtResource( 5 )
[node name="SoundReload" type="AudioStreamPlayer3D" parent="Sounds"]
stream = ExtResource( 3 )
[editable path="Handgun"]

View File

@ -36,6 +36,9 @@ var jetpack_active = false # is the jetpack active?
var jetpack_used = false # Is the jetpack recharging?
var jetpack_fuel = JETPACK_FUEL_MAX # max fuel (in seconds)
onready var weapons = $Camera/Hand/Weapons
onready var active_weapon = weapons.switch_to_weapon(0)
#onready var sfx_foosteps = [$"Sounds/Footstep-Concrete-01",
# $"Sounds/Footstep-Concrete-02",
# $"Sounds/Footstep-Concrete-03",
@ -321,14 +324,14 @@ func spawn():
rotation = Vector3.ZERO
func shoot():
var weapon = find_node("Weapon")
#var weapon = find_node("Weapon")
var remaining_ammo = weapon.shoot($Camera)
var _remaining_ammo = active_weapon.shoot($Camera)
func reload():
var weapon = find_node("Weapon")
#var weapon = find_node("Weapon")
weapon.reload()
active_weapon.reload()
func _input(event):
if is_dead:
@ -360,7 +363,12 @@ func _input(event):
shoot()
if event.is_action_pressed("WeaponReload"):
reload()
if event.is_action_pressed("NextWeapon"):
active_weapon = weapons.next_weapon()
if event.is_action_pressed("PrevWeapon"):
active_weapon = weapons.prev_weapon()
func set_local_player():
set_network_master(get_tree().get_network_unique_id())

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=28 format=2]
[gd_scene load_steps=30 format=2]
[ext_resource path="res://Classes/UI/Health.tscn" type="PackedScene" id=1]
[ext_resource path="res://Classes/UI/Billboard.gd" type="Script" id=2]
@ -8,6 +8,8 @@
[ext_resource path="res://Assets/Weapons/Handgun/Handgun.tscn" type="PackedScene" id=6]
[ext_resource path="res://Assets/UI/HUD.tscn" type="PackedScene" id=7]
[ext_resource path="res://Classes/Player/Player.gd" type="Script" id=8]
[ext_resource path="res://Classes/Player/WeaponsManager.gd" type="Script" id=9]
[ext_resource path="res://Assets/Weapons/Handgun/OtherHandgun.tscn" type="PackedScene" id=10]
[sub_resource type="AnimationNodeAnimation" id=1]
animation = "Idle"
@ -136,7 +138,7 @@ script = ExtResource( 8 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.53592, -0.0651628 )
[node name="Hand" type="Spatial" parent="Camera"]
transform = Transform( -4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0.335, -0.39967, -0.559 )
transform = Transform( -4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0.335, -0.393079, -0.559 )
[node name="WeaponBobAnimationTree" type="AnimationTree" parent="Camera/Hand"]
tree_root = SubResource( 12 )
@ -150,7 +152,12 @@ anims/Jump = SubResource( 15 )
anims/Land = SubResource( 16 )
anims/Walk = SubResource( 17 )
[node name="Weapon" parent="Camera/Hand" instance=ExtResource( 6 )]
[node name="Weapons" type="Spatial" parent="Camera/Hand"]
script = ExtResource( 9 )
[node name="Weapon" parent="Camera/Hand/Weapons" instance=ExtResource( 6 )]
[node name="OtherHandgun" parent="Camera/Hand/Weapons" instance=ExtResource( 10 )]
[node name="Billboard" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.49795, 0 )
@ -240,6 +247,3 @@ autoplay = true
attenuation_filter_cutoff_hz = 15000.0
[node name="HUD" parent="." instance=ExtResource( 7 )]
[editable path="Camera/Hand/Weapon"]
[editable path="Camera/Hand/Weapon/Handgun"]

View File

@ -0,0 +1,43 @@
extends Spatial
# Store available weapons during runtime.
var weapons = []
var current_weapon_index
var active_weapon
func _ready():
# Remove every child node and store them in the weapons array.
for weapon in get_children():
weapon.visible = true
weapons.append(weapon)
remove_child(weapon)
# If we have at least one weapon, activate the first one.
if weapons.size() > 0:
_activate_weapon(0)
func _activate_weapon(index):
current_weapon_index = index
# Deactivate the current weapon.
if active_weapon:
remove_child(active_weapon)
# Activate the requested weapon.
active_weapon = weapons[index]
add_child(active_weapon)
func switch_to_weapon(index):
# Take a modulo here as a quick and easy way to have a valid index.
_activate_weapon(index % weapons.size())
return active_weapon
func next_weapon():
current_weapon_index = (current_weapon_index + 1) % weapons.size()
return switch_to_weapon(current_weapon_index)
func prev_weapon():
current_weapon_index = (current_weapon_index - 1) % weapons.size()
return switch_to_weapon(current_weapon_index)