Files
2026-07-21 08:56:10 +03:00

75 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttachableBlock : MonoBehaviour
{
[Header("Block Info")]
[SerializeField] private BlockType blockType = BlockType.Regular;
private SocketContainer socketContainer;
private List<AttachableBlock> connectedBlocks = new List<AttachableBlock>();
private List<JointBlock> attachedJoints = new List<JointBlock>();
public List<AttachableBlock> GetConnectedBlocks() => connectedBlocks;
public List<JointBlock> GetAttachedJoints() => attachedJoints;
public void SetBlockType(BlockType BlockType) => blockType = BlockType;
public void DisconnectFrom(AttachableBlock otherBlock)
{
if (connectedBlocks.Contains(otherBlock))
{
connectedBlocks.Remove(otherBlock);
}
}
public void UnregisterJoint(JointBlock joint)
{
if (attachedJoints.Contains(joint))
{
attachedJoints.Remove(joint);
}
}
void Awake()
{
socketContainer = GetComponent<SocketContainer>();
if (socketContainer == null)
{
socketContainer = gameObject.AddComponent<SocketContainer>();
}
}
public SocketContainer GetSocketContainer() => socketContainer;
public void ConnectTo(AttachableBlock otherBlock, SocketPoint mySocket, SocketPoint theirSocket)
{
if (!connectedBlocks.Contains(otherBlock))
{
connectedBlocks.Add(otherBlock);
// Simple parenting connection (no physics joint yet)
mySocket.SetOccupied(otherBlock.gameObject);
theirSocket.SetOccupied(this.gameObject);
}
if (!otherBlock.GetConnectedBlocks().Contains(this))
{
otherBlock.GetConnectedBlocks().Add(this);
}
}
public void RegisterJoint(JointBlock joint)
{
if (!attachedJoints.Contains(joint))
{
attachedJoints.Add(joint);
}
}
}
public enum BlockType
{
Regular, // Standard building block
Link, // Block with holes (like a chain link)
Gear, // Gear block
Structural // Frame/support block
}