-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapGenerator.cs
More file actions
89 lines (75 loc) · 2.47 KB
/
MapGenerator.cs
File metadata and controls
89 lines (75 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapGenerator : MonoBehaviour
{
public Transform tilePrefab;
public Transform obstaclePrefab;
public Vector2 mapSize;
[Range(0,1)]
public float outlinePercent;
List<Coord> allTileCoords;
Queue<Coord> shuffledTileCoords;
public int seed = 10;
void Start()
{
GenerateMap();
}
public void GenerateMap ()
{
allTileCoords = new List<Coord>();
for (int x = 0; x < mapSize.x; x++)
{
for (int y = 0; y < mapSize.y; y++)
{
allTileCoords.Add(new Coord(x, y));
}
}
shuffledTileCoords = new Queue<Coord>(Utility.ShuffleArray(allTileCoords.ToArray(), seed));
string holderName = "Generated Map";
if(transform.Find(holderName))
{
DestroyImmediate(transform.Find(holderName).gameObject);
}
Transform mapHolder = new GameObject(holderName).transform;
mapHolder.parent = transform;
for(int x=0; x<mapSize.x; x++)
{
for(int y=0;y < mapSize.y; y++)
{
Vector3 tilePosition = CoordToPosition(x, y);
Transform newTile = Instantiate(tilePrefab, tilePosition, Quaternion.Euler(Vector3.right * 90)) as Transform;
newTile.localScale = Vector3.one * (1 - outlinePercent);
newTile.parent = mapHolder;
}
}
int obstacleCount = 10;
for(int i=0; i<obstacleCount; i++)
{
Coord randomCoord = GetRandomCoord();
Vector3 obstaclePosition = CoordToPosition(randomCoord.x, randomCoord.y);
Transform newObstacle = Instantiate(obstaclePrefab, obstaclePosition + Vector3.up * 0.5f, Quaternion.identity) as Transform;
newObstacle.parent = mapHolder;
}
}
Vector3 CoordToPosition (int x, int y)
{
return new Vector3(-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y / 2 + 0.5f + y);
}
public Coord GetRandomCoord()
{
Coord randomCoord = shuffledTileCoords.Dequeue();
shuffledTileCoords.Enqueue(randomCoord);
return randomCoord;
}
public struct Coord
{
public int x;
public int y;
public Coord(int _x, int _y)
{
x = _x;
y = _y;
}
}
}