Added spread for weapons

This commit is contained in:
MrSisich 2025-02-04 18:35:44 +03:00
parent 3e6eab08fd
commit b4da54c2ac
6 changed files with 66 additions and 10 deletions

View File

@ -26,6 +26,8 @@ window/size/viewport_width=1920
window/size/viewport_height=1964
window/size/mode=4
window/size/borderless=true
window/stretch/mode="canvas_items"
window/stretch/aspect="expand"
window/vsync/vsync_mode=2
[filesystem]

View File

@ -9,12 +9,13 @@ class_name WeaponController extends Node3D
@export var cooldown: float = 0
# bullet speed in m/s
@export var bullet_speed: int = 0
@export var bullet_spread_script: GDScript
@export var bullet_spread_script: Resource
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
if bullet_spread_script:
bullet_spread_script = bullet_spread_script.new()
bullet_spread_script.reset_spread()
@onready var bullet_trace_distance: Node3D = $BulletTraceDistance
@onready var bullet_trail_end: Node3D = $BulletTrailEnd
@ -22,15 +23,21 @@ func shoot() -> Error:
var bullet_start_node: Node3D = bullet_trace_distance.find_child("Start")
var bullet_end_node: Node3D = bullet_trace_distance.find_child("End")
if bullet_start_node and bullet_end_node:
var direction: Vector3 = bullet_end_node.position
if bullet_spread_script:
var spread_offset: Vector3 = bullet_spread_script.get_spread_offset()
direction += spread_offset
direction = direction.normalized()
var path := "res://scenes/weapon/misc/bullet_trail_generic.tscn"
if not ResourceLoader.exists(path):
return ERR_DOES_NOT_EXIST
var scene: PackedScene = ResourceLoader.load(path)
if not scene.can_instantiate():
return ERR_CANT_OPEN
var bullet_distance: float = 100
var bullet_end_position: Vector3 = bullet_start_node.position + direction * bullet_distance
var node: MeshInstance3D = scene.instantiate()
node.init(bullet_start_node.position, bullet_end_node.position)
node.init(bullet_start_node.position, bullet_end_position)
#var root := get_tree().get_root()
bullet_start_node.add_child(node)
return OK
@ -42,3 +49,7 @@ func shoot() -> Error:
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func reset_spread() -> void:
if bullet_spread_script:
bullet_spread_script.reset_spread()

View File

@ -0,0 +1,35 @@
class_name BulletSpread extends Resource
@export var max_spread: float = 5.0
@export var spread_increase_per_shot: float = 0.5
var current_spread: float = 0.0
var shot_count: int = 0
var spread_y : float = 0
var spread_x: float = 0
var last_y_spread: float = 0 # remember y cord for 9th shot
func reset_spread() -> void:
current_spread = 0.0
shot_count = 0
# To get offset for current shot
func get_spread_offset() -> Vector3:
# Increasing spread after each shot
current_spread = min(current_spread + spread_increase_per_shot, max_spread)
shot_count += 1
# If its a 1 shot
if shot_count == 1:
return Vector3.ZERO
if shot_count < 10:
spread_x = randf_range(-current_spread, current_spread) * (shot_count - 1)
spread_y = current_spread * (shot_count - 1)
if shot_count == 9: last_y_spread = spread_y
else:
spread_x = randf_range(-current_spread, current_spread) * 10
spread_y = randf_range(last_y_spread - 10, last_y_spread + 10)
return Vector3(spread_x, spread_y, 0)

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=55 format=4 uid="uid://dtvo21mk1webd"]
[gd_scene load_steps=56 format=4 uid="uid://dtvo21mk1webd"]
[ext_resource type="PackedScene" uid="uid://dab7jttp7ywfh" path="res://scenes/weapon/guns/ak/gun.tscn" id="1_aaafm"]
[ext_resource type="Script" path="res://scenes/weapon/generic_weapon_controller.gd" id="1_h1xyo"]
[ext_resource type="Script" path="res://scenes/weapon/guns/ak/bullet_spread.gd" id="2_4o3my"]
[ext_resource type="PackedScene" uid="uid://bjyltbtx45cqs" path="res://scenes/weapon/misc/bullet_trace_distance.tscn" id="3_5ff4y"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_tg27p"]
@ -555,8 +556,9 @@ shadow_mesh = SubResource("ArrayMesh_he3sr")
[node name="WithHands" type="Node3D"]
script = ExtResource("1_h1xyo")
damage = 50
cooldown = 0.2
cooldown = 0.1
bullet_speed = 200
bullet_spread_script = ExtResource("2_4o3my")
[node name="Gun" parent="." instance=ExtResource("1_aaafm")]

View File

@ -1,7 +1,7 @@
extends MeshInstance3D
var alpha = 1.0
func init(pos1, pos2):
func init(pos1: Vector3, pos2: Vector3):
var draw_mesh := ImmediateMesh.new()
mesh = draw_mesh
draw_mesh.surface_begin(Mesh.PRIMITIVE_LINES, material_override)

View File

@ -99,6 +99,7 @@ func _add_first_view_model() -> Error :
# Define a format string with placeholder '%s'
var path_tmpl := "res://scenes/weapon/guns/%s/with_hands.tscn"
var path := path_tmpl % current_gun
print(path)
if not ResourceLoader.exists(path):
return ERR_DOES_NOT_EXIST
var scene: PackedScene = ResourceLoader.load(path)
@ -168,7 +169,7 @@ var jump_vel: Vector3 # Jumping velocity
@export_range(0.1, 3.0, 0.1) var jump_height: float = 1 # m
@export_range(0.1, 3.0, 0.1, "or_greater") var camera_sens: float = 1
var is_shooting: bool = false
func _unhandled_input(event: InputEvent) -> void:
if _is_current_player():
if event is InputEventMouseMotion:
@ -176,7 +177,10 @@ func _unhandled_input(event: InputEvent) -> void:
if mouse_captured: _rotate_camera()
#if Input.is_action_just_pressed("jump"): jumping = true
if Input.is_action_just_pressed("exit"): get_tree().quit()
if Input.is_action_pressed("shot"): _shoot()
if Input.is_action_pressed("shot"): is_shooting = true
if Input.is_action_just_released("shot"):
is_shooting = false
gun_with_hands.reset_spread()
#if str($"..".name).to_int() == multiplayer.get_unique_id():
#if Input.is_action_just_pressed("shot"): $UpperTorso/ViewModelCamera.shot()
#if Input.is_action_just_pressed("reload"): $Body/UpperTorso/CameraMount/Camera.reload()
@ -201,6 +205,8 @@ func _process(delta: float) -> void:
model.reparent(get_tree().get_root())
model.die()
queue_free()
if is_shooting == true:
_shoot()
func _physics_process(delta: float) -> void: