package shootergame.world.chunk; import java.util.ArrayList; import java.util.Random; import shootergame.display.Camera; import shootergame.entity.Entity; import shootergame.entity.EntityAlive; import shootergame.init.Tiles; import shootergame.tiles.Tile; import shootergame.util.math.MathHelpers; import shootergame.util.math.range.Range2i; import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2i; import shootergame.world.layer.Layer; public class Chunk { public static final Range2i CHUNK_SIZE = new Range2i(16, 16); public static final Chunk CHUNK_EMPTY = new ChunkEmpty(); public static final int CHUNK_INDEX = CHUNK_SIZE.mx * CHUNK_SIZE.my; public static final int SIMULATION_DISTANCE = 5; private Tile tiles_back[] = new Tile[CHUNK_INDEX]; private Tile tiles_front[] = new Tile[CHUNK_INDEX]; private ArrayList entities = new ArrayList(); private Random rand; private Layer layer; private Vec2i c_pos; public Chunk(Layer layer, Vec2i c_pos, Random rand) { // Set some specified values this.rand = rand; this.layer = layer; this.c_pos = c_pos; // Loop over all the tiles in the chunk for(int i=0;i cx + CHUNK_SIZE.mx || px < cx || py > cy + CHUNK_SIZE.my || py < cy) { // Process the entity by layer and remove the entity from the array layer.spawnEntity(e); entities.remove(i); i -= 1; } } } public void setBackTile(Tile tile, Vec2i pos) { // Get the id Vec2i cpos = new Vec2i(0, 0); cpos.x = MathHelpers.mod(pos.x, CHUNK_SIZE.mx); cpos.y = MathHelpers.mod(pos.y, CHUNK_SIZE.my); int id = cpos.getId(CHUNK_SIZE); // Set the back tile this.tiles_back[id] = tile; } public void setFrontTile(Tile tile, Vec2i pos) { // Get the id Vec2i cpos = new Vec2i(0, 0); cpos.x = MathHelpers.mod(pos.x, CHUNK_SIZE.mx); cpos.y = MathHelpers.mod(pos.y, CHUNK_SIZE.my); int id = cpos.getId(CHUNK_SIZE); // Set the front tile this.tiles_front[id] = tile; } public Tile getBackTile(Vec2i pos) { // Get the id Vec2i cpos = new Vec2i(0, 0); cpos.x = MathHelpers.mod(pos.x, CHUNK_SIZE.mx); cpos.y = MathHelpers.mod(pos.y, CHUNK_SIZE.my); int id = cpos.getId(CHUNK_SIZE); // Send back the back tile return this.tiles_back[id]; } public Tile getFrontTile(Vec2i pos) { // Get the id Vec2i cpos = new Vec2i(0, 0); cpos.x = MathHelpers.mod(pos.x, CHUNK_SIZE.mx); cpos.y = MathHelpers.mod(pos.y, CHUNK_SIZE.my); int id = cpos.getId(CHUNK_SIZE); // Send back the front tile return this.tiles_front[id]; } public void killEntity(Entity e) { entities.remove(e); } public ArrayList getNearbyEntities(Vec2d pos, double distance) { // Get the list of entities to send back ArrayList nearby_entities = new ArrayList(); // Loop over the entities for(Entity e : entities) { if( e.pos.x + distance > pos.x && e.pos.x - distance < pos.x && e.pos.y + distance > pos.y && e.pos.y - distance < pos.y ) { nearby_entities.add(e); } } // Send back the entities return nearby_entities; } }