Skip to content

Commit

Permalink
fixed world being wierd, and chunk boundry showing; added description…
Browse files Browse the repository at this point in the history
… to main menu; also fixed chunk loading stuff
  • Loading branch information
chrisj42 committed Feb 11, 2018
1 parent 845c79c commit 0d9b956
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 33 deletions.
24 changes: 24 additions & 0 deletions core/src/miniventure/game/GameScreen.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package miniventure.game;

import miniventure.game.util.MyUtils;
import miniventure.game.world.Chunk;
import miniventure.game.world.Level;
import miniventure.game.world.entity.mob.Player;
import miniventure.game.world.tile.Tile;
Expand All @@ -15,6 +16,7 @@
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
Expand All @@ -39,6 +41,8 @@ public class GameScreen {
private int zoom = 0;
private FrameBuffer lightingBuffer;

private boolean debug = false;

public GameScreen() {
camera = new OrthographicCamera();
uiCamera = new OrthographicCamera();
Expand All @@ -60,6 +64,9 @@ public void handleInput(@NotNull Player player) {

if(Gdx.input.isKeyJustPressed(Keys.R) && Gdx.input.isKeyPressed(Keys.SHIFT_LEFT))
GameCore.getWorld().createWorld(0, 0);

if(Gdx.input.isKeyJustPressed(Keys.B))
debug = !debug;
}

public void render(@NotNull Player mainPlayer, Color[] lightOverlays, @NotNull Level level) {
Expand Down Expand Up @@ -121,6 +128,23 @@ public void render(@NotNull Player mainPlayer, Color[] lightOverlays, @NotNull L
batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); // default
level.render(renderSpace, batch, Gdx.graphics.getDeltaTime(), offset); // renderSpace in world coords, but offset can give render coords

if(debug) {
// render chunk boundaries
int minX = MathUtils.ceil(renderSpace.x) / Chunk.SIZE * Chunk.SIZE;
int minY = MathUtils.ceil(renderSpace.y) / Chunk.SIZE * Chunk.SIZE;
int maxX = MathUtils.ceil((renderSpace.x + renderSpace.width) / Chunk.SIZE) * Chunk.SIZE;
int maxY = MathUtils.ceil((renderSpace.y + renderSpace.height) / Chunk.SIZE) * Chunk.SIZE;

int lineThickness = (int) Math.pow(2, -zoom);

for (int x = minX; x <= maxX; x += Chunk.SIZE) {
MyUtils.fillRect((x - offset.x) * Tile.SIZE-lineThickness, (minY - offset.y) * Tile.SIZE, lineThickness*2+1, (maxY - minY) * Tile.SIZE, Color.PINK, batch);
}
for (int y = minY; y <= maxY; y += Chunk.SIZE) {
MyUtils.fillRect((minX - offset.x) * Tile.SIZE, (y - offset.y) * Tile.SIZE-lineThickness, (maxX - minX) * Tile.SIZE, lineThickness*2+1, Color.PINK, batch);
}
}

Tile interactTile = level.getClosestTile(mainPlayer.getInteractionRect());
if(interactTile != null) {
Vector2 pos = interactTile.getPosition().sub(offset).scl(Tile.SIZE); // world to render coords
Expand Down
4 changes: 2 additions & 2 deletions core/src/miniventure/game/LevelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ public void respawn() {
Rectangle spawnBounds = new Rectangle(0, 0, getSpawnDim(level.getWidth()), getSpawnDim(level.getHeight()));
spawnBounds.setCenter(level.getWidth()/2, level.getHeight()/2);

//level.spawnMob(mainPlayer, spawnBounds);
level.addEntity(mainPlayer, 12.5f, 12.5f, true);
level.spawnMob(mainPlayer, spawnBounds);
//level.addEntity(mainPlayer, 50, 50, true);
}

private int getSpawnDim(int levelDim) {
Expand Down
4 changes: 3 additions & 1 deletion core/src/miniventure/game/screen/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public MainMenu() {

addLabel("Use mouse or arrow keys to move around.", 10);
addLabel("C to attack, V to interact.", 10);
addLabel("E to open your inventory, Z to craft items.", 30);
addLabel("E to open your inventory, Z to craft items.", 10);
addLabel("+ and - keys to zoom in and out.", 30);
//addLabel("(press b to show/hide chunk boundaries)", 30);
//addLabel("", 10);

VisTextButton button = new VisTextButton("Play");
Expand Down
7 changes: 7 additions & 0 deletions core/src/miniventure/game/world/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import miniventure.game.world.tile.Tile;
import miniventure.game.world.tile.TileType;

import com.badlogic.gdx.math.MathUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -34,4 +36,9 @@ public Tile getTile(int x, int y) {
}

@NotNull Tile[][] getTiles() { return tiles; }

public static int getCoord(float pos) {
int worldCoord = MathUtils.floor(pos);
return worldCoord / SIZE;
}
}
56 changes: 37 additions & 19 deletions core/src/miniventure/game/world/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import miniventure.game.world.tile.Tile;
import miniventure.game.world.tile.TileType;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
Expand Down Expand Up @@ -61,7 +60,13 @@ public Level(int depth, LevelGenerator levelGenerator) {
public int getEntityCount() { return entities.size(); }

public void entityMoved(Entity entity) {
if(!GameCore.getWorld().isKeepAlive(entity)) return;
if(!GameCore.getWorld().isKeepAlive(entity)) {
// check if they've gone (mostly?) out of bounds, if so, remove them
Vector2 pos = entity.getCenter();
if(!loadedChunks.containsKey(new Point(Chunk.getCoord(pos.x), Chunk.getCoord(pos.y))))
entity.remove();
return;
}

if(entity.getLevel() == this) {
// load all surrounding chunks
Expand Down Expand Up @@ -207,17 +212,6 @@ public void render(Rectangle renderSpace, SpriteBatch batch, float delta, Vector

for(WorldObject obj: objects)
obj.render(batch, delta, posOffset);

// render chunk boundaries
int minX = MathUtils.ceil(renderSpace.x) / Chunk.SIZE * Chunk.SIZE;
int minY = MathUtils.ceil(renderSpace.y) / Chunk.SIZE * Chunk.SIZE;
int maxX = MathUtils.ceil((renderSpace.x + renderSpace.width) / Chunk.SIZE) * Chunk.SIZE;
int maxY = MathUtils.ceil((renderSpace.y + renderSpace.height) / Chunk.SIZE) * Chunk.SIZE;

for(int x = minX; x <= maxX; x+=Chunk.SIZE)
MyUtils.fillRect((x-posOffset.x)*Tile.SIZE - 2, (minY-posOffset.y)*Tile.SIZE, 5, (maxY-minY)*Tile.SIZE, Color.PINK, batch);
for(int y = minY; y <= maxY; y+=Chunk.SIZE)
MyUtils.fillRect((minX-posOffset.x)*Tile.SIZE, (y-posOffset.y)*Tile.SIZE - 2, (maxX-minX)*Tile.SIZE, 5, Color.PINK, batch);
}

public Array<Vector3> renderLighting(Rectangle renderSpace) {
Expand Down Expand Up @@ -308,24 +302,48 @@ public Tile getTile(float x, float y) {
return null;
}

public Array<Tile> getOverlappingTiles(Rectangle rect) {
Array<Tile> overlappingTiles = new Array<>();
private Array<Point> getOverlappingTileCoords(Rectangle rect) {
Array<Point> overlappingTiles = new Array<>();
int minX = Math.max(0, (int) rect.x);
int minY = Math.max(0, (int) rect.y);
int maxX = Math.min(getWidth(), (int) (rect.x + rect.width));
int maxY = Math.min(getHeight(), (int) (rect.y + rect.height));

for(int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
Tile tile = getTile(x, y);
if(tile != null)
overlappingTiles.add(tile);
overlappingTiles.add(new Point(x, y));
}
}

return overlappingTiles;
}

public Array<Tile> getOverlappingTiles(Rectangle rect) {
Array<Tile> overlappingTiles = new Array<>();
Array<Point> points = getOverlappingTileCoords(rect);

for(Point p: points) {
Tile tile = getTile(p.x, p.y);
if (tile != null)
overlappingTiles.add(tile);
}

return overlappingTiles;
}

private Array<Point> getOverlappingChunks(Rectangle rect) {
Array<Point> overlappingChunks = new Array<>();
Array<Point> points = getOverlappingTileCoords(rect);

for(Point p: points) {
Point chunk = new Point(Chunk.getCoord(p.x), Chunk.getCoord(p.y));
if(!overlappingChunks.contains(chunk, false))
overlappingChunks.add(chunk);
}

return overlappingChunks;
}

public Array<Entity> getOverlappingEntities(Rectangle rect) {
return getOverlappingEntities(rect, (Entity)null);
}
Expand Down Expand Up @@ -360,7 +378,7 @@ private Array<Point> getAreaChunks(Vector2 tilePos, int radius, boolean loaded,
return getAreaChunks(tilePos.x, tilePos.y, radius, loaded, unloaded);
}
private Array<Point> getAreaChunks(float x, float y, int radius, boolean loaded, boolean unloaded) {
return getAreaChunks(((int)x) / Chunk.SIZE, ((int)y) / Chunk.SIZE, radius, loaded, unloaded);
return getAreaChunks(Chunk.getCoord(x), Chunk.getCoord(y), radius, loaded, unloaded);
}
private Array<Point> getAreaChunks(int chunkX, int chunkY, int radius, boolean loaded, boolean unloaded) {
Array<Point> chunkCoords = new Array<>();
Expand Down
11 changes: 4 additions & 7 deletions core/src/miniventure/game/world/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.HashMap;

import miniventure.game.item.Item;
import miniventure.game.world.Chunk;
import miniventure.game.world.Level;
import miniventure.game.world.WorldObject;
import miniventure.game.world.entity.mob.Mob;
Expand Down Expand Up @@ -117,8 +116,6 @@ private float moveAxis(@NotNull Level level, boolean xaxis, float amt, float oth
- calling any interaction methods along the way
*/

boolean debug = this instanceof Player;

Array<Tile> futureTiles = level.getOverlappingTiles(newRect);
Array<Tile> currentTiles = level.getOverlappingTiles(oldRect);
Array<Tile> newTiles = new Array<>(futureTiles);
Expand Down Expand Up @@ -185,14 +182,14 @@ public void moveTo(@NotNull Level level, float x, float y) {
y = Math.min(y, level.getHeight() - size.y);

// check and see if the entity is changing chunks from their current position.
boolean changedChunk = Level.getEntityLevel(this) == level && (
((int)x) / Chunk.SIZE != ((int)this.x) / Chunk.SIZE ||
((int)y) / Chunk.SIZE != ((int)this.y) / Chunk.SIZE);
// boolean changedChunk = Level.getEntityLevel(this) == level && (
// Chunk.getCoord(x) != Chunk.getCoord(this.x) ||
// Chunk.getCoord(y) != Chunk.getCoord(this.y) );

this.x = x;
this.y = y;

if(changedChunk)
// if(changedChunk)
level.entityMoved(this);
}
public void moveTo(@NotNull Tile tile) {
Expand Down
9 changes: 5 additions & 4 deletions core/src/miniventure/game/world/levelgen/LevelGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class LevelGenerator {
BiomeCategory.ROCKY+"_3"
);

private static final int MAX_WORLD_SIZE = Integer.MAX_VALUE/2;
// TO-DO I don't really know why, but things get messed up when you go really far out, and have big numbers for your coordinates, even though it's nowhere near the number limit. I want to fix this, but I have no idea how, and I have a feeling that it is going to be more complicated than I think, and I really don't want to deal with it. So, for now, I'm just going to use a considerably smaller value, in the range of 10,000s. It's plenty big, honestly, so it'll just have to do for now until whenever I decide to try and figure out the issue.
private static final int MAX_WORLD_SIZE = (int) (Math.sqrt(Math.min(Integer.MAX_VALUE, Float.MAX_VALUE)));

private final Coherent2DNoiseFunction categoryNoise, biomeNoise, detailNoise;
public final int worldWidth, worldHeight;
Expand Down Expand Up @@ -52,10 +53,10 @@ public boolean chunkExists(int x, int y) {

public TileType[][] generateChunk(final int x, final int y) {
int width = Chunk.SIZE, height = Chunk.SIZE;
if((x+1) * width > worldWidth)
if(x * width + width >= worldWidth)
width = worldWidth - x*width;
if((y+1) * height > worldHeight)
height = worldHeight - x*height;
if(y * height + height >= worldHeight)
height = worldHeight - y*height;

if(width <= 0 || height <= 0)
throw new IndexOutOfBoundsException("Requested chunk is outside world bounds; chunkX="+x+", chunkY="+y+". Max x requested: "+((x+1)*Chunk.SIZE-1)+", max y requested: "+((y+1)*Chunk.SIZE-1)+". Actual world size: ("+worldWidth+","+worldHeight+")");
Expand Down

0 comments on commit 0d9b956

Please sign in to comment.