built single-resource based script for weapons, most are broken right now
This commit is contained in:
154
scripts/weapon_uberscript.gd
Normal file
154
scripts/weapon_uberscript.gd
Normal file
@@ -0,0 +1,154 @@
|
||||
extends Node3D
|
||||
|
||||
|
||||
@export var weapon_info : weapon_resource
|
||||
@export var casing_ejector : Node
|
||||
@export var mag_ejector : Node
|
||||
@export var barrel_ray : Node
|
||||
@export var anim_player : AnimationPlayer
|
||||
@export var audio_fire : AudioStreamPlayer3D
|
||||
@export var audio_empty : AudioStreamPlayer3D
|
||||
@export var audio_reload : AudioStreamPlayer3D
|
||||
@onready var player = get_tree().current_scene.player
|
||||
|
||||
var start_position
|
||||
var start_rotation
|
||||
var cycle_count_start
|
||||
var cycle_count
|
||||
var rng = RandomNumberGenerator.new()
|
||||
var gun_index
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
|
||||
if weapon_info.fire_mode == 0:
|
||||
cycle_count = 1
|
||||
cycle_count_start = 1
|
||||
elif weapon_info.fire_mode == 1:
|
||||
cycle_count = 1
|
||||
cycle_count_start = 1
|
||||
elif weapon_info.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 GameGlobals.ammo_reserve[str(weapon_info.bullet.ammo_type)] >= weapon_info.max_ammo:
|
||||
GameGlobals.gun_ammo[weapon_info.gun_name] += weapon_info.max_ammo
|
||||
GameGlobals.ammo_reserve[str(weapon_info.bullet.ammo_type)] -= weapon_info.max_ammo
|
||||
else:
|
||||
GameGlobals.gun_ammo[weapon_info.gun_name] += GameGlobals.ammo_reserve[str(weapon_info.bullet.ammo_type)]
|
||||
GameGlobals.ammo_reserve[str(weapon_info.bullet.ammo_type)] -= GameGlobals.ammo_reserve[str(weapon_info.bullet.ammo_type)]
|
||||
|
||||
func shoot(delta):
|
||||
if GameGlobals.gun_ammo[weapon_info.gun_name] > 0 and cycle_count > 0:
|
||||
if !anim_player.is_playing():
|
||||
GameGlobals.gun_ammo[weapon_info.gun_name] -= 1
|
||||
#audio and anims
|
||||
audio_fire.play()
|
||||
anim_player.play("shoot")
|
||||
vibration()
|
||||
bullet_fire()
|
||||
spawn_casing()
|
||||
if weapon_info.smoke_enabled:
|
||||
spawn_muzzle_smoke()
|
||||
player.recoil.add_recoil(Vector3(0,weapon_info.recoil_amount.y,weapon_info.recoil_amount.z),10,10)
|
||||
player.recoil.add_gun_recoil(weapon_info.recoil_amount.x)
|
||||
#player.velocity += player.bullet_ray.global_basis * Vector3(0,0, kick_amount)
|
||||
SignalBus.emit_signal("shot_fired")
|
||||
if weapon_info.fire_mode != 0:
|
||||
cycle_count -= 1
|
||||
|
||||
elif !anim_player.is_playing() and cycle_count != 0:
|
||||
anim_player.play("empty")
|
||||
audio_empty.play()
|
||||
|
||||
func reload():
|
||||
if GameGlobals.gun_ammo[weapon_info.gun_name] < weapon_info.max_ammo and player.gun.anim_player.get_current_animation() != "reload" and GameGlobals.ammo_reserve[str(weapon_info.bullet.ammo_type)] > 0:
|
||||
#player.reloading = true
|
||||
anim_player.play("reload")
|
||||
audio_reload.play()
|
||||
if anim_player.is_playing() and anim_player.current_animation == "reload":
|
||||
if GameGlobals.gun_ammo[weapon_info.gun_name] == 0:
|
||||
GameGlobals.gun_ammo[weapon_info.gun_name] = 0
|
||||
else:
|
||||
GameGlobals.gun_ammo[weapon_info.gun_name] = 1
|
||||
|
||||
func spawn_mag():
|
||||
var instance_mag =weapon_info.mag.instantiate()
|
||||
instance_mag.position = mag_ejector.global_position
|
||||
instance_mag.transform.basis = mag_ejector.global_transform.basis
|
||||
instance_mag.linear_velocity += transform.basis * Vector3(0, -20, 0) + player.velocity
|
||||
get_tree().get_root().add_child(instance_mag)
|
||||
|
||||
func spawn_casing():
|
||||
# Casing transform
|
||||
var instance_casing = weapon_info.casing.instantiate()
|
||||
instance_casing.position = casing_ejector.global_position
|
||||
instance_casing.transform.basis = casing_ejector.global_transform.basis
|
||||
instance_casing.player_velocity = player.velocity * transform.basis
|
||||
get_tree().get_root().add_child(instance_casing)
|
||||
|
||||
func spawn_revolver_casings():
|
||||
if weapon_info.casing_array.size() > 0:
|
||||
pass
|
||||
#for i in weapon_info.casing_array:
|
||||
#i.visible = false
|
||||
#if weapon_info.casings_chamber_last > 0:
|
||||
#var instance_casing = weapon_info.spawn_casing.instantiate()
|
||||
#instance_casing.position = i.global_position
|
||||
#instance_casing.random_rotation = false
|
||||
#instance_casing.transform.basis = i.global_transform.basis
|
||||
#instance_casing.player_velocity = player.velocity * transform.basis
|
||||
#instance_casing.rotation.x += deg_to_rad(90)
|
||||
#instance_casing.linear_velocity.y = -(8 + rng.randf_range(0,3))
|
||||
#get_tree().get_root().add_child(instance_casing)
|
||||
#weapon_info.casings_chamber_last -= 1
|
||||
|
||||
func shotgun_pellet_spawn():
|
||||
var pellets_remaining = weapon_info.pellets_per_shot
|
||||
while pellets_remaining > 0:
|
||||
var lv_x = rng.randf_range(-weapon_info.shotgun_spread.x,weapon_info.shotgun_spread.x)
|
||||
var lv_y = rng.randf_range(-weapon_info.shotgun_spread.y,weapon_info.shotgun_spread.y)
|
||||
# instance bullet
|
||||
var instance_bullet = weapon_info.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, -weapon_info.bullet_speed) + player.velocity
|
||||
instance_bullet.player_position = player.global_position
|
||||
get_tree().get_root().add_child(instance_bullet)
|
||||
pellets_remaining -= 1
|
||||
|
||||
func spawn_muzzle_smoke():
|
||||
var instance_smoke = weapon_info.muzzle_smoke.instantiate()
|
||||
instance_smoke.global_transform.basis = barrel_ray.global_transform.basis
|
||||
|
||||
add_child(instance_smoke)
|
||||
|
||||
func bullet_fire():
|
||||
var instance_bullet = weapon_info.bullet.asset.instantiate()
|
||||
instance_bullet.position = player.bullet_ray.global_position
|
||||
#shoot bullet from real gun if gun is folded up
|
||||
if player.gun_folded == false:
|
||||
instance_bullet.transform.basis = player.bullet_ray.global_transform.basis
|
||||
else:
|
||||
instance_bullet.transform.basis = barrel_ray.global_transform.basis
|
||||
instance_bullet.bullet_speed = weapon_info.bullet_speed
|
||||
instance_bullet.player_velocity = player.velocity * transform.basis
|
||||
instance_bullet.bullet_damage = weapon_info.bullet_damage
|
||||
instance_bullet.bullet_force_mod = weapon_info.bullet_force_mod
|
||||
instance_bullet.instance_bullethole = weapon_info.bullet.bullet_hole.instantiate()
|
||||
instance_bullet.player_position = player.global_position
|
||||
get_tree().current_scene.add_child(instance_bullet)
|
||||
|
||||
func swapped_out():
|
||||
queue_free()
|
||||
|
||||
func vibration():
|
||||
Input.start_joy_vibration(0,.1,.5,.1)
|
||||
Reference in New Issue
Block a user