added weapon holstering

This commit is contained in:
derek
2025-01-15 12:40:22 -06:00
parent b63aa7db37
commit 0a4e4eefb3
9 changed files with 105 additions and 49 deletions

View File

@@ -134,16 +134,17 @@ func gun_spawn(index):
current_gun_index = index
var instance_gun = held_guns[index].instantiate()
instance_gun.global_transform.origin = player.weapon_spawner.position
player.gun = instance_gun
player.def_weapon_holder_pos = player.weapon_holder.position
player.ammo = player.gun.max_ammo
player.ammo_reserve = player.gun.max_ammo * player.gun.start_mags
#player.gun_fire_pitch_starting = player.gun.audio_fire.pitch_scale
instance_gun.gun_index = index
instance_gun.anim_player.play("swap_in")
player.weapon_holder.add_child(instance_gun)
if held_guns != []:
var instance_gun = held_guns[index].instantiate()
instance_gun.global_transform.origin = player.weapon_spawner.position
player.gun = instance_gun
player.def_weapon_holder_pos = player.weapon_holder.position
player.ammo = player.gun.max_ammo
player.ammo_reserve = player.gun.max_ammo * player.gun.start_mags
#player.gun_fire_pitch_starting = player.gun.audio_fire.pitch_scale
instance_gun.gun_index = index
instance_gun.anim_player.play("swap_in")
player.weapon_holder.add_child(instance_gun)
func enemy_count():
var sum = 0

4
scripts/ammo_resource.gd Normal file
View File

@@ -0,0 +1,4 @@
extends Resource
class_name ammo_stats
@export_enum("Light", "Medium", "Heavy", "Shotgun", "Rocket") var ammo_type: int

View File

@@ -19,9 +19,10 @@ var crosshair_target
@onready var stamina_bar: TextureProgressBar = $StaminaBar
@onready var stamina_bar_2: ProgressBar = $StaminaBar2
@onready var health_bar: ProgressBar = $HealthBar
@onready var gun_name: Label = $"MarginContainer/VBoxContainer/Gun Name"
@onready var ammo_current: Label = $MarginContainer/VBoxContainer/HBoxContainer/AmmoCurrent
@onready var ammo_reserve: Label = $MarginContainer/VBoxContainer/HBoxContainer/AmmoReserve
@onready var gun_name: Label = $"GunInfo/VBoxContainer/Gun Name"
@onready var ammo_current: Label = $GunInfo/VBoxContainer/HBoxContainer/AmmoCurrent
@onready var ammo_reserve: Label = $GunInfo/VBoxContainer/HBoxContainer/AmmoReserve
@onready var gun_info: MarginContainer = $GunInfo
@onready var money: Label = $Money
@onready var crosshair: TextureRect = $Crosshair
@onready var crosshair_center: TextureRect = $CrosshairCenter
@@ -69,6 +70,13 @@ func _process(delta: float) -> void:
crosshair_center.position = Vector2(viewportWidth/2,viewportHeight/2) + (crosshair_center.scale * crosshair_center.size/-2)
stamina_bar.scale = (crosshair.size/CROSSHAIR_SIZE) * Vector2(.185,.185)
stamina_bar.position = Vector2(viewportWidth/2,viewportHeight/2) + (stamina_bar.scale * stamina_bar.size/-2)
#hide crosshair and ammo when no gun
if player.gun == null:
crosshair.visible = false
gun_info.visible = false
else:
crosshair.visible = true
gun_info.visible = true
#HEALTH
health_bar.value = level_control.health
@@ -135,11 +143,12 @@ func _process(delta: float) -> void:
change_color(crosshair,FULL_WHITE,10,delta)
if player.ads:
if player.gun.ads:
fade_in_out(crosshair,1,false,5,delta)
fade_in_out(current_stam_bar,.001,true,5,delta)
else:
fade_in_out(crosshair,1,true,5,delta)
if player.gun:
if player.gun.ads:
fade_in_out(crosshair,1,false,5,delta)
fade_in_out(current_stam_bar,.001,true,5,delta)
else:
fade_in_out(crosshair,1,true,5,delta)
else:
fade_in_out(crosshair,1,true,5,delta)

View File

@@ -56,7 +56,12 @@ var start_sensitivity
@export var BASE_FOV : float = 80
@export var FOV_CHANGE = 1.5
## GUNS AND AMMO
var gun : Node
var holstered_gun_id : int
var gun_is_holstered = false
@onready var dead_player : Resource = load("res://assets/dead_cam.tscn")
@onready var pause_menu: Control = $Head/Recoil/Camera3D/PauseMenu
@onready var gun_ray = $Head/Recoil/Camera3D/GunRay
@@ -371,22 +376,30 @@ func _physics_process(delta):
gun_folded = false
#Weapon Swap Up
if Input.is_action_just_pressed("scroll_up") and !gun.anim_player.is_playing():
if held_item == null:
if level_control.held_guns.size() > 1:
gun.anim_player.play("swap_out")
level_control.gun_spawn(level_control.current_gun_index + 1)
if Input.is_action_just_pressed("scroll_up"):
if gun != null:
if !gun.anim_player.is_playing():
if held_item == null:
if level_control.held_guns.size() > 1:
weapon_select(level_control.current_gun_index + 1)
else:
moveable_holder.rotation.y += deg_to_rad(45)
else:
moveable_holder.rotation.y += deg_to_rad(45)
if gun_is_holstered:
holster_gun(false)
#Weapon Swap Down
if Input.is_action_just_pressed("scroll_down") and !gun.anim_player.is_playing():
if held_item == null:
if level_control.held_guns.size() > 1:
gun.anim_player.play("swap_out")
level_control.gun_spawn(level_control.current_gun_index - 1)
if Input.is_action_just_pressed("scroll_down"):
if gun != null:
if !gun.anim_player.is_playing():
if held_item == null:
if level_control.held_guns.size() > 1:
weapon_select(level_control.current_gun_index - 1)
else:
moveable_holder.rotation.y -= deg_to_rad(45)
else:
moveable_holder.rotation.y -= deg_to_rad(45)
if gun_is_holstered:
holster_gun(false)
for i in range(1,9):
if Input.is_action_just_pressed("numb_" + str(i)):
@@ -394,6 +407,12 @@ func _physics_process(delta):
if !gun.anim_player.is_playing():
if i-1 != level_control.current_gun_index and i <= level_control.held_guns.size():
weapon_select((i - 1))
if Input.is_action_just_pressed("holster"):
if gun_is_holstered:
holster_gun(false)
else:
holster_gun(true)
if Input.is_action_just_pressed("weapon_select"):
weapon_select_menu.open()
@@ -476,6 +495,17 @@ func joypad_look(delta):
head.rotate_x(-yAxis * JOYSTICK_SENSITIVITY * 1)
head.rotation.x = clamp(head.rotation.x, deg_to_rad(-90), deg_to_rad(85))
func holster_gun(holster):
if holster:
gun_is_holstered = true
if gun != null:
holstered_gun_id = level_control.current_gun_index
gun.anim_player.play("swap_out")
else:
gun_is_holstered = false
if holstered_gun_id != null:
weapon_select(holstered_gun_id)
func flashlight_toggle():
if flashlight_on:
flashlight.light_energy = FLASHLIGHT_BRIGHTNESS
@@ -554,8 +584,9 @@ func weapon_sway(delta):
weapon_holder.rotation.x = lerp(weapon_holder.rotation.x, (mouse_input.y + joy_input.y) * weapon_sway_amount, 5 * delta)
weapon_holder.rotation.y = lerp(weapon_holder.rotation.y, (mouse_input.x + joy_input.x) * weapon_sway_amount, 5 * delta)
else:
if gun.ads == true:
weapon_holder.rotation = Vector3.ZERO
if gun:
if gun.ads == true:
weapon_holder.rotation = Vector3.ZERO
func weapon_bob(vel : float, delta):
if weapon_holder:
@@ -570,8 +601,10 @@ func weapon_bob(vel : float, delta):
weapon_holder.position.x = lerp(weapon_holder.position.x, def_weapon_holder_pos.x, .1 * delta)
func weapon_select(gun_id):
gun.anim_player.play("swap_out")
level_control.gun_spawn(gun_id)
if gun != null:
gun.anim_player.play("swap_out")
if gun_id != null:
level_control.gun_spawn(gun_id)
func enemy_hit():
var hitmarker_spawn = hitmarker.instantiate()
@@ -594,6 +627,7 @@ func toggle_hud(hud_on):
hud.visible = hud_on
func grab_moveable(body):
holster_gun(true)
held_item = body
#cache rigidbody settings
held_item_linear_damp_cache = body.linear_damp
@@ -626,6 +660,7 @@ func hold_item(_delta):
release_moveable()
func release_moveable():
holster_gun(false)
if held_item != null:
held_item.gravity_scale = held_item_gravity_cache
held_item.linear_damp = held_item_linear_damp_cache