ManiaMap.Unity
Procedural generation of metroidvania style maps for Unity.
CellChild.cs
1using UnityEngine;
2using UnityEngine.Events;
3
5{
9 public abstract class CellChild : MonoBehaviour
10 {
11 [Header("Cell Child:")]
12 [SerializeField]
13 protected bool _autoAssignCell = true;
18 public bool AutoAssignCell { get => _autoAssignCell; set => _autoAssignCell = value; }
19
23 public bool IsInitialized { get; protected set; }
24
25 [SerializeField]
26 protected Vector2Int _cellIndex;
30 public Vector2Int CellIndex { get => _cellIndex; set => _cellIndex = Vector2Int.Max(value, Vector2Int.zero); }
31
32 [SerializeField]
33 protected RoomComponent _room;
37 public RoomComponent Room { get => _room; set => _room = value; }
38
39 [SerializeField] private UnityEvent _onInitialize = new UnityEvent();
43 public UnityEvent OnInitialize { get => _onInitialize; set => _onInitialize = value; }
44
48 public int Row { get => CellIndex.x; set => CellIndex = new Vector2Int(value, CellIndex.y); }
49
53 public int Column { get => CellIndex.y; set => CellIndex = new Vector2Int(CellIndex.x, value); }
54
55 protected virtual void Awake()
56 {
57 Room.OnInitialize.AddListener(Initialize);
58 }
59
60 protected virtual void OnDestroy()
61 {
62 Room.OnInitialize.RemoveListener(Initialize);
63 }
64
69 public virtual void AutoAssign(RoomComponent room)
70 {
71 Room = room;
72
74 CellIndex = room.FindClosestActiveCellIndex(transform.position);
75 }
76
80 protected virtual void Initialize()
81 {
82 if (!IsInitialized)
83 {
84 IsInitialized = true;
85 OnInitialize.Invoke();
86 }
87 }
88 }
89}
An object tied to the cell index of a RoomComponent.
Definition: CellChild.cs:10
bool AutoAssignCell
If true, the cell will be automatically assigned to the closest cell when update and save operations ...
Definition: CellChild.cs:18
UnityEvent OnInitialize
The event invoked after the object is initialized.
Definition: CellChild.cs:43
RoomComponent Room
The containing room.
Definition: CellChild.cs:37
int Column
The column index.
Definition: CellChild.cs:53
virtual void Initialize()
Initializes the object.
Definition: CellChild.cs:80
virtual void AutoAssign(RoomComponent room)
Performs auto assignment on the object.
Definition: CellChild.cs:69
bool IsInitialized
True if the object has been initialized.
Definition: CellChild.cs:23
Vector2Int CellIndex
The containing cell index.
Definition: CellChild.cs:30
A component for creating a room.
UnityEvent OnInitialize
An event invoked when the room is initialized.
Vector2Int FindClosestActiveCellIndex(Vector3 position)
Returns the closest active cell index for the specified global position.