Skip to content

Instantly share code, notes, and snippets.

@wmiller
Created February 3, 2014 05:06
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 wmiller/8779101 to your computer and use it in GitHub Desktop.
Save wmiller/8779101 to your computer and use it in GitHub Desktop.
XML Database MapBuilder2
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class MapBuilder2 : MapBuilder
{
protected override void InitMapObjects()
{
for (int y = 0; y < Height; ++y)
{
for (int x = 0; x < Width; ++x)
{
int height = HeightField[y * Width + x];
// Get all tiles in the database that match the height
TileInfo[] tileInfos = Database.Instance.GetEntries<TileInfo>().Where((info) => info.Height == height).ToArray();
// Instance the tile
TileInfo tileInfo = tileInfos[Random.Range(0, tileInfos.Length)];
InstantiateMapPrefab(x, y, MapObjectDepth.Tile, Resources.Load<GameObject>(tileInfo.PrefabPath));
// See if we drop a feature
if (tileInfo.PossibleFeatures != null &&
tileInfo.PossibleFeatures.Length > 0 &&
Random.Range(0, 100) < tileInfo.FeatureChance)
{
// Select a random feature from the tile infos possible features
FeatureInfo featureInfo = tileInfo.PossibleFeatures[Random.Range(0, tileInfo.PossibleFeatures.Length)];
InstantiateMapPrefab(x, y, MapObjectDepth.Feature, Resources.Load<GameObject>(featureInfo.PrefabPath));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment