basic world data types with dummy data (wip 2)

This commit is contained in:
bixilon 2020-06-03 15:57:29 +02:00
parent faab6664c6
commit 00239e9f18
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
8 changed files with 117 additions and 6 deletions

View File

@ -8,12 +8,11 @@ import java.util.HashMap;
public class Chunk { public class Chunk {
private final HashMap<ChunkLocation, WorldBlock> blocks; private final HashMap<ChunkLocation, WorldBlock> blocks;
public Chunk() { public Chunk(HashMap<ChunkLocation, WorldBlock> blocks) {
blocks = new HashMap<>(); this.blocks = blocks;
} }
public WorldBlock getWorldBlock(ChunkLocation loc) { public WorldBlock getWorldBlock(ChunkLocation loc) {
//ToDo will return air if null
return blocks.get(loc); return blocks.get(loc);
} }

View File

@ -1,5 +1,7 @@
package de.bixilon.minosoft.game.datatypes; package de.bixilon.minosoft.game.datatypes;
import de.bixilon.minosoft.game.datatypes.blocks.Blocks;
import java.util.HashMap; import java.util.HashMap;
/** /**
@ -7,9 +9,13 @@ import java.util.HashMap;
*/ */
public class ChunkColumn { public class ChunkColumn {
private final HashMap<Byte, Chunk> chunks; private final HashMap<Byte, Chunk> chunks;
final int x;
final int z;
public ChunkColumn(int x, int z) { public ChunkColumn(int x, int z) {
chunks = new HashMap<>(); chunks = new HashMap<>();
this.x = x;
this.z = z;
} }
public WorldBlock getWorldBlock(byte x, short y, byte z) { public WorldBlock getWorldBlock(byte x, short y, byte z) {
@ -17,7 +23,14 @@ public class ChunkColumn {
throw new IllegalArgumentException(String.format("Invalid chunk location %s %s %s", x, y, z)); throw new IllegalArgumentException(String.format("Invalid chunk location %s %s %s", x, y, z));
} }
byte heightNumber = (byte) (y / 16); byte heightNumber = (byte) (y / 16);
return chunks.get(heightNumber).getWorldBlock(x, (byte) (y - (heightNumber * 16)), z); BlockPosition pos = new BlockPosition(this.x * 16 + x, y, this.z * 16 + z);
if (heightNumber == 0) {
return Blocks.getBlockInstance(Blocks.DIRT, pos);
} else {
return Blocks.getBlockInstance(Blocks.AIR, pos);
}
//ToDo
//return chunks.get(heightNumber).getWorldBlock(x, (byte) (y - (heightNumber * 16)), z);
} }
} }

View File

@ -19,6 +19,8 @@ public class World {
} }
public ChunkColumn getChunkColumn(ChunkColumnLocation loc) { public ChunkColumn getChunkColumn(ChunkColumnLocation loc) {
return chunks.get(loc); return new ChunkColumn(loc.getX(), loc.getZ());
//ToDo
//return chunks.get(loc);
} }
} }

View File

@ -3,5 +3,6 @@ package de.bixilon.minosoft.game.datatypes;
/** /**
* Basically raw chunk stuff. This is not a block like dirt (it can be). Can be dirt, a flower, fire, torch, sapling, fluid (water/lava), nether wart, banner, ... * Basically raw chunk stuff. This is not a block like dirt (it can be). Can be dirt, a flower, fire, torch, sapling, fluid (water/lava), nether wart, banner, ...
*/ */
public class WorldBlock { public interface WorldBlock {
BlockPosition getBlockPosition();
} }

View File

@ -0,0 +1,26 @@
package de.bixilon.minosoft.game.datatypes.blocks;
import de.bixilon.minosoft.game.datatypes.BlockPosition;
public class Air implements Block {
final BlockPosition position;
public Air(BlockPosition position) {
this.position = position;
}
@Override
public BlockPosition getBlockPosition() {
return position;
}
@Override
public int getLegacyId() {
return 0;
}
@Override
public String getLegacyIdentifier() {
return "minecraft:air";
}
}

View File

@ -0,0 +1,11 @@
package de.bixilon.minosoft.game.datatypes.blocks;
import de.bixilon.minosoft.game.datatypes.WorldBlock;
public interface Block extends WorldBlock {
String getLegacyIdentifier();
int getLegacyId();
}

View File

@ -0,0 +1,33 @@
package de.bixilon.minosoft.game.datatypes.blocks;
import de.bixilon.minosoft.game.datatypes.BlockPosition;
import java.lang.reflect.InvocationTargetException;
public enum Blocks {
DIRT(Dirt.class),
AIR(Air.class);
private final Class<? extends Block> clazz;
Blocks(Class<? extends Block> clazz) {
this.clazz = clazz;
}
public static Block getBlockInstance(Class<? extends Block> clazz, BlockPosition pos) {
try {
return clazz.getConstructor(BlockPosition.class).newInstance(pos);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException("Invalid block constructor!");
}
}
public static Block getBlockInstance(Blocks b, BlockPosition pos) {
return getBlockInstance(b.getClazz(), pos);
}
public Class<? extends Block> getClazz() {
return clazz;
}
}

View File

@ -0,0 +1,26 @@
package de.bixilon.minosoft.game.datatypes.blocks;
import de.bixilon.minosoft.game.datatypes.BlockPosition;
public class Dirt implements Block {
final BlockPosition position;
public Dirt(BlockPosition position) {
this.position = position;
}
@Override
public BlockPosition getBlockPosition() {
return position;
}
@Override
public int getLegacyId() {
return 0;
}
@Override
public String getLegacyIdentifier() {
return "minecraft:dirt";
}
}