Adds a Lettuce/Cabbage field (ids 31-40, x -11..-1 z 2..12) and a Corn field (ids 41-50, x 2..12 z 2..12), each 2 plots of 5 units, placeholder plants reusing plant_007/plant_008. CropUnit gains crop (cosmetic) and sprout_night (0/2/4) fields. LootSystem gains a DORMANT status: a unit whose field hasn't sprouted (FarmState.nights_survived < sprout_night) is hidden and never claimable/recoverable, and is excluded from crops_total so it can't affect scoring or the "farm stripped" check. sync_from_state() derives DORMANT vs. HOME from nights_survived every morning (and after a retry/load), and self-heals crops_total to the sprouted count; FarmSim.grow_crops_total() also grows it right at night_ending so the morning snapshot (retry, the save file) already has it right. FarmZones.crop_units() now gathers CropUnits from every CROP_FIELD zone (there are three now), not just the first found; the same fix applies to FarmSim's crop-area rect and the fence breakers' guarded areas. Reworks test_farm_layout's zone-count and crop checks for the extra fields, and test_theft's start-of-run and FarmState-consistency checks, which assumed every crop unit was always in play; adds test_theft cases for both fields sprouting on schedule, dormant units being unclaimable, and a sprouted unit being stealable and scored, including a save/JSON round trip. Nudges test_coop's co-op build spot into the 3 m walkway between the two new fields, since its old spot now sits on the corn field's rows. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
129 lines
4.2 KiB
GDScript
129 lines
4.2 KiB
GDScript
class_name FarmZones
|
|
extends Node3D
|
|
## The Zones/ node of farm.tscn: every FarmZone (with the props that belong to it), the
|
|
## perimeter (what's left of its fence, on the north and east sides, and that fence's gaps), and
|
|
## the crop units, plus lookups by position. The playable map is the farm's rectangle (where
|
|
## building, navigation, and the night happen). Its edge is where enemies come from and go:
|
|
## edge(margin) is the rectangle that far outside it, and enemies arrive at random points along it
|
|
## (WaveDirector). Beyond the map the scarecrow can roam out to the old farm's outer fences
|
|
## (Perimeter/PlayerBounds), where outside zones hold more to gather.
|
|
|
|
@export var playable := Rect2(-13.0, -45.0, 60.0, 60.0)
|
|
## Where the scarecrow can go: out to the old farm's outer fences.
|
|
@export var roam := Rect2(-27.6, -59.6, 82.4, 106.6)
|
|
|
|
|
|
## The farm's own zones (inside the fence).
|
|
func zones() -> Array[FarmZone]:
|
|
var out: Array[FarmZone] = []
|
|
for child in get_children():
|
|
if child is FarmZone and not (child as FarmZone).outside:
|
|
out.append(child)
|
|
return out
|
|
|
|
|
|
## The zone containing a world position, inside the fence or out (zones don't overlap), or null
|
|
## between zones.
|
|
func zone_at(point: Vector3) -> FarmZone:
|
|
for zone in zones() + outside_zones():
|
|
if zone.contains(point):
|
|
return zone
|
|
return null
|
|
|
|
|
|
## The farm's own zone of a kind (never an outside one). When a kind has more than one zone
|
|
## (#13: three CROP_FIELD zones), this is just the first found: use zones_of() for all of them.
|
|
func zone(kind: FarmZone.Kind) -> FarmZone:
|
|
for z in zones():
|
|
if z.kind == kind:
|
|
return z
|
|
return null
|
|
|
|
|
|
## Every one of the farm's own zones of a kind, in scene order.
|
|
func zones_of(kind: FarmZone.Kind) -> Array[FarmZone]:
|
|
var out: Array[FarmZone] = []
|
|
for z in zones():
|
|
if z.kind == kind:
|
|
out.append(z)
|
|
return out
|
|
|
|
|
|
## The gathering areas beyond the fence.
|
|
func outside_zones() -> Array[FarmZone]:
|
|
var out: Array[FarmZone] = []
|
|
for child in get_children():
|
|
if child is FarmZone and (child as FarmZone).outside:
|
|
out.append(child)
|
|
return out
|
|
|
|
|
|
func is_inside_fence(point: Vector3) -> bool:
|
|
return playable.has_point(Vector2(point.x, point.z))
|
|
|
|
|
|
## The gaps in what's left of the perimeter fence.
|
|
func gaps() -> Array[FarmMarker]:
|
|
var out: Array[FarmMarker] = []
|
|
for node in find_children("*", "Marker3D", true, false):
|
|
if node is FarmMarker:
|
|
out.append(node)
|
|
return out
|
|
|
|
|
|
## The rectangle margin meters outside the playable map's edge.
|
|
func edge(margin: float) -> Rect2:
|
|
return playable.grow(margin)
|
|
|
|
|
|
## The point a share t (0..1, wrapping) of the way around edge(margin), clockwise from its
|
|
## north-west corner.
|
|
func edge_point(t: float, margin: float) -> Vector2:
|
|
var r := edge(margin)
|
|
var d := fposmod(t, 1.0) * 2.0 * (r.size.x + r.size.y)
|
|
if d < r.size.x:
|
|
return r.position + Vector2(d, 0.0)
|
|
d -= r.size.x
|
|
if d < r.size.y:
|
|
return Vector2(r.end.x, r.position.y + d)
|
|
d -= r.size.y
|
|
if d < r.size.x:
|
|
return Vector2(r.end.x - d, r.end.y)
|
|
return Vector2(r.position.x, r.end.y - (d - r.size.x))
|
|
|
|
|
|
## The nearest point on a rectangle's outline to p (from inside: straight out the nearest side).
|
|
static func nearest_on_outline(r: Rect2, p: Vector2) -> Vector2:
|
|
var q := p.clamp(r.position, r.end)
|
|
var to_side := [q.x - r.position.x, r.end.x - q.x, q.y - r.position.y, r.end.y - q.y]
|
|
match to_side.find(to_side.min()):
|
|
0:
|
|
q.x = r.position.x
|
|
1:
|
|
q.x = r.end.x
|
|
2:
|
|
q.y = r.position.y
|
|
_:
|
|
q.y = r.end.y
|
|
return q
|
|
|
|
|
|
## Which side of the farm p is on, as the HUD says it: "north", "south-east", ... (the map split
|
|
## in thirds each way; "" in the middle).
|
|
func side_of(p: Vector2) -> String:
|
|
var c := playable.get_center()
|
|
var u := (p.x - c.x) / (playable.size.x * 0.5)
|
|
var v := (p.y - c.y) / (playable.size.y * 0.5)
|
|
var ns := "north" if v < -1.0 / 3.0 else ("south" if v > 1.0 / 3.0 else "")
|
|
var we := "west" if u < -1.0 / 3.0 else ("east" if u > 1.0 / 3.0 else "")
|
|
return ns + "-" + we if ns != "" and we != "" else ns + we
|
|
|
|
|
|
## Every CropUnit in every CROP_FIELD zone (#13: the original field plus the two later ones).
|
|
func crop_units() -> Array[CropUnit]:
|
|
var out: Array[CropUnit] = []
|
|
for field in zones_of(FarmZone.Kind.CROP_FIELD):
|
|
for node in field.find_children("*", "Node3D", true, false):
|
|
if node is CropUnit:
|
|
out.append(node)
|
|
return out
|