Grass Cutters Together: the project as of moon-sweepers f3666f0

A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and
Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date.
Images, audio, models and binaries go through Git LFS (.gitattributes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiemandKG
2026-11-06 18:44:40 +06:00
parent 0f0467cd0c
commit d1c3761e58
4190 changed files with 270757 additions and 1 deletions

View File

@ -0,0 +1,314 @@
using System.Collections.Generic;
using UnityEngine;
namespace GrassCutter
{
/// <summary>
/// A deck to build on. On the ground: a timber slab, square or hexagonal, 3 m across, its footing sunk into the ground so it
/// stays level on a slope; lifted (Ctrl+wheel while placing) it stands on timber stilts down to the soil instead. A storey up:
/// a floor or a flat roof resting on walls. What stands on a deck sits on a half-metre grid and turns with it, walls go on its
/// edges, and neighbouring decks of a kind line up edge to edge at the same height.
/// </summary>
public class Foundation : MonoBehaviour
{
public const float Across = 3f, Top = 0.3f, Sunk = 1.5f;
/// <summary>A slab lifted more than this stands on stilts; a raised deck is this deep.</summary>
public const float Stilts = 0.25f, DeckDepth = 0.4f;
/// <summary>How high a foundation can be lifted off the ground.</summary>
public const float MaxRaise = 6f;
/// <summary>Height of a deck's walking surface above its origin: foundations stand on the ground, floors hang from their top.</summary>
public static float TopOf(PieceId id) => GameDefs.IsFoundation(id) ? Top : 0f;
static Mesh s_square, s_hex, s_squareDeck, s_hexDeck, s_floor;
static float SquareR => Across * Mathf.Sqrt(0.5f);
static float HexR => Across * 0.5f / Mathf.Cos(30f * Mathf.Deg2Rad);
public static Mesh Shape(PieceId id, bool raised = false)
{
if (id == PieceId.Floor || id == PieceId.FlatRoof) return s_floor ? s_floor : (s_floor = Slab(4, SquareR, 45f, 0f, -0.3f));
if (id == PieceId.Hex)
{
if (raised) return s_hexDeck ? s_hexDeck : (s_hexDeck = Slab(6, HexR, 30f, Top, Top - DeckDepth));
return s_hex ? s_hex : (s_hex = Slab(6, HexR, 30f, Top, -Sunk));
}
if (raised) return s_squareDeck ? s_squareDeck : (s_squareDeck = Slab(4, SquareR, 45f, Top, Top - DeckDepth));
return s_square ? s_square : (s_square = Slab(4, SquareR, 45f, Top, -Sunk));
}
/// <summary>A flat-topped prism with the corners on a circle.</summary>
static Mesh Slab(int sides, float radius, float turn, float top, float bottom)
{
var v = new List<Vector3>();
var n = new List<Vector3>();
var uv = new List<Vector2>();
var t = new List<int>();
Vector3 Corner(int i, float y)
{
float a = (turn + i * 360f / sides) * Mathf.Deg2Rad;
return new Vector3(Mathf.Sin(a) * radius, y, Mathf.Cos(a) * radius);
}
for (int i = 0; i < sides; i++)
{
v.Add(Corner(i, top));
n.Add(Vector3.up);
uv.Add(new Vector2(v[i].x, v[i].z));
}
for (int i = 1; i < sides - 1; i++) { t.Add(0); t.Add(i); t.Add(i + 1); }
for (int i = 0; i < sides; i++)
{
int b = v.Count;
Vector3 p = Corner(i, top), q = Corner(i + 1, top), r = Corner(i + 1, bottom), s = Corner(i, bottom);
var face = Vector3.Cross(r - p, q - p).normalized;
v.Add(p); v.Add(q); v.Add(r); v.Add(s);
n.Add(face); n.Add(face); n.Add(face); n.Add(face);
uv.Add(new Vector2(0f, top)); uv.Add(new Vector2(Across, top)); uv.Add(new Vector2(Across, bottom)); uv.Add(new Vector2(0f, bottom));
t.Add(b); t.Add(b + 2); t.Add(b + 1);
t.Add(b); t.Add(b + 3); t.Add(b + 2);
}
// the underside, so a raised deck isn't hollow from below
int u = v.Count;
for (int i = 0; i < sides; i++)
{
v.Add(Corner(i, bottom));
n.Add(Vector3.down);
uv.Add(new Vector2(v[u + i].x, v[u + i].z));
}
for (int i = 1; i < sides - 1; i++) { t.Add(u); t.Add(u + i + 1); t.Add(u + i); }
var m = new Mesh { name = "Slab" + sides };
m.SetVertices(v);
m.SetNormals(n);
m.SetUVs(0, uv);
m.SetTriangles(t, 0);
m.RecalculateBounds();
return m;
}
BuildPiece _piece;
MeshCollider _collider;
Transform _slab, _model, _stilts;
bool _raised;
Vector3 _fitAt = new Vector3(float.NaN, 0f, 0f);
void Awake()
{
_piece = GetComponent<BuildPiece>();
var slab = new GameObject("Slab") { layer = gameObject.layer };
slab.transform.SetParent(transform, false);
_slab = slab.transform;
_collider = slab.AddComponent<MeshCollider>();
_collider.sharedMesh = Shape(_piece.piece);
_collider.convex = true;
Dress(_piece.Def.model);
}
void Start() => Fit();
void LateUpdate()
{
if ((transform.position - _fitAt).sqrMagnitude > 1e-4f) Fit();
}
void Dress(string name)
{
if (_model) Destroy(_model.gameObject);
_model = null;
var model = AssetBank.I ? AssetBank.I.Model(name) : null;
if (!model) return;
var deck = Instantiate(model, _slab);
foreach (var other in deck.GetComponentsInChildren<Collider>(true)) Destroy(other);
_model = deck.transform;
}
/// <summary>Lifted off the ground: the deck alone on stilts down to the soil; on the ground, the slab with its footing.</summary>
void Fit()
{
_fitAt = transform.position;
if (!GameDefs.IsFoundation(_piece.piece)) return;
bool raised = Planet.Altitude(transform.position) > Stilts;
if (raised != _raised)
{
_raised = raised;
_collider.sharedMesh = Shape(_piece.piece, raised);
string deck = _piece.piece == PieceId.Hex ? "SM_Deck_Hex" : "SM_Deck_Square";
Dress(raised && AssetBank.I && AssetBank.I.Model(deck) ? deck : _piece.Def.model);
}
if (_stilts) Destroy(_stilts.gameObject);
_stilts = null;
if (raised) Stand();
}
/// <summary>Timber legs from under the deck to the soil, braced where they're tall.</summary>
void Stand()
{
_stilts = new GameObject("Stilts") { layer = gameObject.layer }.transform;
_stilts.SetParent(transform, false);
var wood = BuildLook.Mat("M_Timber", new Color(0.55f, 0.4f, 0.25f));
int sides = _piece.piece == PieceId.Hex ? 6 : 4;
float reach = (sides == 6 ? HexR : SquareR) - 0.32f, turn = sides == 6 ? 30f : 45f;
float under = Top - DeckDepth;
var feet = new Vector3[sides];
var lengths = new float[sides];
// the turn alone, not the bounce the piece lands with: the ground is measured in metres
var rot = transform.rotation;
for (int i = 0; i < sides; i++)
{
float a = (turn + i * 360f / sides) * Mathf.Deg2Rad;
var foot = new Vector3(Mathf.Sin(a) * reach, under, Mathf.Cos(a) * reach);
float drop = Mathf.Max(0.2f, Planet.Altitude(transform.position + rot * foot)) + 0.35f;
feet[i] = foot;
lengths[i] = drop;
Beam(wood, foot + Vector3.down * drop * 0.5f, Quaternion.identity, new Vector3(0.22f, drop, 0.22f));
// a stone pad where it meets the soil
Beam(BuildLook.Mat("M_Stone", new Color(0.55f, 0.54f, 0.5f)), foot + Vector3.down * (drop - 0.3f), Quaternion.identity, new Vector3(0.42f, 0.16f, 0.42f));
}
for (int i = 0; i < sides; i++)
{
int j = (i + 1) % sides;
float tall = Mathf.Min(lengths[i], lengths[j]) - 0.35f;
if (tall < 0.9f) continue;
// a rail under the deck and a diagonal brace on each side
var a = feet[i] + Vector3.down * 0.25f;
var b = feet[j] + Vector3.down * Mathf.Min(tall * 0.8f, 2.6f);
Strut(wood, feet[i] + Vector3.down * 0.18f, feet[j] + Vector3.down * 0.18f, 0.12f);
Strut(wood, a, b, 0.1f);
}
}
void Strut(Material m, Vector3 a, Vector3 b, float w)
{
var d = b - a;
Beam(m, (a + b) * 0.5f, Quaternion.LookRotation(d), new Vector3(w, w * 1.2f, d.magnitude));
}
void Beam(Material m, Vector3 at, Quaternion rot, Vector3 size)
{
var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.name = "Stilt";
go.layer = gameObject.layer;
go.transform.SetParent(_stilts, false);
go.transform.localPosition = at;
go.transform.localRotation = rot;
go.transform.localScale = size;
go.GetComponent<Renderer>().sharedMaterial = m;
}
/// <summary>Half-metre grid on a deck, level with its top.</summary>
public static Vector3 Grid(Transform slab, Vector3 point)
{
var piece = slab.GetComponent<BuildPiece>();
var l = Quaternion.Inverse(slab.rotation) * (point - slab.position);
l.x = Mathf.Round(l.x * 2f) * 0.5f;
l.z = Mathf.Round(l.z * 2f) * 0.5f;
l.y = piece ? TopOf(piece.piece) : Top;
return slab.position + slab.rotation * l;
}
/// <summary>The spot next to a deck of the same kind nearest to a point, edge to edge with it and at its height.</summary>
public static bool Beside(Vector3 point, PieceId id, out Vector3 at, out Quaternion rot)
{
at = point; rot = Quaternion.identity;
BuildPiece near = null;
float best = 36f;
foreach (var p in BuildPiece.All)
{
if (!p || !GameDefs.IsDeck(p.piece)) continue;
float d = (p.transform.position + p.transform.rotation * Vector3.up * TopOf(p.piece) - point).sqrMagnitude;
if (d < best) { best = d; near = p; }
}
if (!near || near.piece != id) return false;
int sides = id == PieceId.Hex ? 6 : 4;
var t = near.transform;
rot = t.rotation;
best = float.MaxValue;
for (int i = 0; i < sides; i++)
{
var c = t.position + t.rotation * (Quaternion.Euler(0f, i * 360f / sides, 0f) * Vector3.forward) * Across;
float d = (c - point).sqrMagnitude;
if (d < best) { best = d; at = c; }
}
return true;
}
}
/// <summary>Where walls, floors and roofs go: walls on the edges of a square deck, floors and roofs on top of the walls.</summary>
public static class Snap
{
/// <summary>The edge of the square deck nearest to the point, facing out (or in, every other turn of R).</summary>
public static bool Edge(Vector3 point, BuildPiece on, float yaw, out Vector3 at, out Quaternion rot)
{
at = point; rot = Quaternion.identity;
var deck = on && GameDefs.IsSquareDeck(on.piece) ? on : null;
if (!deck)
{
float near = 4.5f * 4.5f;
foreach (var p in BuildPiece.All)
{
if (!p || !GameDefs.IsSquareDeck(p.piece)) continue;
float d = (Top(p) - point).sqrMagnitude;
if (d < near) { near = d; deck = p; }
}
}
if (!deck) return false;
var t = deck.transform;
var c = Top(deck);
float best = float.MaxValue;
var outward = t.rotation * Vector3.forward;
for (int i = 0; i < 4; i++)
{
var dir = t.rotation * (Quaternion.Euler(0f, i * 90f, 0f) * Vector3.forward);
var e = c + dir * Foundation.Across * 0.5f;
float d = (e - point).sqrMagnitude;
if (d < best) { best = d; at = e; outward = dir; }
}
if (Mathf.Repeat(yaw, 360f) >= 180f) outward = -outward;
rot = Quaternion.LookRotation(outward, t.rotation * Vector3.up);
return true;
}
/// <summary>
/// A storey up: aimed at a wall (or a pillar), the 3 m cell beside it on the side you're looking from, level with its top;
/// a pitched roof's eave goes along that wall. Standing on a floor, a pitched roof goes on the floor itself.
/// </summary>
public static bool OnTop(PieceId id, Vector3 point, BuildPiece target, BuildPiece on, float yaw, out Vector3 at, out Quaternion rot)
{
at = point; rot = Quaternion.identity;
var spin = Quaternion.Euler(0f, Mathf.Round(yaw / 90f) * 90f, 0f);
if (target && GameDefs.Holds(target.piece))
{
var t = target.transform;
var up = t.rotation * Vector3.up;
var top = t.position + up * GameDefs.Storey;
Vector3 cell;
if (target.piece == PieceId.Pillar)
{
var l = Quaternion.Inverse(t.rotation) * (point - top);
cell = top + t.rotation * new Vector3(Mathf.Sign(l.x) * 1.5f, 0f, Mathf.Sign(l.z) * 1.5f);
}
else
{
var f = t.rotation * Vector3.forward;
var inside = top - f * 1.5f;
var outside = top + f * 1.5f;
cell = (inside - point).sqrMagnitude <= (outside - point).sqrMagnitude ? inside : outside;
}
var toWall = top - cell;
toWall = Vector3.ProjectOnPlane(toWall, up);
if (toWall.sqrMagnitude < 0.01f) toWall = t.rotation * Vector3.forward;
at = cell;
rot = Quaternion.LookRotation(toWall.normalized, up) * spin;
return true;
}
if (id == PieceId.Roof && on && GameDefs.IsSquareDeck(on.piece))
{
at = Top(on);
rot = on.transform.rotation * spin;
return true;
}
return false;
}
static Vector3 Top(BuildPiece deck) => deck.transform.position + deck.transform.rotation * Vector3.up * Foundation.TopOf(deck.piece);
}
}