133 lines
3.8 KiB
Java
Executable File
133 lines
3.8 KiB
Java
Executable File
package projectzombie.world.layer.layergen;
|
|
|
|
import java.util.Random;
|
|
|
|
import gl_engine.MathHelpers;
|
|
import gl_engine.vec.Vec2d;
|
|
import gl_engine.vec.Vec2i;
|
|
import gl_engine.vec.Vec3d;
|
|
import projectzombie.Main;
|
|
import projectzombie.entity.Entity;
|
|
import projectzombie.entity.EntityZombie;
|
|
import projectzombie.init.Tiles;
|
|
import projectzombie.util.math.ColorRange;
|
|
import projectzombie.util.math.TileState;
|
|
import projectzombie.util.math.random.OpenSimplexNoise;
|
|
import projectzombie.util.math.random.RandomHelpers;
|
|
import projectzombie.world.chunk.Chunk;
|
|
import projectzombie.world.layer.Layer;
|
|
|
|
public class LayerGenCaves extends LayerGen
|
|
{
|
|
|
|
@Override
|
|
public void generateChunk(Chunk chunk, Layer layer, Random rand, Vec2i c_pos)
|
|
{
|
|
// Is there going to be a portal up in this chunk
|
|
boolean portal = PortalSpawnrates.WorldCavePortal(rand);
|
|
Vec2i portal_pos = null;
|
|
if(portal) portal_pos = new Vec2i(
|
|
RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.mx),
|
|
RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.my));
|
|
|
|
// Is there going to be a portal down in this chunk
|
|
boolean portal_down = PortalSpawnrates.CaveLavaCavePortal(rand);
|
|
Vec2i portal_down_pos = null;
|
|
if(portal_down) portal_down_pos = new Vec2i(
|
|
RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.mx),
|
|
RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.my));
|
|
|
|
// Get some noise generators
|
|
OpenSimplexNoise noisegen_n = new OpenSimplexNoise(new Random(layer.lseed).nextLong());
|
|
|
|
// Loop over the chunk
|
|
for(int x=0;x<Chunk.CHUNK_SIZE.mx;x++) {
|
|
for(int y=0;y<Chunk.CHUNK_SIZE.my;y++)
|
|
{
|
|
// Get the x and y in the world
|
|
int cx = x + c_pos.x * Chunk.CHUNK_SIZE.mx;
|
|
int cy = y + c_pos.y * Chunk.CHUNK_SIZE.my;
|
|
|
|
// Get the noise value and the position vector
|
|
double noise_n = 100 - MathHelpers.positive( noisegen_n.eval(cx/20.0, cy/20.0) * 100 );
|
|
Vec2i pos = new Vec2i(x, y);
|
|
|
|
if(noise_n > 80) {
|
|
chunk.setBackTile(Tiles.STONE.getDefaultState(), pos);
|
|
}
|
|
|
|
else {
|
|
chunk.setBackTile(Tiles.WALL.getDefaultState(), pos);
|
|
}
|
|
}
|
|
}
|
|
|
|
if(portal) {
|
|
chunk.setFrontTile(new TileState(Tiles.LADDER_UP, (short)0), portal_pos);
|
|
}
|
|
|
|
if(portal_down) {
|
|
chunk.setBackTile(new TileState(Tiles.PORTAL_DOWN, (short)2), portal_down_pos);
|
|
chunk.setFrontTile(Tiles.LADDER.getDefaultState(), portal_down_pos);
|
|
}
|
|
|
|
Vec2i chest_pos = new Vec2i(
|
|
RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.mx),
|
|
RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.my));
|
|
|
|
// Only place a chest here if the tile is clear
|
|
if(chunk.getBackTile(chest_pos).tile == getTileDestroyed().tile) {
|
|
chunk.setFrontTile(new TileState(Tiles.CHEST, 1), chest_pos);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void spawnEntities(Layer layer, Random rand)
|
|
{
|
|
if(rand.nextDouble() > 0.9)
|
|
{
|
|
Entity zombie = new EntityZombie(new Vec2d(
|
|
RandomHelpers.randrange(rand, (int)Main.player.pos.x - 128, (int)Main.player.pos.x + 128),
|
|
RandomHelpers.randrange(rand, (int)Main.player.pos.y - 128, (int)Main.player.pos.y + 128)));
|
|
|
|
boolean exists = false;
|
|
|
|
for(Entity e : layer.getChunk(zombie.pos).entities) {
|
|
if(e.getClass() == zombie.getClass()) {
|
|
exists = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(
|
|
!exists && layer.getBackTile(new Vec2i((int)zombie.pos.x,
|
|
(int)zombie.pos.y)).tile == getTileDestroyed().tile &&
|
|
zombie.pos.squareDistance(Main.player.pos) > 32
|
|
) {
|
|
layer.spawnEntity(zombie);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public TileState getTileDestroyed() {
|
|
return Tiles.STONE.getDefaultState();
|
|
}
|
|
|
|
@Override
|
|
public ColorRange getLightLevel() {
|
|
return new ColorRange(new Vec3d(0, 0, 0), new Vec3d(0, 0, 0));
|
|
}
|
|
|
|
@Override
|
|
public double getTemperatureStatic(Layer layer, Vec2d pos) {
|
|
return 0.5;
|
|
}
|
|
|
|
@Override
|
|
public double getTemperatureDynamic(Layer layer, Vec2d pos) {
|
|
return 0.5;
|
|
}
|
|
|
|
}
|