I'm having some issues with inheritance for one of my libGDX projects, I'm making a game similar to (very stripped down) Terraria or Starbound and I am using the TiledMap to load the world, everythings generating fine but the problem comes when I try to place blocks and from my experimenting it seems there's an issue with inheritance that I can't seem to fix. I have a Block class (extends Cell) and and InventoryObject class with two children, InventoryBlock and InventoryItem. I tried to implement the blocks similar to the Minecraft code, and I am having some troubles, destroying blocks works fine and I have used a similar system so I'm guessing it's something simple that I'm over looking. Any help would be appreciated :)
Block.java (parts of)
public class Block extends Cell{
private String name;
private TextureRegion tex;
private boolean hasInventory;
private int maxStackSize = 200;
private static TiledMapTileSet tileset;
private InventoryObject dropped;
Block[] blocks = new Block[256];
public Block(TiledMap map, Material material){
super();
System.out.println(map == null);
tileset = map.getTileSets().getTileSet(0);
setTile(tileset.getTile(material.getVal()));
}
//each of these e.g. BlockStone, BlockDirt, is an individual class.
public static final Block stone = new BlockStone(Play.map, Material.STONE);
public static final Block dirt = new BlockDirt(Play.map, Material.DIRT);
public static final Block grass = new BlockGrass(Play.map, Material.GRASS);
public static final Block air = new BlockAir(Play.map, Material.AIR);
public static final Block leaf = new BlockLeaf(Play.map, Material.LEAF);
public static final Block log = new BlockLog(Play.map, Material.LOG);
InventoryObject.java
public class InventoryObject extends Texture{
private Texture tex;
private int MaxStackSize;
private String name;
private Block block;
public InventoryObject(String name, int MaxStackSize, Block block){
super(Gdx.files.internal("tiles/" + name + ".jpg"));
tex = new Texture(Gdx.files.internal("tiles/" + name + ".jpg"));
this.MaxStackSize = MaxStackSize;
this.name = name;
this.block = block;
//System.out.println(block);
}//getters and setters below
InventoryBlock.java public class InventoryBlock extends InventoryObject{
private int maxStackSize;
public InventoryBlock(String name, int maxStackSize, Block block){
super(name, maxStackSize, block);
setBlock(block);
}
InventoryBlock[] InventoryBlocks = new InventoryBlock[256];
public static final InventoryBlock stone = new InventoryBlockStone("Stone", 200, Block.stone);
public static final InventoryBlock dirt = new InventoryBlockDirt("Dirt", 200, Block.dirt);
public static final InventoryBlock grass = new InventoryBlockGrass("Grass", 200, Block.grass);
public static final InventoryBlock leaf = new InventoryBlockLeaf("Leaf", 200, Block.leaf);
public static final InventoryBlock log = new InventoryBlockLog("Log", 200, Block.log);
I'm wondering if it's an issue with how I'm parsing the block into the InventoryBlocks, When I try to access an InventoryBlock's corresponding block to place it, it's just returning null. any ideas?
This is the create block method:
public void placeBlock(int x, int y, TiledMapTileLayer layer, Inventory inventory){
if(layer.getCell(x, y).getTile() != null && layer.getCell(x, y).getTile().getId() == Block.air.getTile().getId()){
if(inventory.getStack(player.getSelected()) != null){
InventoryBlock block = (InventoryBlock)inventory.getStack(player.getSelected()).getObj();
layer.setCell(x, y, block.getBlock());
inventory.removeItem(player.getSelected(), 1);
}
}
}
The InventoryBlockStone/Dirt etc. simply extend the InventoryBlock class and have a constructor calling super(String name, int maxStackSize, Block block).
I tried echoing out the name of the InventoryBlock and that works fine, so It's just the Block object which is null. Sorry for the long question but I'm stumped, If you need any more information just ask and I'll add it to the question :)
Thanks!
UPDATE: I tried printing the value of Block.stone
in the InventoryBlock constructor and it's returning null so could it be something to do with the order the classes are called? Any help would be appreciated
STONE
,DIRT
, etc, not make a class for every single thing, it is such an overkill and makes something really simple, real hard. Pretty sure you need to redo this... Also why does theBlock
contain an array of 256 blocks ? As far as your question goes, what does theBlockStone
constructor look like ? – Shiro Sep 2 at 4:59BlockStone
creation process. IsPlay.map
initialized when you call theBlockStone
constructor ? I think that should be the problem here. You should show us theBlockStone
constructor as well. – Shiro Sep 2 at 5:13