Map sectors show again, clients get their own machines and the train's swath, machines stay out of the ground
- Map: the sectors are wound the screen's way (most were wound the other way and culled, leaving a blank chart), kept inside the chart, and drawn as ball, sectors and marks layers; the scale bar is no longer sized mid-draw. - Players' save keys are replicated, so a client's garage finds its machines and their upgrades; two players under one key get separate ones. - The grass train sends its mowing to the clients; moving the depot or resizing the planet relays its line. - A hard floor under vehicle springs: hard landings, steep rises and hand-overs no longer sink a machine. - Tests: -demosink, -demorep host|client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -24,7 +24,7 @@ namespace GrassCutter
|
||||
const int MaxHexes = 9000;
|
||||
|
||||
public VisualElement Card { get; }
|
||||
readonly VisualElement _chart, _scaleBar;
|
||||
readonly VisualElement _chart, _ball, _ground, _marks, _scaleBar;
|
||||
readonly Label _stats, _scaleText;
|
||||
Quaternion _view;
|
||||
float _zoom = -1f;
|
||||
@ -61,13 +61,18 @@ namespace GrassCutter
|
||||
UIK.Btn(Loc.T("ME"), () => { _follow = true; Centre(); }, "btn btn-small", tools);
|
||||
UIK.Btn("+", () => ZoomBy(1.6f), "btn btn-small", tools);
|
||||
UIK.Btn("−", () => ZoomBy(1f / 1.6f), "btn btn-small", tools);
|
||||
UIK.Btn(Loc.T("GLOBE"), () => { _zoom = 1f; _chart.MarkDirtyRepaint(); }, "btn btn-small", tools);
|
||||
UIK.Btn(Loc.T("GLOBE"), () => { _zoom = 1f; Repaint(); }, "btn btn-small", tools);
|
||||
UIK.Btn("X", close, "btn btn-red btn-small", header);
|
||||
_chart = UIK.El("map-chart", Card);
|
||||
_chart.pickingMode = PickingMode.Position;
|
||||
_chart.style.overflow = Overflow.Hidden;
|
||||
_chart.focusable = true;
|
||||
_chart.generateVisualContent += Draw;
|
||||
// space is the chart's own colour; over it three layers, each its own element so the order is plain: the
|
||||
// ball, the sectors on it, the marks.
|
||||
_chart.style.backgroundColor = Space;
|
||||
_ball = Layer(DrawBall);
|
||||
_ground = Layer(DrawGround);
|
||||
_marks = Layer(DrawMarks);
|
||||
_chart.RegisterCallback<WheelEvent>(e => ZoomBy(e.delta.y > 0 ? 0.8f : 1.25f));
|
||||
_chart.RegisterCallback<PointerDownEvent>(e => { _dragging = true; _follow = false; _last = e.position; _chart.CapturePointer(e.pointerId); });
|
||||
_chart.RegisterCallback<PointerUpEvent>(e => { _dragging = false; _chart.ReleasePointer(e.pointerId); });
|
||||
@ -78,7 +83,7 @@ namespace GrassCutter
|
||||
_last = e.position;
|
||||
float k = 57.3f / (Radius * Zoom);
|
||||
_view = Quaternion.Euler(d.y * k, d.x * k, 0f) * _view;
|
||||
_chart.MarkDirtyRepaint();
|
||||
Repaint();
|
||||
});
|
||||
_chart.schedule.Execute(() =>
|
||||
{
|
||||
@ -102,6 +107,8 @@ namespace GrassCutter
|
||||
Key(legend, Station, Loc.T("Station"));
|
||||
UIK.Text(Loc.T("DRAG move · WHEEL zoom · SPACE back to you · M close"), "t-small t-muted", legend, false);
|
||||
|
||||
// asked to open at a zoom of its own (1 is "close in on you", the usual)
|
||||
if (zoom != 1f) _zoom = Mathf.Max(1f, zoom);
|
||||
Centre();
|
||||
Refresh();
|
||||
_chart.schedule.Execute(Refresh).Every(500);
|
||||
@ -113,18 +120,18 @@ namespace GrassCutter
|
||||
get
|
||||
{
|
||||
// the first time it knows its size: open a few hundred metres across
|
||||
if (_zoom < 0f && _chart.contentRect.width > 4f) _zoom = Mathf.Clamp(PxPerMetre(OpenMetres) * Planet.Radius / Radius, 1f, MaxZoom);
|
||||
if (_zoom < 0f && Area.width > 4f) _zoom = Mathf.Clamp(PxPerMetre(OpenMetres) * Planet.Radius / Radius, 1f, MaxZoom);
|
||||
return _zoom < 0f ? 1f : _zoom;
|
||||
}
|
||||
}
|
||||
|
||||
float MaxZoom => Mathf.Max(1f, PxPerMetre(NearestMetres) * Planet.Radius / Radius);
|
||||
float PxPerMetre(float across) => Mathf.Max(100f, _chart.contentRect.width) / across;
|
||||
float PxPerMetre(float across) => Mathf.Max(100f, Area.width) / across;
|
||||
|
||||
void ZoomBy(float k)
|
||||
{
|
||||
_zoom = Mathf.Clamp(Zoom * k, 1f, MaxZoom);
|
||||
_chart.MarkDirtyRepaint();
|
||||
Repaint();
|
||||
}
|
||||
|
||||
/// <summary>You in the middle, north up.</summary>
|
||||
@ -135,7 +142,7 @@ namespace GrassCutter
|
||||
var north = Vector3.ProjectOnPlane(Vector3.up, up);
|
||||
if (north.sqrMagnitude < 1e-4f) north = Vector3.forward;
|
||||
_view = Quaternion.Inverse(Quaternion.LookRotation(up, north));
|
||||
_chart?.MarkDirtyRepaint();
|
||||
Repaint();
|
||||
}
|
||||
|
||||
void Refresh()
|
||||
@ -145,7 +152,7 @@ namespace GrassCutter
|
||||
if (ts) _stats.text = Loc.F("{0:N0} started · {1:N0} mown · {2:0.0000}% of the planet", MowMap.Started, ts.Closed.Value, ts.Mown01 * 100.0);
|
||||
// keeps up with you as you go, until you drag it somewhere else
|
||||
if (_follow) Centre();
|
||||
_chart.MarkDirtyRepaint();
|
||||
Repaint();
|
||||
}
|
||||
|
||||
static void Key(VisualElement parent, Color c, string label)
|
||||
@ -159,7 +166,7 @@ namespace GrassCutter
|
||||
{
|
||||
get
|
||||
{
|
||||
var r = _chart.contentRect;
|
||||
var r = Area;
|
||||
return Mathf.Max(40f, Mathf.Min(r.width, r.height) * 0.47f);
|
||||
}
|
||||
}
|
||||
@ -168,7 +175,7 @@ namespace GrassCutter
|
||||
{
|
||||
var v = _view * d;
|
||||
front = v.z > 0f;
|
||||
return _chart.contentRect.center + new Vector2(v.x, -v.y) * Radius * Zoom;
|
||||
return Area.center + new Vector2(v.x, -v.y) * Radius * Zoom;
|
||||
}
|
||||
|
||||
/// <summary>A sector's corners, in order round its middle.</summary>
|
||||
@ -188,24 +195,94 @@ namespace GrassCutter
|
||||
|
||||
static Color Shade(MowMap.Mask m) => m == null || !m.keep ? Meadow : m.done ? Done : Begun;
|
||||
|
||||
void Draw(MeshGenerationContext ctx)
|
||||
VisualElement Layer(Action<MeshGenerationContext> draw)
|
||||
{
|
||||
var r = _chart.contentRect;
|
||||
var e = new VisualElement { pickingMode = PickingMode.Ignore };
|
||||
e.style.position = Position.Absolute;
|
||||
e.style.left = 0f; e.style.top = 0f; e.style.right = 0f; e.style.bottom = 0f;
|
||||
e.generateVisualContent += draw;
|
||||
_chart.Add(e);
|
||||
return e;
|
||||
}
|
||||
|
||||
void Repaint()
|
||||
{
|
||||
_ball?.MarkDirtyRepaint();
|
||||
_ground?.MarkDirtyRepaint();
|
||||
_marks?.MarkDirtyRepaint();
|
||||
// the scale bar is an element: sized here, never while drawing
|
||||
if (_scaleBar != null && Area.width > 4f) Scale(Radius * Zoom);
|
||||
}
|
||||
|
||||
/// <summary>The chart's drawing area, in the layers' own space.</summary>
|
||||
Rect Area => _ground != null ? _ground.contentRect : _chart.contentRect;
|
||||
|
||||
float HexPx(float size) => Mathf.Sqrt(GameDefs.SectorArea) / Planet.Radius * size;
|
||||
|
||||
void DrawBall(MeshGenerationContext ctx)
|
||||
{
|
||||
var r = Area;
|
||||
if (r.width < 4f) return;
|
||||
var ts = TeamState.Instance;
|
||||
_clip = r;
|
||||
float size = Radius * Zoom;
|
||||
float hexPx = Mathf.Sqrt(GameDefs.SectorArea) / Planet.Radius * size;
|
||||
|
||||
var p = ctx.painter2D;
|
||||
p.fillColor = Space;
|
||||
p.BeginPath(); p.MoveTo(new Vector2(r.xMin, r.yMin)); p.LineTo(new Vector2(r.xMax, r.yMin)); p.LineTo(new Vector2(r.xMax, r.yMax)); p.LineTo(new Vector2(r.xMin, r.yMax)); p.ClosePath(); p.Fill();
|
||||
p.fillColor = hexPx >= HexPixels ? Grid : Meadow;
|
||||
p.BeginPath(); p.Arc(r.center, size, 0f, 360f); p.Fill();
|
||||
Disc(r, size, HexPx(size) >= HexPixels ? Grid : Meadow);
|
||||
Flush(ctx);
|
||||
}
|
||||
|
||||
/// <summary>The sectors on the ball, one mesh, kept inside the chart.</summary>
|
||||
void DrawGround(MeshGenerationContext ctx)
|
||||
{
|
||||
var r = Area;
|
||||
if (r.width < 4f) return;
|
||||
_clip = r;
|
||||
float size = Radius * Zoom;
|
||||
float hexPx = HexPx(size);
|
||||
_masks.Clear();
|
||||
foreach (var m in MowMap.All) _masks[m.s.id] = m;
|
||||
if (hexPx >= HexPixels) Hexes(ctx, r);
|
||||
else Dots(ctx, r, Mathf.Max(1.5f, hexPx * 0.5f));
|
||||
Flush(ctx);
|
||||
}
|
||||
|
||||
/// <summary>The ball as a fan, cut to the part of it that shows (close in it is far bigger than the chart).</summary>
|
||||
void Disc(Rect r, float size, Color32 col)
|
||||
{
|
||||
var c = r.center;
|
||||
float reach = new Vector2(r.width, r.height).magnitude * 0.5f;
|
||||
if (size > reach * 1.5f)
|
||||
{
|
||||
// the chart sits wholly inside the ball: a plain quad
|
||||
int q = _verts.Count;
|
||||
Vert(new Vector2(r.xMin, r.yMin), col); Vert(new Vector2(r.xMax, r.yMin), col);
|
||||
Vert(new Vector2(r.xMax, r.yMax), col); Vert(new Vector2(r.xMin, r.yMax), col);
|
||||
_tris.Add((ushort)q); _tris.Add((ushort)(q + 1)); _tris.Add((ushort)(q + 2));
|
||||
_tris.Add((ushort)q); _tris.Add((ushort)(q + 2)); _tris.Add((ushort)(q + 3));
|
||||
return;
|
||||
}
|
||||
const int Steps = 96;
|
||||
int k = _verts.Count;
|
||||
Vert(c, col);
|
||||
for (int i = 0; i < Steps; i++)
|
||||
{
|
||||
float a = i * Mathf.PI * 2f / Steps;
|
||||
Vert(c + new Vector2(Mathf.Cos(a), Mathf.Sin(a)) * size, col);
|
||||
}
|
||||
for (int i = 0; i < Steps; i++)
|
||||
{
|
||||
_tris.Add((ushort)k);
|
||||
_tris.Add((ushort)(k + 1 + i));
|
||||
_tris.Add((ushort)(k + 1 + (i + 1) % Steps));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Over the ground: the lattice, the rim, and everything that stands on the ball.</summary>
|
||||
void DrawMarks(MeshGenerationContext ctx)
|
||||
{
|
||||
var r = Area;
|
||||
if (r.width < 4f) return;
|
||||
var ts = TeamState.Instance;
|
||||
float size = Radius * Zoom;
|
||||
var p = ctx.painter2D;
|
||||
if (Zoom < 6f) Lattice(p);
|
||||
p.strokeColor = Rim;
|
||||
p.lineWidth = 3f;
|
||||
@ -228,14 +305,13 @@ namespace GrassCutter
|
||||
if (v) Arrow(p, v.transform, 7f, new Color(0.96f, 0.9f, 0.72f));
|
||||
foreach (var pl in PlayerAgent.All)
|
||||
if (pl) Arrow(p, pl.transform, pl.IsOwner ? 11f : 8f, pl.Tint);
|
||||
Scale(size);
|
||||
}
|
||||
|
||||
/// <summary>A round number of metres about a fifth of the chart wide.</summary>
|
||||
void Scale(float size)
|
||||
{
|
||||
float pxPerM = size / Planet.Radius;
|
||||
float want = _chart.contentRect.width * 0.18f / pxPerM;
|
||||
float want = Area.width * 0.18f / pxPerM;
|
||||
float step = Mathf.Pow(10f, Mathf.Floor(Mathf.Log10(Mathf.Max(1f, want))));
|
||||
float metres = want >= step * 5f ? step * 5f : want >= step * 2f ? step * 2f : step;
|
||||
_scaleBar.style.width = metres * pxPerM;
|
||||
@ -254,6 +330,14 @@ namespace GrassCutter
|
||||
|
||||
// ───────────────────────── the sectors, as a mesh ─────────────────────────
|
||||
|
||||
/// <summary>The sectors go out in pieces of this size at most, well inside what one mesh may hold.</summary>
|
||||
const int ChunkVerts = 6000, ChunkIndices = 18000;
|
||||
|
||||
void Room(MeshGenerationContext ctx, int verts, int indices)
|
||||
{
|
||||
if (_verts.Count + verts > ChunkVerts || _tris.Count + indices > ChunkIndices) Flush(ctx);
|
||||
}
|
||||
|
||||
void Flush(MeshGenerationContext ctx)
|
||||
{
|
||||
if (_verts.Count == 0) return;
|
||||
@ -264,7 +348,15 @@ namespace GrassCutter
|
||||
_tris.Clear();
|
||||
}
|
||||
|
||||
void Vert(Vector2 at, Color32 c) => _verts.Add(new Vertex { position = new Vector3(at.x, at.y, Vertex.nearZ), tint = c });
|
||||
/// <summary>Every corner is held inside the chart, so a sector across its edge is cut off there instead of spilling
|
||||
/// past it onto the card (the chart's rounded edge doesn't clip a hand-made mesh).</summary>
|
||||
Rect _clip = new Rect(-1e6f, -1e6f, 2e6f, 2e6f);
|
||||
|
||||
void Vert(Vector2 at, Color32 c)
|
||||
{
|
||||
at = new Vector2(Mathf.Clamp(at.x, _clip.xMin, _clip.xMax), Mathf.Clamp(at.y, _clip.yMin, _clip.yMax));
|
||||
_verts.Add(new Vertex { position = new Vector3(at.x, at.y, Vertex.nearZ), tint = c });
|
||||
}
|
||||
|
||||
/// <summary>Far out each worked sector is a speck.</summary>
|
||||
void Dots(MeshGenerationContext ctx, Rect r, float half)
|
||||
@ -275,7 +367,7 @@ namespace GrassCutter
|
||||
if (!m.keep) continue;
|
||||
var at = Screen(m.s.c, out bool front);
|
||||
if (!front || !view.Contains(at)) continue;
|
||||
if (_verts.Count > 60000) Flush(ctx);
|
||||
Room(ctx, 4, 6);
|
||||
Color32 c = m.done ? Done : Begun;
|
||||
int k = _verts.Count;
|
||||
Vert(at + new Vector2(-half, -half), c);
|
||||
@ -298,6 +390,7 @@ namespace GrassCutter
|
||||
_seen.Add(first);
|
||||
var view = new Rect(r.x - 60f, r.y - 60f, r.width + 120f, r.height + 120f);
|
||||
int drawn = 0;
|
||||
Flipped = 0;
|
||||
while (_queue.Count > 0 && drawn < MaxHexes)
|
||||
{
|
||||
var s = HexGrid.Get(_queue.Dequeue());
|
||||
@ -307,22 +400,33 @@ namespace GrassCutter
|
||||
_masks.TryGetValue(s.id, out var m);
|
||||
Color32 col = Shade(m);
|
||||
var c = Corners(s);
|
||||
if (_verts.Count + c.Length + 1 > 65000) Flush(ctx);
|
||||
Room(ctx, c.Length + 1, c.Length * 3);
|
||||
int k = _verts.Count;
|
||||
Vert(at, col);
|
||||
for (int i = 0; i < c.Length; i++) Vert(Vector2.LerpUnclamped(at, Screen(c[i], out _), 0.92f), col);
|
||||
// the screen drops faces wound the wrong way round, and the corners go round whichever way the
|
||||
// sector's own frame does: turned the screen's way here, or half the map never shows
|
||||
var e0 = (Vector2)_verts[k + 1].position - at;
|
||||
var e1 = (Vector2)_verts[k + 2].position - at;
|
||||
bool flip = e0.x * e1.y - e0.y * e1.x < 0f;
|
||||
if (flip) Flipped++;
|
||||
for (int i = 0; i < c.Length; i++)
|
||||
{
|
||||
int i0 = k + 1 + i, i1 = k + 1 + (i + 1) % c.Length;
|
||||
_tris.Add((ushort)k);
|
||||
_tris.Add((ushort)(k + 1 + i));
|
||||
_tris.Add((ushort)(k + 1 + (i + 1) % c.Length));
|
||||
_tris.Add((ushort)(flip ? i1 : i0));
|
||||
_tris.Add((ushort)(flip ? i0 : i1));
|
||||
}
|
||||
foreach (int n in s.neighbours)
|
||||
if (_seen.Add(n)) _queue.Enqueue(n);
|
||||
}
|
||||
Flush(ctx);
|
||||
Drawn = drawn;
|
||||
}
|
||||
|
||||
/// <summary>Tests: sectors in the last close-in drawing, and how many of them had to be turned round.</summary>
|
||||
public static int Drawn, Flipped;
|
||||
|
||||
/// <summary>Latitude and longitude every 30°.</summary>
|
||||
void Lattice(Painter2D p)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user