Files
ft/Client/Assets/Scripts/EventCubeMelt/Data/TetrominoData.cs
2026-06-29 21:18:33 +08:00

149 lines
3.8 KiB
C#

// TetrominoData
using System;
using System.Collections.Generic;
using System.Text;
using cfg;
using EventCubeMelt;
using EventCubeMelt.Utils;
using UnityEngine;
namespace EventCubeMelt
{
public class TetrominoData :ICloneable
{
// 定义高度和宽度
// 应该可以在 属性中控制
public const int WIDTH = 5;
public const int HEIGHT = 5;
#region
private CellType [,] _shape;
//
// public TetrominoType Type { get; private set; }
public int Width { get; private set; } = WIDTH;
public int Height { get; private set; } = HEIGHT;
// public Color Color { get; private set; }
// 获得当前可用数值
public int UsableCount { get; private set; } = 0;
#endregion
public TetrominoData()
{
// Type = type;
}
public TetrominoData(int height, int width)
{
Width = width;
Height = height;
Initialize();
}
public object Clone()
{
return MemberwiseClone();
}
// 初始化
public void Initialize()
{
_shape = new CellType[Width, Height];
ClearGrid();
// Count = 0;
}
private void ClearGrid()
{
for (int row = 0; row < Width; row++)
{
for (int col = 0; col < Height; col++)
{
_shape[row, col] = CellType.Empty;
}
}
}
//
public void LoadData(List<int> pieceData)
{
UsableCount = 0;
for (var row = 0; row < Height; row++)
{
for (var col = 0; col < Width; col++)
{
var ct = (CellType)pieceData[row * Width + col];
_shape[row, col] = ct;
if (ct != CellType.Empty)
{
UsableCount++;
}
}
}
PrintShape();
}
private void PrintShape()
{
var sb = new StringBuilder();
sb.Append("PrintShape-Piece");
for (var i = 0; i < Height; i++)
{
sb.Append("<color=red>");
for (var j = 0; j < Width; j++)
{
sb.Append((int)_shape[i,j]);
if (j < Width - 1)
sb.Append(", ");
}
// sb.AppendLine();
sb.Append("</color> \n");
}
sb.Append($"Count = {UsableCount}");
ELog(sb.ToString());
}
#region
public CellType this[int row, int col]
{
get
{
if (row >= 0 && row < Height && col >= 0 && col < Width)
return _shape[row, col];
// return CellType.Empty;
return CellType.Illegal;
}
}
// public TetrominoData Rotate(bool clockwise)
// {
// var rotated = new TetrominoData();
// // 旋转形状矩阵
// if (Type == TetrominoType.O)
// {
// // O方块不旋转
// return rotated;
// }
// return rotated;
// }
#endregion
private static void ELog(string message)
{
#if UNITY_EDITOR
if (!EventCubeMeltLogPolicy.EnableELog) return;
UnityEngine.Debug.Log($"<color=red>{nameof(TetrominoData)} => {message}</color>");
#endif
}
}
}