Skip to content

Commit

Permalink
fixed door sprite, and with it, the whole basic tile transition syste…
Browse files Browse the repository at this point in the history
…m; server didn't account for the duration of tile exit animations, which caused issues. Now fixed.
  • Loading branch information
chrisj42 committed Apr 4, 2018
1 parent 68f88fd commit bc3fa0c
Show file tree
Hide file tree
Showing 24 changed files with 185 additions and 68 deletions.
8 changes: 4 additions & 4 deletions core/client/src/miniventure/game/client/GameClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import miniventure.game.world.entity.EntityRenderer;
import miniventure.game.world.entity.EntityRenderer.DirectionalAnimationRenderer;
import miniventure.game.world.entity.mob.ClientPlayer;
import miniventure.game.world.tile.Tile;
import miniventure.game.world.tile.ClientTile;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
Expand Down Expand Up @@ -66,11 +66,11 @@ public void received (Connection connection, Object object) {
if(object instanceof TileUpdate) {
// individual tile update
TileUpdate update = (TileUpdate) object;
Level level = world.getLevel(update.levelDepth);
ClientLevel level = world.getLevel(update.levelDepth);
if(level == null) return;
Tile tile = level.getTile(update.x, update.y);
ClientTile tile = level.getTile(update.x, update.y);
if(tile != null)
update.tileData.apply(tile);
level.serverUpdate(tile, update.tileData);
}

if(object instanceof Hurt) {
Expand Down
12 changes: 6 additions & 6 deletions core/client/src/miniventure/game/client/GameScreen.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package miniventure.game.client;

import miniventure.game.GameCore;
import miniventure.game.GameProtocol.DatalessRequest;
import miniventure.game.screen.ChatScreen;
import miniventure.game.util.MyUtils;
import miniventure.game.world.Chunk;
Expand Down Expand Up @@ -69,17 +70,16 @@ public void handleInput(@NotNull ClientPlayer player) {
if(Gdx.input.isKeyJustPressed(Keys.EQUALS) || Gdx.input.isKeyJustPressed(Keys.PLUS))
zoom(1);

//if(Gdx.input.isKeyJustPressed(Keys.T))
// ClientCore.getClient().send(DatalessRequest.Tile);
if(Gdx.input.isKeyJustPressed(Keys.B))
debug = !debug;

//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;

if(Gdx.input.isKeyPressed(Keys.SHIFT_LEFT) && Gdx.input.isKeyJustPressed(Keys.T))
ClientCore.getClient().send(DatalessRequest.Tile); // debug

if(Gdx.input.isKeyJustPressed(Keys.T)) {
else if(Gdx.input.isKeyJustPressed(Keys.T)) {
ClientCore.setScreen(chat);
chat.sendMessage();
}
Expand Down
30 changes: 30 additions & 0 deletions core/client/src/miniventure/game/world/ClientLevel.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package miniventure.game.world;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import miniventure.game.GameProtocol.ChunkRequest;
import miniventure.game.client.ClientCore;
import miniventure.game.client.ClientWorld;
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.TileData;
import miniventure.game.world.tile.TileType;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
Expand All @@ -19,6 +25,8 @@ public class ClientLevel extends Level {

@NotNull private ClientWorld world;

private final Map<ClientTile, TileData> tileUpdates = Collections.synchronizedMap(new HashMap<>());

public ClientLevel(@NotNull ClientWorld world, int depth, int width, int height) {
super(world, depth, width, height);
this.world = world;
Expand All @@ -35,6 +43,8 @@ void pruneLoadedChunks() {
}

public void render(Rectangle renderSpace, SpriteBatch batch, float delta, Vector2 posOffset) {
applyTileUpdates();

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.

Expand All @@ -55,9 +65,29 @@ public void render(Rectangle renderSpace, SpriteBatch batch, float delta, Vector
obj.render(batch, delta, posOffset);
}

@SuppressWarnings("unchecked")
private void applyTileUpdates() {
Entry<ClientTile, TileData>[] tilesToUpdate;
synchronized (tileUpdates) {
tilesToUpdate = tileUpdates.entrySet().toArray((Entry<ClientTile, TileData>[]) new Entry[tileUpdates.size()]);
tileUpdates.clear();
}
for(Entry<ClientTile, TileData> entry: tilesToUpdate) {
entry.getValue().apply(entry.getKey());
}
}

public void serverUpdate(ClientTile tile, TileData data) {
synchronized (tileUpdates) {
tileUpdates.put(tile, data);
}
}

@Override
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) { return (ClientTile) super.getTile(x, y); }

@Override
void loadChunk(Point chunkCoord) {
//System.out.println("Client requesting chunk "+chunkCoord);
Expand Down
11 changes: 7 additions & 4 deletions core/client/src/miniventure/game/world/tile/ClientTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import java.util.Iterator;
import java.util.TreeMap;

import miniventure.game.texture.TextureHolder;
import miniventure.game.world.ClientLevel;

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

Expand Down Expand Up @@ -41,7 +41,7 @@ public void render(SpriteBatch batch, float delta, Vector2 posOffset) {
}
}

Array<AtlasRegion> sprites = new Array<>();
Array<TextureHolder> sprites = new Array<>();

TileType[] mainTypes = getTypes();
int firstIdx = 0;
Expand Down Expand Up @@ -82,8 +82,11 @@ public void render(SpriteBatch batch, float delta, Vector2 posOffset) {
}


for(AtlasRegion texture: sprites)
batch.draw(texture, (x-posOffset.x) * SIZE, (y-posOffset.y) * SIZE);
for(TextureHolder texture: sprites)
batch.draw(texture.texture, (x-posOffset.x) * SIZE, (y-posOffset.y) * SIZE);
}

@Override
public String toString() { return getType().getName()+" ClientTile"; }

}
8 changes: 5 additions & 3 deletions core/game/src/miniventure/game/GameCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class GameCore {

private static final long START_TIME = System.nanoTime();

public static TextureAtlasHolder entityAtlas;
public static TextureAtlas tileAtlas = new TextureAtlas(), tileConnectionAtlas = new TextureAtlas(); // tile overlap atlas not needed b/c the overlap sprite layout is simple enough to code; it goes in binary. However, the tile connection sprite layout is more complicated, so a map is needed to compare against.
public static TextureAtlasHolder entityAtlas, tileAtlas;
public static TextureAtlas tileConnectionAtlas = new TextureAtlas(); // tile overlap atlas not needed b/c the overlap sprite layout is simple enough to code; it goes in binary. However, the tile connection sprite layout is more complicated, so a map is needed to compare against.

private static TextureAtlas iconAtlas;
public static final HashMap<String, TextureHolder> icons = new HashMap<>();
Expand All @@ -48,7 +48,7 @@ public static void initGdx() {
if(initialized) return;
initialized = true;
entityAtlas = new TextureAtlasHolder(new TextureAtlas("sprites/entities.txt"));
tileAtlas = new TextureAtlas("sprites/tiles.txt");
tileAtlas = new TextureAtlasHolder(new TextureAtlas("sprites/tiles.txt"));
tileConnectionAtlas = new TextureAtlas("sprites/tileconnectmap.txt");
iconAtlas = new TextureAtlas("sprites/icons.txt");

Expand All @@ -73,8 +73,10 @@ public static void initNonGdx() {
FileHandle spriteFolder = Gdx.files.internal("sprites");
TextureAtlasData entityData = new TextureAtlasData(spriteFolder.child("entities.txt"), spriteFolder, false);
TextureAtlasData iconData = new TextureAtlasData(spriteFolder.child("icons.txt"), spriteFolder, false);
TextureAtlasData tileData = new TextureAtlasData(spriteFolder.child("tiles.txt"), spriteFolder, false);

entityAtlas = new TextureAtlasHolder(entityData);
tileAtlas = new TextureAtlasHolder(tileData);
for(Region region: iconData.getRegions())
icons.put(region.name, new TextureHolder(region));
}
Expand Down
2 changes: 1 addition & 1 deletion core/game/src/miniventure/game/GameProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ default <T> void forPacket(Object packet, Class<T> type, RequestResponse<T> resp
}

enum DatalessRequest {
Respawn
Respawn, Tile
}

class Login {
Expand Down
4 changes: 2 additions & 2 deletions core/game/src/miniventure/game/item/TileItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import miniventure.game.world.tile.Tile;
import miniventure.game.world.tile.TileType;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -56,7 +56,7 @@ private TileItem(@NotNull TileType type, @Nullable TileType... canPlaceOn) {
this(MyUtils.toTitleCase(type.name()), GameCore.tileAtlas.findRegion(type.name().toLowerCase()+"/00"), type, canPlaceOn); // so, if the placeOn is null, then...
}

private TileItem(String name, TextureRegion texture, @NotNull TileType result, @Nullable TileType... placeOn) { this(name, new TextureHolder(texture, Tile.SIZE, Tile.SIZE), result, placeOn); }
private TileItem(String name, AtlasRegion texture, @NotNull TileType result, @Nullable TileType... placeOn) { this(name, new TextureHolder(texture, Tile.SIZE, Tile.SIZE), result, placeOn); }
private TileItem(String name, TextureHolder texture, @NotNull TileType result, @Nullable TileType... placeOn) {
super(ItemType.Tile, name, texture);
this.canPlaceOn = placeOn;
Expand Down
15 changes: 15 additions & 0 deletions core/game/src/miniventure/game/texture/TextureAtlasHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public Array<TextureHolder> findRegions(String name) {
return textures;
}

public Array<TextureHolder> getRegions() {
Array<TextureHolder> textures = new Array<>();
if(atlas != null) {
for(AtlasRegion r: atlas.getRegions()) {
textures.add(new TextureHolder(r));
}
}
else {
for(Region r: regions)
textures.add(new TextureHolder(r));
}

return textures;
}

public void dispose() {
if(atlas != null)
atlas.dispose();
Expand Down
13 changes: 8 additions & 5 deletions core/game/src/miniventure/game/texture/TextureHolder.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package miniventure.game.texture;

import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class TextureHolder {

public final TextureRegion texture;
public final AtlasRegion texture;
public final int width, height;
public final String name;

public TextureHolder(TextureRegion texture) {
public TextureHolder(AtlasRegion texture) {
this(texture, texture.getRegionWidth(), texture.getRegionHeight());
}

public TextureHolder(Region region) { this(null, region.width, region.height); }
public TextureHolder(Region region) { this(null, region.name, region.width, region.height); }

public TextureHolder(TextureRegion texture, int width, int height) {
public TextureHolder(AtlasRegion texture, int width, int height) { this(texture, texture.name, width, height); }
public TextureHolder(AtlasRegion texture, String name, int width, int height) {
this.texture = texture;
this.name = name;
this.width = width;
this.height = height;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import miniventure.game.GameCore;
import miniventure.game.item.Item;
import miniventure.game.texture.TextureHolder;
import miniventure.game.util.blinker.Blinker;
import miniventure.game.util.MyUtils;
import miniventure.game.util.blinker.Blinker;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Animation;
Expand Down Expand Up @@ -200,13 +200,13 @@ public void render(float x, float y, Batch batch) {

public static class TextRenderer extends EntityRenderer {

private final String text;
@NotNull private final String text;
private final Color main;
private final Color shadow;
private final float width;
private final float height;

public TextRenderer(String text, Color main, Color shadow) {
public TextRenderer(@NotNull String text, Color main, Color shadow) {
this.text = text;
this.main = main;
this.shadow = shadow;
Expand Down
2 changes: 1 addition & 1 deletion core/game/src/miniventure/game/world/levelgen/Biome.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enum Biome {
put(CACTUS, SAND);
put(TREE, GRASS);
put(DIRT, HOLE);
put(GRASS, HOLE);
put(GRASS, DIRT);
put(STONE, DIRT);
}};

Expand Down
24 changes: 12 additions & 12 deletions core/game/src/miniventure/game/world/tile/AnimationProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
import java.util.HashMap;

import miniventure.game.GameCore;
import miniventure.game.texture.TextureHolder;

import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.Array;

import org.jetbrains.annotations.NotNull;

public class AnimationProperty extends TileProperty {

private static HashMap<String, HashMap<String, Array<AtlasRegion>>> tileConnectionAnimations = new HashMap<>();
private static HashMap<String, HashMap<String, Array<AtlasRegion>>> tileOverlapAnimations = new HashMap<>();
private static HashMap<String, HashMap<String, Array<TextureHolder>>> tileConnectionAnimations = new HashMap<>();
private static HashMap<String, HashMap<String, Array<TextureHolder>>> tileOverlapAnimations = new HashMap<>();
static {
Array<AtlasRegion> regions = GameCore.tileAtlas != null ? GameCore.tileAtlas.getRegions() : new Array<>();
for(AtlasRegion region: regions) {
Array<TextureHolder> regions = GameCore.tileAtlas.getRegions();
for(TextureHolder region: regions) {
String tilename = region.name.substring(0, region.name.indexOf("/"));
String spriteIdx = region.name.substring(region.name.indexOf("/")+1);
boolean isOverlap = spriteIdx.startsWith("o");
Expand All @@ -36,15 +36,15 @@ public class AnimationProperty extends TileProperty {
private class TileAnimation {
private final AnimationType animationType;
private final float frameTime;
private final HashMap<String, HashMap<String, Array<AtlasRegion>>> tileAnimationFrames;
private final HashMap<String, HashMap<String, Array<TextureHolder>>> tileAnimationFrames;

TileAnimation(AnimationType animationType, float frameTime, HashMap<String, HashMap<String, Array<AtlasRegion>>> tileAnimationFrames) {
TileAnimation(AnimationType animationType, float frameTime, HashMap<String, HashMap<String, Array<TextureHolder>>> tileAnimationFrames) {
this.animationType = animationType;
this.frameTime = frameTime;
this.tileAnimationFrames = tileAnimationFrames;
}

AtlasRegion getSprite(Tile tile, int spriteIndex, float timeElapsed) {
TextureHolder getSprite(Tile tile, int spriteIndex, float timeElapsed) {
String typeName = tileType.name().toLowerCase();
String indexString = (spriteIndex < 10 ? "0" : "") + spriteIndex;
return animationType.getSprite(tile, tileAnimationFrames.get(typeName).get(indexString), (int)(timeElapsed/frameTime));
Expand All @@ -53,7 +53,7 @@ AtlasRegion getSprite(Tile tile, int spriteIndex, float timeElapsed) {

@FunctionalInterface
private interface SpriteFetcher {
AtlasRegion getSprite(Tile tile, Array<AtlasRegion> frames, int frameIdx);
TextureHolder getSprite(Tile tile, Array<TextureHolder> frames, int frameIdx);
}

enum AnimationType {
Expand All @@ -70,7 +70,7 @@ enum AnimationType {

AnimationType(SpriteFetcher fetcher) { this.fetcher = fetcher; }

public AtlasRegion getSprite(Tile tile, Array<AtlasRegion> frames, int frameIdx) { return fetcher.getSprite(tile, frames, frameIdx); }
public TextureHolder getSprite(Tile tile, Array<TextureHolder> frames, int frameIdx) { return fetcher.getSprite(tile, frames, frameIdx); }
}

private final boolean isOpaque;
Expand All @@ -87,10 +87,10 @@ enum AnimationType {

public boolean isOpaque() { return isOpaque; }

AtlasRegion getSprite(int spriteIndex, boolean isOverlapSprite, Tile tile) {
TextureHolder getSprite(int spriteIndex, boolean isOverlapSprite, Tile tile) {
return getSprite(spriteIndex, isOverlapSprite, tile, GameCore.getElapsedProgramTime());
}
AtlasRegion getSprite(int spriteIndex, boolean isOverlapSprite, Tile tile, float timeElapsed) {
TextureHolder getSprite(int spriteIndex, boolean isOverlapSprite, Tile tile, float timeElapsed) {
if(!isOverlapSprite && !tile.hasType(tileType))
System.err.println("Warning: fetching sprite for tile "+tile+" that doesn't have the intended type " + tileType);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package miniventure.game.world.tile;

import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import miniventure.game.texture.TextureHolder;

import com.badlogic.gdx.utils.Array;

import org.jetbrains.annotations.NotNull;
Expand All @@ -19,7 +20,7 @@ public class ConnectionProperty extends TileProperty {
this.connectingTiles.add(tileType);
}

AtlasRegion getSprite(Tile tile, TileType[][] aroundTypes) {
TextureHolder getSprite(Tile tile, TileType[][] aroundTypes) {
int spriteIdx = 0;

if(connects) {
Expand Down

0 comments on commit bc3fa0c

Please sign in to comment.