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

259 lines
8.5 KiB
GDScript

class_name EnemyCharacters
extends Node3D
## The KayKit characters that draw ground enemies (EnemySystem keeps the simulation; flyers stay
## in its MultiMesh). A fixed pool per type is created at load and never grows: a ground enemy
## only spawns while its type has a free character.
## thief Mannequin_Medium, tinted dark, Rig_Medium clips (player_animations.res)
## breaker Mannequin_Large, rust red, scaled down, a club in its right hand, Rig_Large clips
## (breaker_animations.res)
## Each frame a character takes its enemy's interpolated position and heading and a clip for what
## it's doing (walking or running by speed, taking loot, breaking, fighting), flashes white on a
## hit, and glows faintly at night. A killed enemy's character plays its death clip where it fell
## for CORPSE_TIME; a corpse is only for show, so a new enemy can take it over at once when no
## other character is free.
const POOL := {EnemySystem.TYPE_THIEF: 48, EnemySystem.TYPE_BREAKER: 20}
const CORPSE_TIME := 1.6
const HIT_TIME := 0.35
const KIT := "res://assets/kaykit_characters/characters/"
const SCENES := {EnemySystem.TYPE_THIEF: KIT + "Mannequin_Medium.glb", EnemySystem.TYPE_BREAKER: KIT + "Mannequin_Large.glb"}
const LIBRARIES := {
EnemySystem.TYPE_THIEF: "res://game/resources/player_animations.res",
EnemySystem.TYPE_BREAKER: "res://game/resources/breaker_animations.res",
}
const SCALES := {EnemySystem.TYPE_THIEF: 0.86, EnemySystem.TYPE_BREAKER: 0.62}
const TINTS := {EnemySystem.TYPE_THIEF: Color(0.3, 0.27, 0.36), EnemySystem.TYPE_BREAKER: Color(0.72, 0.32, 0.2)}
## Clips by what the character is doing: idle, walk, run, steal (crops and animals), steal
## (supplies), break, fight, hit, death.
const CLIPS := {
EnemySystem.TYPE_THIEF: {"idle": &"Idle_A", "walk": &"Walking_A", "run": &"Running_A", "steal": &"PickUp",
"steal_supply": &"Lockpicking", "break": &"Melee_Unarmed_Attack_Punch_A", "fight": &"Melee_Unarmed_Attack_Punch_A",
"hit": &"Hit_A", "death": &"Death_A"},
EnemySystem.TYPE_BREAKER: {"idle": &"Melee_2H_Idle", "walk": &"Walking_A", "run": &"Running_A", "steal": &"Melee_2H_Idle",
"steal_supply": &"Melee_2H_Idle", "break": &"Melee_2H_Slam", "fight": &"Melee_2H_Attack",
"hit": &"Hit_A", "death": &"Death_A"},
}
var enemies: EnemySystem
var config: GameConfig
## Per character (index into the arrays below): its type, the enemy id it draws (-1 when free),
## and timers.
var type := PackedByteArray()
var enemy_id := PackedInt32Array()
var corpse := PackedFloat32Array()
var hit_left := PackedFloat32Array()
var _roots: Array[Node3D] = []
var _anims: Array[AnimationPlayer] = []
var _overlays: Array[ShaderMaterial] = []
var _clip: Array[StringName] = []
var _by_id := {} # enemy id -> character index
var _night_glow: float = 0.0
func setup(p_enemies: EnemySystem, p_config: GameConfig) -> void:
enemies = p_enemies
config = p_config
var shader := load("res://game/resources/enemy_overlay.gdshader") as Shader
for t: int in POOL:
var scene := load(SCENES[t]) as PackedScene
var library := load(LIBRARIES[t]) as AnimationLibrary
for k in POOL[t]:
var root := Node3D.new()
root.name = "%s_%d" % [EnemySystem.TYPE_NAMES[t], k]
root.visible = false
root.physics_interpolation_mode = Node.PHYSICS_INTERPOLATION_MODE_OFF
add_child(root)
var model := scene.instantiate() as Node3D
model.scale = Vector3.ONE * float(SCALES[t])
root.add_child(model)
var overlay := ShaderMaterial.new()
overlay.shader = shader
for mi in model.find_children("*", "MeshInstance3D", true, false):
var m := mi as MeshInstance3D
var base := m.get_active_material(0)
if base is BaseMaterial3D:
var tinted := (base as BaseMaterial3D).duplicate() as BaseMaterial3D
tinted.albedo_color = TINTS[t]
m.material_override = tinted
m.material_overlay = overlay
if t == EnemySystem.TYPE_BREAKER:
_add_club(model)
var anim := AnimationPlayer.new()
root.add_child(anim)
anim.root_node = anim.get_path_to(model)
anim.add_animation_library(&"", library)
_roots.append(root)
_anims.append(anim)
_overlays.append(overlay)
_clip.append(&"")
type.append(t)
enemy_id.append(-1)
corpse.append(0.0)
hit_left.append(0.0)
## Characters of the type not drawing a live enemy (corpses included: they can be taken over).
func free_count(t: int) -> int:
var n := 0
for c in type.size():
if type[c] == t and enemy_id[c] < 0:
n += 1
return n
func corpses() -> int:
var n := 0
for c in type.size():
if enemy_id[c] < 0 and corpse[c] > 0.0:
n += 1
return n
## Takes a character of the type for enemy id: a free one, else the corpse closest to fading.
## Returns false if every one is drawing a live enemy.
func acquire(t: int, id: int) -> bool:
var pick := -1
for c in type.size():
if type[c] != t or enemy_id[c] >= 0:
continue
if corpse[c] <= 0.0:
pick = c
break
if pick < 0 or corpse[c] < corpse[pick]:
pick = c
if pick < 0:
return false
enemy_id[pick] = id
corpse[pick] = 0.0
_by_id[id] = pick
hit_left[pick] = 0.0
_clip[pick] = &""
_roots[pick].visible = true
_overlays[pick].set_shader_parameter("flash", 0.0)
return true
## The enemy is gone: a killed one's character plays its death clip where it fell for a moment.
func release(id: int, killed: bool) -> void:
if not _by_id.has(id):
return
var c: int = _by_id[id]
_by_id.erase(id)
enemy_id[c] = -1
if killed:
corpse[c] = CORPSE_TIME
_play(c, CLIPS[type[c]]["death"], 0.1)
else:
_roots[c].visible = false
func hit(id: int) -> void:
if _by_id.has(id):
hit_left[_by_id[id]] = HIT_TIME
## Every character back to the pool, hidden (new morning, retry).
func clear() -> void:
_by_id.clear()
for c in type.size():
enemy_id[c] = -1
corpse[c] = 0.0
_roots[c].visible = false
## How many characters are drawing an enemy right now (tests).
func in_use() -> int:
return _by_id.size()
func set_night_glow(glow: float) -> void:
_night_glow = glow
func _process(delta: float) -> void:
if enemies == null:
return
for c in type.size():
var root := _roots[c]
if corpse[c] > 0.0:
corpse[c] = maxf(corpse[c] - delta, 0.0)
if corpse[c] == 0.0:
root.visible = false
continue
if enemy_id[c] < 0:
continue
var d: int = enemies.id_dense[enemy_id[c]]
if d < 0:
continue
var p := enemies.visual_position(d)
root.position = Vector3(p.x, 0.0, p.y)
root.rotation.y = lerp_angle(root.rotation.y, atan2(enemies.head_x[d], enemies.head_z[d]), 1.0 - exp(-14.0 * delta))
hit_left[c] = maxf(hit_left[c] - delta, 0.0)
_overlays[c].set_shader_parameter("flash", enemies.hurt[d])
_overlays[c].set_shader_parameter("night_glow", _night_glow)
_animate(c, d)
func _animate(c: int, d: int) -> void:
var clips: Dictionary = CLIPS[type[c]]
var speed := Vector2(enemies.vel_x[d], enemies.vel_z[d]).length()
var clip: StringName = clips["idle"]
var scale := 1.0
match enemies.state[d]:
ThiefBehavior.State.STEAL:
var tg := enemies.target[d]
var supply := tg > 0 and enemies.behavior != null and enemies.behavior.loot.category[tg] == LootSystem.Category.SUPPLY
clip = clips["steal_supply"] if supply else clips["steal"]
ThiefBehavior.State.BREAK:
clip = clips["break"]
ThiefBehavior.State.FIGHT:
clip = clips["fight"] if speed < 0.3 else clips["walk"]
_:
if speed > 2.2:
clip = clips["run"]
scale = clampf(speed / 4.5, 0.6, 1.3)
elif speed > 0.25:
clip = clips["walk"]
scale = clampf(speed / 1.6, 0.6, 1.4)
if hit_left[c] > 0.0:
clip = clips["hit"]
_anims[c].speed_scale = scale
if clip != _clip[c] or not _anims[c].is_playing():
_play(c, clip, 0.15)
func _play(c: int, clip: StringName, blend: float) -> void:
_clip[c] = clip
if _anims[c].has_animation(clip):
_anims[c].play(clip, blend)
## A club built from primitives in the large mannequin's right hand.
func _add_club(model: Node3D) -> void:
var skel := model.find_children("*", "Skeleton3D", true, false)
if skel.is_empty():
return
var attach := BoneAttachment3D.new()
attach.bone_name = "handslot.r"
(skel[0] as Skeleton3D).add_child(attach)
var wood := StandardMaterial3D.new()
wood.albedo_color = Color(0.42, 0.27, 0.15)
var handle := MeshInstance3D.new()
var cyl := CylinderMesh.new()
cyl.top_radius = 0.07
cyl.bottom_radius = 0.06
cyl.height = 1.1
cyl.material = wood
handle.mesh = cyl
handle.position = Vector3(0.0, 0.45, 0.0)
attach.add_child(handle)
var head := MeshInstance3D.new()
var knob := CylinderMesh.new()
knob.top_radius = 0.2
knob.bottom_radius = 0.16
knob.height = 0.45
knob.material = wood
head.mesh = knob
head.position = Vector3(0.0, 1.05, 0.0)
attach.add_child(head)