File size: 6,193 Bytes
05c9ac2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;
namespace Unity.MLAgentsExamples
{
public class Match3Drawer : MonoBehaviour
{
public int DebugMoveIndex = -1;
static Color[] s_Colors = new[]
{
Color.red,
Color.green,
Color.blue,
Color.cyan,
Color.magenta,
Color.yellow,
Color.gray,
Color.black,
};
private static Color s_EmptyColor = new Color(0.5f, 0.5f, 0.5f, .25f);
public Dictionary<(int, int), Match3TileSelector> tilesDict = new Dictionary<(int, int), Match3TileSelector>();
public float CubeSpacing = 1.25f;
public GameObject TilePrefab;
private bool m_Initialized;
private Match3Board m_Board;
void Awake()
{
if (!m_Initialized)
{
InitializeDict();
}
}
void InitializeDict()
{
m_Board = GetComponent<Match3Board>();
foreach (var item in tilesDict)
{
if (item.Value)
{
DestroyImmediate(item.Value.gameObject);
}
}
tilesDict.Clear();
for (var i = 0; i < m_Board.MaxRows; i++)
{
for (var j = 0; j < m_Board.MaxColumns; j++)
{
var go = Instantiate(TilePrefab, transform.position, Quaternion.identity, transform);
go.name = $"r{i}_c{j}";
tilesDict.Add((i, j), go.GetComponent<Match3TileSelector>());
}
}
m_Initialized = true;
}
void Update()
{
if (!m_Board)
{
m_Board = GetComponent<Match3Board>();
}
if (!m_Initialized)
{
InitializeDict();
}
var currentSize = m_Board.GetCurrentBoardSize();
for (var i = 0; i < m_Board.MaxRows; i++)
{
for (var j = 0; j < m_Board.MaxColumns; j++)
{
int value = Match3Board.k_EmptyCell;
int specialType = 0;
if (m_Board.Cells != null && i < currentSize.Rows && j < currentSize.Columns)
{
value = m_Board.GetCellType(i, j);
specialType = m_Board.GetSpecialType(i, j);
}
var pos = new Vector3(j, i, 0);
pos *= CubeSpacing;
tilesDict[(i, j)].transform.position = transform.TransformPoint(pos);
tilesDict[(i, j)].SetActiveTile(specialType, value);
}
}
}
void OnDrawGizmos()
{
Profiler.BeginSample("Match3.OnDrawGizmos");
var cubeSize = .5f;
var matchedWireframeSize = .5f * (cubeSize + CubeSpacing);
if (!m_Board)
{
m_Board = GetComponent<Match3Board>();
if (m_Board == null)
{
return;
}
}
var currentSize = m_Board.GetCurrentBoardSize();
for (var i = 0; i < m_Board.MaxRows; i++)
{
for (var j = 0; j < m_Board.MaxColumns; j++)
{
int value = Match3Board.k_EmptyCell;
int specialType = 0;
if (m_Board.Cells != null && i < currentSize.Rows && j < currentSize.Columns)
{
value = m_Board.GetCellType(i, j);
specialType = m_Board.GetSpecialType(i, j);
}
if (value >= 0 && value < s_Colors.Length)
{
Gizmos.color = s_Colors[value];
}
else
{
Gizmos.color = s_EmptyColor;
}
var pos = new Vector3(j, i, 0);
pos *= CubeSpacing;
if (specialType == 2)
{
Gizmos.DrawCube(transform.TransformPoint(pos), cubeSize * new Vector3(1f, .5f, .5f));
Gizmos.DrawCube(transform.TransformPoint(pos), cubeSize * new Vector3(.5f, 1f, .5f));
Gizmos.DrawCube(transform.TransformPoint(pos), cubeSize * new Vector3(.5f, .5f, 1f));
}
else if (specialType == 1)
{
Gizmos.DrawSphere(transform.TransformPoint(pos), .5f * cubeSize);
}
else
{
Gizmos.DrawCube(transform.TransformPoint(pos), cubeSize * Vector3.one);
}
Gizmos.color = Color.yellow;
if (m_Board.Matched != null && m_Board.Matched[j, i])
{
Gizmos.DrawWireCube(transform.TransformPoint(pos), matchedWireframeSize * Vector3.one);
}
}
}
// Draw valid moves
foreach (var move in m_Board.AllMoves())
{
if (DebugMoveIndex >= 0 && move.MoveIndex != DebugMoveIndex)
{
continue;
}
if (!m_Board.IsMoveValid(move))
{
continue;
}
var (otherRow, otherCol) = move.OtherCell();
var pos = new Vector3(move.Column, move.Row, 0) * CubeSpacing;
var otherPos = new Vector3(otherCol, otherRow, 0) * CubeSpacing;
var oneQuarter = Vector3.Lerp(pos, otherPos, .25f);
var threeQuarters = Vector3.Lerp(pos, otherPos, .75f);
Gizmos.DrawLine(transform.TransformPoint(oneQuarter), transform.TransformPoint(threeQuarters));
}
Profiler.EndSample();
}
}
}
|