summaryrefslogtreecommitdiffstats
path: root/src/de/gamezock/metacraft/data/Map.java
blob: 770ffe7ae5e813345c31c2edc235c5e0e81b615f (plain)
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
package de.gamezock.metacraft.data;

import java.util.List;
import java.util.Vector;

import javax.vecmath.Point3f;

public class Map {
  private List<Triangle> triangleData;
  private float[][] heightmap;
  
  
	public Map() {
	  int width = 5;
	  int height = 5;
	  
	  float[][] tileHeightmap = new float[width][height];
	  Tile[][] tiles = new Tile[width][height];
	  
	  for(int i = 0; i < width; ++i) {
	    for(int j = 0; j < height; ++j) {
	      tileHeightmap[i][j] = ((i-2)*(i-2)+(j-2)*(j-2))*0.5f;
	      tiles[i][j] = new Tile(new TestTile());
	    }
	  }
	  
	  createMapData(tiles, tileHeightmap);
	}
	
	private void createMapData(Tile[][] tiles, float[][] tileHeightmap) {
	  int tileSize = tiles[0][0].getData().getSize();
	  
	  heightmap = new float[tiles.length * tileSize][tiles[0].length * tileSize];
	  
	  for(int i = 0; i < tiles.length; ++i) {
	    for(int j = 0; j < tiles[i].length; ++j) {
	      float[][] localHeightmap = tiles[i][j].getData().getHeightmap();
	      
	      for(int x = 0; x < tileSize; ++x) {
	        for(int y = 0; y < tileSize; ++y) {
	          heightmap[i*tileSize + x][j*tileSize + y] = tileHeightmap[i][j] + localHeightmap[x][y];
	        }
	      }
	    }
	  }
	  
	  triangleData = new Vector<Triangle>();
	  
	  for(int x = 0; x < heightmap.length-1; ++x) {
	    for(int y = 0; y < heightmap[x].length-1; ++y) {
	      float[] h = new float[4];
	      h[0] = heightmap[x][y];
	      h[1] = heightmap[x+1][y];
	      h[2] = heightmap[x+1][y+1];
	      h[3] = heightmap[x][y+1];

	      float center = (h[0] + h[1] + h[2] + h[3]) / 4;

	      for(int i = 0; i < 4; ++i) {
	        int i_1 = (i+1)%4;
	        int i_2 = (i+2)%4;

	        Point3f v1 = new Point3f(x + i_1/2, y + i/2, h[i]);
	        Point3f v2 = new Point3f(x+0.5f, y+0.5f, center);
	        Point3f v3 = new Point3f(x + i_2/2, y + i_1/2, h[i_1]);
	        
	        triangleData.add(new Triangle(v1, v2, v3));
	      }
	    }
	  }
	}
	
	/*public Tile getTile(int x, int y) {
	  return tiles[x][y];
	}
	
	public float getTileHeight(int x, int y) {
	  return heightmap[x][y];
	}*/
	
	public int getWidth() {
	  return heightmap.length;
	}
	
	public int getHeight() {
	  return heightmap[0].length;
	}
	
	/*public int getTileSize() {
	  return tileSize;
	}*/
	
	public List<Triangle> getTriangleData() {
	  return triangleData;
	}
}