codingmosaku
This commit is contained in:
@@ -1,105 +1,148 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.XR.Interaction.Toolkit.Interactables; // XR Toolkit用
|
||||
|
||||
public enum BlockColor { Red, Blue, Green }
|
||||
public enum BlockShapeType { TShape, CShape }
|
||||
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>();
|
||||
[Header("スナップ(合体)位置の設定")]
|
||||
[Tooltip("相手と合体する基準位置(空のGameObject)を指定。未設定でも中心位置でスナップします")]
|
||||
[SerializeField] private Transform snapPoint;
|
||||
|
||||
private PuzzleGridBoard currentBoard;
|
||||
private bool isGrabbed = false;
|
||||
private PuzzleBlock nearbyPartner; // 近くにある結合対象のブロック
|
||||
private bool isConnected = false; // 結合済みフラグ
|
||||
|
||||
// リセット用の初期座標・回転保持用
|
||||
private Vector3 initialPosition;
|
||||
private Quaternion initialRotation;
|
||||
private Rigidbody rb;
|
||||
private XRGrabInteractable grabInteractable;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
InitializeShapeCells();
|
||||
rb = GetComponent<Rigidbody>();
|
||||
grabInteractable = GetComponent<XRGrabInteractable>();
|
||||
|
||||
// 初期スポーン位置・回転を記憶しておく
|
||||
initialPosition = transform.position;
|
||||
initialRotation = transform.rotation;
|
||||
}
|
||||
|
||||
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 / 180f) % 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;
|
||||
if (isConnected) return;
|
||||
|
||||
PuzzleBlock partner = other.GetComponentInParent<PuzzleBlock>();
|
||||
if (partner != null && IsValidPartner(partner))
|
||||
{
|
||||
nearbyPartner = partner;
|
||||
Debug.Log($"🧩 パートナー検知: {partner.Color} の {partner.ShapeType}");
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
PuzzleBlock partner = other.GetComponentInParent<PuzzleBlock>();
|
||||
if (partner == nearbyPartner)
|
||||
{
|
||||
currentBoard = overlappingBoard;
|
||||
nearbyPartner = null;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsValidPartner(PuzzleBlock partner)
|
||||
{
|
||||
if (partner == null || partner.isConnected) return false;
|
||||
// 同一色 & 異なる形状(ト字 + コ字)のみ許可
|
||||
return partner.Color == this.Color && partner.ShapeType != this.ShapeType;
|
||||
}
|
||||
|
||||
public void OnReleased()
|
||||
{
|
||||
if (isConnected) return;
|
||||
|
||||
if (nearbyPartner != null)
|
||||
{
|
||||
ConnectToPartner(nearbyPartner);
|
||||
}
|
||||
}
|
||||
|
||||
private void ConnectToPartner(PuzzleBlock partner)
|
||||
{
|
||||
isConnected = true;
|
||||
partner.isConnected = true;
|
||||
|
||||
// 1. 位置・回転のスナップ補正
|
||||
if (snapPoint != null && partner.snapPoint != null)
|
||||
{
|
||||
transform.position = partner.snapPoint.position;
|
||||
transform.rotation = partner.snapPoint.rotation;
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.position = partner.transform.position;
|
||||
transform.rotation = partner.transform.rotation;
|
||||
}
|
||||
|
||||
// 2. 物理&VR掴み動作の完全ロック(完成後に崩れないようにする)
|
||||
LockBlockState();
|
||||
partner.LockBlockState();
|
||||
|
||||
Debug.Log($"🎉 マグネット合体成功! 色: {Color}");
|
||||
|
||||
// 3. バフ適用してCodingMode完了へ
|
||||
string buffName = ConvertColorToBuffName(Color);
|
||||
PlayerStatusManager.Instance.ApplyBuffsAndResume(buffName, buffName);
|
||||
}
|
||||
|
||||
private void LockBlockState()
|
||||
{
|
||||
if (rb != null)
|
||||
{
|
||||
rb.linearVelocity = Vector3.zero;
|
||||
rb.angularVelocity = Vector3.zero;
|
||||
rb.isKinematic = true;
|
||||
}
|
||||
// 掴みコンポーネントをオフにして手から切り離す
|
||||
if (grabInteractable != null) grabInteractable.enabled = false;
|
||||
}
|
||||
|
||||
public void ResetBlock()
|
||||
{
|
||||
isConnected = false;
|
||||
nearbyPartner = null;
|
||||
|
||||
transform.position = initialPosition;
|
||||
transform.rotation = initialRotation;
|
||||
|
||||
if (rb != null)
|
||||
{
|
||||
rb.isKinematic = false;
|
||||
rb.linearVelocity = Vector3.zero;
|
||||
rb.angularVelocity = Vector3.zero;
|
||||
}
|
||||
if (grabInteractable != null) grabInteractable.enabled = true;
|
||||
}
|
||||
|
||||
public static void ResetAllBlocksInScene()
|
||||
{
|
||||
foreach (var block in FindObjectsByType<PuzzleBlock>(FindObjectsSortMode.None))
|
||||
{
|
||||
if (block != null) block.ResetBlock();
|
||||
}
|
||||
}
|
||||
|
||||
private string ConvertColorToBuffName(BlockColor color)
|
||||
{
|
||||
return color switch
|
||||
{
|
||||
BlockColor.Red => "AttackUp",
|
||||
BlockColor.Blue => "DefenseUp",
|
||||
BlockColor.Green => "Heal",
|
||||
_ => "AttackUp"
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user