Hi,
Was wondering how I could add water tiles to my caves, the caves are generated using cellular automata which basically
sets certain cells to blank or walls in 2d array giving illusion of caves. Now, I don’t know the position of these
as there is no real concept of caves, just blank cells, so this is making it difficult to add water tiles.
Anybody any ideas of how I could do this?
Here is my ‘cave’ generation code: (web site here for my blog of the game:
https://sites.google.com/site/sterrialand/development/news/cavesarelookinggood
package com.mygdx.game;
import java.util.Random;
public class CellularAutomata {
private BlankEntity[][] map = null;
private BlankEntity[][] newMap = null;
private Random r = new Random();
private int chanceToStartAlive, w, h, death, life;
public CellularAutomata(int w, int h, int chanceToStartAlive, int death,
int life) {
this.w = w;
this.h = h;
this.chanceToStartAlive = chanceToStartAlive;
this.death = death;
this.life = life;
}
public void initialiseMap() {
map = new BlankEntity[w][h];
for (int x = 0; x < map.length; x++)
for (int y = 0; y < map[0].length; y++)
map[x][y] = r.nextInt(100) > chanceToStartAlive ? new LandscapeEntity(x,y) : null;
}
// Debug
public void displayMap() {
if (map != null) {
for (int x = 0; x < map.length; x++, System.out.println())
for (int y = 0; y < map[0].length; y++)
System.out.print(map[x][y]);
}
}
public BlankEntity[][] getMap() {
return map;
}
public BlankEntity[][] doSimulationStep() {
newMap = new BlankEntity[w][h];
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[0].length; y++) {
int nbs = countAliveNeighbours(map, x, y);
if (map[x][y] instanceof LandscapeEntity)
newMap[x][y] = nbs <= life ? null : new LandscapeEntity(x,y);
else
newMap[x][y] = nbs > death ? new LandscapeEntity(x,y) : null;
}
}
map = newMap;
return newMap;
}
private int countAliveNeighbours(BlankEntity[][] map, int x, int y) {
int count = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
int neighbour_x = x + i;
int neighbour_y = y + j;
if (i == 0 && j == 0) { // do nothing as this is us!
} else if (neighbour_x < 0 || neighbour_y < 0
|| neighbour_x >= map.length
|| neighbour_y >= map[0].length) {
count++;
} else if (map[neighbour_x][neighbour_y] instanceof LandscapeEntity)
count++;
}
}
return count;
}
}
and the BlankEntity class
package com.mygdx.game;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
enum GrassType{ TOP, LEFT, RIGHT, FLAT};
class BlankEntity {
public int x,y;
protected final int size = 4; // 4 if 16x16, 5 if using 32x32 tiles
public BlankEntity(int x, int y)
{
this.x = x; this.y = y;
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[1][0],x << size,y << size); //0,0
}
@Override
public String toString() {
return ".";
}
}
class LandscapeEntity extends BlankEntity{
public LandscapeEntity(int x, int y)
{
super(x,y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[0][0],x << size,y << size);
}
@Override
public String toString() {
return "#";
}
}
class LandscapeEntityRight extends BlankEntity{
public LandscapeEntityRight(int x, int y)
{
super(x,y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[2][1],x << size,y << size);
}
@Override
public String toString() {
return "#";
}
}
class LandscapeEntityLeft extends BlankEntity{
public LandscapeEntityLeft(int x, int y)
{
super(x,y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[2][0],x << size,y << size);
}
@Override
public String toString() {
return "#";
}
}
class CaveEntity extends BlankEntity{
public CaveEntity(int x, int y)
{
super(x,y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[1][1],x << size,y << size);
}
@Override
public String toString() {
return ".";
}
}
class CaveRightEntity extends CaveEntity{
public CaveRightEntity(int x, int y)
{
super(x,y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[1][3],x << size,y << size);
}
@Override
public String toString() {
return ";";
}
}
class CaveLeftEntity extends CaveEntity{
public CaveLeftEntity(int x, int y)
{
super(x,y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[1][2],x << size,y << size);
}
@Override
public String toString() {
return ":";
}
}
class CaveTopEntity extends CaveEntity{
public CaveTopEntity(int x, int y)
{
super(x,y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[1][4],x << size,y << size);
}
@Override
public String toString() {
return ":";
}
}
class CaveBottomEntity extends CaveEntity{
public CaveBottomEntity(int x, int y)
{
super(x,y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[1][5],x << size,y << size);
}
@Override
public String toString() {
return ":";
}
}
class LavaEntity extends BlankEntity {
public LavaEntity(int x, int y)
{
super(x, y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[26][4],x << size,y << size); // 6,5
}
}
class GrassEntity extends BlankEntity{
private GrassType type;
public GrassType getGrassType() { return type; }
public GrassEntity(int x, int y, GrassType type)
{
super(x, y);
this.type = type;
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[0][3], x << size, y << size);
}
}
class WeedsEntity extends BlankEntity{
public WeedsEntity(int x, int y)
{
super(x, y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[13][0], x << size, y << size);
}
}
class GrassLeftEntity extends GrassEntity{
public GrassLeftEntity(int x, int y)
{
super(x, y, GrassType.LEFT);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[0][2], x << size, y << size);
}
}
class GrassRightEntity extends GrassEntity{
public GrassRightEntity(int x, int y)
{
super(x, y, GrassType.RIGHT);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[0][4], x << size, y << size);
}
}
class GrassTopEntity extends GrassEntity{
public GrassTopEntity(int x, int y)
{
super(x, y, GrassType.TOP);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[0][5], x << size, y << size);
}
}
class CoalEntity extends BlankEntity{
public CoalEntity(int x, int y)
{
super(x, y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[2][2], x << size, y << size); // 0,19
}
}
class RockEntity extends BlankEntity{
public RockEntity(int x, int y)
{
super(x, y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[2][10], x << size, y << size); // 0,1
}
}
class WaterEntity extends BlankEntity{
public WaterEntity(int x, int y)
{
super(x, y);
}
public void draw(Batch batch, TextureRegion[][] t)
{
batch.draw(t[2][0], x << size, y << size);
}
}
Hope somebody can shed some light on how I can do this.
Thanks