Skip to content

Commit

Permalink
added main menu background that scrolls across a randomly generated l…
Browse files Browse the repository at this point in the history
…evel at a random time of day (also regressed version back to 1.4.1)
  • Loading branch information
chrisj42 committed Apr 16, 2018
1 parent f42771f commit 0e9cdcd
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 50 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ allprojects {
apply plugin: "java"
apply plugin: "idea"

version = '1.4.2'
version = '1.4.1'
ext {
appName = "miniventure"
gdxVersion = '1.9.8'
Expand Down
57 changes: 57 additions & 0 deletions core/client/src/miniventure/game/client/DisplayLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package miniventure.game.client;

import java.util.HashMap;

import miniventure.game.world.Chunk;
import miniventure.game.world.ClientLevel;
import miniventure.game.world.Level;
import miniventure.game.world.Point;
import miniventure.game.world.entity.Entity;
import miniventure.game.world.levelgen.LevelGenerator;
import miniventure.game.world.tile.Tile;
import miniventure.game.world.tile.TileType;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;

public class DisplayLevel extends Level {

private final LevelGenerator generator;
//private final HashMap<Point, Chunk> chunks = new HashMap<>();

public DisplayLevel(LevelGenerator generator) {
super(new DisplayWorld(), 0, generator.worldWidth, generator.worldHeight);

this.generator = generator;
int y = 0;
while(y * Chunk.SIZE < generator.worldHeight) {
int x = 0;
while(x * Chunk.SIZE < generator.worldWidth) {
loadChunk(new Point(x, y));
x++;
}

y++;
}
}

@Override
public void render(Rectangle renderSpace, SpriteBatch batch, float delta, Vector2 posOffset) {
ClientLevel.render(getOverlappingTiles(renderSpace), new Array<>(Entity.class), batch, delta, posOffset);
}

@Override
protected Tile createTile(int x, int y, TileType[] types, String[] data) {
return new DisplayTile(this, x, y, types, data);
}

@Override
protected void loadChunk(Point chunkCoord) {
loadChunk(new Chunk(chunkCoord.x, chunkCoord.y, this, generator.generateChunk(chunkCoord.x, chunkCoord.y)));
}

@Override
protected void unloadChunk(Point chunkCoord) {}
}
24 changes: 24 additions & 0 deletions core/client/src/miniventure/game/client/DisplayTile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package miniventure.game.client;

import miniventure.game.world.Level;
import miniventure.game.world.tile.ClientTile;
import miniventure.game.world.tile.Tile;
import miniventure.game.world.tile.TileType;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

import org.jetbrains.annotations.NotNull;

public class DisplayTile extends Tile {

protected DisplayTile(@NotNull DisplayLevel level, int x, int y, @NotNull TileType[] types, @NotNull String[] data) {
super(level, x, y, types, data);
}

@Override
public void render(SpriteBatch batch, float delta, Vector2 posOffset) {
ClientTile.render(this, batch, delta, posOffset);
}

}
35 changes: 35 additions & 0 deletions core/client/src/miniventure/game/client/DisplayWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package miniventure.game.client;

import miniventure.game.world.Level;
import miniventure.game.world.WorldManager;
import miniventure.game.world.WorldObject;
import miniventure.game.world.tile.TilePropertyFetcher;

import com.badlogic.gdx.utils.Array;

public class DisplayWorld extends WorldManager {

public DisplayWorld() { super(new TilePropertyFetcher(prop -> prop)); }

@Override
protected boolean doDaylightCycle() {
return false;
}

@Override
public boolean worldLoaded() {
return true;
}

@Override
public void createWorld(int width, int height) {}

@Override
public void exitWorld(boolean save) {}

@Override
public boolean isKeepAlive(WorldObject obj) { return true; }

@Override
public Array<WorldObject> getKeepAlives(Level level) { return new Array<>(); }
}
9 changes: 7 additions & 2 deletions core/client/src/miniventure/game/client/LevelViewport.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import miniventure.game.util.MyUtils;
import miniventure.game.world.Chunk;
import miniventure.game.world.ClientLevel;
import miniventure.game.world.Level;
import miniventure.game.world.tile.Tile;

import com.badlogic.gdx.Gdx;
Expand Down Expand Up @@ -67,7 +68,7 @@ public void handleInput() {
debug = !debug;
}

public void render(@NotNull Vector2 cameraCenter, Color[] lightOverlays, @NotNull ClientLevel level) {
public void render(@NotNull Vector2 cameraCenter, Color[] lightOverlays, @NotNull Level level) {
// get the size of the area of the game on screen by projecting the application window dimensions into world space.
Vector3 screenSize = new Vector3(Gdx.graphics.getWidth(), 0, 0); // because unproject has origin at the top, so the upper right corner is at (width, 0).
camera.unproject(screenSize); // screen to render coords
Expand Down Expand Up @@ -101,7 +102,7 @@ public void render(@NotNull Vector2 cameraCenter, Color[] lightOverlays, @NotNul

batch.setBlendFunction(GL20.GL_ZERO, GL20.GL_ONE_MINUS_SRC_ALPHA);

Array<Vector3> lights = level.renderLighting(lightRenderSpace);
Array<Vector3> lights = ClientLevel.renderLighting(level.getOverlappingObjects(lightRenderSpace));
final TextureRegion lightTexture = GameCore.icons.get("light").texture;

for(Vector3 light: lights) {
Expand All @@ -121,6 +122,7 @@ public void render(@NotNull Vector2 cameraCenter, Color[] lightOverlays, @NotNul
batch.setProjectionMatrix(camera.combined); // tells the batch to use the camera's coordinate system.
batch.begin();
batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); // default
//System.out.println("rendering level in bounds "+renderSpace+" to camera at "+camera.position+" with offset "+offset);
level.render(renderSpace, batch, Gdx.graphics.getDeltaTime(), offset); // renderSpace in world coords, but offset can give render coords

if(debug) {
Expand Down Expand Up @@ -180,4 +182,7 @@ void resize(int width, int height) {
lightingBuffer.dispose();
lightingBuffer = new FrameBuffer(Format.RGBA8888, width, height, false);
}

public float getViewWidth() { return camera.viewportWidth/Tile.SIZE; }
public float getViewHeight() { return camera.viewportHeight/Tile.SIZE; }
}
52 changes: 48 additions & 4 deletions core/client/src/miniventure/game/screen/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
import miniventure.game.GameCore;
import miniventure.game.client.ClientCore;
import miniventure.game.client.ClientWorld;
import miniventure.game.client.DisplayLevel;
import miniventure.game.client.LevelViewport;
import miniventure.game.world.ClientLevel;
import miniventure.game.world.TimeOfDay;
import miniventure.game.world.levelgen.LevelGenerator;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Align;
Expand All @@ -17,20 +25,27 @@ public class MainMenu extends MenuScreen {

private boolean dialog = false;

private final DisplayLevel backgroundLevel;
private final LevelViewport levelView;
private final Color[] lightOverlays;
private final Vector2 cameraPos, cameraDir;

private static final float PAN_SPEED = 3f; // in tiles/second.

public MainMenu() {
super();

ClientWorld world = ClientCore.getWorld();

addLabel("Miniventure", 20);
addLabel("Welcome to Miniventure!", 20);
addLabel("You are playing version " + GameCore.VERSION, 15);

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.", 10);
addLabel("+ and - keys to zoom in and out.", 10);
addLabel("Press \"t\" to chat with other players, and \"/\" to use commands.", 0);
addLabel("(Hint: use up arrow key in chat screen to access previous entries and avoid retyping things.)", 30);
addLabel("(Hint: use the up key to repeat messages, and tab to autocomplete command names.)", 30);
//addLabel("(press b to show/hide chunk boundaries)", 30);
//addLabel("", 10);

Expand Down Expand Up @@ -75,15 +90,44 @@ public void clicked(InputEvent e, float x, float y) {

table.setOrigin(Align.top);
table.setPosition(getWidth()/2, getHeight()/2);

levelView = new LevelViewport();
TimeOfDay time = TimeOfDay.values[MathUtils.random(TimeOfDay.values.length-1)];
lightOverlays = TimeOfDay.getSkyColors(time.getStartOffsetSeconds());

LevelGenerator generator = new LevelGenerator(MathUtils.random.nextLong(), 100, 60, 8, 6);
backgroundLevel = new DisplayLevel(generator);

Vector2 halfSize = new Vector2(levelView.getViewWidth(), levelView.getViewHeight()).scl(0.5f);
cameraPos = new Vector2(MathUtils.random(halfSize.x, backgroundLevel.getWidth()-halfSize.x), MathUtils.random(halfSize.y, backgroundLevel.getHeight()-halfSize.y));

cameraDir = new Vector2().setLength(PAN_SPEED).setToRandomDirection().setLength(PAN_SPEED);
}

@Override
public boolean usesWholeScreen() { return false; }

@Override
public void draw() {
Gdx.gl.glClearColor(0, 0, 0, 1);
levelView.render(cameraPos, lightOverlays, backgroundLevel);

cameraPos.add(cameraDir.cpy().scl(Gdx.graphics.getDeltaTime()));
cameraDir.x = velDir(cameraPos.x, cameraDir.x, levelView.getViewWidth()/2, backgroundLevel.getWidth() - levelView.getViewWidth()/2);
cameraDir.y = velDir(cameraPos.y, cameraDir.y, levelView.getViewHeight()/2, backgroundLevel.getHeight() - levelView.getViewHeight()/2);

super.draw();
}

private float velDir(float pos, float vel, float min, float max) {
if((pos >= max && vel >= 0) || (pos <= min && vel <= 0)) {
vel += MathUtils.random(-PAN_SPEED/4, PAN_SPEED/4);
vel = -vel;
}

return vel;
}

private void addLabel(String msg, int spacing) {
private void addLabel(String msg, int spacing) {
table.add(new VisLabel(msg));
table.row().space(spacing);
}
Expand Down
41 changes: 36 additions & 5 deletions core/client/src/miniventure/game/world/ClientLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import miniventure.game.world.entity.Entity;
import miniventure.game.world.entity.particle.Particle;
import miniventure.game.world.tile.ClientTile;
import miniventure.game.world.tile.Tile;
import miniventure.game.world.tile.Tile.TileData;
import miniventure.game.world.tile.TileType;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -48,10 +50,19 @@ public void render(Rectangle renderSpace, SpriteBatch batch, float delta, Vector
renderSpace = new Rectangle(Math.max(0, renderSpace.x), Math.max(0, renderSpace.y), Math.min(getWidth()-renderSpace.x, renderSpace.width), Math.min(getHeight()-renderSpace.y, renderSpace.height));
// pass the offset vector to all objects being rendered.

Array<Tile> tiles = getOverlappingTiles(renderSpace);
Array<Entity> entities = getOverlappingEntities(renderSpace);

render(tiles, entities, batch, delta, posOffset);
}

public static void render(Array<Tile> tiles, Array<Entity> entities, SpriteBatch batch, float delta, Vector2 posOffset) {
// pass the offset vector to all objects being rendered.

Array<WorldObject> objects = new Array<>();
objects.addAll(getOverlappingTiles(renderSpace)); // tiles first
objects.addAll(tiles); // tiles first

Array<Entity> entities = getOverlappingEntities(renderSpace); // entities second
// entities second
entities.sort((e1, e2) -> {
if(e1 instanceof Particle && !(e2 instanceof Particle))
return 1;
Expand All @@ -65,6 +76,26 @@ public void render(Rectangle renderSpace, SpriteBatch batch, float delta, Vector
obj.render(batch, delta, posOffset);
}

public Array<Vector3> renderLighting(Rectangle renderSpace) {
Array<WorldObject> objects = new Array<>();
objects.addAll(getOverlappingTiles(renderSpace));
objects.addAll(getOverlappingEntities(renderSpace));

return renderLighting(objects);
}

public static Array<Vector3> renderLighting(Array<WorldObject> objects) {
Array<Vector3> lighting = new Array<>();

for(WorldObject obj: objects) {
float lightR = obj.getLightRadius();
if(lightR > 0)
lighting.add(new Vector3(obj.getCenter(), lightR));
}

return lighting;
}

@SuppressWarnings("unchecked")
private void applyTileUpdates() {
Entry<ClientTile, TileData>[] tilesToUpdate;
Expand All @@ -84,7 +115,7 @@ public void serverUpdate(ClientTile tile, TileData data) {
}

@Override
ClientTile createTile(int x, int y, TileType[] types, String[] data) { return new ClientTile(this, x, y, types, data); }
protected ClientTile createTile(int x, int y, TileType[] types, String[] data) { return new ClientTile(this, x, y, types, data); }

@Override public ClientTile getTile(float x, float y) {
ClientTile tile = (ClientTile) super.getTile(x, y);
Expand All @@ -95,13 +126,13 @@ public void serverUpdate(ClientTile tile, TileData data) {
}

@Override
void loadChunk(Point chunkCoord) {
protected void loadChunk(Point chunkCoord) {
//System.out.println("Client requesting chunk "+chunkCoord);
ClientCore.getClient().send(new ChunkRequest(chunkCoord));
}

@Override
void unloadChunk(Point chunkCoord) {
protected void unloadChunk(Point chunkCoord) {
Chunk chunk = getLoadedChunk(chunkCoord);
if(chunk == null) return; // already unloaded

Expand Down

0 comments on commit 0e9cdcd

Please sign in to comment.