Basic informations & Features:
- random Maze / Dungeon generator
- C# scripts
- runtime and Editor generation
- selectable map size and density
- pathfinding + distance calculation
- all parts are connected
- random and evenly spreaded object spawn
- use Wall and Floor objects 1x1 m (you can resize the map afterwards)
- option to merge objects to save computer resources
- C# scripts
- runtime and Editor generation
- selectable map size and density
- pathfinding + distance calculation
- all parts are connected
- random and evenly spreaded object spawn
- use Wall and Floor objects 1x1 m (you can resize the map afterwards)
- option to merge objects to save computer resources
Further informations & Screenshots:
To create a Maze in runtime: currently pressing "Enter" generates a new Maze(s) on all the objects with "Maze.cs" script attached on them. You can change in the code which event will trigger it under the "Update" function of the "Maze.cs" script.
All controls used in Demo are under the "Update" function. These are ready to be changed. The texts used in Demo are in the "OnGUI" function. This function can be removed.
Map Size: if "Fit Size" is checked, map size depends on the object (to which "Maze.cs" script is attached) x and z scale. Map can be rectangular too.
All controls used in Demo are under the "Update" function. These are ready to be changed. The texts used in Demo are in the "OnGUI" function. This function can be removed.
Map Size: if "Fit Size" is checked, map size depends on the object (to which "Maze.cs" script is attached) x and z scale. Map can be rectangular too.
Map Density:
Pathfinding: functions "CreatePath" of the "Maze.cs" script find the random shortest path between the two fields or objects. The path is generated using the "Path Object". These functions return the path in the List<T> class. This List is filled with Field coordinates starting from the first field or object, ending the second one. It can be used for AI or any other purposes. If "Path Object" is null the path is not physically created (no objects created), however it is still returned in the List<T>. The Fields stored in the List<T> can be converted to the local position using the function "GetFieldLocalPos" for further use. If "simplify" argument of the "CreatePath" function is true, the path will contain only even fields of the path. This can be better for AI and for diagonal movement.
Object Spawn: there are three ways to spawn objects on the map. First way ("CreateObject" function) is used to spawn one concrete object at a specific spot (field). Second way ("SpawnObject" function) is used to spawn a specific number of one object randomly at map. And the last way ("SpawnObjects" function) is used to spawn objects from the List<T> ("Objects To Spawn" list) on a specific percentage ("Objects Quantity") of the free fields of the map. You can also choose if the objects will be spawned at the same amount (+-1) or at random amount (using "Spawn Evenly" property). For example there is 300 free fields on the map and "Objects Quantity" is 5 and there are 3 objects in the "Objects To Spawn" list. 5% of 300 is 15, so 15 objects will be spawned. If "Spawn Evenly" is checked, each of the 3 objects from the list will be spawned 5 times. This function also spread all these spawned objects on the map (there is always some minimum distance between objects). This includes objects already placed on the map (if they are inside the "spawnedObjects" list). "SpawnObjects" function is also called with the map generation.
Walls and Floors Merging: merging is used to increase performance and save computer memory. It can take very long to create big mazes without merging. Following images show how merging works:
Additional Screenshots:
Functions of the "Maze.cs" script:
Unity defined functions:
void Start () - can be deleted
Public functions:
public void GenerateMaze()
Private functions:
void CreateTerrainObject(GameObject obj, int objectID, int i, int j, bool merge)
void Start () - can be deleted
- Create Map and place White and Purple Ball.
- Checking player input (controls).
- Display controls and Map Info.
Public functions:
public void GenerateMaze()
- Delete the old maze (including "spawnedObjects" list) and create a new one.
- Create Walls and Floors.
- Spawn objects using "objectsToSpawn", "objectsQuantity" and "spawnEvenly" properties.
- Objects will be spawned with distances between each other depending on the map shape and on the number of objects to spawn.
- Objects will not stack each other (objects will not stack any object from the "spawnedObjects" list).
- Spawn specific number of one object at random spots.
- Objects will not stack each other (objects will not stack any object from the "spawnedObjects" list).
- If "count" > free fields: only maximum possible amount (free fields) of objects will be spawned.
- Create one object of concrete scale at specific spot (center of the field).
- Object will be stacked if another object is already there.
- Create one object of concrete scale multiple at specific spot (center of the field).
- Object will be stacked if another object is already there.
- Create one object at specific spot (center of the field).
- Object will be stacked if another object is already there.
- Returns random floor field (not checking if the field is free or not).
- Delete Maze and clear "spawnedObjects" list.
- Returns distance (in fields) between the two fields.
- Returns distance (in fields) between the GameObject and the field.
- Returns distance (in fields) between the two GameObjects.
- Returns maximum possible distance (in fields) you can go from the field (-1 returned for invalid entry).
- Returns maximum possible distance (in fields) you can go from the GameObject (-1 returned for invalid entry).
- Returns random but minimum path from "fieldA" to "fieldB" in the List<Field>.
- If "simplify" is true: the path contains only each second field (enough for movement and in most cases better - for diagonal movement).
- If "generatePath" is true: the path will be filled with the "pathObject".
- Returns random but minimum path from "field" to GameObject "obj" in the List<Field>.
- If "simplify" is true: the path contains only each second field (enough for movement and in most cases better - for diagonal movement).
- If "generatePath" is true: the path will be filled with the "pathObject".
- Returns random but minimum path from GameObject "obj" to "field" in the List<Field>.
- If "simplify" is true: the path contains only each second field (enough for movement and in most cases better - for diagonal movement).
- If "generatePath" is true: the path will be filled with the "pathObject".
- Returns random but minimum path from GameObject "objA" to GameObject "objB" in the List<Field>.
- If "simplify" is true: the path contains only each second field (enough for movement and in most cases better - for diagonal movement).
- If "generatePath" is true: the path will be filled with the "pathObject".
- Delete the old path (delete all "pathObject" generated by "CreatePath" function).
- Returns GameObject current position as a Field (map coordinates).
- Returns local position of the center of the field.
Private functions:
void CreateTerrainObject(GameObject obj, int objectID, int i, int j, bool merge)
- Used to instantiate wall and floor objects.
- Count size of a merged object.
- Returns the next GameObject from the list (if "nextObject" is true) or the random one (if "nextObject" is false).
- Used to handle "spawnEvenly" property.
- Returns all free map fields in the List<Field>.
- Used to clear the map array into default values (0 = wall, 1 = floor) after the calculations (distance, path, etc.).