Owning the Sprint upgrade now multiplies the scarecrow's speed by
sprint_speed_mult (1.25) all the time, walking and running, on every
platform; the run clip speeds up to match. The hold-to-sprint burst is
gone: sprint_time, sprint_cooldown, the stamina state in PlayerCharacter
(can_sprint is now has_sprint), and the HUD's stamina bar. The upgrade
keeps its id, cost, and name; its text is "Run 25% faster, always".
The sprint input action stays in project.godot, unused.
Tests: test_scarecrow's sprint checks cover 1x without it, x1.25 with it
(run and walk, clip speed, network strength), 30 s with no time limit,
no cooldown, the next morning, and no platform dependence; test_nights
checks it after the retry and a loaded save; test_economy checks the
text matches the multiplier, the menu row, and that an old save's
{"sprint": 1} still owns it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
356 lines
13 KiB
GDScript
356 lines
13 KiB
GDScript
class_name PlayerCharacter
|
|
extends CharacterBody3D
|
|
## The scarecrow's body: camera-relative movement on the move_* actions (WASD / arrows / left
|
|
## stick) with gravity. The model turns toward the direction of travel and crossfades idle /
|
|
## walk / run; the walk clip plays for partial stick deflection, full keyboard input runs.
|
|
## While shooting (face_aim) the model turns toward the aim instead. During a channel
|
|
## (start_channel: patching up, gathering) it is rooted and loops the channel's clip; once fallen
|
|
## (die) it plays the death clip and takes no input until revived. set_flash() tints the model
|
|
## for hit feedback.
|
|
## Sprint (the workbench's Sprint upgrade): once it's bought, the scarecrow moves
|
|
## GameConfig.sprint_speed_mult times as fast for good (speed_factor), walking and running alike,
|
|
## on every platform, and the run clip plays that much faster to match. There's no button, time
|
|
## limit, or cooldown. FarmSim sets config and campaign; without them, or without the upgrade, it
|
|
## moves at run_speed.
|
|
## Co-op: the other machine's scarecrow is remote_driven: it eases toward the position and facing
|
|
## it was last sent (net_position, net_yaw) and animates by net_speed (like input strength: 1 at
|
|
## run speed, more with the Sprint upgrade) instead of reading the input. Each scarecrow's speed
|
|
## is worked out on the machine that plays it. set_tint() and set_tag() tell the two scarecrows
|
|
## apart.
|
|
## The core glow: every scarecrow has a small blue core in its chest (the alien part it absorbed
|
|
## in the opening intro): a glowing gem on the chest bone (so it follows the animation), a soft
|
|
## halo, and a weak blue light, pulsing slowly. It's unshaded (the toon look and the hit flash
|
|
## leave it alone, and the tint doesn't touch it), so it reads faintly by day and clearly at
|
|
## night. set_core_glow(false) hides it (the intro, until the flash).
|
|
|
|
@export var run_speed := 5.0
|
|
@export var acceleration := 30.0
|
|
## How quickly the model turns to face the movement direction (higher = snappier).
|
|
@export var turn_speed := 12.0
|
|
## How quickly the model turns toward the aim while shooting.
|
|
@export var aim_turn_speed := 30.0
|
|
## Input strength (0..1) above which the run clip plays instead of the walk clip.
|
|
@export_range(0.0, 1.0) var run_threshold := 0.6
|
|
|
|
@export_group("Animation")
|
|
@export var idle_animation: StringName = &"Idle_A"
|
|
@export var walk_animation: StringName = &"Walking_A"
|
|
@export var run_animation: StringName = &"Running_A"
|
|
@export var channel_animation: StringName = &"Use_Item"
|
|
@export var death_animation: StringName = &"Death_A"
|
|
@export var blend_time := 0.2
|
|
|
|
@onready var model: Node3D = $Model
|
|
@onready var animation_player: AnimationPlayer = $Model/AnimationPlayer
|
|
|
|
## Where the core sits in the mannequin skeleton's space (its front is +Z): on the front of the
|
|
## chest, a little more than half sunk in; the bone it rides; and the fallback spot in the
|
|
## model's space (it faces -Z) for a model without that bone.
|
|
const CORE_SPOT := Vector3(0.0, 0.93, 0.25)
|
|
const CORE_BONE := "chest"
|
|
const CORE_SPOT_MODEL := Vector3(0.0, 0.87, -0.25)
|
|
const CORE_COLOR := Color(0.3, 0.62, 1.0)
|
|
const CORE_GEM_COLOR := Color(0.5, 0.8, 1.0)
|
|
## Seconds per pulse, and the light's and the halo's strength from the pulse's low to its high.
|
|
const CORE_PULSE := 2.8
|
|
const CORE_LIGHT := Vector2(0.25, 0.45)
|
|
const CORE_HALO := Vector2(0.4, 0.75)
|
|
## The halo: an additive, camera-facing disc, drawn pulled toward the camera so the chest it
|
|
## sits in doesn't cut it in half (the body still hides it from behind).
|
|
const HALO_SHADER := """
|
|
shader_type spatial;
|
|
render_mode unshaded, blend_add, depth_draw_never, cull_disabled, shadows_disabled, fog_disabled;
|
|
|
|
uniform vec4 color : source_color = vec4(0.3, 0.62, 1.0, 1.0);
|
|
uniform float strength = 1.0;
|
|
uniform float pull = 0.2;
|
|
|
|
void vertex() {
|
|
vec3 center = (MODELVIEW_MATRIX * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
|
|
vec3 at = center + vec3(VERTEX.xy, 0.0) + normalize(-center) * pull;
|
|
POSITION = PROJECTION_MATRIX * vec4(at, 1.0);
|
|
}
|
|
|
|
void fragment() {
|
|
float d = length(UV - 0.5) * 2.0;
|
|
ALBEDO = color.rgb;
|
|
ALPHA = strength * pow(clamp(1.0 - d, 0.0, 1.0), 2.2);
|
|
}
|
|
"""
|
|
|
|
var alive := true
|
|
## Standing still for a channel: no movement input, the channel clip loops.
|
|
var rooted := false
|
|
var channel_clip: StringName = &"Use_Item"
|
|
|
|
## Off while a co-op menu is up (the farm doesn't pause): no movement input.
|
|
var input_enabled := true
|
|
## Co-op: moved by the network (see above).
|
|
var remote_driven := false
|
|
var net_position := Vector3.ZERO
|
|
var net_yaw: float = 0.0
|
|
var net_speed: float = 0.0
|
|
|
|
## Set by FarmSim: Sprint's speed boost, and the farm state that says whether it's bought.
|
|
var config: GameConfig
|
|
var campaign: CampaignController
|
|
|
|
var _aim := Vector2.ZERO
|
|
var _aim_hold: float = 0.0
|
|
var _flash_mat: ShaderMaterial
|
|
var _tag: Label3D
|
|
## The core glow's root (hide it to hide all of it), its light and halo, and the pulse's clock.
|
|
var core_glow: Node3D
|
|
var _core_light: OmniLight3D
|
|
var _core_halo: ShaderMaterial
|
|
var _core_time: float = 0.0
|
|
## The halo's shader, compiled once for every scarecrow.
|
|
static var _halo_shader: Shader
|
|
|
|
|
|
func _ready() -> void:
|
|
_flash_mat = ShaderMaterial.new()
|
|
_flash_mat.shader = load("res://prototype/resources/hit_flash.gdshader") as Shader
|
|
for mi in model.find_children("*", "MeshInstance3D", true, false):
|
|
(mi as MeshInstance3D).material_overlay = _flash_mat
|
|
_build_core_glow()
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
if core_glow != null and core_glow.visible:
|
|
_core_time = fmod(_core_time + delta, CORE_PULSE)
|
|
_pulse_core()
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if remote_driven:
|
|
_follow_net(delta)
|
|
return
|
|
var move := _move_direction() if alive and not rooted else Vector3.ZERO
|
|
var horizontal := Vector3(velocity.x, 0.0, velocity.z).move_toward(move * run_speed * speed_factor(), acceleration * delta)
|
|
velocity.x = horizontal.x
|
|
velocity.z = horizontal.z
|
|
if not is_on_floor():
|
|
velocity += get_gravity() * delta
|
|
move_and_slide()
|
|
if not alive:
|
|
return
|
|
if _aim_hold > 0.0:
|
|
_aim_hold -= delta
|
|
var aim_yaw := atan2(-_aim.x, -_aim.y)
|
|
model.rotation.y = lerp_angle(model.rotation.y, aim_yaw, 1.0 - exp(-aim_turn_speed * delta))
|
|
elif move.length_squared() > 1e-4:
|
|
var yaw := atan2(-move.x, -move.z)
|
|
model.rotation.y = lerp_angle(model.rotation.y, yaw, 1.0 - exp(-turn_speed * delta))
|
|
_update_animation(move.length() * speed_factor())
|
|
|
|
|
|
func _follow_net(delta: float) -> void:
|
|
velocity = Vector3.ZERO
|
|
if global_position.distance_to(net_position) > 6.0:
|
|
global_position = net_position # too far behind (a join, a respawn): jump
|
|
else:
|
|
global_position = global_position.lerp(net_position, 1.0 - exp(-14.0 * delta))
|
|
if not alive:
|
|
return
|
|
model.rotation.y = lerp_angle(model.rotation.y, net_yaw, 1.0 - exp(-20.0 * delta))
|
|
_update_animation(net_speed)
|
|
|
|
|
|
## Starts following the network from where it is (no glide in from the old spot).
|
|
func snap_to_net(at: Vector3, yaw: float) -> void:
|
|
net_position = at
|
|
net_yaw = yaw
|
|
global_position = at
|
|
model.rotation.y = yaw
|
|
|
|
|
|
## Movement strength for the network: 0 standing .. 1 at run speed (sprint_speed_mult at full
|
|
## speed with the Sprint upgrade).
|
|
func move_strength() -> float:
|
|
return clampf(Vector2(velocity.x, velocity.z).length() / run_speed, 0.0, 2.0)
|
|
|
|
|
|
## Whether the Sprint upgrade is owned.
|
|
func has_sprint() -> bool:
|
|
return config != null and campaign != null and Upgrades.owned(campaign.state, "sprint")
|
|
|
|
|
|
## Speed relative to run_speed: sprint_speed_mult with the Sprint upgrade, else 1.
|
|
func speed_factor() -> float:
|
|
return config.sprint_speed_mult if has_sprint() else 1.0
|
|
|
|
|
|
## Shows or hides the core glow in the chest (on from the start).
|
|
func set_core_glow(on: bool) -> void:
|
|
if core_glow != null:
|
|
core_glow.visible = on
|
|
|
|
|
|
## The core glow: on the chest bone (through a BoneAttachment3D) when the model has one, else on
|
|
## the model at chest height.
|
|
func _build_core_glow() -> void:
|
|
var parent: Node3D = model
|
|
var spot := CORE_SPOT_MODEL
|
|
var front := Vector3.FORWARD
|
|
var down := Vector3.DOWN
|
|
var skeletons := model.find_children("*", "Skeleton3D", true, false)
|
|
if not skeletons.is_empty():
|
|
var skel := skeletons[0] as Skeleton3D
|
|
var bone := skel.find_bone(CORE_BONE)
|
|
if bone >= 0:
|
|
var attach := BoneAttachment3D.new()
|
|
attach.name = "CoreAttachment"
|
|
attach.bone_name = CORE_BONE
|
|
skel.add_child(attach)
|
|
parent = attach
|
|
var rest := skel.get_bone_global_rest(bone).affine_inverse() # skeleton -> the bone's space
|
|
spot = rest * CORE_SPOT
|
|
front = (rest.basis * Vector3.BACK).normalized()
|
|
down = (rest.basis * Vector3.DOWN).normalized()
|
|
core_glow = Node3D.new()
|
|
core_glow.name = "CoreGlow"
|
|
core_glow.position = spot
|
|
parent.add_child(core_glow)
|
|
|
|
var gem := MeshInstance3D.new()
|
|
gem.name = "Gem"
|
|
var sphere := SphereMesh.new()
|
|
sphere.radius = 0.06
|
|
sphere.height = 0.12
|
|
sphere.radial_segments = 12
|
|
sphere.rings = 6
|
|
var gem_mat := StandardMaterial3D.new()
|
|
gem_mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
|
|
gem_mat.albedo_color = CORE_GEM_COLOR
|
|
sphere.material = gem_mat
|
|
gem.mesh = sphere
|
|
gem.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
|
core_glow.add_child(gem)
|
|
|
|
var halo := MeshInstance3D.new()
|
|
halo.name = "Halo"
|
|
var quad := QuadMesh.new()
|
|
quad.size = Vector2(0.55, 0.55)
|
|
if _halo_shader == null:
|
|
_halo_shader = Shader.new()
|
|
_halo_shader.code = HALO_SHADER
|
|
_core_halo = ShaderMaterial.new()
|
|
_core_halo.shader = _halo_shader
|
|
_core_halo.set_shader_parameter("color", CORE_COLOR)
|
|
halo.mesh = quad
|
|
halo.material_override = _core_halo
|
|
halo.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
|
halo.extra_cull_margin = 0.5 # the shader moves it toward the camera
|
|
core_glow.add_child(halo)
|
|
|
|
_core_light = OmniLight3D.new()
|
|
_core_light.name = "CoreLight"
|
|
_core_light.light_color = CORE_COLOR
|
|
_core_light.omni_range = 2.2
|
|
_core_light.omni_attenuation = 1.0
|
|
_core_light.shadow_enabled = false
|
|
# Just in front of the chest and a little below the core, so it lights the arms and the ground
|
|
# more than the face.
|
|
_core_light.position = front * 0.25 + down * 0.08
|
|
core_glow.add_child(_core_light)
|
|
_pulse_core()
|
|
|
|
|
|
## The pulse: a slow sine between the light's and the halo's low and high.
|
|
func _pulse_core() -> void:
|
|
var k := 0.5 + 0.5 * sin(TAU * _core_time / CORE_PULSE)
|
|
_core_light.light_energy = lerpf(CORE_LIGHT.x, CORE_LIGHT.y, k)
|
|
_core_halo.set_shader_parameter("strength", lerpf(CORE_HALO.x, CORE_HALO.y, k))
|
|
|
|
|
|
## Tints the model (co-op: player 2's scarecrow is blue), all but the core glow.
|
|
func set_tint(color: Color) -> void:
|
|
for mi in model.find_children("*", "MeshInstance3D", true, false):
|
|
var m := mi as MeshInstance3D
|
|
if core_glow != null and core_glow.is_ancestor_of(m):
|
|
continue
|
|
var base := m.get_active_material(0)
|
|
if base is BaseMaterial3D:
|
|
var tinted := (base as BaseMaterial3D).duplicate() as BaseMaterial3D
|
|
tinted.albedo_color = color
|
|
m.material_override = tinted
|
|
|
|
|
|
## A name tag over the head ("" hides it).
|
|
func set_tag(text: String, color: Color) -> void:
|
|
if _tag == null:
|
|
_tag = Label3D.new()
|
|
_tag.billboard = BaseMaterial3D.BILLBOARD_ENABLED
|
|
_tag.no_depth_test = true
|
|
_tag.shaded = false
|
|
_tag.font_size = 44
|
|
_tag.outline_size = 12
|
|
_tag.pixel_size = 0.009
|
|
_tag.position = Vector3(0.0, 2.75, 0.0)
|
|
add_child(_tag)
|
|
_tag.text = text
|
|
_tag.modulate = color
|
|
_tag.visible = text != ""
|
|
|
|
|
|
## Faces the (unit, XZ) aim direction instead of the travel direction for the next hold seconds.
|
|
func face_aim(aim: Vector2, hold: float) -> void:
|
|
_aim = aim
|
|
_aim_hold = maxf(_aim_hold, hold)
|
|
|
|
|
|
func start_channel(clip: StringName) -> void:
|
|
rooted = true
|
|
channel_clip = clip
|
|
|
|
|
|
func stop_channel() -> void:
|
|
rooted = false
|
|
|
|
|
|
func die() -> void:
|
|
alive = false
|
|
rooted = false
|
|
animation_player.speed_scale = 1.0
|
|
animation_player.play(death_animation, 0.15)
|
|
|
|
|
|
func revive() -> void:
|
|
alive = true
|
|
rooted = false
|
|
_aim_hold = 0.0
|
|
animation_player.play(idle_animation, 0.1)
|
|
|
|
|
|
## Hit tint over the model, 0 (none) .. 1 (solid red).
|
|
func set_flash(amount: float) -> void:
|
|
_flash_mat.set_shader_parameter("flash", amount)
|
|
|
|
|
|
## Input rotated by the active camera's yaw, so "forward" is always up the screen.
|
|
func _move_direction() -> Vector3:
|
|
if not input_enabled:
|
|
return Vector3.ZERO
|
|
var input := Input.get_vector("move_left", "move_right", "move_forward", "move_back")
|
|
var camera := get_viewport().get_camera_3d()
|
|
var yaw := camera.global_rotation.y if camera != null else 0.0
|
|
return Vector3(input.x, 0.0, input.y).rotated(Vector3.UP, yaw)
|
|
|
|
|
|
func _update_animation(strength: float) -> void:
|
|
if rooted:
|
|
animation_player.speed_scale = 1.0
|
|
# Some channel clips don't loop; replay them for as long as the channel lasts.
|
|
if animation_player.current_animation != channel_clip or not animation_player.is_playing():
|
|
animation_player.play(channel_clip, blend_time)
|
|
return
|
|
var clip := idle_animation
|
|
if strength > run_threshold:
|
|
clip = run_animation
|
|
elif strength > 0.0:
|
|
clip = walk_animation
|
|
# The Sprint upgrade's speed (strength above 1) plays the run clip faster.
|
|
animation_player.speed_scale = maxf(strength, 1.0) if clip == run_animation else 1.0
|
|
if animation_player.current_animation != clip:
|
|
animation_player.play(clip, blend_time)
|