2452aba0d7
- WeaponData resource class with configurable weapon properties and pre-configured constants for rifle, pistol, shotgun, and SMG - WeaponDefinitions singleton/static registry with WEAPONS dictionary - WeaponServer node for server-authoritative weapon logic including fire rate cooldowns, ammo tracking, reload state machine, and multi-pellet hit-scan raycasting - Extended WeaponManager with weapon inventory array, weapon switching, per-weapon state persistence, fire_animation_triggered signal
106 lines
3.3 KiB
GDScript
106 lines
3.3 KiB
GDScript
## WeaponData — A resource defining a weapon's static properties.
|
||
##
|
||
## Used as the data contract between weapon_definitions.gd, weapon_server.gd,
|
||
## and weapon_manager.gd. Each weapon type is pre-configured and accessible
|
||
## via the class-level constants (RIFLE, PISTOL, SHOTGUN, SMG).
|
||
##
|
||
## Usage:
|
||
## var data: WeaponData = WeaponData.RIFLE
|
||
## print(data.display_name) # "Assault Rifle"
|
||
##
|
||
extends Resource
|
||
class_name WeaponData
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Properties
|
||
# ---------------------------------------------------------------------------
|
||
|
||
## Unique identifier string (e.g., "rifle", "pistol").
|
||
@export var weapon_id: String = ""
|
||
|
||
## Human-readable weapon name.
|
||
@export var display_name: String = ""
|
||
|
||
## Base damage per projectile/pellet.
|
||
@export var damage: float = 0.0
|
||
|
||
## Rate of fire in rounds per second (Hz).
|
||
@export var fire_rate: float = 0.0
|
||
|
||
## Magazine capacity (max ammo per reload).
|
||
@export var mag_size: int = 0
|
||
|
||
## Reload duration in seconds.
|
||
@export var reload_time: float = 0.0
|
||
|
||
## If true, hold to fire continuously. If false, single-shot per press.
|
||
@export var is_automatic: bool = false
|
||
|
||
## Base weapon spread in degrees (used for accuracy cone).
|
||
@export var spread_degrees: float = 0.0
|
||
|
||
## Effective range in Godot units (metres conceptual).
|
||
@export var range: float = 0.0
|
||
|
||
## Number of pellets/projectiles per shot (1 for most weapons, 8 for shotgun).
|
||
@export var pellets_per_shot: int = 1
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Pre-configured weapon instances (class-level constants)
|
||
# ---------------------------------------------------------------------------
|
||
|
||
## Assault Rifle — 30 dmg, 10 rps, 30 mag, 2.1s reload, automatic, 0.5° spread, 200m range.
|
||
const RIFLE: Resource = pref(
|
||
"rifle", "Assault Rifle",
|
||
30.0, 10.0, 30, 2.1, true, 0.5, 200.0, 1
|
||
)
|
||
|
||
## Pistol — 25 dmg, 4 rps, 12 mag, 1.5s reload, semi-auto, 0.3° spread, 80m range.
|
||
const PISTOL: Resource = pref(
|
||
"pistol", "Pistol",
|
||
25.0, 4.0, 12, 1.5, false, 0.3, 80.0, 1
|
||
)
|
||
|
||
## Shotgun — 8 dmg × 8 pellets, 1 rps, 8 mag, 3.0s reload, semi-auto, 3° spread, 30m range.
|
||
const SHOTGUN: Resource = pref(
|
||
"shotgun", "Shotgun",
|
||
8.0, 1.0, 8, 3.0, false, 3.0, 30.0, 8
|
||
)
|
||
|
||
## SMG — 18 dmg, 12 rps, 25 mag, 1.8s reload, automatic, 1.5° spread, 100m range.
|
||
const SMG: Resource = pref(
|
||
"smg", "Submachine Gun",
|
||
18.0, 12.0, 25, 1.8, true, 1.5, 100.0, 1
|
||
)
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Factory helper
|
||
# ---------------------------------------------------------------------------
|
||
|
||
## Internal helper that constructs a WeaponData instance from raw values.
|
||
## This avoids repeating the constructor boilerplate for every constant.
|
||
static func pref(
|
||
_weapon_id: String,
|
||
_display_name: String,
|
||
_damage: float,
|
||
_fire_rate: float,
|
||
_mag_size: int,
|
||
_reload_time: float,
|
||
_is_automatic: bool,
|
||
_spread_degrees: float,
|
||
_range: float,
|
||
_pellets: int = 1
|
||
) -> WeaponData:
|
||
var w := WeaponData.new()
|
||
w.weapon_id = _weapon_id
|
||
w.display_name = _display_name
|
||
w.damage = _damage
|
||
w.fire_rate = _fire_rate
|
||
w.mag_size = _mag_size
|
||
w.reload_time = _reload_time
|
||
w.is_automatic = _is_automatic
|
||
w.spread_degrees = _spread_degrees
|
||
w.range = _range
|
||
w.pellets_per_shot = _pellets
|
||
return w
|