package de.gamezock.metacraft.data; import java.util.List; import java.util.Vector; import javax.vecmath.Point3f; public class Map { private List 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(); 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 getTriangleData() { return triangleData; } }