-
-
Save AG-Dan/47f31bebc9b6007ed314250a062b00c8 to your computer and use it in GitHub Desktop.
A script allowing us to limit the number of tiles that will appear in the dungeon to only one (per TileSet listed in the component's properties)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DunGen; | |
using System.Collections.Generic; | |
using UnityEngine; | |
/// <summary> | |
/// Only allows one tile to be placed from each of a group of specific TileSets | |
/// </summary> | |
public class TileSetLimitRule : MonoBehaviour | |
{ | |
public RuntimeDungeon RuntimeDungeon; | |
public TileSet[] TileSets; | |
private TileConnectionRule rule; | |
private List<TileSet> usedTileSets = new List<TileSet>(); | |
private void OnEnable() | |
{ | |
// Create & apply our tile connection rule | |
rule = new TileConnectionRule(CanTilesConnect); | |
DoorwayPairFinder.CustomConnectionRules.Add(rule); | |
RuntimeDungeon.Generator.OnGenerationStatusChanged += OnDungeonGeneratorstatusChanged; | |
} | |
private void OnDisable() | |
{ | |
// Unregister our tile connection rule | |
DoorwayPairFinder.CustomConnectionRules.Remove(rule); | |
rule = null; | |
RuntimeDungeon.Generator.OnGenerationStatusChanged -= OnDungeonGeneratorstatusChanged; | |
} | |
private void OnDungeonGeneratorstatusChanged(DungeonGenerator generator, GenerationStatus status) | |
{ | |
// Reset out list of used TileSets before the generator starts placing tiles | |
if (status == GenerationStatus.PreProcessing) | |
usedTileSets.Clear(); | |
} | |
private TileConnectionRule.ConnectionResult CanTilesConnect(Tile tileA, Tile tileB, Doorway doorwayA, Doorway doorwayB) | |
{ | |
// Find which limited TileSet the prospective tile belongs to (if any) | |
var tileSet = GetTileSetContainingTile(tileB); | |
// If the tile belongs to a TileSet that we want to limit.. | |
if (tileSet != null) | |
{ | |
// ..and we've already used a tile from that set, disallow the new tile | |
if (usedTileSets.Contains(tileSet)) | |
return TileConnectionRule.ConnectionResult.Deny; | |
// ..otherwise, add it to the list so we can avoid placing any more tiles from that set | |
else | |
usedTileSets.Add(tileSet); | |
} | |
return TileConnectionRule.ConnectionResult.Passthrough; | |
} | |
private TileSet GetTileSetContainingTile(Tile tile) | |
{ | |
foreach (var tileSet in TileSets) | |
{ | |
foreach (var tileWeight in tileSet.TileWeights.Weights) | |
{ | |
if (tileWeight.Value == tile.gameObject) | |
return tileSet; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment