149 lines
4.6 KiB
GDScript
149 lines
4.6 KiB
GDScript
extends Node3D
|
|
|
|
var start_position
|
|
var start_rotation
|
|
var random_spread_start
|
|
var cycle_count_start
|
|
var cycle_count
|
|
|
|
@export_group("Gun Feel")
|
|
@export var gun_name : String
|
|
@export var gun_icon : Texture2D
|
|
@export_enum("Auto", "Single", "Burst") var fire_mode: int
|
|
@export var fov_zoom_amt = .98
|
|
@export var ads : bool = false
|
|
@export var recoil_amount : Vector3 = Vector3(.2,0,0)
|
|
@export var spread : Vector3 = Vector3(1,1,1)
|
|
@export var kick_amount : float = 5
|
|
@export var max_ammo = 15
|
|
@export var start_mags = 3
|
|
@export var pellets_per_shot = 12
|
|
@export var bullet_damage = 1
|
|
@export var bullet_force_mod = 5
|
|
@export var blast_power = 50.0
|
|
@export var bullet_speed : float = 150
|
|
@export var bullet_drop = .3
|
|
@export var random_spread_amt = 1.0
|
|
@export var fire_pitch_scale_amt = .2
|
|
@export_group("Gun Assets")
|
|
@export_subgroup("Main Assets")
|
|
@export var bullet : Resource
|
|
@export_subgroup("Raycast Nodes")
|
|
@export var anim_player : Node
|
|
@export var barrel_raycast : Node
|
|
@export_subgroup("Audio Clips")
|
|
@export var audio_fire : Node
|
|
@export var audio_empty : Node
|
|
@export var audio_reload : Node
|
|
|
|
@onready var smoke: GPUParticles3D = $BlunderbusHandle/Blunderbus/Smoke
|
|
@onready var player = get_tree().current_scene.player
|
|
@onready var level_control = get_tree().current_scene
|
|
@onready var ammo_current
|
|
@onready var smoke_timer: Timer = $SmokeTimer
|
|
@onready var fire_smoke: GPUParticles3D = $BlunderbusHandle/Blunderbus/fire_smoke
|
|
|
|
var rng = RandomNumberGenerator.new()
|
|
var gun_index
|
|
#var ammo_current
|
|
var ammo_reserve
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
#smoke.emitting = false
|
|
start_position = self.position
|
|
start_rotation = self.rotation
|
|
random_spread_start = random_spread_amt
|
|
|
|
#ammo_current = level_control.ammo_current[gun_index]
|
|
#ammo_reserve = level_control.ammo_reserve[gun_index]
|
|
|
|
|
|
if fire_mode == 0:
|
|
cycle_count = 1
|
|
cycle_count_start = 1
|
|
elif fire_mode == 1:
|
|
cycle_count = 1
|
|
cycle_count_start = 1
|
|
elif fire_mode == 2:
|
|
cycle_count = 3
|
|
cycle_count_start = 3
|
|
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(_delta):
|
|
pass
|
|
|
|
|
|
func reload_finished():
|
|
if level_control.ammo_reserve[gun_index] >= max_ammo:
|
|
level_control.ammo_current[gun_index] += max_ammo
|
|
level_control.ammo_reserve[gun_index] -= max_ammo
|
|
|
|
else:
|
|
level_control.ammo_current[gun_index] += level_control.ammo_reserve[gun_index]
|
|
level_control.ammo_reserve[gun_index] -= level_control.ammo_reserve[gun_index]
|
|
|
|
|
|
func shoot(delta):
|
|
if level_control.ammo_current[gun_index] > 0:
|
|
if !anim_player.is_playing():
|
|
anim_player.play("shoot")
|
|
elif !anim_player.is_playing() and cycle_count != 0:
|
|
anim_player.play("empty")
|
|
audio_empty.play()
|
|
|
|
func fire():
|
|
level_control.ammo_current[gun_index] -= 1
|
|
audio_fire.pitch_scale = 1 + rng.randf_range(-fire_pitch_scale_amt,fire_pitch_scale_amt)
|
|
audio_fire.play()
|
|
pellet_spawn()
|
|
vibration()
|
|
fire_smoke.restart()
|
|
fire_smoke.emitting = true
|
|
smoke_timer.start()
|
|
player.recoil.add_recoil(Vector3(0,recoil_amount.y,recoil_amount.z),10,10)
|
|
player.recoil.add_gun_recoil(recoil_amount.x)
|
|
player.velocity += player.bullet_ray.global_basis * Vector3(0,0, kick_amount)
|
|
SignalBus.emit_signal("shot_fired")
|
|
|
|
func reload():
|
|
if level_control.ammo_current[gun_index] < max_ammo and player.gun.anim_player.get_current_animation() != "reload" and level_control.ammo_reserve[gun_index] > 0:
|
|
#player.reloading = true
|
|
anim_player.play("reload")
|
|
audio_reload.play()
|
|
if anim_player.is_playing() and anim_player.current_animation == "reload":
|
|
if level_control.ammo_current[gun_index] == 0:
|
|
level_control.ammo_current[gun_index] = 0
|
|
else:
|
|
level_control.ammo_current[gun_index] = 1
|
|
|
|
func pellet_spawn():
|
|
var pellets_remaining = pellets_per_shot
|
|
while pellets_remaining > 0:
|
|
var lv_x = rng.randf_range(-spread.x,spread.x)
|
|
var lv_y = rng.randf_range(-spread.y,spread.y)
|
|
# instance bullet
|
|
var instance_bullet = bullet.instantiate()
|
|
instance_bullet.position = player.bullet_ray.global_position
|
|
instance_bullet.transform.basis = player.bullet_ray.global_transform.basis
|
|
instance_bullet.linear_velocity += instance_bullet.transform.basis * Vector3(lv_x, lv_y, -bullet_speed) + player.velocity
|
|
instance_bullet.bullet_damage = bullet_damage
|
|
instance_bullet.bullet_force_mod = bullet_force_mod
|
|
instance_bullet.player_position = player.global_position
|
|
get_tree().get_root().add_child(instance_bullet)
|
|
pellets_remaining -= 1
|
|
|
|
func swapped_out():
|
|
queue_free()
|
|
|
|
|
|
func _on_smoke_timer_timeout() -> void:
|
|
smoke.restart()
|
|
smoke.emitting = true
|
|
smoke_timer.stop()
|
|
|
|
func vibration():
|
|
Input.start_joy_vibration(0,.5,.9,.2)
|