128 lines
5.1 KiB
C#
128 lines
5.1 KiB
C#
using Bogatyri.World;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
// Войско в походе на карте: группа маленьких квадратиков + подпись с числом солдат и таймером.
|
|
// Сам ничего не решает -- позицию и состояние берёт из WorldSimulation.
|
|
public class MarchView : MonoBehaviour
|
|
{
|
|
const int MaxSquares = 15; // больше квадратиков не рисуем, чтобы не загромождать карту
|
|
const int SquaresPerRow = 5;
|
|
const float SquareSize = 0.18f;
|
|
const float SquareSpacing = 0.26f;
|
|
|
|
static readonly Color OutboundColor = new Color(0.3f, 0.55f, 1f);
|
|
static readonly Color ReturningColor = new Color(0.55f, 0.8f, 1f);
|
|
|
|
public int MarchId { get; private set; }
|
|
|
|
private SpriteRenderer[] squares;
|
|
private TextMeshPro label;
|
|
private GameObject routeLine;
|
|
private MarchState shownState;
|
|
private int shownSoldiers = -1;
|
|
|
|
public static MarchView Create(Transform parent, MarchData march)
|
|
{
|
|
var go = new GameObject($"March_{march.id}");
|
|
go.transform.SetParent(parent, false);
|
|
var view = go.AddComponent<MarchView>();
|
|
view.MarchId = march.id;
|
|
view.CreateLabel();
|
|
return view;
|
|
}
|
|
|
|
void CreateLabel()
|
|
{
|
|
var go = new GameObject("Label");
|
|
go.transform.SetParent(transform, false);
|
|
go.transform.localPosition = new Vector3(0, 0.75f, 0);
|
|
label = go.AddComponent<TextMeshPro>();
|
|
label.fontSize = 3f;
|
|
label.alignment = TextAlignmentOptions.Center;
|
|
label.rectTransform.sizeDelta = new Vector2(4f, 0.8f);
|
|
label.sortingOrder = 31;
|
|
}
|
|
|
|
// Перестраиваем квадратики, только когда поменялось число солдат (после боя)
|
|
void RebuildSquares(int soldiers, Color color)
|
|
{
|
|
if (squares != null)
|
|
{
|
|
foreach (var sq in squares) Destroy(sq.gameObject);
|
|
}
|
|
|
|
int count = Mathf.Clamp(soldiers, 1, MaxSquares);
|
|
int rows = Mathf.CeilToInt(count / (float)SquaresPerRow);
|
|
squares = new SpriteRenderer[count];
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
int col = i % SquaresPerRow;
|
|
int row = i / SquaresPerRow;
|
|
int inThisRow = Mathf.Min(SquaresPerRow, count - row * SquaresPerRow);
|
|
|
|
var go = new GameObject("Soldier");
|
|
go.transform.SetParent(transform, false);
|
|
go.transform.localPosition = new Vector3(
|
|
(col - (inThisRow - 1) / 2f) * SquareSpacing,
|
|
((rows - 1) / 2f - row) * SquareSpacing,
|
|
0f);
|
|
go.transform.localScale = Vector3.one * SquareSize;
|
|
|
|
var sr = go.AddComponent<SpriteRenderer>();
|
|
sr.sprite = PrimitiveSprites.Square;
|
|
sr.color = color;
|
|
sr.sortingOrder = 30;
|
|
squares[i] = sr;
|
|
}
|
|
}
|
|
|
|
// Линия от города до цели -- видно, куда идёт войско. Лежит отдельно, чтобы не ездить вместе с отрядом.
|
|
void CreateRouteLine(WorldSimulation world, MarchData march)
|
|
{
|
|
Vector3 home = WorldMapController.ToWorld(world.GetNode(march.fromNodeId));
|
|
Vector3 target = WorldMapController.ToWorld(world.GetNode(march.targetNodeId));
|
|
Vector3 delta = target - home;
|
|
|
|
routeLine = new GameObject($"Route_{march.id}");
|
|
routeLine.transform.SetParent(transform.parent, false);
|
|
routeLine.transform.position = (home + target) / 2f;
|
|
routeLine.transform.localScale = new Vector3(delta.magnitude, 0.08f, 1f);
|
|
routeLine.transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(delta.y, delta.x) * Mathf.Rad2Deg);
|
|
|
|
var sr = routeLine.AddComponent<SpriteRenderer>();
|
|
sr.sprite = PrimitiveSprites.Square;
|
|
sr.color = new Color(1f, 1f, 1f, 0.35f);
|
|
sr.sortingOrder = -5;
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (routeLine != null) Destroy(routeLine);
|
|
}
|
|
|
|
public void Refresh(WorldSimulation world, MarchData march, long nowMs)
|
|
{
|
|
if (routeLine == null) CreateRouteLine(world, march);
|
|
|
|
world.GetMarchSegment(march, nowMs, out MapNode from, out MapNode to, out double progress);
|
|
transform.position = Vector3.Lerp(WorldMapController.ToWorld(from), WorldMapController.ToWorld(to), (float)progress);
|
|
|
|
bool returning = march.state == MarchState.Returning;
|
|
int soldiers = returning ? march.survivors : march.soldiers;
|
|
|
|
if (soldiers != shownSoldiers || march.state != shownState)
|
|
{
|
|
shownSoldiers = soldiers;
|
|
shownState = march.state;
|
|
RebuildSquares(soldiers, returning ? ReturningColor : OutboundColor);
|
|
}
|
|
|
|
long endMs = returning ? march.returnMs : march.arriveMs;
|
|
int secondsLeft = Mathf.Max(0, Mathf.CeilToInt((endMs - nowMs) / 1000f));
|
|
string direction = returning ? "домой" : "в бой";
|
|
label.text = $"{soldiers} сол. {direction} {secondsLeft / 60}:{secondsLeft % 60:00}";
|
|
}
|
|
}
|