パーツ準備
This commit is contained in:
106
Assets/_Scripts/PuzzleGridBoard.cs
Normal file
106
Assets/_Scripts/PuzzleGridBoard.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class PuzzleGridBoard : MonoBehaviour
|
||||
{
|
||||
[Header("盤面設定")]
|
||||
[SerializeField] private float cellSize = 0.1f; // 1マスのサイズ(メートル)
|
||||
[SerializeField] private Transform originAnchor; // 3x3の左下(0,0)の基準Transform
|
||||
|
||||
// 3x3 のマス目データ(設置されているブロックを管理)
|
||||
private PuzzleBlock[,] grid = new PuzzleBlock[3, 3];
|
||||
private List<PuzzleBlock> placedBlocks = new List<PuzzleBlock>();
|
||||
|
||||
public bool TryPlaceBlock(PuzzleBlock block)
|
||||
{
|
||||
// ブロックの原点位置から盤面のグリッド座標(0~2, 0~2)を算出
|
||||
Vector3 localPos = originAnchor.InverseTransformPoint(block.transform.position);
|
||||
int originX = Mathf.RoundToInt(localPos.x / cellSize);
|
||||
int originY = Mathf.RoundToInt(localPos.z / cellSize);
|
||||
|
||||
List<Vector2Int> shapeCells = block.GetRotatedCells();
|
||||
|
||||
// 設置可能かチェック(はみ出し・重ね被りがないか)
|
||||
foreach (var cell in shapeCells)
|
||||
{
|
||||
int targetX = originX + cell.x;
|
||||
int targetY = originY + cell.y;
|
||||
|
||||
if (targetX < 0 || targetX >= 3 || targetY < 0 || targetY >= 3) return false; // 盤面外
|
||||
if (grid[targetX, targetY] != null) return false; // すでに埋まっている
|
||||
}
|
||||
|
||||
// 設置確定処理
|
||||
foreach (var cell in shapeCells)
|
||||
{
|
||||
grid[originX + cell.x, originY + cell.y] = block;
|
||||
}
|
||||
placedBlocks.Add(block);
|
||||
|
||||
// 吸い付き(スナップ)位置の固定
|
||||
Vector3 snapLocalPos = new Vector3(originX * cellSize, 0f, originY * cellSize);
|
||||
block.transform.position = originAnchor.TransformPoint(snapLocalPos);
|
||||
|
||||
// 成功チェック(全9マスが単一色で埋まったか)
|
||||
EvaluateBoardComplete();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void RemoveBlock(PuzzleBlock block)
|
||||
{
|
||||
for (int x = 0; x < 3; x++)
|
||||
{
|
||||
for (int y = 0; y < 3; y++)
|
||||
{
|
||||
if (grid[x, y] == block) grid[x, y] = null;
|
||||
}
|
||||
}
|
||||
placedBlocks.Remove(block);
|
||||
}
|
||||
|
||||
private void EvaluateBoardComplete()
|
||||
{
|
||||
// 全9マスが埋まっているかチェック
|
||||
for (int x = 0; x < 3; x++)
|
||||
{
|
||||
for (int y = 0; y < 3; y++)
|
||||
{
|
||||
if (grid[x, y] == null) return; // 空きマスがあるので未完成
|
||||
}
|
||||
}
|
||||
|
||||
// 全ブロックの色が統一されているかチェック
|
||||
BlockColor firstColor = placedBlocks[0].Color;
|
||||
foreach (var b in placedBlocks)
|
||||
{
|
||||
if (b.Color != firstColor)
|
||||
{
|
||||
Debug.Log("❌ 3x3は埋まりましたが、色が混ざっているため失敗です。");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// ★パズル成功! 色に対応したバフを発動してCodingModeを終了する
|
||||
Debug.Log($"🎉 パズル成功! 単一色 ({firstColor}) で完成しました!");
|
||||
string buffName = ConvertColorToBuffName(firstColor);
|
||||
|
||||
PlayerStatusManager.Instance.ApplyBuffsAndResume(buffName, buffName);
|
||||
}
|
||||
|
||||
private string ConvertColorToBuffName(BlockColor color)
|
||||
{
|
||||
return color switch
|
||||
{
|
||||
BlockColor.Red => "AttackUp",
|
||||
BlockColor.Blue => "DefenseUp",
|
||||
BlockColor.Green => "Heal",
|
||||
_ => "AttackUp"
|
||||
};
|
||||
}
|
||||
|
||||
public void ClearBoard()
|
||||
{
|
||||
grid = new PuzzleBlock[3, 3];
|
||||
placedBlocks.Clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user