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

120 lines
3.8 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).
func zone(kind: FarmZone.Kind) -> FarmZone:
for z in zones():
if z.kind == kind:
return z
return null
## 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
func crop_units() -> Array[CropUnit]:
var out: Array[CropUnit] = []
var field := zone(FarmZone.Kind.CROP_FIELD)
if field == null:
return out
for node in field.find_children("*", "Node3D", true, false):
if node is CropUnit:
out.append(node)
return out