Feat/6 knife and melee fundamentals #10

Merged
Dotts merged 2 commits from feat/6-knife-and-melee-fundamentals into main 2026-06-05 23:26:43 -07:00
7 changed files with 226 additions and 3 deletions
+8 -1
View File
@@ -62,6 +62,11 @@ var weapon_db = {
"name": "Mortar", "name": "Mortar",
"category": "special", "category": "special",
"script": "res://weapons/mortar.gd" "script": "res://weapons/mortar.gd"
},
"knife": {
"name": "Knife",
"category": "melee",
"script": "res://weapons/knife.gd"
} }
} }
@@ -80,7 +85,8 @@ func _ensure_default_loadouts() -> void:
"name": "Loadout " + str(i + 1), "name": "Loadout " + str(i + 1),
"primary_1": "double_barrel_shotgun", "primary_1": "double_barrel_shotgun",
"primary_2": "none", "primary_2": "none",
"special": "none" "special": "none",
"melee": "knife"
}) })
func get_active_loadout() -> Dictionary: func get_active_loadout() -> Dictionary:
@@ -123,3 +129,4 @@ func load_loadouts() -> void:
if sl.has("primary_1"): loadouts[i]["primary_1"] = sl["primary_1"] if sl.has("primary_1"): loadouts[i]["primary_1"] = sl["primary_1"]
if sl.has("primary_2"): loadouts[i]["primary_2"] = sl["primary_2"] if sl.has("primary_2"): loadouts[i]["primary_2"] = sl["primary_2"]
if sl.has("special"): loadouts[i]["special"] = sl["special"] if sl.has("special"): loadouts[i]["special"] = sl["special"]
if sl.has("melee"): loadouts[i]["melee"] = sl["melee"]
+2
View File
@@ -11,6 +11,7 @@ var default_bindings = {
"grapple": {"type": "mouse", "code": MOUSE_BUTTON_MIDDLE}, "grapple": {"type": "mouse", "code": MOUSE_BUTTON_MIDDLE},
"reload": {"type": "key", "code": KEY_R}, "reload": {"type": "key", "code": KEY_R},
"interact": {"type": "key", "code": KEY_E}, "interact": {"type": "key", "code": KEY_E},
"melee": {"type": "key", "code": KEY_F},
"move_forward": {"type": "key", "code": KEY_W}, "move_forward": {"type": "key", "code": KEY_W},
"move_back": {"type": "key", "code": KEY_S}, "move_back": {"type": "key", "code": KEY_S},
"move_left": {"type": "key", "code": KEY_A}, "move_left": {"type": "key", "code": KEY_A},
@@ -20,6 +21,7 @@ var default_bindings = {
"weapon_1": {"type": "key", "code": KEY_1}, "weapon_1": {"type": "key", "code": KEY_1},
"weapon_2": {"type": "key", "code": KEY_2}, "weapon_2": {"type": "key", "code": KEY_2},
"weapon_3": {"type": "key", "code": KEY_3}, "weapon_3": {"type": "key", "code": KEY_3},
"weapon_4": {"type": "key", "code": KEY_4},
} }
var current_bindings = {} var current_bindings = {}
+10
View File
@@ -16,6 +16,7 @@ var name_edit: LineEdit
var primary1_opt: OptionButton var primary1_opt: OptionButton
var primary2_opt: OptionButton var primary2_opt: OptionButton
var special_opt: OptionButton var special_opt: OptionButton
var melee_opt: OptionButton
var save_loadout_btn: Button var save_loadout_btn: Button
var back_to_main_btn: Button var back_to_main_btn: Button
var editing_index: int = -1 var editing_index: int = -1
@@ -191,6 +192,12 @@ func _build_ui() -> void:
special_opt = OptionButton.new() special_opt = OptionButton.new()
edit_vbox.add_child(special_opt) edit_vbox.add_child(special_opt)
var melee_lbl = Label.new()
melee_lbl.text = "Melee"
edit_vbox.add_child(melee_lbl)
melee_opt = OptionButton.new()
edit_vbox.add_child(melee_opt)
save_loadout_btn = Button.new() save_loadout_btn = Button.new()
save_loadout_btn.text = "Save Loadout" save_loadout_btn.text = "Save Loadout"
save_loadout_btn.custom_minimum_size = Vector2(0, 40) save_loadout_btn.custom_minimum_size = Vector2(0, 40)
@@ -756,6 +763,8 @@ func _edit_loadout(idx: int) -> void:
_populate_options(primary1_opt, "primary", l["primary_1"]) _populate_options(primary1_opt, "primary", l["primary_1"])
_populate_options(primary2_opt, "primary", l["primary_2"]) _populate_options(primary2_opt, "primary", l["primary_2"])
_populate_options(special_opt, "special", l["special"]) _populate_options(special_opt, "special", l["special"])
if l.has("melee"):
_populate_options(melee_opt, "melee", l["melee"])
edit_panel.visible = true edit_panel.visible = true
@@ -788,6 +797,7 @@ func _save_current_loadout() -> void:
l["primary_1"] = primary1_opt.get_item_metadata(primary1_opt.get_selected_id()) l["primary_1"] = primary1_opt.get_item_metadata(primary1_opt.get_selected_id())
l["primary_2"] = primary2_opt.get_item_metadata(primary2_opt.get_selected_id()) l["primary_2"] = primary2_opt.get_item_metadata(primary2_opt.get_selected_id())
l["special"] = special_opt.get_item_metadata(special_opt.get_selected_id()) l["special"] = special_opt.get_item_metadata(special_opt.get_selected_id())
l["melee"] = melee_opt.get_item_metadata(melee_opt.get_selected_id())
LoadoutManager.save_loadouts() LoadoutManager.save_loadouts()
_populate_loadout_list() _populate_loadout_list()
edit_panel.visible = false edit_panel.visible = false
+14
View File
@@ -4,6 +4,7 @@ class_name ImpactSpawner
static var bullet_hole_mat: StandardMaterial3D = null static var bullet_hole_mat: StandardMaterial3D = null
static var crater_mat: StandardMaterial3D = null static var crater_mat: StandardMaterial3D = null
static var plasma_mat: StandardMaterial3D = null static var plasma_mat: StandardMaterial3D = null
static var slash_mat: StandardMaterial3D = null
static func _ensure_mats(): static func _ensure_mats():
if bullet_hole_mat != null: if bullet_hole_mat != null:
@@ -52,11 +53,22 @@ static func _ensure_mats():
plasma_mat.emission_texture = tex_p plasma_mat.emission_texture = tex_p
plasma_mat.emission_energy_multiplier = 2.0 plasma_mat.emission_energy_multiplier = 2.0
var tex_s = make_tex.call(
[Color(0.9, 0.9, 0.9, 1.0), Color(0.8, 0.8, 0.8, 0.8), Color(0.8, 0.8, 0.8, 0.0)],
[0.0, 0.5, 1.0], 64)
slash_mat = StandardMaterial3D.new()
slash_mat.albedo_texture = tex_s
slash_mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
static func spawn(scene_tree: SceneTree, type: String, pos: Vector3, normal: Vector3, size: float = 0.2): static func spawn(scene_tree: SceneTree, type: String, pos: Vector3, normal: Vector3, size: float = 0.2):
_ensure_mats() _ensure_mats()
var mi = MeshInstance3D.new() var mi = MeshInstance3D.new()
var qm = QuadMesh.new() var qm = QuadMesh.new()
if type == "slash":
qm.size = Vector2(size * 4.0, size * 0.4)
else:
qm.size = Vector2(size, size) qm.size = Vector2(size, size)
var mat: StandardMaterial3D var mat: StandardMaterial3D
@@ -66,6 +78,8 @@ static func spawn(scene_tree: SceneTree, type: String, pos: Vector3, normal: Vec
mat = crater_mat.duplicate() mat = crater_mat.duplicate()
elif type == "plasma": elif type == "plasma":
mat = plasma_mat.duplicate() mat = plasma_mat.duplicate()
elif type == "slash":
mat = slash_mat.duplicate()
qm.material = mat qm.material = mat
mi.mesh = qm mi.mesh = qm
+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 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
+1
View File
@@ -0,0 +1 @@
uid://dcja1t3x8u65m
+12 -1
View File
@@ -4,7 +4,7 @@ class_name WeaponManager
var player: CharacterBody3D var player: CharacterBody3D
var camera: Camera3D 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 var weapons: Dictionary = {} # slot_index: Node3D
func _ready() -> void: func _ready() -> void:
@@ -26,6 +26,8 @@ func _build_loadout() -> void:
_spawn_weapon(1, l["primary_1"]) _spawn_weapon(1, l["primary_1"])
_spawn_weapon(2, l["primary_2"]) _spawn_weapon(2, l["primary_2"])
_spawn_weapon(3, l["special"]) _spawn_weapon(3, l["special"])
if l.has("melee"):
_spawn_weapon(4, l["melee"])
_equip_slot(1) _equip_slot(1)
@@ -80,3 +82,12 @@ func _input(event: InputEvent) -> void:
_equip_slot(2) _equip_slot(2)
elif event.is_action_pressed("weapon_3"): elif event.is_action_pressed("weapon_3"):
_equip_slot(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)