Skip to content

Instantly share code, notes, and snippets.

@AG-Dan
Created April 28, 2020 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AG-Dan/04e0d85ae651dc15b7e24b6ec416cc09 to your computer and use it in GitHub Desktop.
Save AG-Dan/04e0d85ae651dc15b7e24b6ec416cc09 to your computer and use it in GitHub Desktop.
using DunGen;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Injects multiple tiles from a TileSet (between MinCount - MaxCount)
/// Tiles can appear anywhere throughout the dungeon
/// </summary>
public class InjectTileRange : MonoBehaviour
{
public TileSet TileSet = null;
public int MinCount = 1;
public int MaxCount = 3;
private void Start()
{
var runtimeDungeon = FindObjectOfType<RuntimeDungeon>();
var generator = runtimeDungeon.Generator;
generator.TileInjectionMethods += InjectTiles;
}
private void InjectTiles(System.Random randomStream, ref List<InjectedTile> tilesToInject)
{
// Required tiles
for (int i = 0; i < MinCount; i++)
InjectSingleTile(randomStream, ref tilesToInject, true);
// Optional tiles
int optionalTileCount = MaxCount - MinCount;
for (int i = 0; i < optionalTileCount; i++)
InjectSingleTile(randomStream, ref tilesToInject, false);
}
private void InjectSingleTile(System.Random randomStream, ref List<InjectedTile> tilesToInject, bool isRequired)
{
// Just allow the tile to appear anywhere in the dungeon
bool isOnMainPath = randomStream.NextDouble() < 0.5; // Equal chance to appear on main or branch path
float pathDepth = (float)randomStream.NextDouble(); // 0-1
float branchDepth = (float)randomStream.NextDouble(); // 0-1
var tile = new InjectedTile(TileSet, isOnMainPath, pathDepth, branchDepth, isRequired);
tilesToInject.Add(tile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment