This commit is contained in:
Forrest 2026-10-04 17:30:48 -04:00
parent 4298caf5cb
commit 2e6638b744
19 changed files with 484 additions and 185 deletions

View file

@ -2,17 +2,24 @@ class_name ScarecrowCombat
extends Node3D
## The scarecrow's weapons, ported from the prototype's PlayerInput + WeaponController (Step 4
## finished the kit; the weapons came after Step 11):
## * Three weapons (Weapon): the pitchfork, every scarecrow's from the start; the corn shooter
## and pumpkin bombs, bought at the workbench (Upgrades "corn_shooter", "pumpkin_bomb").
## weapon_next (Tab / d-pad down) cycles through the ones owned, weapon_1..3 (1-3) pick one,
## both only outside build mode (where those keys pick structures) and with no menu open.
## One no longer owned (a retry from before it was bought) goes back to the pitchfork.
## * Three weapons (Weapon) in two slots: the main weapon in the right hand, the pitchfork
## (every scarecrow's from the start) or the corn shooter, and pumpkin bombs in the off-hand
## (the left); the corn shooter and the bombs are bought at the workbench (Upgrades
## "corn_shooter", "pumpkin_bomb"). weapon_next (Tab / d-pad down) cycles through the main
## weapons owned, weapon_1..2 (1-2) pick one, both only outside build mode (where those keys
## pick structures) and with no menu open. The off-hand holds the off-hand weapon owned
## (OFFHAND_WEAPONS; there's no pick while there's only one). A main weapon no longer owned (a
## retry from before it was bought) goes back to the pitchfork, an off-hand one out of the hand.
## * Aim with the mouse (the cursor projected onto the combat-height plane through the follow
## camera, corrected for the gun's side offset so shots pass through the cursor; bombs land
## on the ground point under the cursor) or the right stick (camera-relative, radial dead
## zone, last aim kept). The active device switches only on deliberate input: mouse travel
## past a threshold or a click, stick deflection past the dead zone or the trigger.
## * Attack with the fire action, needing a fresh press after every reset; holding it repeats:
## * Attack with the main weapon with fire (LMB / RT), and use the off-hand with offhand
## (RMB / LT), each on its own, both at once too. Each needs a fresh press after every reset
## (FarmSim resets on entering and leaving build mode, where the right mouse button is cancel),
## and offhand also after any tick the scarecrow couldn't attack (a menu open, patching; in
## co-op the partner's build mode, which its own machine toggles). Holding either repeats:
## pitchfork a stab every pitchfork_interval: the nearest pitchfork_targets enemies
## within pitchfork_reach and pitchfork_arc of the aim, with a clear line,
## take pitchfork_damage
@ -21,12 +28,13 @@ extends Node3D
## pumpkin bomb one every bomb_cooldown, thrown at the aim point (bomb_min_range ..
## bomb_range away; GAMEPAD_THROW ahead with the stick), bursting where it
## lands (PumpkinBombs)
## * The weapon in hand on the scarecrow's model (WeaponModels), and an aim marker (with bombs:
## on the ground where one would land, ringed by its blast).
## * Both weapons on the scarecrow's model (WeaponModels), an aim marker, and while a pumpkin
## bomb is ready a ring on the ground where one would land, as wide as its blast.
## While attacking, the model faces the aim.
## Co-op: the partner's weapons on the host read its RemoteControls (aim, fire, and its pick of
## weapon come from the partner's game); on the partner's machine its own only aims and shows the
## attack motions (replica: the host attacks), and only the local player's aim marker shows.
## Co-op: the partner's weapons on the host read its RemoteControls (aim, fire and offhand, and
## its pick of main weapon come from the partner's game); on the partner's machine its own only
## aims and shows the attack motions (replica: the host attacks), and only the local player's aim
## marker shows.
enum Device { MOUSE, GAMEPAD }
enum Weapon { PITCHFORK, CORN_SHOOTER, PUMPKIN_BOMB }
@ -38,7 +46,8 @@ signal stabbed(hits: int)
signal struck(at: Vector3, enemy_type: int)
## A pumpkin bomb thrown (where it will land).
signal threw(at: Vector3)
## Any attack (co-op: the partner's machine shows its motion), and a change of weapon.
## Any attack, with either hand (co-op: the partner's machine shows its motion), and a change of
## main weapon.
signal attacked(weapon: Weapon)
signal switched(weapon: Weapon)
@ -47,14 +56,23 @@ const SIDE_OFFSET := 0.35
## Upgrade ids that unlock the weapons (the pitchfork needs none), and what they're called.
const WEAPON_IDS: Array[String] = ["pitchfork", "corn_shooter", "pumpkin_bomb"]
const WEAPON_NAMES: Array[String] = ["Pitchfork", "Corn shooter", "Pumpkin bombs"]
const SWITCH_ACTIONS: Array[StringName] = [&"weapon_next", &"weapon_1", &"weapon_2", &"weapon_3"]
## The main weapons (Weapon), in the order weapon_next cycles and weapon_1.. pick them, and the
## off-hand ones.
const MAIN_WEAPONS: Array[int] = [Weapon.PITCHFORK, Weapon.CORN_SHOOTER]
const OFFHAND_WEAPONS: Array[int] = [Weapon.PUMPKIN_BOMB]
const SWITCH_ACTIONS: Array[StringName] = [&"weapon_next", &"weapon_1", &"weapon_2"]
## How far ahead a bomb lands when aiming with the stick, and how high it leaves the hand.
const GAMEPAD_THROW := 7.0
const THROW_HEIGHT := 1.6
## Opacity of the bomb's landing ring, and while offhand is held.
const BLAST_ALPHA := 0.45
const BLAST_HELD_ALPHA := 0.9
const WORLD_LAYER := 1
var device: Device = Device.MOUSE
## The main weapon, and the off-hand one (-1: none owned).
var weapon: Weapon = Weapon.PITCHFORK
var offhand: int = -1
## Last valid horizontal aim direction (unit, XZ).
var aim := Vector2(0.0, -1.0)
## World point the mouse aims at (combat-height plane), and the ground point under the cursor;
@ -86,6 +104,10 @@ var _blast: MeshInstance3D
## through switches so swapping weapons can't skip a wait.
var _cooldown := PackedInt32Array([0, 0, 0])
var _needs_release := true
var _offhand_needs_release := true
## Ticks so far (tick), and the last one the scarecrow could attack in (step).
var _ticks: int = 0
var _stepped: int = 0
var _mouse_travel: float = 0.0
var _edges := ActionEdges.new()
var _ray := PhysicsRayQueryParameters3D.new()
@ -104,10 +126,12 @@ func setup(cfg: GameConfig, player: PlayerCharacter, camera: Camera3D, shots: Fa
_build_marker()
## Requires a fresh fire press, so a held button can't fire into a new morning or a retry.
## Requires fresh fire and offhand presses, so a held button can't attack into a new morning or a
## retry.
func reset() -> void:
_cooldown.fill(0)
_needs_release = true
_offhand_needs_release = true
## A new morning (also after a retry): a pumpkin bomb back in hand.
@ -116,6 +140,11 @@ func ready_for_morning() -> void:
bomb_left = 0.0
## Whether the weapon goes in the off-hand rather than the hand.
static func is_offhand(w: int) -> bool:
return OFFHAND_WEAPONS.has(w)
## Whether this scarecrow has the weapon: the pitchfork always, the others once bought.
func owns(w: int) -> bool:
if w == Weapon.PITCHFORK:
@ -123,27 +152,33 @@ func owns(w: int) -> bool:
return w > 0 and w < WEAPON_IDS.size() and campaign != null and Upgrades.owned(campaign.state, WEAPON_IDS[w])
## Takes the weapon in hand if it's owned. Returns whether it's in hand now.
## Takes the weapon up if it's owned: a main weapon into the hand, an off-hand one into the
## off-hand. Returns whether it's there now.
func equip(w: int) -> bool:
if not owns(w):
return false
if w != weapon:
if is_offhand(w):
_set_offhand(w)
elif w != weapon:
show_weapon(w)
switched.emit(weapon)
return true
## The next weapon owned after the one in hand.
## The next main weapon owned after the one in hand.
func cycle() -> void:
for k in range(1, WEAPON_IDS.size()):
if equip((weapon + k) % WEAPON_IDS.size()):
var at := MAIN_WEAPONS.find(weapon)
for k in range(1, MAIN_WEAPONS.size()):
if equip(MAIN_WEAPONS[(at + k) % MAIN_WEAPONS.size()]):
return
## Shows the weapon in hand without the ownership check (co-op: the other scarecrow's, as its
## machine says).
## Shows the main weapon in hand without the ownership check (co-op: the other scarecrow's, as
## its machine says). An off-hand weapon doesn't go into the hand.
func show_weapon(w: int) -> void:
var next := clampi(w, 0, WEAPON_IDS.size() - 1) as Weapon
if not MAIN_WEAPONS.has(w):
return
var next := w as Weapon
if next == weapon and models.weapon == next:
return # (a snapshot saying so again mustn't cut an attack's motion short)
weapon = next
@ -165,18 +200,24 @@ func _input(event: InputEvent) -> void:
_to_gamepad()
elif event is InputEventJoypadMotion:
var jm := event as InputEventJoypadMotion
if jm.axis == JOY_AXIS_TRIGGER_RIGHT and jm.axis_value > 0.5:
if (jm.axis == JOY_AXIS_TRIGGER_RIGHT or jm.axis == JOY_AXIS_TRIGGER_LEFT) and jm.axis_value > 0.5:
_to_gamepad()
## Every tick, attacking or not (building, patching): the bomb's recharge, and the pick of
## weapon (the switch actions count only when can_switch: outside build mode, no menu open; the
## partner's pick comes from its game).
## Every tick, attacking or not (building, patching): the bomb's recharge, the off-hand weapon,
## and the pick of main weapon (the switch actions count only when can_switch: outside build
## mode, no menu open; the partner's pick comes from its game).
func tick(dt: float, can_switch: bool) -> void:
_ticks += 1
bomb_left = maxf(bomb_left - dt, 0.0)
models.set_bomb_charge(1.0 - bomb_left / _cfg.bomb_cooldown if _cfg.bomb_cooldown > 0.0 else 1.0)
if not owns(weapon):
equip(Weapon.PITCHFORK)
if not owns(offhand):
_set_offhand(-1)
for w in OFFHAND_WEAPONS:
if equip(w):
break
if remote != null:
if remote.weapon >= 0:
equip(remote.weapon)
@ -188,64 +229,63 @@ func tick(dt: float, can_switch: bool) -> void:
return
if p[&"weapon_next"]:
cycle()
for k in WEAPON_IDS.size():
for k in MAIN_WEAPONS.size():
if p[SWITCH_ACTIONS[k + 1]]:
equip(k)
equip(MAIN_WEAPONS[k])
## One physics tick while the scarecrow can attack: update the aim, then attack with the weapon
## in hand if fire is held and it's ready.
## One physics tick while the scarecrow can attack: update the aim, then attack with the main
## weapon if fire is held, and use the off-hand one if offhand is held, each if it's ready.
func step(_dt: float) -> void:
_update_aim()
var held := remote.held(&"fire") if remote != null else Input.is_action_pressed("fire")
var held := _held(&"fire")
var off_held := _held(&"offhand")
for i in _cooldown.size():
_cooldown[i] = maxi(_cooldown[i] - 1, 0)
if _ticks - _stepped > 1:
_offhand_needs_release = true # couldn't attack last tick (a menu, the partner's build mode)
_stepped = _ticks
if replica:
if held:
_player.face_aim(aim, 0.35)
_show_attack()
return
if _needs_release:
if held:
return
if _needs_release and not held:
_needs_release = false
if not held:
return
_player.face_aim(aim, 0.35)
attack()
if _offhand_needs_release and not off_held:
_offhand_needs_release = false
if off_held and not _offhand_needs_release:
_player.face_aim(aim, 0.35)
use_offhand()
if held and not _needs_release:
_player.face_aim(aim, 0.35)
attack()
## The weapon in hand attacks along the aim, if it's ready. Returns whether it did.
## The main weapon attacks along the aim, if it's ready. Returns whether it did.
func attack() -> bool:
if _cooldown[weapon] > 0:
return false
match weapon:
Weapon.CORN_SHOOTER:
_cooldown[weapon] = interval_ticks()
_shoot()
Weapon.PUMPKIN_BOMB:
if bomb_left > 0.0 or not _throw():
return false
_:
_cooldown[weapon] = stab_ticks()
_stab()
attacked.emit(weapon)
return true
return _use(weapon)
## A scripted attack (the soak's scarecrow, tests): aims at target and attacks if ready.
## The off-hand weapon is used (a pumpkin bomb thrown at the aim point), if one is ready.
## Returns whether it was.
func use_offhand() -> bool:
return offhand >= 0 and _use(offhand)
## A scripted attack (the soak's scarecrow, tests): aims at target and attacks with the main
## weapon if it's ready.
func attack_toward(target: Vector2) -> bool:
var p := Vector2(_player.global_position.x, _player.global_position.z)
var to := target - p
if to.length() > 1e-3:
aim = PlayerInput.muzzle_corrected_aim(to, to.normalized()) if weapon == Weapon.CORN_SHOOTER else to.normalized()
aim_point = Vector3(target.x, _cfg.combat_height, target.y)
ground_point = Vector3(target.x, 0.0, target.y)
device = Device.MOUSE
_player.face_aim(aim, 0.35)
_aim_at(target, weapon == Weapon.CORN_SHOOTER)
return attack()
## The same with the off-hand weapon (a pumpkin bomb thrown at target).
func offhand_toward(target: Vector2) -> bool:
_aim_at(target, false)
return use_offhand()
## Attacks so far, with any weapon.
func attacks() -> int:
return stabs + shots_fired + bombs_thrown
@ -276,6 +316,44 @@ func throw_target() -> Vector3:
return Vector3(at.x, 0.0, at.y)
func _held(action: StringName) -> bool:
return remote.held(action) if remote != null else Input.is_action_pressed(action)
func _use(w: int) -> bool:
if _cooldown[w] > 0:
return false
match w:
Weapon.CORN_SHOOTER:
_cooldown[w] = interval_ticks()
_shoot()
Weapon.PUMPKIN_BOMB:
if bomb_left > 0.0 or not _throw():
return false
_:
_cooldown[w] = stab_ticks()
_stab()
attacked.emit(w)
return true
func _set_offhand(w: int) -> void:
if w != offhand:
offhand = w
models.show_offhand(w)
func _aim_at(target: Vector2, from_muzzle: bool) -> void:
var p := Vector2(_player.global_position.x, _player.global_position.z)
var to := target - p
if to.length() > 1e-3:
aim = PlayerInput.muzzle_corrected_aim(to, to.normalized()) if from_muzzle else to.normalized()
aim_point = Vector3(target.x, _cfg.combat_height, target.y)
ground_point = Vector3(target.x, 0.0, target.y)
device = Device.MOUSE
_player.face_aim(aim, 0.35)
func _shoot() -> void:
var origin := Vector3(_player.global_position.x, _cfg.combat_height, _player.global_position.z)
var dir := Vector3(aim.x, 0.0, aim.y)
@ -324,7 +402,7 @@ func _throw() -> bool:
if bombs == null:
return false
var p := _player.global_position
var hand := Vector3(p.x - aim.y * 0.4, THROW_HEIGHT, p.z + aim.x * 0.4)
var hand := Vector3(p.x + aim.y * 0.4, THROW_HEIGHT, p.z - aim.x * 0.4) # the left
var to := throw_target()
if not bombs.throw(hand, to, _cfg.bomb_damage, _cfg.bomb_radius):
return false
@ -335,10 +413,11 @@ func _throw() -> bool:
return true
## The partner's machine: the attack motion on the weapon's own cadence (the host attacks).
## The partner's machine: the main weapon's attack motion on its own cadence (the host attacks;
## its throws show the bomb, and empty the off-hand).
func _show_attack() -> void:
if weapon == Weapon.PUMPKIN_BOMB or _cooldown[weapon] > 0:
return # the host's throw shows the bomb, and empties the hand
if _cooldown[weapon] > 0:
return
_cooldown[weapon] = interval_ticks() if weapon == Weapon.CORN_SHOOTER else stab_ticks()
models.attack(weapon)
@ -390,24 +469,26 @@ func _to_gamepad() -> void:
func _process(_delta: float) -> void:
if _player == null:
return
var bomb := weapon == Weapon.PUMPKIN_BOMB
_marker.visible = show_marker
_blast.visible = show_marker and bomb
if bomb:
var at := throw_target()
_marker.global_position = Vector3(at.x, 0.12, at.z)
_blast.global_position = Vector3(at.x, 0.1, at.z)
elif device == Device.MOUSE:
if device == Device.MOUSE:
_marker.global_position = Vector3(aim_point.x, _cfg.combat_height, aim_point.z)
else:
var p := _player.get_global_transform_interpolated().origin
_marker.global_position = Vector3(p.x + aim.x * 3.0, _cfg.combat_height, p.z + aim.y * 3.0)
# A pumpkin bomb ready in the off-hand: where it would land, brighter while offhand is held.
var bomb := offhand == Weapon.PUMPKIN_BOMB and bomb_left == 0.0
_blast.visible = show_marker and bomb
if _blast.visible:
var at := throw_target()
_blast.global_position = Vector3(at.x, 0.1, at.z)
var held := remote == null and Input.is_action_pressed("offhand")
(_blast.material_override as StandardMaterial3D).albedo_color.a = BLAST_HELD_ALPHA if held else BLAST_ALPHA
func _build_marker() -> void:
_marker = _ring(0.2, 0.3, Color(1.0, 0.85, 0.25))
_marker.name = "AimMarker"
_blast = _ring(_cfg.bomb_radius - 0.08, _cfg.bomb_radius, Color(1.0, 0.55, 0.12, 0.8))
_blast = _ring(_cfg.bomb_radius - 0.08, _cfg.bomb_radius, Color(1.0, 0.55, 0.12, BLAST_ALPHA))
_blast.name = "BlastMarker"