Task #10: per-instance Turret/Tar Patch leveling (L1-L3)
Adds a per-placed-structure "level" (1-3) to automated_defense and slowing_trap, upgraded independently (not account-wide) by holding interact at that specific structure, by day, once it's undamaged (repair still takes priority while it isn't), for a config-tunable per-level cost. Turret L2 fires turret_level_fire_mult (3x, matching the live Rapid Fire multiplier) as often; L3 keeps that rate and fires turret_l3_projectiles shots at once in a fan. A tar patch's trigger footprint and visual grow by trap_level_size_mult per level (L2 double, L3 triple its L1 size); a bigger footprint that would overlap a neighbor or the world refuses the upgrade with a reason. FarmState.VERSION bumps 5->6 (STRUCTURE_KEYS gains "level"; migrate() backfills level 1 on older saves, following the v2->v3 precedent for night_log). Co-op: an upgrade is driven the same way repair already is -- entirely host-side, off the partner's held interact button, no new "req" case -- so the only wire change is forwarding the upgrade channel's target/progress in the avatar snapshot, same as repair's. New test_structures.gd coverage: fire interval and projectile count per turret level, tar patch footprint per level, each proven independently for two separately-placed structures of the same type, plus the upgrade rules (repair-first, room, afford) and that level survives a save/retry round trip. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
0613cca408
commit
62324ff4b1
8 changed files with 468 additions and 33 deletions
|
|
@ -5,8 +5,8 @@ extends Node3D
|
|||
## fence 2 m segment that ground enemies can't walk through.
|
||||
## gate a fence segment the scarecrow walks through (it swings open as the
|
||||
## scarecrow nears; it has no collision for the scarecrow) but enemies can't.
|
||||
## slowing_trap 1.5 m patch: a ground enemy stepping on it moves at trap_slow_factor speed
|
||||
## for trap_slow_time, using up one of its trap_triggers. Doesn't block.
|
||||
## slowing_trap 1.5 m patch (at L1): a ground enemy stepping on it moves at trap_slow_factor
|
||||
## speed for trap_slow_time, using up one of its trap_triggers. Doesn't block.
|
||||
## automated_defense 1 m post that shoots the nearest enemy within turret_range with a clear
|
||||
## line, from the shared shot pool, every turret_fire_interval (leading the
|
||||
## target). Flying enemies are hit too (Step 9); fences, gates, and traps
|
||||
|
|
@ -16,13 +16,23 @@ extends Node3D
|
|||
## bar, a red tint, and wobble on each hit; a broken one leaves debris and emits destroyed
|
||||
## ("breach"). Positions snap to a 0.5 m grid, rotations to 45 degree steps (fences run 2 m along
|
||||
## the grid, 1.5 x 1.5 m on the diagonal, so chains line up either way).
|
||||
##
|
||||
## Leveling (#10): a turret or a tar patch (LEVELABLE) can be upgraded independently, L1-L3, from
|
||||
## L1 (Interactions, holding interact at that specific structure; it must be undamaged and the
|
||||
## cost affordable -- see upgrade_blocker). A turret at L2 fires turret_level_fire_mult times as
|
||||
## often (3x, the same live multiplier as the player's Rapid Fire upgrade); L3 keeps that rate but
|
||||
## fires turret_l3_projectiles shots at once in a fan. A tar patch's footprint and visual grow by
|
||||
## trap_level_size_mult[level - 1] (L2 double, L3 triple its L1 size); upgrading refuses with a
|
||||
## reason if the bigger footprint would overlap another structure or the world.
|
||||
|
||||
signal layout_changed
|
||||
signal built(id: int, type: String)
|
||||
signal destroyed(id: int, type: String, position: Vector3)
|
||||
## Damaged but still standing; repaired fully; an automated defense shot; a trap slowed someone.
|
||||
## Damaged but still standing; repaired fully; leveled up; an automated defense shot; a trap
|
||||
## slowed someone.
|
||||
signal hurt(id: int, position: Vector3)
|
||||
signal repaired(id: int)
|
||||
signal leveled_up(id: int, level: int)
|
||||
signal turret_fired(position: Vector3)
|
||||
signal trap_sprung(position: Vector3)
|
||||
|
||||
|
|
@ -34,6 +44,9 @@ const NAMES := {
|
|||
"slowing_trap": "Tar patch",
|
||||
"automated_defense": "Turret",
|
||||
}
|
||||
## Structure types with a per-instance level (#10); every other type is always level 1.
|
||||
const LEVELABLE: Array[String] = ["automated_defense", "slowing_trap"]
|
||||
const MAX_LEVEL := 3
|
||||
const GRID := 0.5
|
||||
const FENCE_LENGTH := 2.0
|
||||
const DIAGONAL_FENCE_LENGTH := 2.1213 # 1.5 * sqrt(2)
|
||||
|
|
@ -128,9 +141,10 @@ static func snap(p: Vector2) -> Vector2:
|
|||
return Vector2(roundf(p.x / GRID) * GRID, roundf(p.y / GRID) * GRID)
|
||||
|
||||
|
||||
## Oriented footprint {"center", "half", "angle"} (angle: the long axis from +X toward +Z).
|
||||
func footprint(type: String, x: float, z: float, rot: int) -> Dictionary:
|
||||
var half := Vector2(0.75, 0.75) if type == "slowing_trap" else Vector2(0.5, 0.5)
|
||||
## Oriented footprint {"center", "half", "angle"} (angle: the long axis from +X toward +Z). level
|
||||
## only matters for a slowing_trap (trap_level_size_mult); every other type ignores it.
|
||||
func footprint(type: String, x: float, z: float, rot: int, level: int = 1) -> Dictionary:
|
||||
var half := Vector2(0.75, 0.75) * trap_size_mult(level) if type == "slowing_trap" else Vector2(0.5, 0.5)
|
||||
var angle := 0.0
|
||||
if type == "fence" or type == "gate":
|
||||
half = Vector2((DIAGONAL_FENCE_LENGTH if rot % 2 == 1 else FENCE_LENGTH) * 0.5, FENCE_THICKNESS * 0.5)
|
||||
|
|
@ -138,6 +152,21 @@ func footprint(type: String, x: float, z: float, rot: int) -> Dictionary:
|
|||
return {"center": Vector2(x, z), "half": half, "angle": angle}
|
||||
|
||||
|
||||
## A slowing trap's footprint/visual size at level (1-3), relative to its L1 size.
|
||||
func trap_size_mult(level: int) -> float:
|
||||
var mults := config.trap_level_size_mult
|
||||
return float(mults[clampi(level - 1, 0, mults.size() - 1)]) if not mults.is_empty() else 1.0
|
||||
|
||||
|
||||
func is_levelable(type: String) -> bool:
|
||||
return LEVELABLE.has(type)
|
||||
|
||||
|
||||
func level_of(id: int) -> int:
|
||||
var e := campaign.state.structure(id)
|
||||
return int(e.get("level", 1)) if not e.is_empty() else 1
|
||||
|
||||
|
||||
## Closed outline (5 points) of a footprint, optionally shrunk along its length / width.
|
||||
static func corners(fp: Dictionary, shrink_long: float = 0.0, shrink_wide: float = 0.0) -> PackedVector2Array:
|
||||
var c: Vector2 = fp["center"]
|
||||
|
|
@ -167,7 +196,7 @@ func validate(type: String, x: float, z: float, rot: int, scarecrow: Vector2) ->
|
|||
return "Not on the crops"
|
||||
var tight := corners(fp, 0.2, 0.02)
|
||||
for e in campaign.state.structures:
|
||||
if _overlap(tight, corners(footprint(e["type"], e["x"], e["z"], e["rot"]), 0.2, 0.02)):
|
||||
if _overlap(tight, corners(footprint(e["type"], e["x"], e["z"], e["rot"], int(e.get("level", 1))), 0.2, 0.02)):
|
||||
return "Something is built here already"
|
||||
if _world_blocked(fp):
|
||||
return "Blocked"
|
||||
|
|
@ -196,7 +225,7 @@ func place(type: String, x: float, z: float, rot: int) -> int:
|
|||
return -1
|
||||
var id := s.next_structure_id
|
||||
s.next_structure_id += 1
|
||||
s.structures.append({"id": id, "type": type, "x": x, "z": z, "rot": rot, "hp": max_hp(type)})
|
||||
s.structures.append({"id": id, "type": type, "x": x, "z": z, "rot": rot, "hp": max_hp(type), "level": 1})
|
||||
s.record_event("structures_built")
|
||||
_add_view(s.structures[s.structures.size() - 1])
|
||||
_update_nav()
|
||||
|
|
@ -274,6 +303,65 @@ func repair(id: int) -> bool:
|
|||
return true
|
||||
|
||||
|
||||
## What upgrading this structure to its next level costs now (empty if it isn't levelable or is
|
||||
## already at MAX_LEVEL, regardless of whether it's otherwise affordable or in reach).
|
||||
func upgrade_cost(id: int) -> Dictionary:
|
||||
var e := campaign.state.structure(id)
|
||||
if e.is_empty() or not is_levelable(e["type"]):
|
||||
return {}
|
||||
var level := int(e.get("level", 1))
|
||||
if level >= MAX_LEVEL:
|
||||
return {}
|
||||
var tiers: Array = config.structure_level_costs.get(e["type"], [])
|
||||
var idx := level - 1 # 0: L1 -> L2, 1: L2 -> L3
|
||||
return tiers[idx] if idx >= 0 and idx < tiers.size() else {}
|
||||
|
||||
|
||||
## Why id can't be upgraded right now ("" if it can): levelable, undamaged (repair takes
|
||||
## priority), under MAX_LEVEL, a bigger tar patch footprint clear of neighbors and the world, and
|
||||
## the cost affordable. Doesn't check the time of day (Interactions, like the build-mode rules).
|
||||
func upgrade_blocker(id: int) -> String:
|
||||
var e := campaign.state.structure(id)
|
||||
if e.is_empty() or not is_levelable(e["type"]):
|
||||
return "Can't be upgraded"
|
||||
if int(e["hp"]) < max_hp(e["type"]):
|
||||
return "Repair it first"
|
||||
var level := int(e.get("level", 1))
|
||||
var cost := upgrade_cost(id)
|
||||
if level >= MAX_LEVEL or cost.is_empty():
|
||||
return "Max level"
|
||||
if e["type"] == "slowing_trap":
|
||||
var fp := footprint(e["type"], e["x"], e["z"], e["rot"], level + 1)
|
||||
var tight := corners(fp, 0.2, 0.02)
|
||||
for other in campaign.state.structures:
|
||||
if int(other["id"]) == id:
|
||||
continue
|
||||
var other_fp := footprint(other["type"], other["x"], other["z"], other["rot"], int(other.get("level", 1)))
|
||||
if _overlap(tight, corners(other_fp, 0.2, 0.02)):
|
||||
return "Not enough room to upgrade"
|
||||
if _world_blocked(fp):
|
||||
return "Not enough room to upgrade"
|
||||
if not campaign.state.resources.can_afford(cost):
|
||||
return "Not enough: need %s more" % ResourceStore.describe(campaign.state.resources.missing(cost))
|
||||
return ""
|
||||
|
||||
|
||||
## Pays and levels a structure up by one (see upgrade_blocker). Returns false if it couldn't be.
|
||||
func upgrade(id: int) -> bool:
|
||||
if upgrade_blocker(id) != "":
|
||||
return false
|
||||
var cost := upgrade_cost(id)
|
||||
if not campaign.state.pay(cost):
|
||||
return false
|
||||
var e := campaign.state.structure(id)
|
||||
e["level"] = int(e.get("level", 1)) + 1
|
||||
campaign.state.record_event("upgrades_bought")
|
||||
_rebuild_view(id)
|
||||
_update_nav()
|
||||
leveled_up.emit(id, int(e["level"]))
|
||||
return true
|
||||
|
||||
|
||||
## The structure whose footprint is within reach of p, preferring damaged ones, or -1.
|
||||
func structure_near(p: Vector2, reach: float, damaged_only: bool = false) -> int:
|
||||
var best := -1
|
||||
|
|
@ -281,7 +369,22 @@ func structure_near(p: Vector2, reach: float, damaged_only: bool = false) -> int
|
|||
for e in campaign.state.structures:
|
||||
if damaged_only and int(e["hp"]) >= max_hp(e["type"]):
|
||||
continue
|
||||
var d := _distance_to_outline(p, corners(footprint(e["type"], e["x"], e["z"], e["rot"])))
|
||||
var d := _distance_to_outline(p, corners(footprint(e["type"], e["x"], e["z"], e["rot"], int(e.get("level", 1)))))
|
||||
if d <= reach and d < best_d:
|
||||
best = int(e["id"])
|
||||
best_d = d
|
||||
return best
|
||||
|
||||
|
||||
## The nearest levelable structure (turret, tar patch) whose footprint is within reach of p, or -1
|
||||
## (whether or not it can actually be upgraded right now -- see upgrade_blocker).
|
||||
func upgradeable_near(p: Vector2, reach: float) -> int:
|
||||
var best := -1
|
||||
var best_d := INF
|
||||
for e in campaign.state.structures:
|
||||
if not is_levelable(e["type"]):
|
||||
continue
|
||||
var d := _distance_to_outline(p, corners(footprint(e["type"], e["x"], e["z"], e["rot"], int(e.get("level", 1)))))
|
||||
if d <= reach and d < best_d:
|
||||
best = int(e["id"])
|
||||
best_d = d
|
||||
|
|
@ -294,7 +397,8 @@ func try_trap(x: float, z: float) -> bool:
|
|||
for e in campaign.state.structures:
|
||||
if e["type"] != "slowing_trap" or int(e["hp"]) <= 0:
|
||||
continue
|
||||
if absf(x - float(e["x"])) <= 0.75 and absf(z - float(e["z"])) <= 0.75:
|
||||
var half := 0.75 * trap_size_mult(int(e.get("level", 1)))
|
||||
if absf(x - float(e["x"])) <= half and absf(z - float(e["z"])) <= half:
|
||||
e["hp"] = int(e["hp"]) - 1
|
||||
trap_sprung.emit(Vector3(e["x"], 0.0, e["z"]))
|
||||
return true
|
||||
|
|
@ -329,9 +433,23 @@ func step(dt: float) -> void:
|
|||
var aim := Vector2(enemies.pos_x[target] + enemies.vel_x[target] * lead, enemies.pos_z[target] + enemies.vel_z[target] * lead) - from
|
||||
var dir := Vector3(aim.x, 0.0, aim.y).normalized()
|
||||
var origin := Vector3(from.x, config.combat_height, from.y)
|
||||
projectiles.fire(origin, origin + dir * 0.6, dir)
|
||||
var level := int(e.get("level", 1))
|
||||
if level >= 3:
|
||||
# L3: turret_l3_projectiles shots at once, fanned turret_l3_spread_deg apart (a
|
||||
# triangle spread: the center shot plus one off either side of dir).
|
||||
var shots: int = config.turret_l3_projectiles
|
||||
var spread := deg_to_rad(config.turret_l3_spread_deg)
|
||||
for i in shots:
|
||||
var offset := (float(i) - float(shots - 1) * 0.5) * spread
|
||||
var shot_dir := dir.rotated(Vector3.UP, offset)
|
||||
projectiles.fire(origin, origin + shot_dir * 0.6, shot_dir)
|
||||
else:
|
||||
projectiles.fire(origin, origin + dir * 0.6, dir)
|
||||
turret_fired.emit(origin)
|
||||
_turret_cd[id] = config.turret_fire_interval
|
||||
var interval := config.turret_fire_interval
|
||||
if level >= 2:
|
||||
interval /= config.turret_level_fire_mult
|
||||
_turret_cd[id] = interval
|
||||
var head: Node3D = _heads.get(id)
|
||||
if head != null:
|
||||
head.rotation.y = atan2(-dir.x, -dir.z)
|
||||
|
|
@ -414,18 +532,32 @@ func _remove(id: int) -> void:
|
|||
_update_nav()
|
||||
|
||||
|
||||
## Rebuilds a structure's view after a level change (its size or its gun may have changed), keeping
|
||||
## its cooldown (a turret doesn't reset its reload on leveling up).
|
||||
func _rebuild_view(id: int) -> void:
|
||||
var e := campaign.state.structure(id)
|
||||
if e.is_empty():
|
||||
return
|
||||
var view: Node3D = _views.get(id)
|
||||
if view != null:
|
||||
view.queue_free()
|
||||
for d in [_views, _bars, _tints, _wobble, _heads]:
|
||||
(d as Dictionary).erase(id)
|
||||
_add_view(e)
|
||||
|
||||
|
||||
func _update_nav() -> void:
|
||||
var entries: Array = []
|
||||
for e in campaign.state.structures:
|
||||
if blocks(e["type"]):
|
||||
entries.append({"id": e["id"], "outline": corners(footprint(e["type"], e["x"], e["z"], e["rot"]))})
|
||||
entries.append({"id": e["id"], "outline": corners(footprint(e["type"], e["x"], e["z"], e["rot"], int(e.get("level", 1))))})
|
||||
nav.set_structures(entries)
|
||||
layout_changed.emit()
|
||||
|
||||
|
||||
func _add_view(e: Dictionary) -> void:
|
||||
var id: int = e["id"]
|
||||
var view := _build_view(e["type"], e["rot"])
|
||||
var view := _build_view(e["type"], e["rot"], int(e.get("level", 1)))
|
||||
view.name = "%s_%d" % [e["type"], id]
|
||||
add_child(view)
|
||||
_views[id] = view
|
||||
|
|
@ -460,11 +592,11 @@ func _add_view(e: Dictionary) -> void:
|
|||
|
||||
|
||||
## The visual (and, for barriers other than gates, collision) of a structure, centered on its
|
||||
## footprint and rotated to it.
|
||||
func _build_view(type: String, rot: int) -> Node3D:
|
||||
## footprint and rotated to it. level only changes a slowing_trap's size (trap_size_mult).
|
||||
func _build_view(type: String, rot: int, level: int = 1) -> Node3D:
|
||||
var root := Node3D.new()
|
||||
root.physics_interpolation_mode = Node.PHYSICS_INTERPOLATION_MODE_OFF
|
||||
var fp := footprint(type, 0.0, 0.0, rot)
|
||||
var fp := footprint(type, 0.0, 0.0, rot, level)
|
||||
root.rotation.y = -float(fp["angle"])
|
||||
match type:
|
||||
"fence":
|
||||
|
|
@ -484,10 +616,11 @@ func _build_view(type: String, rot: int) -> Node3D:
|
|||
post.position = Vector3(end * 0.95, 0.0, 0.0)
|
||||
root.add_child(post)
|
||||
"slowing_trap":
|
||||
var mult := trap_size_mult(level)
|
||||
var tar := MeshInstance3D.new()
|
||||
tar.name = "Tar"
|
||||
var box := BoxMesh.new()
|
||||
box.size = Vector3(1.4, 0.06, 1.4)
|
||||
box.size = Vector3(1.4, 0.06, 1.4) * mult
|
||||
var mat := StandardMaterial3D.new()
|
||||
mat.albedo_color = Color(0.1, 0.07, 0.05)
|
||||
mat.roughness = 0.2
|
||||
|
|
@ -497,7 +630,7 @@ func _build_view(type: String, rot: int) -> Node3D:
|
|||
tar.position.y = 0.04
|
||||
root.add_child(tar)
|
||||
var bucket := _instance("prop/bucket_001.tscn")
|
||||
bucket.position = Vector3(0.55, 0.0, 0.55)
|
||||
bucket.position = Vector3(0.55 * mult, 0.0, 0.55 * mult)
|
||||
_strip_collision(bucket)
|
||||
root.add_child(bucket)
|
||||
"automated_defense":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue