feat: added the knife

This commit is contained in:
DottsGit
2026-06-06 02:22:27 -04:00
parent 90876611f4
commit 18b8b3ee99
6 changed files with 211 additions and 2 deletions
+178
View File
@@ -0,0 +1,178 @@
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.6)
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 mat_handle = StandardMaterial3D.new()
mat_handle.albedo_color = Color(0.1, 0.1, 0.1)
var mat_blade = StandardMaterial3D.new()
mat_blade.albedo_color = Color(0.8, 0.8, 0.8)
mat_blade.metallic = 0.8
mat_blade.roughness = 0.2
# Handle
var handle = MeshInstance3D.new()
var h_mesh = CylinderMesh.new()
h_mesh.top_radius = 0.02
h_mesh.bottom_radius = 0.02
h_mesh.height = 0.15
h_mesh.material = mat_handle
handle.mesh = h_mesh
handle.rotation.x = deg_to_rad(90)
handle.position = Vector3(0, 0, 0)
model_root.add_child(handle)
# Blade
var blade = MeshInstance3D.new()
var b_mesh = BoxMesh.new()
b_mesh.size = Vector3(0.01, 0.04, 0.25)
b_mesh.material = mat_blade
blade.mesh = b_mesh
blade.position = Vector3(0, 0, -0.2)
model_root.add_child(blade)
# Guard
var guard = MeshInstance3D.new()
var g_mesh = BoxMesh.new()
g_mesh.size = Vector3(0.06, 0.05, 0.01)
g_mesh.material = mat_handle
guard.mesh = g_mesh
guard.position = Vector3(0, 0, -0.075)
model_root.add_child(guard)
# Audio (reusing shotgun reload or something generic for now, ideally a swish)
fire_sound = AudioStreamPlayer3D.new()
fire_sound.bus = "SFX"
fire_sound.stream = load("res://assets/sounds/dash.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
func _swing() -> void:
is_swinging = true
fire_cooldown = fire_rate
if fire_sound:
fire_sound.play()
# Tween for swipe animation
var tween = create_tween()
# Windup: Move to the right, point the blade left and tilt it
tween.tween_property(self, "position", Vector3(0.6, -0.2, -0.4), 0.1)
tween.parallel().tween_property(self, "rotation", Vector3(deg_to_rad(10), deg_to_rad(80), deg_to_rad(-40)), 0.1)
# Swipe: Move across the screen to the left quickly
tween.tween_property(self, "position", Vector3(-0.6, -0.3, -0.5), 0.1)
tween.parallel().tween_property(self, "rotation", Vector3(deg_to_rad(10), deg_to_rad(110), deg_to_rad(-60)), 0.1)
# Return to idle
tween.tween_property(self, "position", default_pos, 0.2)
tween.parallel().tween_property(self, "rotation", Vector3.ZERO, 0.2)
# Damage occurs partway through the swing
get_tree().create_timer(0.15).timeout.connect(_do_damage)
# Finish swing
get_tree().create_timer(0.4).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 col is StaticBody3D or col is CSGShape3D:
ImpactSpawner.spawn(get_tree(), "bullet", ray_result.position, ray_result.normal, 0.1)
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
+1
View File
@@ -0,0 +1 @@
uid://dcja1t3x8u65m
+12 -1
View File
@@ -4,7 +4,7 @@ class_name WeaponManager
var player: CharacterBody3D
var camera: Camera3D
var active_slot: int = 1 # 1: Primary 1, 2: Primary 2, 3: Special
var active_slot: int = 1 # 1: Primary 1, 2: Primary 2, 3: Special, 4: Melee
var weapons: Dictionary = {} # slot_index: Node3D
func _ready() -> void:
@@ -26,6 +26,8 @@ func _build_loadout() -> void:
_spawn_weapon(1, l["primary_1"])
_spawn_weapon(2, l["primary_2"])
_spawn_weapon(3, l["special"])
if l.has("melee"):
_spawn_weapon(4, l["melee"])
_equip_slot(1)
@@ -80,3 +82,12 @@ func _input(event: InputEvent) -> void:
_equip_slot(2)
elif event.is_action_pressed("weapon_3"):
_equip_slot(3)
elif event.is_action_pressed("weapon_4"):
_equip_slot(4)
elif event.is_action_pressed("melee"):
if active_slot != 4:
var prev = active_slot
_equip_slot(4)
if weapons.has(4):
if weapons[4].has_method("do_quick_melee"):
weapons[4].do_quick_melee(prev)