-
Notifications
You must be signed in to change notification settings - Fork 0
/
Desert_Generator.cs
145 lines (106 loc) · 2.77 KB
/
Desert_Generator.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Desert_Generator : MonoBehaviour
{
public GameObject sandTile;
public int desertSize=7;
public int x=0;
public int z=0;
private int skip=0;
private RaycastHit hit;
//still need to fix an edge case where it spawns deserts uner the water tile. the raycast seems to sill think it is hitting grass. not sure why.
//destroy what is underneath
void destroyTile(){
if(x%2==0){
if (Physics.Raycast(new Vector3(-.75f*x,1f,0-.86f*z), Vector3.down,out hit, 2f)){
//checks if hit water tile. be sure to use IEnumerator on the previous step if needs to be completed first
if (hit.collider.gameObject.name=="Water(Clone)"){
skip++;
}
if(skip!=1){
hit.transform.gameObject.SendMessage("destroySelf");
}
}
}
if(x%2==1){
if (Physics.Raycast(new Vector3(-.75f*x,1f,-.43f-.86f*z), Vector3.down,out hit, 2f)){
if (hit.collider.gameObject.name=="Water(Clone)"){
skip++;
}
if(skip!=1){
hit.transform.gameObject.SendMessage("destroySelf");
}
}
}
}
//spawns a sand tile
void createTile(){
if(x%2==0 && skip==0){
GameObject tile = Instantiate(sandTile, new Vector3(-.75f*x,0f,-.86f*z), Quaternion.identity);
gameObject.GetComponent<Resource_Placer>().DesertTilePosition.Add(tile.transform.position);
}
if(x%2==1 && skip==0){
GameObject tile = Instantiate(sandTile, new Vector3(-.75f*x,0f,-.43f-.86f*z), Quaternion.identity);
gameObject.GetComponent<Resource_Placer>().DesertTilePosition.Add(tile.transform.position);
}
if(skip==1){
skip--;
}
}
public void makeDesert(int xmapmax, int zmapmax){
x=Random.Range(2,xmapmax-2);
z=Random.Range(2,zmapmax-2);
for (int col=1; col<4; col++){
//place leftmost tiles
if (col==1){
//place two tiles one below the other
for (int t=1; t<3; t++){
z++;
destroyTile();
createTile();
}
}
if (col==2){
//step to the right one
x++;
if (x%2==0){
z++;
destroyTile();
createTile();
z--;
destroyTile();
createTile();
z--;
destroyTile();
createTile();
}
else{
z=z-2;
destroyTile();
createTile();
z++;
destroyTile();
createTile();
z++;
destroyTile();
createTile();
}
}
if (col==3){
//step to the right one
x++;
//place two tiles one above the other
z=z+1;
destroyTile();
createTile();
z--;
destroyTile();
createTile();
}
}
//spawn hills here
gameObject.GetComponent<Hill_Generator>().makeHill(xmapmax, zmapmax);
//yield return null;
}
}