ManiaMap.Unity
Procedural generation of metroidvania style maps for Unity.
CellArea2D.cs
1using UnityEngine;
2
4{
8 [RequireComponent(typeof(BoxCollider2D))]
9 public class CellArea2D : CellArea
10 {
14 public BoxCollider2D Collider { get; private set; }
15
16 private void Awake()
17 {
18 Collider = GetComponent<BoxCollider2D>();
19 }
20
29 public static CellArea2D InstantiateCellArea2D(int row, int column, RoomComponent room,
30 int cellLayer, LayerMask triggeringLayers)
31 {
32 var obj = new GameObject("Cell Area 2D");
33 obj.transform.SetParent(room.transform);
34 obj.transform.localPosition = room.CellCenterLocalPosition(row, column);
35
36 var area = obj.AddComponent<CellArea2D>();
37 area.Initialize(row, column, room);
38
39 var collider = area.Collider;
40 collider.gameObject.layer = cellLayer;
41 collider.isTrigger = true;
42 collider.size = room.CellSize;
43 collider.includeLayers = triggeringLayers;
44 collider.excludeLayers = ~triggeringLayers;
45 collider.contactCaptureLayers = triggeringLayers;
46 collider.callbackLayers = triggeringLayers;
47
48 return area;
49 }
50
55 private void OnTriggerEnter2D(Collider2D collision)
56 {
57 Room.RoomState.SetCellVisibility(Row, Column, true);
58 Room.OnCellAreaEntered.Invoke(this, collision.gameObject);
59 }
60
65 private void OnTriggerExit2D(Collider2D collision)
66 {
67 Room.OnCellAreaExited.Invoke(this, collision.gameObject);
68 }
69 }
70}
A 2D trigger to detect if an object on a monitored physics layer mask enters or exits a RoomComponent...
Definition: CellArea2D.cs:10
static CellArea2D InstantiateCellArea2D(int row, int column, RoomComponent room, int cellLayer, LayerMask triggeringLayers)
Instantiates a new cell area within the specified room.
Definition: CellArea2D.cs:29
void OnTriggerExit2D(Collider2D collision)
Calls the OnCellAreaExited event on the containing RoomComponent.
Definition: CellArea2D.cs:65
BoxCollider2D Collider
The attached box collider.
Definition: CellArea2D.cs:14
void OnTriggerEnter2D(Collider2D collision)
Sets the RoomState cell visibility to true and calls the OnCellAreaEntered event on the containing Ro...
Definition: CellArea2D.cs:55
The base class for cell trigger areas.
Definition: CellArea.cs:9
int Row
The row index corresponding to the area.
Definition: CellArea.cs:13
RoomComponent Room
The containing room.
Definition: CellArea.cs:23
void Initialize(int row, int column, RoomComponent room)
Initializes the base properties for the area.
Definition: CellArea.cs:55
A component for creating a room.
Vector3 CellCenterLocalPosition(int row, int column)
Returns the cell center for the specified index in local coordinates.
Vector3 CellSize
The size of each cell within the grid coordinate system.
CellAreaTriggerEvent OnCellAreaEntered
The event invoked when a room cell has been entered. Message includes the enterered cell and the coll...
RoomState RoomState
The room state.
CellAreaTriggerEvent OnCellAreaExited
The event invoked when a room cell has been exited. Message includes the exited cell and the collidin...