- farm.tscn: LettuceField moves to (-1, 0.44, 25) on the SW soil tile (ground_008_004) and CornField to (29, 0.31, 25) on the SE tile (ground_011_003), both still 10 x 10 m. Their units keep ids 31-40 / 41-50, plots 7-10 and sprout nights 2 / 4. The sunflower children are replaced: lettuce rows get 8 cabbage heads each (plant_004/005), corn rows 10 stalks each (plant_001, scaled ~0.8). Every plant's height follows the soil under it (raycast), so none floats. - South Fields (outside zone) moves to (9.5, 41), 66 x 10, between the new south edge (z 35) and the old south fence; its four hay points move there too and rest on the soil. - FarmZones.playable: Rect2(-13, -45, 60, 80), i.e. z -45..35. - ThiefBehavior: one rect per crop field (field_rects) instead of their bounding box; area_for() takes the target's position. - LootSystem: carried/dropped crop tokens show the crop's own plant (CROP_TOKENS), each scene loaded once. - Tests: test_farm_layout checks the 60 x 80 map, outside zones off the map, the later fields on the soil tiles with their own plants standing on the soil, side names for both fields, the open east side south of the fence, and splits the travel bound (12 s core rule, 16 s for the south fields). test_theft: new south edge cell, crop tokens. run_tests EDGE/SKY south points follow the edge. test_targets: the dropped animal is watched from outside the gate (the carrier now dies ~4 m from the west edge). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
4.4 KiB
GDScript
131 lines
4.4 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.
|
|
|
|
## 60 x 80 m, x -13..47, z -45..35. #20: it reaches 20 m further south than the original 60 x 60 m
|
|
## map, over the old plowed soil where the lettuce/cabbage and corn fields now grow.
|
|
@export var playable := Rect2(-13.0, -45.0, 60.0, 80.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
|