preparing for rewrite of tile and item system

v5stable
Ondřej Hruška 11 years ago
parent b30f39a762
commit 7927d9a67b
  1. 29
      src/mightypork/gamecore/render/DisplaySystem.java
  2. 10
      src/mightypork/rogue/App.java
  3. 9
      src/mightypork/rogue/Res.java
  4. 21
      src/mightypork/rogue/world/EntityModel.java
  5. 4
      src/mightypork/rogue/world/WorldMap.java
  6. 4
      src/mightypork/rogue/world/item/ItemData.java
  7. 37
      src/mightypork/rogue/world/tile/TileData.java
  8. 2
      src/mightypork/rogue/world/tile/TileGrid.java
  9. 2
      src/mightypork/rogue/world/tile/TileModel.java
  10. 28
      src/mightypork/rogue/world/tile/TileRenderContext.java
  11. 15
      src/mightypork/rogue/world/tile/impl/SimpleFloor.java
  12. 71
      src/mightypork/rogue/world/tile/impl/SimpleTile.java
  13. 16
      src/mightypork/rogue/world/tile/impl/SimpleWall.java
  14. 2
      src/mightypork/test/FakeTile.java
  15. 34
      src/mightypork/util/constraints/rect/Rect.java
  16. 13
      src/mightypork/util/math/noise/NoiseGen.java

@ -32,7 +32,7 @@ public class DisplaySystem extends AppModule implements RectBound {
private FpsMeter fpsMeter;
/** Current screen size */
private final Vect screenSize = new Vect() {
private static final Vect screenSize = new Vect() {
@Override
public double y()
@ -48,7 +48,7 @@ public class DisplaySystem extends AppModule implements RectBound {
}
};
private final Rect rect = Rect.make(screenSize);
private static final Rect rect = Rect.make(screenSize);
/**
@ -168,7 +168,7 @@ public class DisplaySystem extends AppModule implements RectBound {
/**
* @return true if close was requested (i.e. click on cross)
*/
public boolean isCloseRequested()
public static boolean isCloseRequested()
{
return Display.isCloseRequested();
}
@ -179,7 +179,7 @@ public class DisplaySystem extends AppModule implements RectBound {
*
* @return is fullscreen
*/
public boolean isFullscreen()
public static boolean isFullscreen()
{
return Display.isFullscreen();
}
@ -190,16 +190,27 @@ public class DisplaySystem extends AppModule implements RectBound {
*
* @return size
*/
public Vect getSize()
public static Vect getSize()
{
return screenSize;
}
/**
* Get screen rect. Static version of getRect().
*
* @return size
*/
public static Rect getBounds()
{
return rect;
}
/**
* @return screen width
*/
public int getWidth()
public static int getWidth()
{
return screenSize.xi();
}
@ -208,7 +219,7 @@ public class DisplaySystem extends AppModule implements RectBound {
/**
* @return screen height
*/
public int getHeight()
public static int getHeight()
{
return screenSize.yi();
}
@ -246,7 +257,7 @@ public class DisplaySystem extends AppModule implements RectBound {
@Override
public Rect getRect()
{
return rect;
return getBounds();
}
@ -264,7 +275,7 @@ public class DisplaySystem extends AppModule implements RectBound {
*
* @return screen center.
*/
public Vect getCenter()
public static Vect getCenter()
{
return rect.center();
}

@ -24,7 +24,9 @@ import mightypork.rogue.screens.test_cat_sound.ScreenTestCat;
import mightypork.rogue.screens.test_render.ScreenTestRender;
import mightypork.rogue.world.WorldMap;
import mightypork.rogue.world.item.Item;
import mightypork.rogue.world.item.ItemData;
import mightypork.rogue.world.tile.Tile;
import mightypork.rogue.world.tile.TileData;
import mightypork.util.control.eventbus.EventBus;
import mightypork.util.control.eventbus.events.Event;
import mightypork.util.files.ion.Ion;
@ -106,9 +108,11 @@ public final class App extends BaseApp {
@Override
protected void preInit()
{
Ion.registerIonizable(Tile.ION_MARK, Tile.class);
Ion.registerIonizable(Item.ION_MARK, Item.class);
Ion.registerIonizable(WorldMap.ION_MARK, WorldMap.class);
Ion.registerIonizable(Tile.ION_MARK, Tile.class); // 700
Ion.registerIonizable(Item.ION_MARK, Item.class); // 701
Ion.registerIonizable(WorldMap.ION_MARK, WorldMap.class); // 702
Ion.registerIonizable(TileData.ION_MARK, TileData.class); // 703
Ion.registerIonizable(ItemData.ION_MARK, ItemData.class); // 704
}

@ -80,7 +80,8 @@ public final class Res {
texture = textures.loadTexture("tiles", "/res/img/map_tiles.png", FilterMode.NEAREST, WrapMode.CLAMP);
QuadGrid tiles = texture.grid(32, 32);
textures.addSheet("tile.mossy_bricks.wall", tiles.makeSheet(4, 0, 7, 1));
textures.addSheet("tile.mossy_bricks.floor", tiles.makeSheet(16, 5, 7, 1));
}
@ -104,6 +105,12 @@ public final class Res {
}
public static TxSheet getTxSheet(String key)
{
return textures.getSheet(key);
}
public static LoopPlayer getLoop(String key)
{
return sounds.getLoop(key);

@ -54,27 +54,6 @@ public abstract class EntityModel<D, R extends RectBound> {
public abstract D createData();
/**
* Load an entity from ION input stream.
*
* @param data data to load
* @param in input stream
* @throws IOException
*/
public abstract void load(D data, InputStream in) throws IOException;
/**
* Save an entity to ION output stream.
*
* @param data data to save
* @param out output stream
* @throws IOException
*/
@DefaultImpl
public abstract void save(D data, OutputStream out) throws IOException;
/**
* Render the item according to given context.
*

@ -6,12 +6,12 @@ import java.io.InputStream;
import java.io.OutputStream;
import mightypork.rogue.world.tile.Tile;
import mightypork.rogue.world.tile.TileHolder;
import mightypork.rogue.world.tile.TileGrid;
import mightypork.util.files.ion.Ion;
import mightypork.util.files.ion.Ionizable;
public class WorldMap implements TileHolder, Ionizable {
public class WorldMap implements TileGrid, Ionizable {
public static final int ION_MARK = 702;

@ -6,6 +6,8 @@ package mightypork.rogue.world.item;
*
* @author MightyPork
*/
public abstract class ItemData {
public final class ItemData {
public static final int ION_MARK = 704;
}

@ -1,9 +1,14 @@
package mightypork.rogue.world.tile;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Stack;
import mightypork.rogue.world.item.Item;
import mightypork.util.files.ion.IonList;
import mightypork.util.files.ion.Ionizable;
/**
@ -11,9 +16,39 @@ import mightypork.rogue.world.item.Item;
*
* @author MightyPork
*/
public abstract class TileData {
public final class TileData implements Ionizable {
public static final short ION_MARK = 703;
/** Items dropped onto this tile */
public final Stack<Item> items = new Stack<>();
public int id;
public boolean[] flags;
public int[] numbers;
@Override
public void loadFrom(InputStream in) throws IOException
{
}
@Override
public void saveTo(OutputStream out) throws IOException
{
}
@Override
public short getIonMark()
{
return 0;
}
public Tile toTile() {
Tile t = new Tile(Tiles.get(id));
t.s
}
}

@ -1,7 +1,7 @@
package mightypork.rogue.world.tile;
public interface TileHolder {
public interface TileGrid {
Tile getTile(int x, int y);

@ -25,7 +25,7 @@ public abstract class TileModel extends EntityModel<TileData, TileRenderContext>
*
* @return can be walked through (if discovered / open)
*/
public abstract boolean isPotentiallyWalkable();
public abstract boolean isWalkable();
/**

@ -4,18 +4,22 @@ package mightypork.rogue.world.tile;
import mightypork.util.constraints.rect.Rect;
import mightypork.util.constraints.rect.builders.TiledRect;
import mightypork.util.constraints.rect.proxy.RectBound;
import mightypork.util.math.noise.NoiseGen;
public final class TileRenderContext implements RectBound {
private final TileHolder map;
private final TileGrid map;
private final TiledRect tiler;
private int x, y;
private final NoiseGen noise;
public int x, y;
public TileRenderContext(TileHolder map, Rect mapArea) {
public TileRenderContext(TileGrid map, Rect drawArea, long renderNoiseSeed) {
this.map = map;
this.tiler = mapArea.tiles(map.getWidth(), map.getHeight());
this.tiler = drawArea.tiles(map.getWidth(), map.getHeight());
this.noise = new NoiseGen(0.2, 0, 0.5, 1, renderNoiseSeed);
}
@ -36,4 +40,20 @@ public final class TileRenderContext implements RectBound {
{
return tiler.tile(x, y);
}
/**
* @return per-coord noise value 0..1
*/
public double getNoise()
{
return noise.valueAt(x, y);
}
public void setCoord(int x, int y)
{
this.x = x;
this.y = y;
}
}

@ -0,0 +1,15 @@
package mightypork.rogue.world.tile.impl;
public class SimpleFloor extends SimpleTile {
public SimpleFloor(int id, String sheetKey) {
super(id, sheetKey);
}
@Override
public boolean isWalkable()
{
return true;
}
}

@ -0,0 +1,71 @@
package mightypork.rogue.world.tile.impl;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import mightypork.gamecore.render.DisplaySystem;
import mightypork.gamecore.render.Render;
import mightypork.gamecore.render.textures.TxSheet;
import mightypork.rogue.Res;
import mightypork.rogue.world.tile.TileData;
import mightypork.rogue.world.tile.TileModel;
import mightypork.rogue.world.tile.TileRenderContext;
import mightypork.util.annotations.DefaultImpl;
public abstract class SimpleTile extends TileModel {
private TxSheet sheet;
public SimpleTile(int id, String sheetKey) {
super(id);
this.sheet = Res.getTxSheet(sheetKey);
}
@Override
@DefaultImpl
public void load(TileData data, InputStream in) throws IOException
{
// do nothing
}
@Override
@DefaultImpl
public void save(TileData data, OutputStream out) throws IOException
{
// do nothing
}
@Override
public void render(TileData data, TileRenderContext context)
{
// TODO worldmap should take care of this and break the row drawing when it encounters end of screen etc
// not in screen -> no draw
if (!context.getRect().intersectsWith(DisplaySystem.getBounds())) return;
Render.quadTextured(context.getRect(), sheet.getRandomQuad(context.getNoise()));
}
@Override
@DefaultImpl
public void update(TileData item, double delta)
{
// do nothing
}
@Override
@DefaultImpl
public boolean isWalkable(TileData data)
{
return isWalkable();
}
}

@ -0,0 +1,16 @@
package mightypork.rogue.world.tile.impl;
public class SimpleWall extends SimpleTile {
public SimpleWall(int id, String sheetKey) {
super(id, sheetKey);
}
@Override
public boolean isWalkable()
{
return false;
}
}

@ -26,7 +26,7 @@ public class FakeTile extends TileModel {
@Override
public boolean isPotentiallyWalkable()
public boolean isWalkable()
{
return true;
}

@ -1,6 +1,5 @@
package mightypork.util.constraints.rect;
import mightypork.util.annotations.FactoryMethod;
import mightypork.util.constraints.DigestCache;
import mightypork.util.constraints.Digestable;
@ -952,4 +951,37 @@ public abstract class Rect implements RectBound, Digestable<RectDigest> {
{
return new TiledRect(this, 1, rows);
}
/**
* Check for intersection
*
* @param other other rect
* @return true if they intersect
* @see org.lwjgl.util.Rectangle
*/
public boolean intersectsWith(Rect other)
{
double tw = this.size().x();
double th = this.size().y();
double rw = other.size().x();
double rh = other.size().y();
if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
return false;
}
double tx = this.origin().x();
double ty = this.origin().y();
double rx = other.origin().x();
double ry = other.origin().y();
rw += rx;
rh += ry;
tw += tx;
th += ty;
// overflow || intersect
return ((rw < rx || rw > tx) && (rh < ry || rh > ty) && (tw < tx || tw > rx) && (th < ty || th > ry));
}
}

@ -19,6 +19,19 @@ public class NoiseGen {
private final double density;
/**
* make a new noise generator with a random seed
*
* @param density noise density (0..1). Lower density means larger "spots".
* @param low low bound ("valley")
* @param middle middle bound ("surface")
* @param high high bound ("hill")
*/
public NoiseGen(double density, double low, double middle, double high) {
this(density, low, middle, high, Double.doubleToLongBits(Math.random()));
}
/**
* make a new noise generator
*

Loading…
Cancel
Save