DungeonManager.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using UnityEngine;
  2. [DefaultExecutionOrder(-200)]
  3. public class DungeonManager : MonoBehaviour
  4. {
  5. public int m_Width = 10;
  6. public int m_Height = 10;
  7. public float m_Spacing = 4.0f;
  8. public GameObject[] m_Tiles = new GameObject[16];
  9. void Awake()
  10. {
  11. Random.InitState(23431);
  12. var map = new int[m_Width * m_Height];
  13. for (int y = 0; y < m_Height; y++)
  14. {
  15. for (int x = 0; x < m_Width; x++)
  16. {
  17. bool px = false;
  18. bool py = false;
  19. if (x > 0)
  20. px = (map[(x - 1) + y * m_Width] & 1) != 0;
  21. if (y > 0)
  22. py = (map[x + (y - 1) * m_Width] & 2) != 0;
  23. int tile = 0;
  24. if (px)
  25. tile |= 4;
  26. if (py)
  27. tile |= 8;
  28. if (x + 1 < m_Width && Random.value > 0.5f)
  29. tile |= 1;
  30. if (y + 1 < m_Height && Random.value > 0.5f)
  31. tile |= 2;
  32. map[x + y * m_Width] = tile;
  33. }
  34. }
  35. for (int y = 0; y < m_Height; y++)
  36. {
  37. for (int x = 0; x < m_Width; x++)
  38. {
  39. var pos = new Vector3(x * m_Spacing, 0, y * m_Spacing);
  40. if (m_Tiles[map[x + y * m_Width]] != null)
  41. Instantiate(m_Tiles[map[x + y * m_Width]], pos, Quaternion.identity);
  42. }
  43. }
  44. }
  45. }