ManiaMap.Unity
Procedural generation of metroidvania style maps for Unity.
RoomComponent Class Reference

A component for creating a room. More...

Inheritance diagram for RoomComponent:

Public Member Functions

int AutoAssign ()
 Runs auto assign on the room's cell children. Returns the number of children. More...
 
Vector3 CellCenterGlobalPosition (int row, int column)
 Returns the cell center for the specified index in global coordinates. More...
 
Vector3 CellCenterGridPosition (int row, int column)
 Returns the cell center for the specified index in grid coordinates. More...
 
Vector3 CellCenterLocalPosition (int row, int column)
 Returns the cell center for the specified index in local coordinates. More...
 
bool CellIndexExists (int row, int column)
 Returns true if the cell index exists. More...
 
bool CellIndexRangeExists (Vector2Int startIndex, Vector2Int endIndex)
 Returns true if both cell indexes exist. More...
 
Vector3 CenterGlobalPosition ()
 Returns the center of the cell grid in global coordinates. More...
 
Vector3 CenterGridPosition ()
 Returns the center of the cell grid in grid coordinates. More...
 
Vector3 CenterLocalPosition ()
 Returns the center of the cell grid in local coordinates. More...
 
Vector2Int FindClosestActiveCellIndex (Vector3 position)
 Returns the closest active cell index for the specified global position. More...
 
DoorDirection FindClosestDoorDirection (int row, int column, Vector3 position)
 Returns the closest door direction based on the specified global position. More...
 
bool GetCellActivity (int row, int column)
 Returns the cell activity for the specified index. More...
 
Quaternion GetCellViewDirection ()
 Returns the quaternion angle perpendicular to the cell plane. More...
 
RoomTemplate GetMMRoomTemplate (int id, string name)
 Returns the Mania Map room template used by the procedural generator. More...
 
Vector2Int GlobalPositionToCellIndex (Vector3 position)
 Converts the specified global position to the containing cell index. If the cell index does not exist, returns (-1, -1). More...
 
Vector3 GlobalToGridPosition (Vector3 globalPosition)
 Converts the specified global position to grid coordinates. More...
 
Vector2Int GridPositionToCellIndex (Vector3 position)
 Converts the specified grid position to the containing cell index. If the cell index does not exist, returns (-1, -1). More...
 
Vector3 GridToLocalPosition (Vector3 gridPosition)
 Converts the specified grid coordinate to local coordinates. More...
 
bool Initialize (LayoutPack layoutPack, Room roomLayout, RoomState roomState, int cellLayer, LayerMask triggeringLayers, bool assignLayoutPosition)
 Initializes the room. Returns false if the room has already been initialized. More...
 
Vector3 LocalCellSize ()
 Returns the cell size in local coordinates. More...
 
Vector2Int LocalPositionToCellIndex (Vector3 position)
 Converts the specified local position to the containing cell index. If the cell index does not exist, returns (-1, -1). More...
 
Vector3 LocalToGridPosition (Vector3 localPosition)
 Converts the specified local coordinate to grid coordinates. More...
 
bool SetCellActivities (Vector2Int startIndex, Vector2Int endIndex, CellActivity activity)
 Sets the cell activities for the specified index range. No action is taken if one of the indexes is out of range. Returns false if no action is taken. More...
 
void SetCellActivity (int row, int column, bool activity)
 Sets the cell activity for the specified index. More...
 
void SetCellActivity (int row, int column, CellActivity activity)
 Sets the cell activity for the specified index. More...
 
void SizeActiveCells ()
 Sizes the active cells list to match the room size. More...
 
void ValidateRoomFlags ()
 Validates that the room flag ID's are unique. More...
 

Static Public Member Functions

static RoomComponent InstantiateRoom (GameObject prefab, Transform parent, LayoutPack layoutPack, Room roomLayout, RoomState roomState, int cellLayer, LayerMask triggeringLayers, bool assignLayoutPosition)
 Instantiates and initializes a room and returns it. More...
 
static RoomComponent InstantiateRoom (Uid id, LayoutPack layoutPack, GameObject prefab, Transform parent=null, bool assignLayoutPosition=false)
 Instantiates and initializes a room and returns it. More...
 
static AsyncOperationHandle< GameObject > InstantiateRoomAsync (AssetReferenceGameObject prefab, Transform parent, LayoutPack layoutPack, Room roomLayout, RoomState roomState, int cellLayer, LayerMask triggeringLayers, bool assignLayoutPosition)
 Instantiates and initializes a room asynchronously. Returns the operation handle. More...
 
static AsyncOperationHandle< GameObject > InstantiateRoomAsync (Uid id, LayoutPack layoutPack, AssetReferenceGameObject prefab, Transform parent=null, bool assignLayoutPosition=false)
 Instantiates and initializes a room asynchronously. Returns the operation handle. More...
 

Properties

List< ActiveCellsRowActiveCells [getset]
 A list of active cell rows. More...
 
Vector3 CellSize [getset]
 The size of each cell within the grid coordinate system. More...
 
int Columns [getset]
 The column index. More...
 
bool IsInitialized [getprivate set]
 True if the room has been initialized. More...
 
LayoutPack LayoutPack [getprivate set]
 The layout pack. More...
 
string Name [getset]
 The room name. More...
 
CellAreaTriggerEvent OnCellAreaEntered [getset]
 The event invoked when a room cell has been entered. Message includes the enterered cell and the colliding GameObject. More...
 
CellAreaTriggerEvent OnCellAreaExited [getset]
 The event invoked when a room cell has been exited. Message includes the exited cell and the colliding GameObject. More...
 
UnityEvent OnInitialize [getset]
 An event invoked when the room is initialized. More...
 
UnityEvent OnInitialized [getset]
 The event invoked after the room has completed initialization. More...
 
Room RoomLayout [getprivate set]
 The room data. More...
 
RoomState RoomState [getprivate set]
 The room state. More...
 
RoomTemplateResource RoomTemplate [getset]
 The room template. More...
 
RoomType RoomType [getset]
 The room type. More...
 
int Rows [getset]
 The row index. More...
 
Vector2Int Size [getset]
 The size of the room grid in rows (x) and columns (y). More...
 

Private Member Functions

void AddMMDoors (Array2D< Cell > cells)
 Adds the Mania Map doors to the cells array. More...
 
void AddMMFeatures (Array2D< Cell > cells)
 Adds the cell features to the cells array. More...
 
void CreateCellAreas (int cellLayer, LayerMask triggeringLayers)
 Creates the cell area triggers as children of the room. More...
 
void DrawCells (Color fillColor, Color lineColor, bool activity)
 Draws the cell cube gizmos for the specified cell activity. More...
 
Array2D< Cell > GetMMCells ()
 Returns the Mania Map cells used by the procedural generator. More...
 
Dictionary< int, CollectableSpot > GetMMCollectableSpots ()
 Returns the Mania Map collectable spots by ID. More...
 
void MoveToLayoutPosition ()
 Sets the room's local position to its position in the layout. More...
 
void OnDrawGizmos ()
 
void OnValidate ()
 
void SetSizeField (ref Vector2Int field, Vector2Int value)
 

Private Attributes

List< ActiveCellsRow_activeCells = new List<ActiveCellsRow>()
 
Vector3 _cellSize = new Vector3(1, 1, 0.001f)
 
string _name = "<None>"
 
CellAreaTriggerEvent _onCellAreaEntered = new CellAreaTriggerEvent()
 
CellAreaTriggerEvent _onCellAreaExited = new CellAreaTriggerEvent()
 
UnityEvent _onInitialize = new UnityEvent()
 
UnityEvent _onInitialized = new UnityEvent()
 
RoomTemplateResource _roomTemplate
 
RoomType _roomType
 
Vector2Int _size = Vector2Int.one
 

Detailed Description

A component for creating a room.

The room contains methods for various coordinate systems:

  • Grid Coordinates - The origin is in the top left of the room. Positive X is right in the direction of increasing columns. Positive Y is down in the direction of increasing rows. Looking at the XY plane, positive Z points out towards the viewer.
  • Local Coordinates - The coordinates assuming the room's transform position is the origin. Axes are in the same direction as Unity's. Coordinates are assumed to be axis aligned (not rotated).
  • Global Coordinates - The coordinates in Unity's global coordinates. Coordinates are assumed to be axis aligned (not rotated).

Cell related calculations are generally performed within Grid Coordinate space since that is consistent between room types.

Definition at line 23 of file RoomComponent.cs.

Member Function Documentation

◆ AddMMDoors()

void AddMMDoors ( Array2D< Cell >  cells)
private

Adds the Mania Map doors to the cells array.

Parameters
cellsThe cells array.

Definition at line 680 of file RoomComponent.cs.

◆ AddMMFeatures()

void AddMMFeatures ( Array2D< Cell >  cells)
private

Adds the cell features to the cells array.

Parameters
cellsThe cells array.

Definition at line 693 of file RoomComponent.cs.

◆ AutoAssign()

int AutoAssign ( )

Runs auto assign on the room's cell children. Returns the number of children.

Definition at line 453 of file RoomComponent.cs.

◆ CellCenterGlobalPosition()

Vector3 CellCenterGlobalPosition ( int  row,
int  column 
)

Returns the cell center for the specified index in global coordinates.

Parameters
rowThe row index.
columnThe column index.

Definition at line 589 of file RoomComponent.cs.

◆ CellCenterGridPosition()

Vector3 CellCenterGridPosition ( int  row,
int  column 
)

Returns the cell center for the specified index in grid coordinates.

Parameters
rowThe row index.
columnThe column index.

Definition at line 579 of file RoomComponent.cs.

◆ CellCenterLocalPosition()

Vector3 CellCenterLocalPosition ( int  row,
int  column 
)

Returns the cell center for the specified index in local coordinates.

Parameters
rowThe row index.
columnThe column index.

Definition at line 599 of file RoomComponent.cs.

◆ CellIndexExists()

bool CellIndexExists ( int  row,
int  column 
)

Returns true if the cell index exists.

Parameters
rowThe row index.
columnThe column index.

Definition at line 435 of file RoomComponent.cs.

◆ CellIndexRangeExists()

bool CellIndexRangeExists ( Vector2Int  startIndex,
Vector2Int  endIndex 
)

Returns true if both cell indexes exist.

Parameters
startIndexThe first index.
endIndexThe second index.

Definition at line 445 of file RoomComponent.cs.

◆ CenterGlobalPosition()

Vector3 CenterGlobalPosition ( )

Returns the center of the cell grid in global coordinates.

Definition at line 503 of file RoomComponent.cs.

◆ CenterGridPosition()

Vector3 CenterGridPosition ( )

Returns the center of the cell grid in grid coordinates.

Definition at line 487 of file RoomComponent.cs.

◆ CenterLocalPosition()

Vector3 CenterLocalPosition ( )

Returns the center of the cell grid in local coordinates.

Definition at line 495 of file RoomComponent.cs.

◆ CreateCellAreas()

void CreateCellAreas ( int  cellLayer,
LayerMask  triggeringLayers 
)
private

Creates the cell area triggers as children of the room.

Parameters
cellLayerThe physics layer assigned to cell triggers.
triggeringLayersThe physics layers that trigger the room cell triggers.

Definition at line 300 of file RoomComponent.cs.

◆ DrawCells()

void DrawCells ( Color  fillColor,
Color  lineColor,
bool  activity 
)
private

Draws the cell cube gizmos for the specified cell activity.

Parameters
fillColorThe gizmo fill color.
lineColorThe gizmo line color.
activityThe activity of the cells to draw.

Definition at line 153 of file RoomComponent.cs.

◆ FindClosestActiveCellIndex()

Vector2Int FindClosestActiveCellIndex ( Vector3  position)

Returns the closest active cell index for the specified global position.

Parameters
positionThe global position.

Definition at line 737 of file RoomComponent.cs.

◆ FindClosestDoorDirection()

DoorDirection FindClosestDoorDirection ( int  row,
int  column,
Vector3  position 
)

Returns the closest door direction based on the specified global position.

Parameters
rowThe cell row index.
columnThe cell column index.
positionThe global position.

Definition at line 775 of file RoomComponent.cs.

◆ GetCellActivity()

bool GetCellActivity ( int  row,
int  column 
)

Returns the cell activity for the specified index.

Parameters
rowThe row index.
columnThe column index.
Exceptions
System.IndexOutOfRangeExceptionRaised if the specified index is out of range.

Definition at line 422 of file RoomComponent.cs.

◆ GetCellViewDirection()

Quaternion GetCellViewDirection ( )

Returns the quaternion angle perpendicular to the cell plane.

Exceptions
System.ArgumentExceptionRaised if the room type is not handled.

Definition at line 470 of file RoomComponent.cs.

◆ GetMMCells()

Array2D< Cell > GetMMCells ( )
private

Returns the Mania Map cells used by the procedural generator.

Definition at line 660 of file RoomComponent.cs.

◆ GetMMCollectableSpots()

Dictionary< int, CollectableSpot > GetMMCollectableSpots ( )
private

Returns the Mania Map collectable spots by ID.

Definition at line 705 of file RoomComponent.cs.

◆ GetMMRoomTemplate()

RoomTemplate GetMMRoomTemplate ( int  id,
string  name 
)

Returns the Mania Map room template used by the procedural generator.

Parameters
idThe template ID.
nameThe template name.

Definition at line 645 of file RoomComponent.cs.

◆ GlobalPositionToCellIndex()

Vector2Int GlobalPositionToCellIndex ( Vector3  position)

Converts the specified global position to the containing cell index. If the cell index does not exist, returns (-1, -1).

Parameters
positionThe global position.

Definition at line 635 of file RoomComponent.cs.

◆ GlobalToGridPosition()

Vector3 GlobalToGridPosition ( Vector3  globalPosition)

Converts the specified global position to grid coordinates.

Parameters
globalPositionThe global coordinate position.
Exceptions
System.ArgumentExceptionRaised if the room type is not handled.

Definition at line 569 of file RoomComponent.cs.

◆ GridPositionToCellIndex()

Vector2Int GridPositionToCellIndex ( Vector3  position)

Converts the specified grid position to the containing cell index. If the cell index does not exist, returns (-1, -1).

Parameters
positionThe grid position.

Definition at line 609 of file RoomComponent.cs.

◆ GridToLocalPosition()

Vector3 GridToLocalPosition ( Vector3  gridPosition)

Converts the specified grid coordinate to local coordinates.

Parameters
gridPositionThe grid coordinate position.
Exceptions
System.ArgumentExceptionRaised if the room type is not handled.

Definition at line 531 of file RoomComponent.cs.

◆ Initialize()

bool Initialize ( LayoutPack  layoutPack,
Room  roomLayout,
RoomState  roomState,
int  cellLayer,
LayerMask  triggeringLayers,
bool  assignLayoutPosition 
)

Initializes the room. Returns false if the room has already been initialized.

Parameters
layoutPackThe layout pack.
roomLayoutThe room's layout.
roomStateThe room's layout state.
cellLayerThe physics layer assigned to cell triggers.
triggeringLayersThe physics layers that trigger the room cell triggers.
assignLayoutPositionIf true, the room will have its local position moved to its location within the layout.

Definition at line 264 of file RoomComponent.cs.

◆ InstantiateRoom() [1/2]

static RoomComponent InstantiateRoom ( GameObject  prefab,
Transform  parent,
LayoutPack  layoutPack,
Room  roomLayout,
RoomState  roomState,
int  cellLayer,
LayerMask  triggeringLayers,
bool  assignLayoutPosition 
)
static

Instantiates and initializes a room and returns it.

Parameters
prefabThe room prefab.
parentThe parent transform.
layoutPackThe layout pack.
roomLayoutThe room's layout.
roomStateThe room's layout state.
cellLayerThe physics layer assigned to cell triggers.
triggeringLayersThe physics layers that trigger the room cell triggers.
assignLayoutPositionIf true, the room will have its local position moved to its location within the layout.

Definition at line 246 of file RoomComponent.cs.

◆ InstantiateRoom() [2/2]

static RoomComponent InstantiateRoom ( Uid  id,
LayoutPack  layoutPack,
GameObject  prefab,
Transform  parent = null,
bool  assignLayoutPosition = false 
)
static

Instantiates and initializes a room and returns it.

Parameters
idThe room ID.
layoutPackThe layout pack.
prefabThe room prefab.
parentThe parent transform.
assignLayoutPositionIf true, the room will have its local position moved to its location within the layout.

Definition at line 225 of file RoomComponent.cs.

◆ InstantiateRoomAsync() [1/2]

static AsyncOperationHandle< GameObject > InstantiateRoomAsync ( AssetReferenceGameObject  prefab,
Transform  parent,
LayoutPack  layoutPack,
Room  roomLayout,
RoomState  roomState,
int  cellLayer,
LayerMask  triggeringLayers,
bool  assignLayoutPosition 
)
static

Instantiates and initializes a room asynchronously. Returns the operation handle.

Parameters
prefabThe room prefab asset reference.
parentThe parent transform.
layoutPackThe layout pack.
roomLayoutThe room's layout.
roomStateThe room's layout state.
cellLayerThe physics layer assigned to cell triggers.
triggeringLayersThe physics layers that trigger the room cell triggers.
assignLayoutPositionIf true, the room will have its local position moved to its location within the layout.

Definition at line 205 of file RoomComponent.cs.

◆ InstantiateRoomAsync() [2/2]

static AsyncOperationHandle< GameObject > InstantiateRoomAsync ( Uid  id,
LayoutPack  layoutPack,
AssetReferenceGameObject  prefab,
Transform  parent = null,
bool  assignLayoutPosition = false 
)
static

Instantiates and initializes a room asynchronously. Returns the operation handle.

Parameters
idThe room ID.
layoutPackThe layout pack.
prefabThe room prefab asset reference.
parentThe parent transform.
assignLayoutPositionIf true, the room will have its local position moved to its location within the layout.

Definition at line 183 of file RoomComponent.cs.

◆ LocalCellSize()

Vector3 LocalCellSize ( )

Returns the cell size in local coordinates.

Exceptions
System.ArgumentExceptionRaised if the room type is not handled.

Definition at line 512 of file RoomComponent.cs.

◆ LocalPositionToCellIndex()

Vector2Int LocalPositionToCellIndex ( Vector3  position)

Converts the specified local position to the containing cell index. If the cell index does not exist, returns (-1, -1).

Parameters
positionThe local position.

Definition at line 625 of file RoomComponent.cs.

◆ LocalToGridPosition()

Vector3 LocalToGridPosition ( Vector3  localPosition)

Converts the specified local coordinate to grid coordinates.

Parameters
localPositionThe local coordinate position.
Exceptions
System.ArgumentExceptionRaised if the room type is not handled.

Definition at line 550 of file RoomComponent.cs.

◆ MoveToLayoutPosition()

void MoveToLayoutPosition ( )
private

Sets the room's local position to its position in the layout.

Definition at line 287 of file RoomComponent.cs.

◆ OnDrawGizmos()

void OnDrawGizmos ( )
private

Definition at line 138 of file RoomComponent.cs.

◆ OnValidate()

void OnValidate ( )
private

Definition at line 132 of file RoomComponent.cs.

◆ SetCellActivities()

bool SetCellActivities ( Vector2Int  startIndex,
Vector2Int  endIndex,
CellActivity  activity 
)

Sets the cell activities for the specified index range. No action is taken if one of the indexes is out of range. Returns false if no action is taken.

Parameters
startIndexThe first index.
endIndexThe second index.
activityThe cell activity.

Definition at line 349 of file RoomComponent.cs.

◆ SetCellActivity() [1/2]

void SetCellActivity ( int  row,
int  column,
bool  activity 
)

Sets the cell activity for the specified index.

Parameters
rowThe row index.
columnThe column index.
activityThe cell activity.
Exceptions
System.IndexOutOfRangeExceptionRaised if the specified index is out of range.

Definition at line 408 of file RoomComponent.cs.

◆ SetCellActivity() [2/2]

void SetCellActivity ( int  row,
int  column,
CellActivity  activity 
)

Sets the cell activity for the specified index.

Parameters
rowThe row index.
columnThe column index.
activityThe cell activity.
Exceptions
System.IndexOutOfRangeExceptionRaised if the specified index is out of range.
System.NotImplementedExceptionRaised if the specified cell activity is not handled.

Definition at line 378 of file RoomComponent.cs.

◆ SetSizeField()

void SetSizeField ( ref Vector2Int  field,
Vector2Int  value 
)
private

Definition at line 126 of file RoomComponent.cs.

◆ SizeActiveCells()

void SizeActiveCells ( )

Sizes the active cells list to match the room size.

Definition at line 315 of file RoomComponent.cs.

◆ ValidateRoomFlags()

void ValidateRoomFlags ( )

Validates that the room flag ID's are unique.

Exceptions
DuplicateIdExceptionRaised if two room flags have the same ID.

Definition at line 722 of file RoomComponent.cs.

Member Data Documentation

◆ _activeCells

List<ActiveCellsRow> _activeCells = new List<ActiveCellsRow>()
private

Definition at line 62 of file RoomComponent.cs.

◆ _cellSize

Vector3 _cellSize = new Vector3(1, 1, 0.001f)
private

Definition at line 54 of file RoomComponent.cs.

◆ _name

string _name = "<None>"
private

Definition at line 33 of file RoomComponent.cs.

◆ _onCellAreaEntered

CellAreaTriggerEvent _onCellAreaEntered = new CellAreaTriggerEvent()
private

Definition at line 83 of file RoomComponent.cs.

◆ _onCellAreaExited

CellAreaTriggerEvent _onCellAreaExited = new CellAreaTriggerEvent()
private

Definition at line 90 of file RoomComponent.cs.

◆ _onInitialize

UnityEvent _onInitialize = new UnityEvent()
private

Definition at line 69 of file RoomComponent.cs.

◆ _onInitialized

UnityEvent _onInitialized = new UnityEvent()
private

Definition at line 76 of file RoomComponent.cs.

◆ _roomTemplate

RoomTemplateResource _roomTemplate
private

Definition at line 26 of file RoomComponent.cs.

◆ _roomType

RoomType _roomType
private

Definition at line 40 of file RoomComponent.cs.

◆ _size

Vector2Int _size = Vector2Int.one
private

Definition at line 47 of file RoomComponent.cs.

Property Documentation

◆ ActiveCells

List<ActiveCellsRow> ActiveCells
getset

A list of active cell rows.

Definition at line 66 of file RoomComponent.cs.

◆ CellSize

Vector3 CellSize
getset

The size of each cell within the grid coordinate system.

Definition at line 58 of file RoomComponent.cs.

◆ Columns

int Columns
getset

The column index.

Definition at line 124 of file RoomComponent.cs.

◆ IsInitialized

bool IsInitialized
getprivate set

True if the room has been initialized.

Definition at line 99 of file RoomComponent.cs.

◆ LayoutPack

LayoutPack LayoutPack
getprivate set

The layout pack.

Definition at line 104 of file RoomComponent.cs.

◆ Name

string Name
getset

The room name.

Definition at line 37 of file RoomComponent.cs.

◆ OnCellAreaEntered

CellAreaTriggerEvent OnCellAreaEntered
getset

The event invoked when a room cell has been entered. Message includes the enterered cell and the colliding GameObject.

Definition at line 87 of file RoomComponent.cs.

◆ OnCellAreaExited

CellAreaTriggerEvent OnCellAreaExited
getset

The event invoked when a room cell has been exited. Message includes the exited cell and the colliding GameObject.

Definition at line 94 of file RoomComponent.cs.

◆ OnInitialize

UnityEvent OnInitialize
getset

An event invoked when the room is initialized.

Definition at line 73 of file RoomComponent.cs.

◆ OnInitialized

UnityEvent OnInitialized
getset

The event invoked after the room has completed initialization.

Definition at line 80 of file RoomComponent.cs.

◆ RoomLayout

Room RoomLayout
getprivate set

The room data.

Definition at line 109 of file RoomComponent.cs.

◆ RoomState

RoomState RoomState
getprivate set

The room state.

Definition at line 114 of file RoomComponent.cs.

◆ RoomTemplate

RoomTemplateResource RoomTemplate
getset

The room template.

Definition at line 30 of file RoomComponent.cs.

◆ RoomType

The room type.

Definition at line 44 of file RoomComponent.cs.

◆ Rows

int Rows
getset

The row index.

Definition at line 119 of file RoomComponent.cs.

◆ Size

Vector2Int Size
getset

The size of the room grid in rows (x) and columns (y).

Definition at line 51 of file RoomComponent.cs.


The documentation for this class was generated from the following file: