diff options
Diffstat (limited to 'src/main/java/net/tylermurphy/Minecraft/Tick')
4 files changed, 172 insertions, 0 deletions
diff --git a/src/main/java/net/tylermurphy/Minecraft/Tick/BlockUpdate.java b/src/main/java/net/tylermurphy/Minecraft/Tick/BlockUpdate.java new file mode 100755 index 0000000..6c5fef1 --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Tick/BlockUpdate.java @@ -0,0 +1,53 @@ +package net.tylermurphy.Minecraft.Tick;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import net.tylermurphy.Minecraft.Chunk.Cube;
+import net.tylermurphy.Minecraft.Scene.World;
+
+public class BlockUpdate {
+
+ public int x,y,z;
+ public byte old_block_id;
+ public byte new_block_id;
+ private long delay_of_update, creation_of_update;
+
+ private static List<BlockUpdate> current_block_updates = new ArrayList<BlockUpdate>();
+ private static List<BlockUpdate> waiting_block_updates = new ArrayList<BlockUpdate>();
+
+ public static void createBlockUpdate(int x, int y, int z, byte old_block_id, byte new_block_id) {
+ BlockUpdate update = new BlockUpdate();
+ update.x = x;
+ update.y = y;
+ update.z = z;
+ update.old_block_id = old_block_id;
+ update.new_block_id = new_block_id;
+ if(new_block_id != -1)
+ update.delay_of_update = Cube.getBlock(new_block_id).tick_update_delay*1000000;
+ else
+ update.delay_of_update = Cube.getBlock("water").tick_update_delay*1000000;
+ update.creation_of_update = System.nanoTime();
+ waiting_block_updates.add(update);
+ }
+
+ public static List<BlockUpdate> loadBirthedUpdates() {
+ int j = waiting_block_updates.size();
+ for(int i = 0; i < j; i++) {
+ BlockUpdate update = waiting_block_updates.get(i);
+ if(World.getBlock(update.x, update.y, update.z) != update.new_block_id) continue;
+ if(System.nanoTime() - update.creation_of_update >= update.delay_of_update) {
+ waiting_block_updates.remove(i);
+ j--;
+ i--;
+ current_block_updates.add(update);
+ }
+ }
+ return current_block_updates;
+ }
+
+ public static void purgeUpdates() {
+ current_block_updates.clear();
+ }
+
+}
diff --git a/src/main/java/net/tylermurphy/Minecraft/Tick/GameTicks/GrassTick.java b/src/main/java/net/tylermurphy/Minecraft/Tick/GameTicks/GrassTick.java new file mode 100755 index 0000000..f7201f1 --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Tick/GameTicks/GrassTick.java @@ -0,0 +1,50 @@ +package net.tylermurphy.Minecraft.Tick.GameTicks;
+
+import net.tylermurphy.Minecraft.Scene.World;
+import net.tylermurphy.Minecraft.Tick.BlockUpdate;
+
+public class GrassTick {
+
+ private static boolean foundGrass(int x, int y, int z)
+ {
+ for(int a = x-1; a<x+2; a++) {
+ for(int b = y-1; b<y+2; b++) {
+ for(int c = z-1; c<z+2; c++) {
+ if(x == a && c == z) continue;
+ if(World.getBlock(a, b, c) == 0) return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ private static void searchForDirt(int x, int y, int z)
+ {
+ for(int a = x-1; a<x+2; a++)
+ for(int b = y-1; b<y+2; b++)
+ for(int c = z-1; c<z+2; c++) {
+ if(x == a && c == z)
+ continue;
+ if(World.getBlock(a, b, c) == 1 && World.getBlock(a, b+1, c) == -1) {
+ BlockUpdate.createBlockUpdate(a, b, c, (byte)1, (byte)1);
+ }
+ }
+ }
+
+ public static void doGrassTick(BlockUpdate update) {
+ if(update.new_block_id == 1) {
+ int rand = (int)(Math.random()*100);
+ if(rand>95) {
+ if(World.getBlock(update.x,update.y+1,update.z) == -1 && foundGrass(update.x,update.y,update.z)) {
+ World.setBlock(update.x,update.y,update.z, (byte)0);
+ }
+ } else {
+ BlockUpdate.createBlockUpdate(update.x,update.y,update.z, (byte)1, (byte)1);
+ }
+ }
+ if(update.new_block_id == 0) {
+ searchForDirt(update.x,update.y,update.z);
+ }
+ }
+
+}
diff --git a/src/main/java/net/tylermurphy/Minecraft/Tick/GameTicks/WaterTick.java b/src/main/java/net/tylermurphy/Minecraft/Tick/GameTicks/WaterTick.java new file mode 100755 index 0000000..01b008c --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Tick/GameTicks/WaterTick.java @@ -0,0 +1,45 @@ +package net.tylermurphy.Minecraft.Tick.GameTicks;
+
+import net.tylermurphy.Minecraft.Scene.World;
+import net.tylermurphy.Minecraft.Tick.BlockUpdate;
+
+public class WaterTick {
+
+ public static void doWaterTick(BlockUpdate update) {
+ if(update.new_block_id == 17) {
+ if(World.getBlock(update.x, update.y-1, update.z) == -1) {
+ World.setBlock(update.x, update.y-1, update.z, (byte)17);
+ }
+ if(World.getBlock(update.x-1, update.y, update.z) == -1) {
+ World.setBlock(update.x-1, update.y, update.z, (byte)17);
+ }
+ if(World.getBlock(update.x+1, update.y, update.z) == -1) {
+ World.setBlock(update.x+1, update.y, update.z, (byte)17);
+ }
+ if(World.getBlock(update.x, update.y, update.z-1) == -1) {
+ World.setBlock(update.x, update.y, update.z-1, (byte)17);
+ }
+ if(World.getBlock(update.x, update.y, update.z+1) == -1) {
+ World.setBlock(update.x, update.y, update.z+1, (byte)17);
+ }
+ }
+ if(update.new_block_id == -1) {
+ if(World.getBlock(update.x, update.y+1, update.z) == 17) {
+ World.setBlock(update.x, update.y+1, update.z, (byte)17);
+ }
+ if(World.getBlock(update.x-1, update.y, update.z) == 17) {
+ World.setBlock(update.x-1, update.y, update.z, (byte)17);
+ }
+ if(World.getBlock(update.x+1, update.y, update.z) == 17) {
+ World.setBlock(update.x+1, update.y, update.z, (byte)17);
+ }
+ if(World.getBlock(update.x, update.y, update.z-1) == 17) {
+ World.setBlock(update.x, update.y, update.z-1, (byte)17);
+ }
+ if(World.getBlock(update.x, update.y, update.z+1) == 17) {
+ World.setBlock(update.x, update.y, update.z+1, (byte)17);
+ }
+ }
+ }
+
+}
diff --git a/src/main/java/net/tylermurphy/Minecraft/Tick/TickManager.java b/src/main/java/net/tylermurphy/Minecraft/Tick/TickManager.java new file mode 100755 index 0000000..c705d37 --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Tick/TickManager.java @@ -0,0 +1,24 @@ +package net.tylermurphy.Minecraft.Tick;
+
+import java.util.List;
+
+import net.tylermurphy.Minecraft.Tick.GameTicks.GrassTick;
+import net.tylermurphy.Minecraft.Tick.GameTicks.WaterTick;
+
+
+public class TickManager {
+
+ public static void doTick() {
+ List<BlockUpdate> block_updates = BlockUpdate.loadBirthedUpdates();
+ for(BlockUpdate update : block_updates) {
+ if(update.new_block_id == 17 || update.new_block_id == -1) {
+ WaterTick.doWaterTick(update);
+ }
+ if(update.new_block_id == 0 || update.new_block_id == 1) {
+ GrassTick.doGrassTick(update);
+ }
+ }
+ BlockUpdate.purgeUpdates();
+ }
+
+}
|