ThreeKingdomsSlingshot/Assets/Scripts/Runtime/Board.cs

268 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using UnityEngine;
namespace TKS
{
/// <summary>전장 가장자리 벽 표식. 캐스트 질의에서 벽으로 판정한다.</summary>
public class BoardWall : MonoBehaviour
{
}
/// <summary>전장(바닥 = 원화 배경 또는 도형·격자 · 테두리 · 4면 벽 콜라이더). 화면보다 넓어 카메라로 스크롤한다.</summary>
public class Board : MonoBehaviour
{
public Vector2 Size { get; private set; }
/// <summary>Resources/Board/&lt;boardArt&gt; 원화 배경을 썼는가(없으면 도형·격자).</summary>
public bool HasArt { get; private set; }
public Rect Rect
{
get { return new Rect(-Size.x * 0.5f, -Size.y * 0.5f, Size.x, Size.y); }
}
public void Build(Vector2 size, string boardArt = null)
{
Size = size;
float w = size.x;
float h = size.y;
Sprite art = string.IsNullOrEmpty(boardArt) ? null : Resources.Load<Sprite>("Board/" + boardArt);
HasArt = art != null;
if (HasArt)
{
// 원화 세션 납품 배경(5:8 · 위 = 적 진영) — 전장 크기에 맞춰 늘린다 · 격자·진영 띠는 그리지 않음
var bg = ProceduralSprites.NewSprite(transform, "Background", art, Color.white, 1f, -20);
bg.transform.localScale = new Vector3(w / art.bounds.size.x, h / art.bounds.size.y, 1f);
background = bg;
defaultArt = art;
}
else
{
var bg = ProceduralSprites.NewSprite(transform, "Background", ProceduralSprites.Square, new Color(0.17f, 0.24f, 0.17f), 1f, -20);
bg.transform.localScale = new Vector3(w, h, 1f);
// 아군·적군 진영 구분용 옅은 띠
var enemyHalf = ProceduralSprites.NewSprite(transform, "EnemyHalf", ProceduralSprites.Square, new Color(0.45f, 0.2f, 0.2f, 0.18f), 1f, -19);
enemyHalf.transform.localScale = new Vector3(w, h * 0.5f, 1f);
enemyHalf.transform.localPosition = new Vector3(0f, h * 0.25f, 0f);
// 격자(4 단위) — 스크롤 체감용
var gridColor = new Color(1f, 1f, 1f, 0.07f);
const float gridStep = 4f;
for (float x = -w * 0.5f + gridStep; x < w * 0.5f - 0.01f; x += gridStep)
{
var g = ProceduralSprites.NewSprite(transform, "GridV", ProceduralSprites.Square, gridColor, 1f, -18);
g.transform.localPosition = new Vector3(x, 0f, 0f);
g.transform.localScale = new Vector3(0.04f, h, 1f);
}
for (float y = -h * 0.5f + gridStep; y < h * 0.5f - 0.01f; y += gridStep)
{
var g = ProceduralSprites.NewSprite(transform, "GridH", ProceduralSprites.Square, gridColor, 1f, -18);
g.transform.localPosition = new Vector3(0f, y, 0f);
g.transform.localScale = new Vector3(w, 0.04f, 1f);
}
var mid = ProceduralSprites.NewSprite(transform, "MidLine", ProceduralSprites.Square, new Color(1f, 1f, 1f, 0.14f), 1f, -17);
mid.transform.localScale = new Vector3(w, 0.06f, 1f);
}
// 테두리
var borderColor = new Color(0.85f, 0.75f, 0.45f);
const float t = 0.16f;
MakeBorder("BorderTop", new Vector2(0f, h * 0.5f + t * 0.5f), new Vector2(w + t * 2f, t), borderColor);
MakeBorder("BorderBottom", new Vector2(0f, -h * 0.5f - t * 0.5f), new Vector2(w + t * 2f, t), borderColor);
MakeBorder("BorderLeft", new Vector2(-w * 0.5f - t * 0.5f, 0f), new Vector2(t, h), borderColor);
MakeBorder("BorderRight", new Vector2(w * 0.5f + t * 0.5f, 0f), new Vector2(t, h), borderColor);
// 벽 콜라이더 (바깥쪽 1 단위 두께)
const float wallT = 1f;
MakeWall("WallTop", new Vector2(0f, h * 0.5f + wallT * 0.5f), new Vector2(w + wallT * 2f, wallT));
MakeWall("WallBottom", new Vector2(0f, -h * 0.5f - wallT * 0.5f), new Vector2(w + wallT * 2f, wallT));
MakeWall("WallLeft", new Vector2(-w * 0.5f - wallT * 0.5f, 0f), new Vector2(wallT, h));
MakeWall("WallRight", new Vector2(w * 0.5f + wallT * 0.5f, 0f), new Vector2(wallT, h));
}
SpriteRenderer background;
Sprite defaultArt;
static Dictionary<string, string> themeOf, fileOf;
/// <summary>
/// 판 배경 = 스테이지 bg_theme → 테마(T01~T19 · bg_theme_map.csv) → 그림 이름(bg_theme_files.csv) → Resources/Board/&lt;이름&gt;.
/// 그림이 아직 없으면(디자인 PC 납품 전) 기본 배경(battlefield_01) 그대로 — 그림 파일만 넣으면 코드 수정 없이 바뀐다.
/// 표 SOT = 기획 tables/bg_theme_map.csv · stage_terrain_art.csv(bg · bg_edit · share 행) → 사본 Resources/Board/(2026-11-18 PD 「배경이 시나리오 상황과 맞지 않아 · 다른 로컬 PC 배경 작업 반영」).
/// 반환 = 바꾼 그림 이름(없으면 "").
/// </summary>
public string SetThemeArt(string bgTheme)
{
if (background == null || defaultArt == null) return "";
LoadThemeTables();
string theme, file;
Sprite art = null;
if (!string.IsNullOrEmpty(bgTheme) && themeOf.TryGetValue(bgTheme, out theme) && fileOf.TryGetValue(theme, out file))
art = Resources.Load<Sprite>("Board/" + file);
if (art == null) art = defaultArt;
if (background.sprite != art)
{
background.sprite = art;
background.transform.localScale = new Vector3(Size.x / art.bounds.size.x, Size.y / art.bounds.size.y, 1f);
}
return art == defaultArt ? "" : art.name;
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ResetStatics()
{
themeOf = null;
fileOf = null;
}
static void LoadThemeTables()
{
if (themeOf != null) return;
themeOf = new Dictionary<string, string>();
fileOf = new Dictionary<string, string>();
var errors = new List<string>();
var a = Resources.Load<TextAsset>("Board/bg_theme_map");
if (a != null)
foreach (var r in StageTable.ReadCsv(a.text, "bg_theme_map.csv", errors))
themeOf[Get(r, "s_bg_theme")] = Get(r, "s_theme_id");
var b = Resources.Load<TextAsset>("Board/bg_theme_files");
if (b != null)
foreach (var r in StageTable.ReadCsv(b.text, "bg_theme_files.csv", errors))
fileOf[Get(r, "s_theme_id")] = Get(r, "s_file");
foreach (var e in errors) Debug.LogError("[TKS] 배경 테마 표 " + e);
}
static string Get(Dictionary<string, string> r, string k)
{
string v;
return r.TryGetValue(k, out v) ? v : "";
}
SpriteRenderer eventArt;
/// <summary>
/// 시나리오 이벤트 바닥(scenario_event bg 명령 · Resources/Board/&lt;name&gt; · 2026-11-18) — 판 배경 위(−19 · 격자·기믹 아래)에 전장 크기로 덮는다.
/// 그림이 없으면 지금 바닥 그대로(false). 다음 전투 시작(BattleManager.Begin)에 ClearEventArt.
/// </summary>
public bool SetEventArt(string name)
{
var art = string.IsNullOrEmpty(name) ? null : Resources.Load<Sprite>("Board/" + name);
if (art == null)
{
Debug.LogWarning("[TKS] 이벤트 바닥 그림 없음: Board/" + name + " → 지금 바닥 유지");
return false;
}
if (eventArt == null) eventArt = ProceduralSprites.NewSprite(transform, "EventBackground", art, Color.white, 1f, -19);
eventArt.sprite = art;
eventArt.transform.localScale = new Vector3(Size.x / art.bounds.size.x, Size.y / art.bounds.size.y, 1f);
eventArt.gameObject.SetActive(true);
return true;
}
public void ClearEventArt()
{
if (eventArt != null) eventArt.gameObject.SetActive(false);
}
/// <summary>이벤트 바닥이 켜져 있는가(테스트·캡처 확인용).</summary>
public bool EventArtOn
{
get { return eventArt != null && eventArt.gameObject.activeSelf; }
}
void MakeBorder(string name, Vector2 pos, Vector2 size, Color color)
{
var sr = ProceduralSprites.NewSprite(transform, name, ProceduralSprites.Square, color, 1f, -16);
sr.transform.localPosition = pos;
sr.transform.localScale = new Vector3(size.x, size.y, 1f);
}
void MakeWall(string name, Vector2 pos, Vector2 size)
{
var go = new GameObject(name);
go.transform.SetParent(transform, false);
go.transform.localPosition = pos;
var col = go.AddComponent<BoxCollider2D>();
col.size = size;
go.AddComponent<BoardWall>();
}
// ---------- 좁힌 전장(투기장 12×18 · 컨텐츠 모드 ④ · 2026-11-18) ----------
Vector2 fullSize;
readonly System.Collections.Generic.List<GameObject> areaObjs = new System.Collections.Generic.List<GameObject>();
/// <summary>
/// 전장을 가운데 area 크기로 좁힌다(바깥 = 어둡게 덮음 · 안쪽 테두리 · 벽 콜라이더 · ClampInside · Rect 모두 좁힌 크기) — null = 원래 크기로.
/// 배경 그림은 그대로 둔다(전투마다 BattleManager.Begin 이 부른다).
/// </summary>
public void Restrict(Vector2? area)
{
foreach (var o in areaObjs)
if (o != null) Destroy(o);
areaObjs.Clear();
if (fullSize == Vector2.zero) fullSize = Size;
Size = area ?? fullSize;
if (area == null) return;
float w = area.Value.x, h = area.Value.y, W = fullSize.x, H = fullSize.y;
var dim = new Color(0f, 0f, 0f, 0.55f);
Dim(new Vector2(0f, (H + h) * 0.25f), new Vector2(W, (H - h) * 0.5f), dim);
Dim(new Vector2(0f, -(H + h) * 0.25f), new Vector2(W, (H - h) * 0.5f), dim);
Dim(new Vector2(-(W + w) * 0.25f, 0f), new Vector2((W - w) * 0.5f, h), dim);
Dim(new Vector2((W + w) * 0.25f, 0f), new Vector2((W - w) * 0.5f, h), dim);
var borderColor = new Color(0.85f, 0.75f, 0.45f);
const float t = 0.16f, wallT = 1f;
Track(MakeBorderObj("AreaTop", new Vector2(0f, h * 0.5f + t * 0.5f), new Vector2(w + t * 2f, t), borderColor));
Track(MakeBorderObj("AreaBottom", new Vector2(0f, -h * 0.5f - t * 0.5f), new Vector2(w + t * 2f, t), borderColor));
Track(MakeBorderObj("AreaLeft", new Vector2(-w * 0.5f - t * 0.5f, 0f), new Vector2(t, h), borderColor));
Track(MakeBorderObj("AreaRight", new Vector2(w * 0.5f + t * 0.5f, 0f), new Vector2(t, h), borderColor));
Track(MakeWallObj("AreaWallTop", new Vector2(0f, h * 0.5f + wallT * 0.5f), new Vector2(w + wallT * 2f, wallT)));
Track(MakeWallObj("AreaWallBottom", new Vector2(0f, -h * 0.5f - wallT * 0.5f), new Vector2(w + wallT * 2f, wallT)));
Track(MakeWallObj("AreaWallLeft", new Vector2(-w * 0.5f - wallT * 0.5f, 0f), new Vector2(wallT, h)));
Track(MakeWallObj("AreaWallRight", new Vector2(w * 0.5f + wallT * 0.5f, 0f), new Vector2(wallT, h)));
Physics2D.SyncTransforms();
}
void Track(GameObject go)
{
areaObjs.Add(go);
}
void Dim(Vector2 pos, Vector2 size, Color c)
{
var sr = ProceduralSprites.NewSprite(transform, "AreaDim", ProceduralSprites.Square, c, 1f, -15);
sr.transform.localPosition = pos;
sr.transform.localScale = new Vector3(size.x, size.y, 1f);
areaObjs.Add(sr.gameObject);
}
GameObject MakeBorderObj(string name, Vector2 pos, Vector2 size, Color color)
{
var sr = ProceduralSprites.NewSprite(transform, name, ProceduralSprites.Square, color, 1f, -14);
sr.transform.localPosition = pos;
sr.transform.localScale = new Vector3(size.x, size.y, 1f);
return sr.gameObject;
}
GameObject MakeWallObj(string name, Vector2 pos, Vector2 size)
{
var go = new GameObject(name);
go.transform.SetParent(transform, false);
go.transform.localPosition = pos;
var col = go.AddComponent<BoxCollider2D>();
col.size = size;
go.AddComponent<BoardWall>();
return go;
}
public Vector2 ClampInside(Vector2 p, float radius)
{
var r = Rect;
float x = Mathf.Clamp(p.x, r.xMin + radius, r.xMax - radius);
float y = Mathf.Clamp(p.y, r.yMin + radius, r.yMax - radius);
return new Vector2(x, y);
}
}
}