-
Notifications
You must be signed in to change notification settings - Fork 3
/
Terrain.pde
95 lines (91 loc) · 3.95 KB
/
Terrain.pde
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
class Terrain {
int mapSize, mapSquareSize, worldEdge; /*mapSquareSize is the size of each map rectangle section, worldEdge is the size of the border*/
PImage[] s = new PImage[8]; /*Array of the Terrain textures*/
PImage cMap; /*Image of the complete map*/
JSONObject map = new JSONObject(); /*Map with key:value setting in .json*/
int pX, pY; /*Save player position*/
void init() { /*Initialization of the object*/
cMap = createImage(1000,1000,RGB); /*map unique image*/
for (int i = 0; i<8; i++){
s[i] = loadImage("Terrain/Floor ("+(i+1)+").jpg"); /*Load the terrain textures*/
s[i].resize(cMap.width/mapSize,cMap.width/mapSize);
}
/**/ /*<-- Noise Generator in HashMap and minimap -->*/
noiseDetail(4, 0.53);
float increment = 0.1;
float xoff = 0.0;
for (int x = 0; x <= mapSize; x++) { /*Terrain preset*/
xoff += increment;
float yoff = 0.0;
for (int y = 0; y <= mapSize; y++) {
yoff += increment;
float perlinValue = noise(xoff, yoff);
if(perlinValue<0.25) {
map.setFloat(x+"-"+y,3);
cMap.set(x*cMap.width/mapSquareSize,y*cMap.width/mapSquareSize,s[6]);
}else if(perlinValue<0.55) {
map.setFloat(x+"-"+y,0);
cMap.set(x*cMap.width/mapSquareSize,y*cMap.width/mapSquareSize,s[4]);
}else if(perlinValue<0.8) {
map.setFloat(x+"-"+y,1);
cMap.set(x*cMap.width/mapSquareSize,y*cMap.width/mapSquareSize,s[3]);
}else {
map.setFloat(x+"-"+y,2);
cMap.set(x*cMap.width/mapSquareSize,y*cMap.width/mapSquareSize,s[2]);
}
worldEdge = (int)map(noise(xoff,yoff*2), 0, 1, 5, 15);
if (x<worldEdge || y<worldEdge || x > mapSize-worldEdge || y > mapSize-worldEdge) {
map.setFloat(x+"-"+y,1);
cMap.set(x*cMap.width/mapSquareSize,y*cMap.width/mapSquareSize,s[3]);
}
}
}
PVector dun = new PVector(int(random(0,mapSize)),int(random(0,mapSize)));
for (int i = 0; i < 9; i++) {
while(map.getFloat((int)dun.x+"-"+(int)dun.y) != 0){
dun = new PVector(int(random(15,mapSize-15)),int(random(15,mapSize-15)));
}
map.setInt((int)dun.x+"-"+(int)dun.y,10*(i+1));
cMap.set(int(dun.x)*cMap.width/mapSquareSize,int(dun.y)*cMap.width/mapSquareSize,s[5]);
}
cMap.save("data/Save/Map.png"); /*save Minimap image*/
saveJSONObject(map,"data/Save/Map.json"); /*save Map json*/
/**/ /*<-- end -->*/
}
void paint() { /*Terrain Visualization*/
pushMatrix();
rotateX(PI/2);
translate(-mapSquareSize*mapSize/2, -mapSquareSize*mapSize/2);
for (int x = 0; x <= mapSize; x++) {
for (int y = 0; y <= mapSize; y++) {
if (map.getFloat(x+"-"+y)==0)
image(s[4], x*mapSquareSize, y*mapSquareSize, mapSquareSize, mapSquareSize);
if (map.getFloat(x+"-"+y)==1)
image(s[3], x*mapSquareSize, y*mapSquareSize, mapSquareSize, mapSquareSize);
if (map.getFloat(x+"-"+y)==2)
image(s[2], x*mapSquareSize, y*mapSquareSize, mapSquareSize, mapSquareSize);
if (map.getFloat(x+"-"+y)==3)
image(s[6], x*mapSquareSize, y*mapSquareSize, mapSquareSize, mapSquareSize);
if (map.getInt(x+"-"+y)==10 || map.getInt(x+"-"+y)==20 || map.getInt(x+"-"+y)==30 || map.getInt(x+"-"+y)==40 || map.getInt(x+"-"+y)==50 || map.getInt(x+"-"+y)==60 || map.getInt(x+"-"+y)==70 || map.getInt(x+"-"+y)==80 || map.getInt(x+"-"+y)==90)
image(s[5], x*mapSquareSize, y*mapSquareSize, mapSquareSize, mapSquareSize);
}
}
popMatrix();
}
void paintMinimap() { /*Minimap*/
pushMatrix();
translate(width/2, height/2);
imageMode(CENTER);
image(cMap, 0, 0, height,height);
/**/ /*Player in minimap*/
fill(255, 0, 0);
rectMode(CORNER);
rect(map(mapP.x,-mapSize/2,mapSize/2,-height/2,height/2), map(mapP.z,-mapSize/2,mapSize/2,-height/2,height/2), height/mapSquareSize, height/mapSquareSize);
popMatrix();
}
Terrain() {
noiseSeed((int)random(0, 1000)); /*Random noiseSeed for terrain*/
mapSize = mapSquareSize = 100;
init();
}
}