Created
November 18, 2018 20:02
-
-
Save AG-Dan/d14ceab9a920ae889caf53e1b557eabd to your computer and use it in GitHub Desktop.
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; | |
sealed class InjectSpecialRoom : MonoBehaviour | |
{ | |
public TileSet TileSet = null; | |
public bool Required = true; | |
public int CountMin = 1; | |
public int CountMax = 1; | |
public bool CanAppearOnMainPath = true; | |
public bool CanAppearOnBranchPath = true; | |
[Range(0, 1)] | |
public float PathDepthMin = 0f; | |
[Range(0, 1)] | |
public float PathDepthMax = 1f; | |
[Range(0, 1)] | |
public float BranchDepthMin = 0f; | |
[Range(0, 1)] | |
public float BranchDepthMax = 1f; | |
private DungeonGenerator generator; | |
private void Awake() | |
{ | |
var runtimeDungeon = GetComponent<RuntimeDungeon>(); | |
generator = runtimeDungeon.Generator; | |
generator.TileInjectionMethods += InjectCustomTiles; | |
} | |
private void OnDestroy() | |
{ | |
generator.TileInjectionMethods -= InjectCustomTiles; | |
} | |
private void InjectCustomTiles(System.Random randomStream, ref List<InjectedTile> tilesToInject) | |
{ | |
// Make sure none of our variables have unexpected values | |
Debug.Assert(CanAppearOnMainPath || CanAppearOnBranchPath); | |
Debug.Assert(CountMax >= CountMin); | |
Debug.Assert(CountMin >= 0 && CountMax >= 0); | |
Debug.Assert(PathDepthMax >= PathDepthMin); | |
Debug.Assert(BranchDepthMax >= BranchDepthMin); | |
// Pick how many rooms to inject | |
// All numbers between min & max are equally likely, so you might want to adjust the logic here | |
int tileCount = randomStream.Next(CountMin, CountMax + 1); | |
for (int i = 0; i < tileCount; i++) | |
{ | |
bool isOnMainPath; | |
// If it can be on both main and branch path, pick one at random | |
if (CanAppearOnBranchPath && CanAppearOnMainPath) | |
isOnMainPath = randomStream.NextDouble() > 0.5; | |
else | |
isOnMainPath = CanAppearOnMainPath; | |
// Randomize path and branch depths | |
float pathDepth = (float)(randomStream.NextDouble() * (PathDepthMax - PathDepthMin)) + PathDepthMin; | |
float branchDepth = (float)(randomStream.NextDouble() * (BranchDepthMax - BranchDepthMin)) + BranchDepthMin; | |
// Inject tile into dungeon generator | |
var tileInfo = new InjectedTile(TileSet, isOnMainPath, pathDepth, branchDepth, Required); | |
tilesToInject.Add(tileInfo); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment