-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapManager.cs
114 lines (97 loc) · 3.45 KB
/
MapManager.cs
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapManager : MonoBehaviour {
private Transform mapHolder;
public GameObject[] floorArray;
public GameObject[] outWallArray;
public GameObject[] wallArray;
public GameObject[] foodArray;
public GameObject[] enemyArray;
public GameObject exitUI;
private GameManager gameManager;
public int rows;
public int cols;
public int level;
private List<Vector2> positionList = new List<Vector2>();
void Awake() {
level = PlayerPrefs.GetInt("level");
gameManager =this.GetComponent<GameManager>();
InitMap();
}
private void InitMap(){
mapHolder = new GameObject("Map").transform;
makeFrame(rows,cols);
makeHinder(rows,cols);
}
private void makeFrame(int rows,int cols){
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
{
if (x == 0 || y == 0 || x == rows - 1 || y == cols - 1)
{
creatMap(new Vector2(x,y),outWallArray);
}
else
{
creatMap(new Vector2(x,y),floorArray);
}
}
}
}
private void makeHinder(int rows,int cols){
positionList.Clear();
for (int x = 2; x < rows-2 ; x++) {
for (int y = 2; y < cols-2 ; y++) {
positionList.Add(new Vector2(x, y));
}
}
int minCountWall = 0;
int minFood = 0;
int minEnemy = 0;
if (level < 3)
{
int positionIndex = Random.Range(0, 4);
creatMap(positionList[positionIndex], exitUI);
positionList.RemoveAt(positionIndex);
positionIndex = Random.Range(0, 5);
creatMap(positionList[positionIndex], foodArray[1]);
positionList.RemoveAt(positionIndex);
positionIndex = Random.Range(0, 5);
creatMap(positionList[positionIndex], wallArray[1]);
positionList.RemoveAt(positionIndex);
}
else
{
decorateExit(exitUI);
decorateHinder(minCountWall, level * 9, wallArray);
decorateHinder(minFood, level * 5, foodArray);
decorateHinder(minEnemy, level * 15, enemyArray);
}
}
private void decorateHinder(int mix,int max,GameObject[] array) {
int count = Random.Range(mix, max);
for (int i = 0; i < count; i++) {
int positionIndex = Random.Range(0, positionList.Count);
creatMap(positionList[positionIndex], array);
positionList.RemoveAt(positionIndex);
}
}
private void decorateExit(GameObject exitUI) {
int positionIndex = Random.Range(0,positionList.Count);
creatMap(positionList[positionIndex],exitUI);
positionList.RemoveAt(positionIndex);
}
private void creatMap(Vector2 pos, GameObject go)
{
GameObject map = Instantiate(go, pos, Quaternion.identity);
map.transform.SetParent(mapHolder);
}
private void creatMap(Vector2 pos, GameObject[] array )
{
int arrayIndex = Random.Range(0, array.Length);
GameObject map = Instantiate(array[arrayIndex], pos, Quaternion.identity);
map.transform.SetParent(mapHolder);
}
}