items
This commit is contained in:
@@ -12,7 +12,7 @@ import mightypork.gamecore.util.math.constraints.num.mutable.NumAnimated;
|
||||
*/
|
||||
public class AnimatorBounce extends Animator {
|
||||
|
||||
private final boolean wasUp = false;
|
||||
private boolean wasUp = false;
|
||||
|
||||
|
||||
public AnimatorBounce(double start, double end, double period, Easing easing)
|
||||
@@ -47,6 +47,8 @@ public class AnimatorBounce extends Animator {
|
||||
} else {
|
||||
anim.fadeIn();
|
||||
}
|
||||
|
||||
wasUp = !wasUp;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -76,7 +76,6 @@ public final class App extends BaseApp {
|
||||
{
|
||||
super.registerIonizables();
|
||||
|
||||
Ion.registerType(Item.ION_MARK, Item.class);
|
||||
Ion.registerType(Level.ION_MARK, Level.class);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,12 +18,12 @@ public abstract class PlayerControl {
|
||||
private World lastWorld;
|
||||
|
||||
|
||||
protected abstract World getWorld();
|
||||
protected abstract World provideWorld();
|
||||
|
||||
|
||||
private World getWorld2()
|
||||
public World getWorld()
|
||||
{
|
||||
final World newWorld = getWorld();
|
||||
final World newWorld = provideWorld();
|
||||
|
||||
if (newWorld != lastWorld) {
|
||||
for (final EntityMoveListener eml : playerMoveListeners) {
|
||||
@@ -40,9 +40,9 @@ public abstract class PlayerControl {
|
||||
|
||||
public Entity getPlayerEntity()
|
||||
{
|
||||
if (getWorld2() == null) return null;
|
||||
if (getWorld() == null) return null;
|
||||
|
||||
return getWorld2().getPlayerEntity();
|
||||
return getWorld().getPlayerEntity();
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ public abstract class PlayerControl {
|
||||
|
||||
public Level getLevel()
|
||||
{
|
||||
return getWorld2().getCurrentLevel();
|
||||
return getWorld().getCurrentLevel();
|
||||
}
|
||||
|
||||
|
||||
@@ -114,7 +114,7 @@ public abstract class PlayerControl {
|
||||
{
|
||||
if (pos.dist(getCoord()) > 8) return false; // too far
|
||||
|
||||
return getLevel().getTile(pos).onClick();
|
||||
return getLevel().getTile(pos).onClick(getWorld());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ public class WorldProvider extends RootBusNode {
|
||||
private final PlayerControl playerControl = new PlayerControl() {
|
||||
|
||||
@Override
|
||||
protected World getWorld()
|
||||
protected World provideWorld()
|
||||
{
|
||||
return world;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,10 @@ import mightypork.rogue.world.entity.Entities;
|
||||
import mightypork.rogue.world.entity.Entity;
|
||||
import mightypork.rogue.world.gen.rooms.Rooms;
|
||||
import mightypork.rogue.world.gen.themes.ThemeBrick;
|
||||
import mightypork.rogue.world.item.Item;
|
||||
import mightypork.rogue.world.item.Items;
|
||||
import mightypork.rogue.world.level.Level;
|
||||
import mightypork.rogue.world.tile.Tile;
|
||||
|
||||
|
||||
public class LevelGenerator {
|
||||
@@ -56,6 +59,20 @@ public class LevelGenerator {
|
||||
if (lvl.addEntity(e, pos)) break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4+complexity + rand.nextInt(1+complexity); i++) {
|
||||
|
||||
Item meat = Items.MEAT.createItem();
|
||||
|
||||
for (int j = 0; j < 20; j++) {
|
||||
pos.x = rand.nextInt(lvl.getWidth());
|
||||
pos.y = rand.nextInt(lvl.getHeight());
|
||||
|
||||
Tile t = lvl.getTile(pos);
|
||||
if(t.dropItem(meat)) break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return lvl;
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public class MIPMouse implements MapInteractionPlugin {
|
||||
final Coord pos = view.toWorldPos(mouse);
|
||||
final Tile t = pc.getLevel().getTile(pos);
|
||||
|
||||
if (t.onClick()) return true;
|
||||
if (t.onClick(pc.getWorld())) return true;
|
||||
|
||||
if (!down && t.isWalkable()) {
|
||||
if (troToNav(view, pc, mouse)) return true;
|
||||
|
||||
@@ -3,66 +3,54 @@ package mightypork.rogue.world.item;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import mightypork.gamecore.util.annot.DefaultImpl;
|
||||
import mightypork.gamecore.util.ion.IonInput;
|
||||
import mightypork.gamecore.util.ion.IonObjBinary;
|
||||
import mightypork.gamecore.util.ion.IonObjBlob;
|
||||
import mightypork.gamecore.util.ion.IonOutput;
|
||||
import mightypork.gamecore.util.math.constraints.rect.Rect;
|
||||
import mightypork.gamecore.util.math.constraints.rect.proxy.RectBound;
|
||||
|
||||
|
||||
public class Item implements IonObjBinary {
|
||||
public abstract class Item implements IonObjBlob {
|
||||
|
||||
public static final short ION_MARK = 51;
|
||||
|
||||
private ItemModel model;
|
||||
|
||||
public int id;
|
||||
private final ItemModel model;
|
||||
private ItemRenderer renderer;
|
||||
|
||||
|
||||
public Item(int id)
|
||||
{
|
||||
this(Items.get(id));
|
||||
}
|
||||
|
||||
|
||||
public Item()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public Item(ItemModel model)
|
||||
{
|
||||
public Item(ItemModel model) {
|
||||
this.model = model;
|
||||
this.id = model.id;
|
||||
}
|
||||
|
||||
|
||||
public void render(RectBound context)
|
||||
public final void render(Rect rect)
|
||||
{
|
||||
model.render(this, context);
|
||||
}
|
||||
if (renderer == null) {
|
||||
renderer = makeRenderer();
|
||||
}
|
||||
|
||||
renderer.render(rect);
|
||||
};
|
||||
|
||||
|
||||
protected abstract ItemRenderer makeRenderer();
|
||||
|
||||
|
||||
@Override
|
||||
@DefaultImpl
|
||||
public void save(IonOutput out) throws IOException
|
||||
{
|
||||
out.writeIntByte(id);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@DefaultImpl
|
||||
public void load(IonInput in) throws IOException
|
||||
{
|
||||
id = in.readIntByte();
|
||||
|
||||
// if id changed, get new model
|
||||
if (model == null || id != model.id) model = Items.get(id);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public short getIonMark()
|
||||
public final ItemModel getModel()
|
||||
{
|
||||
return ION_MARK;
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,32 +1,57 @@
|
||||
package mightypork.rogue.world.item;
|
||||
|
||||
|
||||
import mightypork.gamecore.util.annot.DefaultImpl;
|
||||
import mightypork.gamecore.util.math.constraints.rect.proxy.RectBound;
|
||||
import java.io.IOException;
|
||||
|
||||
import mightypork.gamecore.util.ion.IonInput;
|
||||
import mightypork.gamecore.util.ion.IonOutput;
|
||||
|
||||
public abstract class ItemModel {
|
||||
/**
|
||||
* Item model (builder)
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public final class ItemModel {
|
||||
|
||||
/** Model ID */
|
||||
public final int id;
|
||||
public final Class<? extends Item> itemClass;
|
||||
|
||||
|
||||
public ItemModel(int id)
|
||||
public ItemModel(int id, Class<? extends Item> item)
|
||||
{
|
||||
Items.register(id, this);
|
||||
this.id = id;
|
||||
this.itemClass = item;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return new tile with this model
|
||||
* @return new item instance of this type
|
||||
*/
|
||||
@DefaultImpl
|
||||
public Item create()
|
||||
public <T extends Item> T createItem()
|
||||
{
|
||||
return new Item(this);
|
||||
try {
|
||||
return (T) itemClass.getConstructor(ItemModel.class).newInstance(this);
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException("Could not instantiate an item.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public abstract void render(Item item, RectBound context);
|
||||
public Item loadItem(IonInput in) throws IOException
|
||||
{
|
||||
final Item t = createItem();
|
||||
t.load(in);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
public void saveItem(IonOutput out, Item tile) throws IOException
|
||||
{
|
||||
if (itemClass != tile.getClass()) throw new RuntimeException("Item class mismatch.");
|
||||
|
||||
tile.save(out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
package mightypork.rogue.world.item;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
import mightypork.gamecore.util.ion.IonInput;
|
||||
import mightypork.gamecore.util.ion.IonOutput;
|
||||
import mightypork.rogue.world.item.items.ItemMeat;
|
||||
|
||||
|
||||
/**
|
||||
* Item registry
|
||||
*
|
||||
@@ -10,10 +18,18 @@ public final class Items {
|
||||
|
||||
private static final ItemModel[] items = new ItemModel[256];
|
||||
|
||||
public static final ItemModel MEAT = new ItemModel(1, ItemMeat.class);
|
||||
|
||||
|
||||
public static void register(int id, ItemModel model)
|
||||
{
|
||||
if (id < 0 || id >= items.length) if (items[id] != null) throw new IllegalArgumentException("Item ID " + id + " already in use.");
|
||||
if (id < 0 || id >= items.length) {
|
||||
throw new IllegalArgumentException("Item ID " + id + " is out of range.");
|
||||
}
|
||||
|
||||
if (items[id] != null) {
|
||||
throw new IllegalArgumentException("Item ID " + id + " already in use.");
|
||||
}
|
||||
|
||||
items[id] = model;
|
||||
}
|
||||
@@ -22,7 +38,56 @@ public final class Items {
|
||||
public static ItemModel get(int id)
|
||||
{
|
||||
final ItemModel m = items[id];
|
||||
if (m == null) throw new IllegalArgumentException("No item with ID " + id + ".");
|
||||
|
||||
if (m == null) {
|
||||
throw new IllegalArgumentException("No item with ID " + id + ".");
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
public static Item loadItem(IonInput in) throws IOException
|
||||
{
|
||||
final int id = in.readIntByte();
|
||||
|
||||
final ItemModel model = get(id);
|
||||
return model.loadItem(in);
|
||||
}
|
||||
|
||||
|
||||
public static void saveItem(IonOutput out, Item item) throws IOException
|
||||
{
|
||||
final ItemModel model = item.getModel();
|
||||
|
||||
out.writeIntByte(model.id);
|
||||
model.saveItem(out, item);
|
||||
}
|
||||
|
||||
|
||||
public static Item create(int tileId)
|
||||
{
|
||||
return get(tileId).createItem();
|
||||
}
|
||||
|
||||
|
||||
public static void loadItems(IonInput in, Collection<Item> items) throws IOException
|
||||
{
|
||||
items.clear();
|
||||
while (in.hasNextEntry()) {
|
||||
items.add(loadItem(in));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void saveItems(IonOutput out, Collection<Item> items) throws IOException
|
||||
{
|
||||
for (final Item entity : items) {
|
||||
out.startEntry();
|
||||
saveItem(out, entity);
|
||||
}
|
||||
|
||||
out.endSequence();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,8 +16,9 @@ public class DroppedItemRenderer {
|
||||
|
||||
// prepared constraints, to avoid re-building each frame
|
||||
private final RectBoundAdapter tileRectAdapter = new RectBoundAdapter();
|
||||
private final Rect itemRect = tileRectAdapter.shrink(tileRectAdapter.height().perc(10)).moveY(itemAnim.neg());
|
||||
|
||||
private final Rect itemRect = tileRectAdapter
|
||||
.shrink(tileRectAdapter.height().perc(10))
|
||||
.moveY(itemAnim.neg().mul(tileRectAdapter.height().mul(0.2)));
|
||||
|
||||
public Animator getItemAnim()
|
||||
{
|
||||
|
||||
@@ -10,6 +10,7 @@ import mightypork.gamecore.util.ion.IonInput;
|
||||
import mightypork.gamecore.util.ion.IonObjBlob;
|
||||
import mightypork.gamecore.util.ion.IonOutput;
|
||||
import mightypork.gamecore.util.math.color.Color;
|
||||
import mightypork.rogue.world.World;
|
||||
import mightypork.rogue.world.item.Item;
|
||||
import mightypork.rogue.world.level.Level;
|
||||
import mightypork.rogue.world.level.render.TileRenderContext;
|
||||
@@ -33,6 +34,8 @@ public abstract class Tile implements IonObjBlob {
|
||||
// temporary flag for map.
|
||||
protected boolean occupied;
|
||||
protected boolean explored;
|
||||
|
||||
private TileRenderer renderer;
|
||||
|
||||
|
||||
public Tile(TileModel model)
|
||||
@@ -49,21 +52,24 @@ public abstract class Tile implements IonObjBlob {
|
||||
{
|
||||
if (!isExplored()) return;
|
||||
|
||||
final TileRenderer r = getRenderer();
|
||||
if (r == null) {
|
||||
Log.e("Tile with no renderer: " + Log.str(this));
|
||||
if (renderer == null) {
|
||||
renderer = makeRenderer();
|
||||
}
|
||||
|
||||
if(renderer == null) {
|
||||
Log.w("No renderer for tile "+Log.str(this));
|
||||
return;
|
||||
}
|
||||
|
||||
r.renderTile(context);
|
||||
renderer.renderTile(context);
|
||||
|
||||
if (doesReceiveShadow()) r.renderShadows(context);
|
||||
if (doesReceiveShadow()) renderer.renderShadows(context);
|
||||
|
||||
r.renderUnexploredFog(context);
|
||||
renderer.renderUnexploredFog(context);
|
||||
}
|
||||
|
||||
|
||||
protected abstract TileRenderer getRenderer();
|
||||
protected abstract TileRenderer makeRenderer();
|
||||
|
||||
|
||||
/**
|
||||
@@ -143,7 +149,7 @@ public abstract class Tile implements IonObjBlob {
|
||||
@DefaultImpl
|
||||
public void update(Level level, double delta)
|
||||
{
|
||||
getRenderer().update(delta);
|
||||
makeRenderer().update(delta);
|
||||
}
|
||||
|
||||
|
||||
@@ -220,7 +226,7 @@ public abstract class Tile implements IonObjBlob {
|
||||
* @return true if the tile is interactive and did something.
|
||||
*/
|
||||
@DefaultImpl
|
||||
public boolean onClick()
|
||||
public boolean onClick(World world)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class NullTile extends Tile {
|
||||
|
||||
|
||||
@Override
|
||||
protected TileRenderer getRenderer()
|
||||
protected TileRenderer makeRenderer()
|
||||
{
|
||||
return TileRenderer.NONE;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public abstract class TileBaseDoor extends TileSolid {
|
||||
|
||||
|
||||
@Override
|
||||
protected TileRenderer getRenderer()
|
||||
protected TileRenderer makeRenderer()
|
||||
{
|
||||
return renderer;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import mightypork.rogue.world.tile.TileType;
|
||||
import mightypork.rogue.world.tile.renderers.BasicTileRenderer;
|
||||
|
||||
|
||||
public abstract class TileBaseFloor extends TileWalkable {
|
||||
public abstract class TileBaseFloor extends TileWithItems {
|
||||
|
||||
private final BasicTileRenderer renderer;
|
||||
|
||||
@@ -21,7 +21,7 @@ public abstract class TileBaseFloor extends TileWalkable {
|
||||
|
||||
|
||||
@Override
|
||||
protected TileRenderer getRenderer()
|
||||
protected TileRenderer makeRenderer()
|
||||
{
|
||||
return renderer;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public abstract class TileBasePassage extends TileSolid {
|
||||
|
||||
|
||||
@Override
|
||||
protected TileRenderer getRenderer()
|
||||
protected TileRenderer makeRenderer()
|
||||
{
|
||||
return renderer;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import mightypork.gamecore.util.ion.IonInput;
|
||||
import mightypork.gamecore.util.ion.IonOutput;
|
||||
import mightypork.gamecore.util.math.color.Color;
|
||||
import mightypork.gamecore.util.math.color.pal.RGB;
|
||||
import mightypork.rogue.world.World;
|
||||
import mightypork.rogue.world.tile.TileModel;
|
||||
import mightypork.rogue.world.tile.TileType;
|
||||
|
||||
@@ -24,7 +25,7 @@ public abstract class TileBaseSecretDoor extends TileBaseDoor {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onClick()
|
||||
public boolean onClick(World world)
|
||||
{
|
||||
if (!locked) return false;
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ public abstract class TileBaseWall extends TileSolid {
|
||||
|
||||
|
||||
@Override
|
||||
public BasicTileRenderer getRenderer()
|
||||
public BasicTileRenderer makeRenderer()
|
||||
{
|
||||
return renderer;
|
||||
}
|
||||
|
||||
+7
-6
@@ -7,6 +7,8 @@ import java.util.Stack;
|
||||
import mightypork.gamecore.util.ion.IonInput;
|
||||
import mightypork.gamecore.util.ion.IonOutput;
|
||||
import mightypork.rogue.world.item.Item;
|
||||
import mightypork.rogue.world.item.Items;
|
||||
import mightypork.rogue.world.item.items.ItemMeat;
|
||||
import mightypork.rogue.world.level.Level;
|
||||
import mightypork.rogue.world.level.render.TileRenderContext;
|
||||
import mightypork.rogue.world.tile.DroppedItemRenderer;
|
||||
@@ -14,14 +16,14 @@ import mightypork.rogue.world.tile.Tile;
|
||||
import mightypork.rogue.world.tile.TileModel;
|
||||
|
||||
|
||||
public abstract class TileWalkable extends Tile {
|
||||
public abstract class TileWithItems extends Tile {
|
||||
|
||||
private final DroppedItemRenderer itemRenderer = new DroppedItemRenderer();
|
||||
|
||||
protected final Stack<Item> items = new Stack<>();
|
||||
|
||||
|
||||
public TileWalkable(TileModel model)
|
||||
public TileWithItems(TileModel model)
|
||||
{
|
||||
super(model);
|
||||
}
|
||||
@@ -30,7 +32,7 @@ public abstract class TileWalkable extends Tile {
|
||||
@Override
|
||||
public void renderExtra(TileRenderContext context)
|
||||
{
|
||||
if (!items.isEmpty()) {
|
||||
if (isExplored() && !items.isEmpty()) {
|
||||
itemRenderer.render(items.peek(), context);
|
||||
}
|
||||
}
|
||||
@@ -49,7 +51,7 @@ public abstract class TileWalkable extends Tile {
|
||||
{
|
||||
super.save(out);
|
||||
|
||||
out.writeSequence(items);
|
||||
Items.saveItems(out, items);
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +60,7 @@ public abstract class TileWalkable extends Tile {
|
||||
{
|
||||
super.load(in);
|
||||
|
||||
in.readSequence(items);
|
||||
Items.loadItems(in, items);
|
||||
}
|
||||
|
||||
|
||||
@@ -96,5 +98,4 @@ public abstract class TileWalkable extends Tile {
|
||||
{
|
||||
return !items.isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user