A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
#nullable enable
using System.Collections.Generic;
using UnityEngine;
namespace Statecraft.Game.Map.Units
{
/// <summary>The promo's unit badge (promo/final/03-gameplay-1936.png): a small dark rounded plate with a light glyph,
/// floating over a token so a player sees it is a unit and can click it. Two flat unlit meshes in the local XY plane
/// (the plate, and the glyph a little towards the camera), turned to face each camera as it renders (UnitLayer). The
/// glyph says the service: two figures for a brigade, an aircraft for the squadron, a ship for the navy. A military sim
/// unit's badge also carries its strength in pips under the glyph (<see cref="PipsMesh"/>, #2432).</summary>
public sealed class UnitBadge : MonoBehaviour
{
/// <summary>The plate's width at map scale (m) from a far camera; nearer it is capped on the screen
/// (<see cref="MaxScreenShare"/>).</summary>
public const float SizeM = 1400f;
/// <summary>The largest share of the screen's height a plate is drawn at (owner, 31 October 2026, "Shrink up close
/// (Recommended)": a nearer camera draws it smaller, so it never grows past this). #2430: 0.15 left the plate nearly
/// as wide as the figures are tall at 31 km, the darkest thing on the token, where promo 03's is about half their
/// height. DESIGN VALUE 0.055, 59 of 1080 pixels; from about 60 km out a plate is <see cref="SizeM"/> wide and no taller than
/// this, and the plate, not the figures, is what reads (UnitBadgeScreenCapTests).</summary>
public const float MaxScreenShare = 0.055f;
/// <summary>Clearance (m) between the token's top and the plate's lower edge.</summary>
public const float LiftM = 1300f;
/// <summary>A rifleman's height (m, real) and the corvette's mast top (m, real): the token's top before scaling.</summary>
public const float FigureHeightM = 1.78f, MastHeightM = 21f;
/// <summary>The glyph's offset towards the camera, in plate widths.</summary>
public const float GlyphLift = 0.02f;
/// <summary>The promo badge's dark navy and its ivory glyph (the selection ring's colour).</summary>
public static readonly Color PlateColour = new Color(0.10f, 0.13f, 0.20f), GlyphColour = MapScene.SelectionColour;
/// <summary>The plate's width (m) for a perspective camera whose projection scales y by <paramref name="m11"/>
/// (Camera.projectionMatrix[1, 1]) and that sees the plate <paramref name="depth"/> metres ahead: a plate w wide spans
/// w m11 / (2 depth) of the screen's height, so <see cref="SizeM"/>, or less to keep that within
/// <see cref="MaxScreenShare"/>. SizeM for an orthographic camera (m11 is then not a perspective scale) or a plate
/// behind the camera.</summary>
public static float SizeFor(float depth, float m11, bool orthographic) =>
orthographic || depth <= 0f || m11 <= 0f ? SizeM : Mathf.Min(SizeM, 2f * MaxScreenShare * depth / m11);
/// <summary>A rounded square, 1 x 1 centred on the origin, corner radius 0.22.</summary>
public static Mesh PlateMesh()
{
var outline = new List<Vector2>();
const float half = 0.5f, rad = 0.22f; const int steps = 5;
var corners = new[] { new Vector2(half - rad, half - rad), new Vector2(-half + rad, half - rad), new Vector2(-half + rad, -half + rad), new Vector2(half - rad, -half + rad) };
for (int c = 0; c < 4; c++)
for (int s = 0; s <= steps; s++)
{
float a = Mathf.PI / 2f * (c + (float)s / steps);
outline.Add(corners[c] + new Vector2(Mathf.Cos(a), Mathf.Sin(a)) * rad);
}
var m = new Flat();
m.Fan(outline);
return m.Build("UnitBadgePlate");
}
/// <summary>The service's glyph inside the plate (about 0.6 across). With <paramref name="pips"/> (a badge that
/// carries <see cref="PipsMesh"/>) it is drawn at <see cref="PippedGlyphScale"/> and raised by
/// <see cref="PippedGlyphRise"/>, clear of the pips' strip.</summary>
public static Mesh GlyphMesh(string service, bool pips = false)
{
var m = pips ? new Flat(PippedGlyphScale, PippedGlyphRise) : new Flat();
switch (service)
{
case UnitData.Army: // two standing figures: a head and a tapered body each
foreach (var x in new[] { -0.13f, 0.13f })
{
m.Fan(Circle(new Vector2(x, 0.17f), 0.07f, 12));
m.Fan(new List<Vector2> { new Vector2(x - 0.10f, -0.26f), new Vector2(x + 0.10f, -0.26f), new Vector2(x + 0.08f, 0.07f), new Vector2(x - 0.08f, 0.07f) });
}
break;
case UnitData.Air: // an aircraft seen from above: fuselage, wings, tailplane
m.Fan(Rect(0f, 0f, 0.07f, 0.56f));
m.Fan(Rect(0f, 0.06f, 0.56f, 0.10f));
m.Fan(Rect(0f, -0.22f, 0.22f, 0.07f));
break;
default: // a ship in profile: hull, bridge, funnel
m.Fan(new List<Vector2> { new Vector2(-0.30f, -0.04f), new Vector2(0.30f, -0.04f), new Vector2(0.22f, -0.16f), new Vector2(-0.24f, -0.16f) });
m.Fan(Rect(0.04f, 0.04f, 0.16f, 0.16f));
m.Fan(Rect(-0.10f, 0.08f, 0.08f, 0.24f));
break;
}
return m.Build("UnitBadgeGlyph-" + service + (pips ? "-pips" : ""));
}
/// <summary>#2432, the damaged look (owner, 8 November 2026, "A. Pips on badge (Recommended)" and "(c) No bands, pips
/// (Recommended)"; 9 November 2026, "Rows of five (Recommended)", "Every military unit (Recommended)" and "All 3 as
/// recommended (Recommended)"): a military unit's badge carries one pip per population unit of its full strength,
/// a disc for each unit it has and a ring for each it lost, so a loss reads by shape, not by colour alone. Five to a
/// row, rows from the top, filled pips first from the left in both languages (a count, not text), in a strip inside
/// the plate under the glyph, which is drawn smaller and higher (<see cref="GlyphMesh"/>): the plate keeps its size.</summary>
public const int PipsPerRow = 5;
/// <summary>The pips' strip inside the plate (plate widths): its top and its bottom edge.</summary>
public const float PipStripTop = -0.10f, PipStripBottom = -0.43f;
/// <summary>The distance between neighbouring pips' centres (plate widths; less when more rows than two must fit the
/// strip), a pip's radius as a share of it, and a hollow pip's hole as a share of its radius. Two rows of five fit
/// at the full pitch: a pip about 0.11 of the plate across, 6 of its 59 pixels at the gameplay zoom.</summary>
public const float PipPitch = 0.16f, PipRadiusShare = 0.34f, PipHoleShare = 0.5f;
/// <summary>The glyph's scale and rise (plate widths) on a badge with pips.</summary>
public const float PippedGlyphScale = 0.7f, PippedGlyphRise = 0.12f;
const int PipSides = 12;
/// <summary>The pips' centre spacing for a unit of <paramref name="full"/>: <see cref="PipPitch"/>, or less so that
/// its rows fit the strip.</summary>
public static float PipSpacing(int full) =>
Mathf.Min(PipPitch, (PipStripTop - PipStripBottom) / Mathf.Max(1, (full + PipsPerRow - 1) / PipsPerRow));
/// <summary>Where each pip of a unit at <paramref name="strength"/> of <paramref name="full"/> stands in the plate
/// (plate widths, the plate's centre at the origin), in order: row by row from the top, each row centred and from the
/// left; the first <paramref name="strength"/> are filled. A strength above the full counts as full. None for a full
/// strength of 0.</summary>
public static List<(Vector2 centre, bool filled)> PipLayout(int strength, int full)
{
full = Mathf.Max(full, strength);
var pips = new List<(Vector2, bool)>(Mathf.Max(0, full));
if (full <= 0) return pips;
int rows = (full + PipsPerRow - 1) / PipsPerRow;
float pitch = PipSpacing(full), middle = (PipStripTop + PipStripBottom) / 2f;
for (int i = 0; i < full; i++)
{
int row = i / PipsPerRow, inRow = Mathf.Min(PipsPerRow, full - row * PipsPerRow);
float x = (i % PipsPerRow - (inRow - 1) / 2f) * pitch, y = middle + ((rows - 1) / 2f - row) * pitch;
pips.Add((new Vector2(x, y), i < strength));
}
return pips;
}
/// <summary>The pips of a unit at <paramref name="strength"/> of <paramref name="full"/> as one mesh (<see cref="PipLayout"/>):
/// a disc per filled pip, a ring per hollow one, drawn with the glyph's material.</summary>
public static Mesh PipsMesh(int strength, int full)
{
full = Mathf.Max(full, strength);
float r = PipRadiusShare * PipSpacing(full);
var m = new Flat();
foreach (var (centre, filled) in PipLayout(strength, full))
if (filled) m.Fan(Circle(centre, r, PipSides)); else m.Ring(centre, r, r * PipHoleShare, PipSides);
return m.Build(System.FormattableString.Invariant($"UnitBadgePips-{strength}of{full}"));
}
static List<Vector2> Rect(float cx, float cy, float w, float h) => new List<Vector2>
{ new Vector2(cx - w / 2, cy - h / 2), new Vector2(cx + w / 2, cy - h / 2), new Vector2(cx + w / 2, cy + h / 2), new Vector2(cx - w / 2, cy + h / 2) };
static List<Vector2> Circle(Vector2 c, float r, int n)
{
var pts = new List<Vector2>(n);
for (int i = 0; i < n; i++) { float a = 2f * Mathf.PI * i / n; pts.Add(c + new Vector2(Mathf.Cos(a), Mathf.Sin(a)) * r); }
return pts;
}
/// <summary>Convex polygons as triangle fans in the XY plane (the badge shader draws both faces).</summary>
sealed class Flat
{
readonly List<Vector3> verts = new List<Vector3>(); readonly List<int> tris = new List<int>();
readonly float scale, rise;
/// <param name="scale">Every point is drawn at this scale about the origin, then raised by <paramref name="rise"/>.</param>
public Flat(float scale = 1f, float rise = 0f) { this.scale = scale; this.rise = rise; }
Vector3 At(Vector2 p) => new Vector3(p.x * scale, p.y * scale + rise, 0f);
public void Fan(List<Vector2> outline)
{
int b = verts.Count;
foreach (var p in outline) verts.Add(At(p));
for (int i = 1; i + 1 < outline.Count; i++) tris.AddRange(new[] { b, b + i, b + i + 1 });
}
/// <summary>A ring of <paramref name="n"/> quads between radii <paramref name="inner"/> and <paramref name="outer"/>.</summary>
public void Ring(Vector2 centre, float outer, float inner, int n)
{
int b = verts.Count;
for (int i = 0; i < n; i++)
{
float a = 2f * Mathf.PI * i / n; var d = new Vector2(Mathf.Cos(a), Mathf.Sin(a));
verts.Add(At(centre + d * outer)); verts.Add(At(centre + d * inner));
}
for (int i = 0; i < n; i++)
{
int o = b + 2 * i, o2 = b + 2 * ((i + 1) % n);
tris.AddRange(new[] { o, o2, o2 + 1, o, o2 + 1, o + 1 });
}
}
public Mesh Build(string name)
{
var mesh = new Mesh { name = name };
mesh.SetVertices(verts); mesh.SetTriangles(tris, 0); mesh.RecalculateBounds();
return mesh;
}
}
}
}