Weapons no longer reload by spinning the gun 360 (that was the upside-down
flip; nothing detached). New ViewmodelAnim module drives per-style manual-of-
arms sequences on the existing model_root + named arm pivots, with a temporary
magazine/shell/rocket/cell prop that visibly leaves and returns to the gun:
- mag (M4/AK/MP7/DMR): roll gun to present the well, hand rips the mag
down and away, fresh mag up, seat, charge. Gun stays UPRIGHT.
- boltmag (AWP): mag swap + a distinct bolt-cycle rock.
- break (double barrel): hinge open, flick both shells out, drop fresh
shells in, snap shut.
- tube (rocket launcher): tip the tube in, shove a rocket home, shoulder.
- cell (plasma/nail gun): glowing energy cell swapped on the side.
Per-shot viewmodel recoil kick (ViewmodelAnim.apply_kick) scaled by each
weapon's recoil_amplitude, so an AWP slams and an MP7 chatters — replaces the
old flat model behaviour. Weapons expose reload_style; base classes call
play_reload / stop (on unequip) / apply_kick.
Knife: the old barely-visible flick is now a real diagonal slash that
alternates backhand/forehand, whips the blade through screen centre, and
fires the third-person melee arm-swing action (synced to other players).
Hair/materials: specular fully to 0 (even a 2% stepped glint swept as shine
across Taila's hair when the camera moved); rim trimmed to a whisper. Fully
matte cloth/hair now.
Verify: debug/fp_weapon_capture.gd screenshots each weapon's reload phases,
the knife slash, and the fire kick.
Co-Authored-By: Claude Fable 5 <[email protected]>
168 lines
5.6 KiB
GDScript
168 lines
5.6 KiB
GDScript
extends Node3D
|
|
class_name Knife
|
|
|
|
var weapon_name: String = "Knife"
|
|
var base_damage: float = 100.0
|
|
var max_range: float = 3.0
|
|
var fire_rate: float = 0.5
|
|
var fire_cooldown: float = 0.0
|
|
|
|
var player: CharacterBody3D
|
|
var camera: Camera3D
|
|
var model_root: Node3D
|
|
|
|
var auto_switch_slot: int = -1
|
|
var is_swinging: bool = false
|
|
var default_pos: Vector3 = Vector3(0.3, -0.3, -0.945)
|
|
|
|
var fire_sound: AudioStreamPlayer3D
|
|
|
|
func _ready() -> void:
|
|
set_process(true)
|
|
set_process_input(true)
|
|
model_root = Node3D.new()
|
|
add_child(model_root)
|
|
_build_model()
|
|
position = default_pos
|
|
|
|
func _build_model() -> void:
|
|
var model_scene = load("res://assets/weapons/knife/source/Knife.fbx")
|
|
if model_scene:
|
|
var model = model_scene.instantiate()
|
|
model_root.add_child(model)
|
|
model.scale = Vector3(0.125, 0.125, 0.125)
|
|
model.rotation_degrees.y = 270
|
|
for light in model.find_children("*", "Light3D", true, false):
|
|
light.queue_free()
|
|
|
|
var mat = StandardMaterial3D.new()
|
|
mat.albedo_texture = load("res://assets/weapons/knife/textures/Color Palette.png")
|
|
|
|
for child in model.find_children("*", "MeshInstance3D", true, false):
|
|
child.set_surface_override_material(0, mat)
|
|
|
|
# Audio (reusing shotgun reload or something generic for now, ideally a swish)
|
|
fire_sound = AudioStreamPlayer3D.new()
|
|
fire_sound.bus = "SFX"
|
|
fire_sound.stream = AudioManager.stream_for("knife_swing", "res://assets/sounds/knife_swing.wav")
|
|
fire_sound.pitch_scale = 1.2
|
|
fire_sound.volume_db = 0.0
|
|
model_root.add_child(fire_sound)
|
|
|
|
func _process(delta: float) -> void:
|
|
if fire_cooldown > 0.0:
|
|
fire_cooldown -= delta
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if not visible or is_swinging:
|
|
return
|
|
|
|
if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
|
|
return
|
|
|
|
if event.is_action_pressed("fire") and fire_cooldown <= 0.0:
|
|
_swing()
|
|
|
|
func do_quick_melee(prev_slot: int) -> void:
|
|
auto_switch_slot = prev_slot
|
|
if fire_cooldown <= 0.0 and not is_swinging:
|
|
_swing()
|
|
else:
|
|
if auto_switch_slot != -1 and get_parent() and get_parent().has_method("_equip_slot"):
|
|
get_parent()._equip_slot(auto_switch_slot)
|
|
auto_switch_slot = -1
|
|
|
|
var _swing_alt: bool = false # alternate slash direction each attack
|
|
|
|
func _swing() -> void:
|
|
is_swinging = true
|
|
fire_cooldown = fire_rate
|
|
_swing_alt = not _swing_alt
|
|
|
|
if fire_sound:
|
|
fire_sound.play()
|
|
|
|
# A real slash: windup opposite, then a fast diagonal arc THROUGH screen
|
|
# centre with the blade leading, alternating backhand/forehand.
|
|
var s := 1.0 if _swing_alt else -1.0
|
|
var tween = create_tween()
|
|
# Windup: cock back and up on the swing-origin side, blade rolled outward.
|
|
tween.tween_property(self, "position",
|
|
Vector3(0.55 * s, 0.05, -0.55), 0.08).set_ease(Tween.EASE_OUT)
|
|
tween.parallel().tween_property(self, "rotation", Vector3(
|
|
deg_to_rad(35), deg_to_rad(70 * s), deg_to_rad(-70 * s)), 0.08)
|
|
# Slash: whip across to the other side, low — a full diagonal cut.
|
|
tween.tween_property(self, "position",
|
|
Vector3(-0.55 * s, -0.42, -0.80), 0.09).set_ease(Tween.EASE_IN)
|
|
tween.parallel().tween_property(self, "rotation", Vector3(
|
|
deg_to_rad(-25), deg_to_rad(120 * s), deg_to_rad(-100 * s)), 0.09)
|
|
# Follow-through drift before recovering to guard.
|
|
tween.tween_property(self, "position",
|
|
Vector3(-0.62 * s, -0.5, -0.72), 0.06)
|
|
tween.tween_property(self, "position", default_pos, 0.18).set_ease(Tween.EASE_OUT)
|
|
tween.parallel().tween_property(self, "rotation", Vector3.ZERO, 0.18)
|
|
|
|
# Third-person: the model swings an arm (synced to other players too).
|
|
if player and player.has_method("_trigger_action"):
|
|
player._trigger_action("melee")
|
|
|
|
# Damage lands mid-slash.
|
|
get_tree().create_timer(0.12).timeout.connect(_do_damage)
|
|
|
|
# Finish swing
|
|
get_tree().create_timer(0.41).timeout.connect(_finish_swing)
|
|
|
|
func _do_damage() -> void:
|
|
if not camera: return
|
|
|
|
var space_state = camera.get_world_3d().direct_space_state
|
|
var origin = camera.global_position
|
|
var forward = -camera.global_transform.basis.z.normalized()
|
|
|
|
# 1. Area damage using a sphere
|
|
var shape = SphereShape3D.new()
|
|
shape.radius = max_range / 2.0
|
|
|
|
var shape_pos = origin + forward * (max_range / 2.0)
|
|
var query = PhysicsShapeQueryParameters3D.new()
|
|
query.shape = shape
|
|
query.transform = Transform3D(Basis(), shape_pos)
|
|
if player: query.exclude = [player.get_rid()]
|
|
|
|
var results = space_state.intersect_shape(query)
|
|
var damaged_targets = []
|
|
|
|
for res in results:
|
|
var col = res.collider
|
|
if col.has_method("take_damage") and not damaged_targets.has(col):
|
|
# Simple line of sight check to prevent hitting through walls
|
|
var los_query = PhysicsRayQueryParameters3D.create(origin, col.global_position)
|
|
if player: los_query.exclude = [player.get_rid()]
|
|
var los_result = space_state.intersect_ray(los_query)
|
|
|
|
if not los_result or los_result.collider == col:
|
|
col.take_damage(base_damage, col.global_position, player)
|
|
damaged_targets.append(col)
|
|
|
|
# 2. Wall decal using a raycast down the center
|
|
var ray_query = PhysicsRayQueryParameters3D.create(origin, origin + forward * max_range)
|
|
if player: ray_query.exclude = [player.get_rid()]
|
|
var ray_result = space_state.intersect_ray(ray_query)
|
|
|
|
if ray_result:
|
|
var col = ray_result.collider
|
|
if not col.has_method("take_damage") and (col is StaticBody3D or col is CSGShape3D):
|
|
ImpactSpawner.spawn(get_tree(), "slash", ray_result.position, ray_result.normal, 0.5)
|
|
|
|
func _finish_swing() -> void:
|
|
is_swinging = false
|
|
if auto_switch_slot != -1 and get_parent() and get_parent().has_method("_equip_slot"):
|
|
get_parent()._equip_slot(auto_switch_slot)
|
|
auto_switch_slot = -1
|
|
|
|
func unequip() -> void:
|
|
# If we are swapped manually, reset state
|
|
is_swinging = false
|
|
position = default_pos
|
|
rotation = Vector3.ZERO
|