Add invisible inventory

This commit is contained in:
IntegratedQuantum 2019-03-27 21:19:42 +01:00
parent 444d1ac927
commit 0c20ec6358
5 changed files with 108 additions and 16 deletions

View File

@ -40,6 +40,7 @@ import io.cubyz.blocks.Block;
import io.cubyz.blocks.BlockInstance;
import io.cubyz.entity.Entity;
import io.cubyz.entity.Player;
import io.cubyz.items.Inventory;
import io.cubyz.modding.ModLoader;
import io.cubyz.multiplayer.client.CubzClient;
import io.cubyz.ui.DebugGUI;
@ -68,7 +69,8 @@ public class Cubyz implements IGameLogic {
public static UISystem gameUI;
public static World world;
private int inventorySelection = 1; // Selected slot in inventory
private int inventorySelection = 0; // Selected slot in inventory
private static Inventory inventory;
private CubyzMeshSelectionDetector msd;
@ -117,6 +119,7 @@ public class Cubyz implements IGameLogic {
world.synchronousSeek(dx, dz);
int highestY = world.getHighestBlock(dx, dz);
world.getLocalPlayer().setPosition(new Vector3f(dx, highestY+2, dz));
inventory = new Inventory();
}
public static void requestJoin(String host) {
@ -373,8 +376,8 @@ public class Cubyz implements IGameLogic {
breakCooldown = 10;
BlockInstance bi = msd.getSelectedBlockInstance();
if (bi != null && bi.getBlock().getHardness() != -1f) {
inventorySelection = bi.getID();// To be able to build that block again
world.removeBlock(bi.getX(), bi.getY(), bi.getZ());
inventory.addBlock(bi.getBlock(), 1);
}
}
}
@ -383,9 +386,10 @@ public class Cubyz implements IGameLogic {
if (buildCooldown == 0) {
buildCooldown = 10;
Vector3i pos = msd.getEmptyPlace(ctx.getCamera().getPosition());
Block b = world.getBlocks()[inventorySelection]; // TODO: add inventory
Block b = inventory.getBlock(inventorySelection); // TODO: add inventory
if (b != null && pos != null) {
world.placeBlock(pos.x, pos.y, pos.z, b);
inventory.addBlock(b, -1);
}
}
}

View File

@ -0,0 +1,60 @@
package io.cubyz.items;
import io.cubyz.blocks.Block;
public class Inventory {
private ItemStack[] items = new ItemStack[32]; // First 8 item stacks are the hotbar
public void addItem(Item i, int amount) {
for(int j = 0; j < items.length; j++) {
if(items[j] != null && items[j].getItem() == i && !items[j].filled()) {
amount -= items[j].add(amount);
if(items[j].empty()) {
items[j] = null;
}
if(amount == 0) {
return;
}
}
}
for(int j = 0; j < items.length; j++) {
if(items[j] == null) {
items[j] = new ItemStack(i);
amount -= items[j].add(amount);
if(amount == 0) {
return;
}
}
}
// TODO: Consider a full inventory
}
public void addBlock(Block b, int amount) {
for(int j = 0; j < items.length; j++) {
if(items[j] != null && items[j].getBlock() == b && !items[j].filled()) {
amount -= items[j].add(amount);
if(items[j].empty()) {
items[j] = null;
}
if(amount == 0) {
return;
}
}
}
for(int j = 0; j < items.length; j++) {
if(items[j] == null) {
items[j] = new ItemStack(b);
amount -= items[j].add(amount);
if(amount == 0) {
return;
}
}
}
}
public Block getBlock(int selection) {
if(items[selection] == null)
return null;
return items[selection].getBlock();
}
}

View File

@ -65,5 +65,11 @@ public class Item implements IRegistryElement {
public Resource getRegistryID() {
return id;
}
@Override
public void setID(int ID) {
// TODO Auto-generated method stub
}
}

View File

@ -1,12 +0,0 @@
package io.cubyz.items;
import io.cubyz.blocks.Block;
public class ItemBlock extends Item {
public ItemBlock(Block block) {
this.fullTexturePath = "block/" + block.getTexture();
this.itemDisplayName = block.getClass().getSimpleName();
}
}

View File

@ -6,16 +6,25 @@ import org.jungle.Texture;
import org.jungle.util.Material;
import org.jungle.util.OBJLoader;
@SuppressWarnings("deprecation")
import io.cubyz.blocks.Block;
public class ItemStack {
private Item item;
private Block block;
private Spatial spatial;
int number = 0;
public ItemStack(Item item) {
this.item = item;
}
public ItemStack(Block block) {
this.block = block;
item = new Item();
item.setTexture(block.getTexture());
}
public void update() {}
public Mesh getMesh() {
@ -36,10 +45,35 @@ public class ItemStack {
return item._meshCache;
}
public boolean filled() {
return number >= item.stackSize;
}
public boolean empty() {
return number <= 0;
}
public int add(int number) {
this.number += number;
if(this.number > item.stackSize) {
number = number-this.number+item.stackSize;
this.number = item.stackSize;
}
if(this.number < 0) {
number = number-this.number;
this.number = 0;
}
return number;
}
public Item getItem() {
return item;
}
public Block getBlock() {
return block;
}
public Spatial getSpatial() {
if (spatial == null) {
spatial = new Spatial(getMesh());