143 lines
3.7 KiB
GDScript
143 lines
3.7 KiB
GDScript
extends Node3D
|
|
|
|
const CONFIG_PATH = "user://weapons.scoom"
|
|
const REVOLVER = 0
|
|
|
|
|
|
const REST = 0
|
|
const BOB = 1
|
|
|
|
var state = 0
|
|
@onready var player_root = get_node("../../../../PlayerQ3") as CharacterBody3D
|
|
@onready var raycast = get_node("../Camera3D/AimCast") as RayCast3D
|
|
|
|
var weapons = []
|
|
|
|
var current_weapon = 0
|
|
|
|
var file_data = {
|
|
REVOLVER:
|
|
{
|
|
"HAND_POS": Vector3(0.4,-0.45,-1),
|
|
"HAND_ROT": Vector3(0,PI,0),
|
|
"MODEL": "Revolver",
|
|
"MAX_CLIP": 5,
|
|
"DAMAGE": 25,
|
|
"MOMENTUM": Vector2(0.3, -0.25),
|
|
"ANGULAR_MOMENTUM": Vector3(-60, 7, 10),
|
|
"RECOIL_COOLDOWN": 0.35,
|
|
"RPM": 120,
|
|
"DRAW_POS": Vector3(0,-0.5,-0.3),
|
|
"DRAW_ROT": Vector3(1.3, 0, 0),
|
|
"HITSCAN": true
|
|
}
|
|
}
|
|
|
|
var mouse_mov : Vector3
|
|
var sway_lerp = 5
|
|
|
|
func _ready():
|
|
save_data(file_data)
|
|
load_data()
|
|
|
|
for i in range(0, file_data.size()):
|
|
var root = Weapon.new()
|
|
root.name = str(i)
|
|
var resource = "res://scenes/weapons/" + file_data[i].MODEL + ".tscn"
|
|
var weapon_model_resource = load(resource)
|
|
var weapon_model = weapon_model_resource.instantiate()
|
|
|
|
#Set Weapon nodes
|
|
root.add_child(weapon_model)
|
|
root.position = file_data[i].HAND_POS
|
|
root.rotation = file_data[i].HAND_ROT
|
|
root.hide()
|
|
add_child(root)
|
|
|
|
#Set Weapon Data
|
|
root.MAX_CLIP = file_data[i].MAX_CLIP
|
|
root.clip = root.MAX_CLIP
|
|
root.DAMAGE = file_data[i].DAMAGE
|
|
root.MOMENTUM = file_data[i].MOMENTUM
|
|
root.ANGULAR_MOMENTUM = file_data[i].ANGULAR_MOMENTUM
|
|
root.RECOIL_COOLDOWN = file_data[i].RECOIL_COOLDOWN
|
|
root.RPM = file_data[i].RPM
|
|
root.MODEL = root.get_node(file_data[i].MODEL)
|
|
root.DRAW_POS = file_data[i].DRAW_POS
|
|
root.DRAW_ROT = file_data[i].DRAW_ROT
|
|
root.HITSCAN = file_data[i].HITSCAN
|
|
if(not root.HITSCAN):
|
|
print("Projectile stats")
|
|
|
|
weapons.append(root)
|
|
weapons[current_weapon].init()
|
|
raycast.target_position.z = -weapons[current_weapon].RAY_LEN
|
|
|
|
|
|
func _input(event):
|
|
if event is InputEventMouseMotion:
|
|
mouse_mov.y = clampf(-event.relative.x, -0.1, 0.1)
|
|
mouse_mov.x = clampf(event.relative.y, -0.1, 0.1)
|
|
|
|
|
|
func _unhandled_input(_event):
|
|
if weapons.size() > 1 && Input.is_action_just_released("wheel_up"):
|
|
ChangeWeapon((current_weapon + 1) % weapons.size())
|
|
if weapons.size() > 1 && Input.is_action_just_released("wheel_down"):
|
|
ChangeWeapon((current_weapon - 1) % weapons.size())
|
|
|
|
if Input.is_action_just_pressed("reload"):
|
|
weapons[current_weapon].Reload()
|
|
|
|
func ChangeWeapon(i):
|
|
weapons[current_weapon].hide()
|
|
current_weapon = (i)
|
|
weapons[current_weapon].init()
|
|
raycast.target_position.z = -weapons[current_weapon].RAY_LEN
|
|
|
|
var time = 0
|
|
func _process(delta):
|
|
if Input.is_action_pressed("shoot") && weapons[current_weapon].Shoot():
|
|
if(weapons[current_weapon].HITSCAN):
|
|
HitScan()
|
|
else:
|
|
Projectile()
|
|
|
|
if Input.is_action_just_released("shoot"):
|
|
weapons[current_weapon].Release()
|
|
|
|
|
|
#SWAY
|
|
if mouse_mov != null && mouse_mov.length() > 0:
|
|
weapons[current_weapon].MODEL.rotation = weapons[current_weapon].MODEL.rotation.lerp(mouse_mov, sway_lerp * delta)
|
|
mouse_mov = Vector3.ZERO
|
|
else:
|
|
weapons[current_weapon].MODEL.rotation = weapons[current_weapon].MODEL.rotation.lerp(Vector3.ZERO, sway_lerp * delta)
|
|
|
|
#BOB
|
|
if player_root.velocity.length() > 0:
|
|
time += delta
|
|
var bobOscillate = sin(time * 4 * (2 * PI)) * 0.01
|
|
position.y = bobOscillate
|
|
bobOscillate = sin(time * 4 * (PI) + PI/4) * 0.015
|
|
position.x = bobOscillate
|
|
|
|
|
|
func HitScan():
|
|
if(raycast.is_colliding()):
|
|
var target = raycast.get_collider()
|
|
print("Hit target ", target.name)
|
|
|
|
func Projectile():
|
|
pass
|
|
|
|
func save_data(data):
|
|
var file = FileAccess.open(CONFIG_PATH, FileAccess.WRITE)
|
|
file.store_var(data)
|
|
|
|
func load_data():
|
|
var file = FileAccess.open(CONFIG_PATH, FileAccess.READ)
|
|
var data = file.get_var()
|
|
file_data = data
|
|
|