From e14be3ec1b0e0e6fb5548fd7d9efaed7651fa341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 22 Apr 2014 08:45:16 +0200 Subject: [PATCH] start of server-client decoupling, unfinished --- src/mightypork/rogue/App.java | 4 +- .../rogue/world/LocalWorldAccess.java | 6 ++ src/mightypork/rogue/world/MapGenerator.java | 6 +- src/mightypork/rogue/world/Player.java | 45 +++++---- src/mightypork/rogue/world/WorldAccess.java | 6 ++ .../world/{World.java => WorldClient.java} | 96 +++---------------- src/mightypork/rogue/world/WorldServer.java | 93 ++++++++++++++++++ src/mightypork/rogue/world/map/Level.java | 21 +++- .../rogue/world/tile/DroppedItemRenderer.java | 5 +- src/mightypork/rogue/world/tile/Tile.java | 15 +-- .../rogue/world/tile/TileModel.java | 13 ++- .../world/tile/models/AbstractNullTile.java | 2 +- .../rogue/world/tile/models/SimpleTile.java | 2 +- 13 files changed, 193 insertions(+), 121 deletions(-) create mode 100644 src/mightypork/rogue/world/LocalWorldAccess.java create mode 100644 src/mightypork/rogue/world/WorldAccess.java rename src/mightypork/rogue/world/{World.java => WorldClient.java} (53%) create mode 100644 src/mightypork/rogue/world/WorldServer.java diff --git a/src/mightypork/rogue/App.java b/src/mightypork/rogue/App.java index f8a832c..2011cd7 100644 --- a/src/mightypork/rogue/App.java +++ b/src/mightypork/rogue/App.java @@ -22,6 +22,7 @@ import mightypork.rogue.screens.main_menu.ScreenMainMenu; import mightypork.rogue.screens.test_bouncyboxes.ScreenTestBouncy; import mightypork.rogue.screens.test_cat_sound.ScreenTestCat; import mightypork.rogue.screens.test_render.ScreenTestRender; +import mightypork.rogue.world.Player; import mightypork.rogue.world.item.Item; import mightypork.rogue.world.map.Level; import mightypork.rogue.world.tile.Tile; @@ -106,9 +107,10 @@ public final class App extends BaseApp { @Override protected void preInit() { + Ion.registerBinary(Tile.ION_MARK, Tile.class); Ion.registerBinary(Item.ION_MARK, Item.class); Ion.registerBinary(Level.ION_MARK, Level.class); - Ion.registerBinary(Tile.ION_MARK, Tile.class); + Ion.registerBinary(Level.ION_MARK, Player.class); } diff --git a/src/mightypork/rogue/world/LocalWorldAccess.java b/src/mightypork/rogue/world/LocalWorldAccess.java new file mode 100644 index 0000000..d2e9cc8 --- /dev/null +++ b/src/mightypork/rogue/world/LocalWorldAccess.java @@ -0,0 +1,6 @@ +package mightypork.rogue.world; + + +public class LocalWorldAccess implements WorldAccess { + +} diff --git a/src/mightypork/rogue/world/MapGenerator.java b/src/mightypork/rogue/world/MapGenerator.java index 29ab4b9..0c3efa0 100644 --- a/src/mightypork/rogue/world/MapGenerator.java +++ b/src/mightypork/rogue/world/MapGenerator.java @@ -13,19 +13,19 @@ public class MapGenerator { public static final Random rand = new Random(); - public static World createWorld(long seed) + public static WorldServer createWorld(long seed) { synchronized (rand) { rand.setSeed(seed); - final World w = new World(); + final WorldServer w = new WorldServer(); w.setSeed(seed); w.addLevel(createLevel(rand.nextLong(), Tiles.CRYSTAL_FLOOR, Tiles.CRYSTAL_WALL)); w.addLevel(createLevel(rand.nextLong(), Tiles.BRCOBBLE_FLOOR, Tiles.BRCOBBLE_WALL)); // TODO place on start position - w.getPlayer().teleport(new WorldPos(10, 10, 0)); + w.addPlayer("local", new Player(10, 10, 0)); return w; } } diff --git a/src/mightypork/rogue/world/Player.java b/src/mightypork/rogue/world/Player.java index d18d7af..d7a1bc6 100644 --- a/src/mightypork/rogue/world/Player.java +++ b/src/mightypork/rogue/world/Player.java @@ -3,12 +3,10 @@ package mightypork.rogue.world; import java.io.IOException; -import mightypork.util.constraints.vect.Vect; -import mightypork.util.constraints.vect.mutable.VectAnimated; -import mightypork.util.control.timing.Updateable; +import mightypork.util.ion.IonBinary; import mightypork.util.ion.IonBundle; -import mightypork.util.ion.IonBundled; -import mightypork.util.math.Easing; +import mightypork.util.ion.IonInput; +import mightypork.util.ion.IonOutput; /** @@ -16,7 +14,9 @@ import mightypork.util.math.Easing; * * @author MightyPork */ -public class Player implements IonBundled, MapObserver, Updateable { +public class Player implements IonBinary, MapObserver { + + public static final short ION_MARK = 0; private final double walktime = 0.3; // possibly make changeable for speed potion @@ -71,18 +71,26 @@ public class Player implements IonBundled, MapObserver, Updateable { @Override - public void load(IonBundle in) throws IOException + public void load(IonInput in) throws IOException + { + IonBundle ib = in.readBundle(); + ib.loadBundled("pos", position); + } + + + @Override + public void save(IonOutput out) throws IOException { - in.loadBundled("pos", position); - in.loadBundled("target", target); + IonBundle ib = new IonBundle(); + ib.putBundled("target", target); + out.writeBundle(ib); } @Override - public void save(IonBundle out) throws IOException + public short getIonMark() { - out.putBundled("pos", position); - out.putBundled("target", target); + return ION_MARK; } @@ -113,15 +121,20 @@ public class Player implements IonBundled, MapObserver, Updateable { } - @Override - public void update(double delta) + public void setMoveListener(Runnable r) + { + this.moveListenerCustom = r; + } + + + public void updateVisual(double delta) { position.update(delta); } - public void setMoveListener(Runnable r) + public void updateLogic(double delta) { - this.moveListenerCustom = r; + // server stuffs (sleep timer etc) } } diff --git a/src/mightypork/rogue/world/WorldAccess.java b/src/mightypork/rogue/world/WorldAccess.java new file mode 100644 index 0000000..a34edbc --- /dev/null +++ b/src/mightypork/rogue/world/WorldAccess.java @@ -0,0 +1,6 @@ +package mightypork.rogue.world; + + +public interface WorldAccess { + +} diff --git a/src/mightypork/rogue/world/World.java b/src/mightypork/rogue/world/WorldClient.java similarity index 53% rename from src/mightypork/rogue/world/World.java rename to src/mightypork/rogue/world/WorldClient.java index 1458447..e63db14 100644 --- a/src/mightypork/rogue/world/World.java +++ b/src/mightypork/rogue/world/WorldClient.java @@ -1,11 +1,6 @@ package mightypork.rogue.world; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Set; - import mightypork.rogue.world.map.Level; import mightypork.rogue.world.map.TileRenderContext; import mightypork.util.constraints.rect.Rect; @@ -13,76 +8,21 @@ import mightypork.util.constraints.rect.RectConst; import mightypork.util.constraints.rect.proxy.RectBound; import mightypork.util.constraints.vect.VectConst; import mightypork.util.control.timing.Updateable; -import mightypork.util.ion.IonBundle; -import mightypork.util.ion.IonBundled; -public class World implements IonBundled, Updateable { - - private final ArrayList levels = new ArrayList<>(); - - private final Player player = new Player(); - - private transient final Set observers = new HashSet<>(); - - /** This seed can be used to re-create identical world. */ - private long seed; - - - @Override - public void load(IonBundle in) throws IOException - { - in.loadBundled("player", player); - seed = in.get("seed", 0L); - in.loadSequence("levels", levels); - } - +public class WorldClient implements Updateable { - @Override - public void save(IonBundle out) throws IOException - { - out.putBundled("player", player); - out.put("seed", seed); - out.putSequence("levels", levels); - } - - - public void removeObserver(MapObserver observer) - { - observers.remove(observer); - } - - - public void addObserver(MapObserver observer) - { - observers.add(observer); - } - - - public void addLevel(Level level) - { - levels.add(level); - } + private Level level = null; + private final Player player = null; + @Override public void update(double delta) { - player.update(delta); + player.updateVisual(delta); - for (int level = 0; level < levels.size(); level++) { - for (final MapObserver observer : observers) { - if (observer.getPosition().floor == level) { - levels.get(level).update(observer, delta); - } - } - } - } - - - public Level getLevelForObserver(MapObserver observer) - { - return levels.get(observer.getPosition().floor); + level.updateVisual(player, delta); } @@ -95,9 +35,7 @@ public class World implements IonBundled, Updateable { * @param minSize minimum tile size */ public void render(final RectBound viewport, final int yTiles, final int xTiles, final int minSize) - { - final Level floor = getLevelForObserver(player); // TODO fractional movement - + { final Rect r = viewport.getRect(); final double vpH = r.height().value(); final double vpW = r.width().value(); @@ -119,14 +57,14 @@ public class World implements IonBundled, Updateable { //@formatter:off final RectConst mapRect = vpCenter.startRect().grow( playerX*tileSize, - (floor.getWidth() - playerX) * tileSize, + (level.getWidth() - playerX) * tileSize, playerY*tileSize, - (floor.getHeight() - playerY) * tileSize + (level.getHeight() - playerY) * tileSize ).freeze(); //@formatter:on System.out.println(playerX + "," + playerY + " : " + mapRect); - System.out.println(floor.getWidth() + "," + floor.getHeight()); + System.out.println(level.getWidth() + "," + level.getHeight()); // tiles to render final int x1 = (int) Math.floor(playerX - (vpW / tileSize)); @@ -134,7 +72,7 @@ public class World implements IonBundled, Updateable { final int x2 = (int) Math.ceil(playerX + (vpW / tileSize)); final int y2 = (int) Math.ceil(playerY + (vpH / tileSize)); - final TileRenderContext trc = new TileRenderContext(floor, mapRect); //-tileSize*0.5 + final TileRenderContext trc = new TileRenderContext(level, mapRect); //-tileSize*0.5 for (trc.y = y1; trc.y <= y2; trc.y++) { for (trc.x = x1; trc.x <= x2; trc.x++) { trc.render(); @@ -143,18 +81,6 @@ public class World implements IonBundled, Updateable { } - public void setSeed(long seed) - { - this.seed = seed; - } - - - public long getSeed() - { - return seed; - } - - public Player getPlayer() { return player; diff --git a/src/mightypork/rogue/world/WorldServer.java b/src/mightypork/rogue/world/WorldServer.java new file mode 100644 index 0000000..9d9fa66 --- /dev/null +++ b/src/mightypork/rogue/world/WorldServer.java @@ -0,0 +1,93 @@ +package mightypork.rogue.world; + + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import mightypork.rogue.world.map.Level; +import mightypork.util.control.timing.Updateable; +import mightypork.util.ion.IonBundle; +import mightypork.util.ion.IonBundled; + + +/** + * World server. To a server, all players and levels are equal. + * + * @author MightyPork + */ +public class WorldServer implements IonBundled, Updateable { + + private final ArrayList levels = new ArrayList<>(); + + private final Map players = new HashMap<>(); + + /** This seed can be used to re-create identical world. */ + private long seed; + + + @Override + public void load(IonBundle in) throws IOException + { + seed = in.get("seed", 0L); + in.loadSequence("levels", levels); + in.loadMap("players", players); + } + + + @Override + public void save(IonBundle out) throws IOException + { + out.put("seed", seed); + out.putSequence("levels", levels); + out.putMap("players", players); + } + + + public void addLevel(Level level) + { + levels.add(level); + } + + public void addPlayer(String name, Player player) + { + players.put(name, player); + } + + public void removePlayer(String name) + { + players.remove(name); + } + + @Override + public void update(double delta) + { + // food meters and such + for (final Player pl : players.values()) { + pl.updateLogic(delta); + } + + for (int level = 0; level < levels.size(); level++) { + + // more than 1 player can be on floor, update for all of them + for (final Player pl : players.values()) { + if (pl.getPosition().floor == level) { + levels.get(level).updateLogic(pl, delta); + } + } + + } + } + + + public void setSeed(long seed) + { + this.seed = seed; + } + + + public long getSeed() + { + return seed; + } +} diff --git a/src/mightypork/rogue/world/map/Level.java b/src/mightypork/rogue/world/map/Level.java index c2ab772..d24bf48 100644 --- a/src/mightypork/rogue/world/map/Level.java +++ b/src/mightypork/rogue/world/map/Level.java @@ -4,6 +4,7 @@ package mightypork.rogue.world.map; import java.io.IOException; import mightypork.rogue.world.MapObserver; +import mightypork.rogue.world.Player; import mightypork.rogue.world.WorldPos; import mightypork.rogue.world.tile.Tile; import mightypork.rogue.world.tile.TileModel; @@ -22,7 +23,7 @@ import mightypork.util.math.noise.NoiseGen; */ public class Level implements MapAccess, IonBinary { - public static final int ION_MARK = 52; + public static final int ION_MARK = 53; private int width, height; @@ -177,7 +178,19 @@ public class Level implements MapAccess, IonBinary { } - public void update(MapObserver observer, double delta) + public void updateLogic(MapObserver observer, double delta) + { + updateForObserver(observer, delta, true, false); + } + + + public void updateVisual(Player player, double delta) + { + updateForObserver(player, delta, false, true); + } + + + private void updateForObserver(MapObserver observer, double delta, boolean logic, boolean visual) { final int viewRange = observer.getViewRange(); final WorldPos position = observer.getPosition(); @@ -195,7 +208,8 @@ public class Level implements MapAccess, IonBinary { for (int y = y1; y <= y2; y++) { for (int x = x1; x <= x2; x++) { - getTile(x, y).update(delta); + if (logic) getTile(x, y).updateLogic(delta); + if (visual) getTile(x, y).updateVisual(delta); } } } @@ -210,4 +224,5 @@ public class Level implements MapAccess, IonBinary { return noiseGen; } + } diff --git a/src/mightypork/rogue/world/tile/DroppedItemRenderer.java b/src/mightypork/rogue/world/tile/DroppedItemRenderer.java index 5f9c9bb..f3b0409 100644 --- a/src/mightypork/rogue/world/tile/DroppedItemRenderer.java +++ b/src/mightypork/rogue/world/tile/DroppedItemRenderer.java @@ -11,7 +11,7 @@ import mightypork.util.control.timing.Updateable; import mightypork.util.math.Easing; -public class DroppedItemRenderer implements Updateable { +public class DroppedItemRenderer { private Animator itemAnim = new AnimatorBounce(2, Easing.SINE_BOTH); @@ -37,8 +37,7 @@ public class DroppedItemRenderer implements Updateable { } - @Override - public void update(double delta) + public void updateVisual(double delta) { itemAnim.update(delta); } diff --git a/src/mightypork/rogue/world/tile/Tile.java b/src/mightypork/rogue/world/tile/Tile.java index 00e9435..6eb2a7b 100644 --- a/src/mightypork/rogue/world/tile/Tile.java +++ b/src/mightypork/rogue/world/tile/Tile.java @@ -14,7 +14,7 @@ import mightypork.util.ion.IonInput; import mightypork.util.ion.IonOutput; -public final class Tile implements IonBinary, Updateable { +public final class Tile implements IonBinary { public static final short ION_MARK = 50; @@ -99,16 +99,19 @@ public final class Tile implements IonBinary, Updateable { } - @Override - public void update(double delta) + public void updateLogic(double delta) { - model.update(this, delta); + model.updateLogic(this, delta); + } + + public void updateVisual(double delta) + { + model.updateVisual(this, delta); if (hasItems()) { - getItemRenderer().update(delta); + getItemRenderer().updateVisual(delta); } } - private DroppedItemRenderer getItemRenderer() { if (itemRenderer == null) { diff --git a/src/mightypork/rogue/world/tile/TileModel.java b/src/mightypork/rogue/world/tile/TileModel.java index 12aee6b..7f8f6cb 100644 --- a/src/mightypork/rogue/world/tile/TileModel.java +++ b/src/mightypork/rogue/world/tile/TileModel.java @@ -70,12 +70,21 @@ public abstract class TileModel { /** - * Update a tile + * Update tile state etc * * @param tile tile * @param delta delta time */ - public abstract void update(Tile tile, double delta); + public abstract void updateLogic(Tile tile, double delta); + + + /** + * Update tile effects + * + * @param tile tile + * @param delta delta time + */ + public abstract void updateVisual(Tile tile, double delta); /** diff --git a/src/mightypork/rogue/world/tile/models/AbstractNullTile.java b/src/mightypork/rogue/world/tile/models/AbstractNullTile.java index 4250e84..0beca78 100644 --- a/src/mightypork/rogue/world/tile/models/AbstractNullTile.java +++ b/src/mightypork/rogue/world/tile/models/AbstractNullTile.java @@ -30,7 +30,7 @@ public abstract class AbstractNullTile extends TileModel { @Override - public void update(Tile tile, double delta) + public void updateLogic(Tile tile, double delta) { } diff --git a/src/mightypork/rogue/world/tile/models/SimpleTile.java b/src/mightypork/rogue/world/tile/models/SimpleTile.java index febd5c7..9c472b1 100644 --- a/src/mightypork/rogue/world/tile/models/SimpleTile.java +++ b/src/mightypork/rogue/world/tile/models/SimpleTile.java @@ -37,7 +37,7 @@ public abstract class SimpleTile extends TileModel { @Override @DefaultImpl - public void update(Tile tile, double delta) + public void updateLogic(Tile tile, double delta) { }