A planned ground arrival whose type has no free character used to be
skipped (spawn_skipped) and lost. Now it waits and comes in as soon as
one frees, without holding back other types; any still waiting at
sunrise are dropped and counted.
- EnemySystem.can_spawn(type): room and, on foot, a free character
(EnemyCharacters.has_free; a corpse counts), no side effects.
- WaveDirector.step: due arrivals that can't spawn go to a per-type
FIFO; waiting ones are admitted first each tick. waited / dropped
counters next to spawned, reset at nightfall; waiting() for tests.
- GameConfig.character_pool {"thief": 48, "breaker": 20} replaces
EnemyCharacters.POOL; the pools are built from it at setup.
- RULES.md, Night plans: the real limits and the waiting rule.
- test_roster: full thief pool -> the thief waits, crows stay on time,
a freed character (corpse) lets it in, sunrise drops and counts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
282 lines
9.3 KiB
GDScript
282 lines
9.3 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 (GameConfig.character_pool) is created at setup and
|
|
## never grows: a ground enemy only spawns while its type has a free character (has_free; a due
|
|
## arrival waits for one, WaveDirector).
|
|
## 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 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
|
|
## Characters per ground type (EnemySystem type id -> count), from GameConfig.character_pool.
|
|
var pool := {}
|
|
## 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
|
|
pool.clear()
|
|
for type_name in p_config.character_pool:
|
|
var t := EnemySystem.TYPE_NAMES.find(String(type_name))
|
|
if not SCENES.has(t):
|
|
push_warning("GameConfig.character_pool: '%s' isn't an enemy type on foot" % type_name)
|
|
continue
|
|
pool[t] = maxi(int(p_config.character_pool[type_name]), 0)
|
|
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)
|
|
|
|
|
|
## The type's characters in all (0 for a type without any).
|
|
func pool_size(t: int) -> int:
|
|
return int(pool.get(t, 0))
|
|
|
|
|
|
## Whether acquire(t, ...) would succeed now: a character of the type not drawing a live enemy (a
|
|
## corpse counts: it can be taken over). No side effects.
|
|
func has_free(t: int) -> bool:
|
|
for c in type.size():
|
|
if type[c] == t and enemy_id[c] < 0:
|
|
return true
|
|
return false
|
|
|
|
|
|
## 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)
|