harvest/game/scripts/weapon_models.gd
2026-10-04 12:07:36 -04:00

116 lines
4.1 KiB
GDScript

class_name WeaponModels
extends Node3D
## The scarecrow's weapons as it holds them (ScarecrowCombat), from the models in
## assets/weapons/, on the scarecrow's model (which faces -Z), only the one in use showing. The
## Blender exports sit far from the origin, so each mesh is re-centered (mesh_of) and posed here:
## pitchfork held low like a spear at the right side, tines forward; a stab thrusts it out
## corn shooter at the gun's spot, its corn-cob muzzle at the gameplay muzzle point; each shot
## kicks it back
## pumpkin bomb in the right hand; after a throw the next one grows back in the hand over the
## recharge (set_bomb_charge)
## attack() plays the weapon's motion (the pumpkin just leaves the hand: the thrown one is
## PumpkinBombs').
const PATHS: Array[String] = ["res://assets/weapons/PitchFork.glb", "res://assets/weapons/CornShooter.glb",
"res://assets/weapons/PumpkinBomb.glb"]
## Model scale: the pitchfork is 3 m long as modeled, the pumpkin 0.75 m across.
const SCALES: Array[float] = [0.6, 0.55, 0.45]
## Pumpkin bombs in flight use the same scale.
const BOMB_SCALE := 0.45
## Seconds of each weapon's attack motion, and how far it goes.
const STAB_TIME := 0.24
const STAB_REACH := 0.75
const KICK_TIME := 0.09
const KICK := 0.09
static var _meshes := {}
var weapon: int = 0
var _pivots: Array[Node3D] = []
var _rest: Array[Transform3D] = []
var _attack_t := -1.0
var _bomb_charge := 1.0
## A model's mesh, re-centered (the scene's root node sits where it was in Blender).
static func mesh_of(index: int) -> Mesh:
if not _meshes.has(index):
var scene := (load(PATHS[index]) as PackedScene).instantiate()
var mi := scene.find_children("*", "MeshInstance3D", true, false)[0] as MeshInstance3D
_meshes[index] = mi.mesh
scene.free()
return _meshes[index]
## Builds the three under model. muzzle: the gameplay muzzle point on the model (the corn
## shooter's tip goes there).
func setup(model: Node3D, muzzle: Vector3) -> void:
name = "Weapons"
model.add_child(self)
# Pitchfork: tines (the mesh's +Y) forward and a little down, the shaft past the right hip.
_add(0, Transform3D(Basis.from_euler(Vector3(-1.75, 0.0, 0.0)), Vector3(0.32, 1.15, -0.95)))
# Corn shooter: its tip (the mesh's +Z end, 0.59 m out) turned forward onto the muzzle point.
var tip := 0.589 * SCALES[1]
_add(1, Transform3D(Basis.from_euler(Vector3(0.0, PI, 0.0)), muzzle + Vector3(0.0, 0.0, tip)))
# Pumpkin: in the right hand, in front.
_add(2, Transform3D(Basis.IDENTITY, Vector3(0.42, 1.2, -0.5)))
show_weapon(0)
func _add(index: int, rest: Transform3D) -> void:
var pivot := Node3D.new()
pivot.name = ["Pitchfork", "CornShooter", "PumpkinBomb"][index]
add_child(pivot)
var mi := MeshInstance3D.new()
mi.mesh = mesh_of(index)
mi.scale = Vector3.ONE * SCALES[index]
pivot.add_child(mi)
pivot.transform = rest
_pivots.append(pivot)
_rest.append(rest)
## Shows only this weapon (ScarecrowCombat.Weapon).
func show_weapon(index: int) -> void:
weapon = index
_attack_t = -1.0
for i in _pivots.size():
_pivots[i].visible = i == index
_pivots[i].transform = _rest[i]
_apply_charge()
## The weapon's attack motion: a stab or a kick.
func attack(index: int) -> void:
if index == weapon and index < 2:
_attack_t = 0.0
## Share of the pumpkin bomb's recharge done (1 = a pumpkin in the hand).
func set_bomb_charge(charge: float) -> void:
_bomb_charge = clampf(charge, 0.0, 1.0)
_apply_charge()
func _apply_charge() -> void:
if _pivots.size() > 2:
var s := _bomb_charge * _bomb_charge
_pivots[2].scale = Vector3.ONE * maxf(s, 0.001)
_pivots[2].get_child(0).visible = s > 0.05
func _process(delta: float) -> void:
if _attack_t < 0.0 or _pivots.is_empty():
return
_attack_t += delta / (STAB_TIME if weapon == 0 else KICK_TIME)
var u := clampf(_attack_t, 0.0, 1.0)
var at := _rest[weapon].origin
if weapon == 0: # out fast, back slower
var push := sin(PI * pow(u, 0.6)) * STAB_REACH
at += Vector3(0.0, 0.08 * push, -push)
else:
at += Vector3(0.0, 0.02, KICK) * (1.0 - u)
if _attack_t >= 1.0:
_attack_t = -1.0
at = _rest[weapon].origin
_pivots[weapon].position = at