パーツ準備
This commit is contained in:
105
Assets/_Scripts/PuzzleBlock.cs
Normal file
105
Assets/_Scripts/PuzzleBlock.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public enum BlockColor { Red, Blue, Green }
|
||||
public enum BlockShapeType { TShape, CShape }
|
||||
|
||||
public class PuzzleBlock : MonoBehaviour
|
||||
{
|
||||
[field: SerializeField] public BlockColor Color { get; private set; }
|
||||
[field: SerializeField] public BlockShapeType ShapeType { get; private set; }
|
||||
|
||||
// 原点(0,0)を基準としたブロックのマス配置データ
|
||||
public List<Vector2Int> LocalCells { get; private set; } = new List<Vector2Int>();
|
||||
|
||||
private PuzzleGridBoard currentBoard;
|
||||
private bool isGrabbed = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
InitializeShapeCells();
|
||||
}
|
||||
|
||||
private void InitializeShapeCells()
|
||||
{
|
||||
LocalCells.Clear();
|
||||
|
||||
if (ShapeType == BlockShapeType.TShape)
|
||||
{
|
||||
LocalCells.Add(new Vector2Int(0, 0));
|
||||
LocalCells.Add(new Vector2Int(0, 1));
|
||||
LocalCells.Add(new Vector2Int(0, 2));
|
||||
LocalCells.Add(new Vector2Int(1, 1));
|
||||
}
|
||||
else if (ShapeType == BlockShapeType.CShape)
|
||||
{
|
||||
LocalCells.Add(new Vector2Int(0, 0));
|
||||
LocalCells.Add(new Vector2Int(1, 0));
|
||||
LocalCells.Add(new Vector2Int(0, 1));
|
||||
LocalCells.Add(new Vector2Int(0, 2));
|
||||
LocalCells.Add(new Vector2Int(1, 2));
|
||||
}
|
||||
}
|
||||
|
||||
public List<Vector2Int> GetRotatedCells()
|
||||
{
|
||||
List<Vector2Int> rotated = new List<Vector2Int>();
|
||||
// 現在のY回転を90度単位に丸める (0, 1, 2, 3)
|
||||
int steps = Mathf.RoundToInt(transform.eulerAngles.y / 90f) % 4;
|
||||
if (steps < 0) steps += 4;
|
||||
|
||||
foreach (var cell in LocalCells)
|
||||
{
|
||||
Vector2Int c = cell;
|
||||
for (int i = 0; i < steps; i++)
|
||||
{
|
||||
// 時計回りに90度回転: (x, y) -> (y, -x)
|
||||
c = new Vector2Int(c.y, -c.x);
|
||||
}
|
||||
rotated.Add(c);
|
||||
}
|
||||
return rotated;
|
||||
}
|
||||
|
||||
public void OnGrabbed()
|
||||
{
|
||||
isGrabbed = true;
|
||||
if (currentBoard != null)
|
||||
{
|
||||
currentBoard.RemoveBlock(this);
|
||||
currentBoard = null;
|
||||
}
|
||||
}
|
||||
|
||||
// PuzzleBlock.cs 内に以下を追加・修正
|
||||
private PuzzleGridBoard overlappingBoard;
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
var board = other.GetComponentInParent<PuzzleGridBoard>();
|
||||
if (board != null) overlappingBoard = board;
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
var board = other.GetComponentInParent<PuzzleGridBoard>();
|
||||
if (board == overlappingBoard) overlappingBoard = null;
|
||||
}
|
||||
|
||||
// 引数なしで呼べるように変更
|
||||
public void OnReleased()
|
||||
{
|
||||
isGrabbed = false;
|
||||
|
||||
// 1. 手の傾きを最も近い90度単位にスナップ補正
|
||||
Vector3 currentEuler = transform.eulerAngles;
|
||||
float snappedY = Mathf.Round(currentEuler.y / 90f) * 90f;
|
||||
transform.rotation = Quaternion.Euler(0f, snappedY, 0f);
|
||||
|
||||
// 2. 近くにある盤面へのセットを試みる
|
||||
if (overlappingBoard != null && overlappingBoard.TryPlaceBlock(this))
|
||||
{
|
||||
currentBoard = overlappingBoard;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user