Files
VRProject/Assets/_Scripts/PuzzleGridBoard.cs
2026-07-21 16:45:01 +09:00

106 lines
3.6 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 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();
}
}