From 180aad05decc7eefa87e4e45d6747c48f40e5361 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Mon, 17 Apr 2023 12:12:01 -0400 Subject: save --- .../tylermurphy/Minecraft/Tick/BlockUpdate.java | 53 ++++++++++++++++++++++ .../Minecraft/Tick/GameTicks/GrassTick.java | 50 ++++++++++++++++++++ .../Minecraft/Tick/GameTicks/WaterTick.java | 45 ++++++++++++++++++ .../tylermurphy/Minecraft/Tick/TickManager.java | 24 ++++++++++ 4 files changed, 172 insertions(+) create mode 100755 src/main/java/net/tylermurphy/Minecraft/Tick/BlockUpdate.java create mode 100755 src/main/java/net/tylermurphy/Minecraft/Tick/GameTicks/GrassTick.java create mode 100755 src/main/java/net/tylermurphy/Minecraft/Tick/GameTicks/WaterTick.java create mode 100755 src/main/java/net/tylermurphy/Minecraft/Tick/TickManager.java (limited to 'src/main/java/net/tylermurphy/Minecraft/Tick') 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 current_block_updates = new ArrayList(); + private static List waiting_block_updates = new ArrayList(); + + 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 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; a95) { + 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 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(); + } + +} -- cgit v1.2.3-freya