From b5edf54e1ffb539aab1b76f7fa543e38c248c0aa Mon Sep 17 00:00:00 2001 From: Soper Date: Wed, 19 May 2021 03:05:32 -0400 Subject: [PATCH] mortar now does some failure calculation, need to complete todos for fireShell method --- src/xyz/soper/arty/item/Mortar.java | 89 ++++++++++++++++------ src/xyz/soper/arty/util/MortarHandler.java | 15 +++- 2 files changed, 79 insertions(+), 25 deletions(-) diff --git a/src/xyz/soper/arty/item/Mortar.java b/src/xyz/soper/arty/item/Mortar.java index a40aa1a..db6e496 100644 --- a/src/xyz/soper/arty/item/Mortar.java +++ b/src/xyz/soper/arty/item/Mortar.java @@ -1,36 +1,50 @@ package xyz.soper.arty.item; import org.bukkit.ChatColor; -import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Nameable; import org.bukkit.block.Block; import org.bukkit.util.Vector; +import java.util.Random; + public class Mortar { - public Location location; + /**The block that represents this mortar.*/ + private final Block block; + + /** + * The direction this mortar is facing. The shell will ultimately be fired in this direction. + */ + private Vector direction; public static final String BASIC_MORTAR_NAME = ChatColor.GRAY + "Basic Mortar"; public static final String NORMAL_MORTAR_NAME = ChatColor.GRAY + "Mortar"; public static final String INCENDIARY_CAPABLE_MORTAR_NAME = ChatColor.GRAY + "Incendiary Capable Mortar"; public static final String REINFORCED_MORTAR_NAME = ChatColor.GRAY + "Reinforced Mortar"; - public int failMultiplier; //Multiplier to a fail chance. - public double jamChance; //If a fail happens, chance of it being a jam. - public double dudChance; //If a fail happens, chance of it being a dud shell (that fires but has no impact explosion) - public double catastrophicFailChance; //If a fail happens, chance of it being catastrophic. - public double maxVelocity; //Max initial velocity supported by this mortar - public double velocityMultiplier; //Multiplier applied to the shell's velocity - public boolean incendiaryCapable; //Is the mortar fit to fire an incendiary-type shell? - - private Vector direction; - - private Block block; + /**Multiplier to a fail chance.*/ + public int failMultiplier; + /**If a fail happens, chance of it being a jam.*/ + public double jamChance; + /**If a fail happens, chance of it being a dud shell (that fires but has no explosion on impact)*/ + public double dudChance; + /**If a fail happens, chance of it being catastrophic.*/ + public double catastrophicFailChance; + /**Max initial velocity supported by this mortar*/ + public double maxVelocity; + /**Multiplier applied to the shell's velocity*/ + public double velocityMultiplier; + /**Is the mortar fit to fire an incendiary-type shell?*/ + public boolean incendiaryCapable; + /** + * Creates a new Mortar block wrapper using the specified block. + * WARNING: This does NOT check if the block is actually a mortar. + * @param mortar Placed block in the world that represents a mortar. + */ public Mortar(Block mortar){ block = mortar; - location = mortar.getLocation(); Nameable brewingStandMortar = (Nameable) mortar.getState(); String mortarName = brewingStandMortar.getCustomName(); @@ -72,6 +86,42 @@ public class Mortar { } } + /** + * Returns the Block that represents this mortar object + * @return Mortar as a Block + */ + public Block getBlock() { + return block; + } + + /** + * Determines type of failure the provided shell and this mortar experienced + * @param shell Shell that interacted with this mortar object + * @return + * 0 for non-failure; + * 1 for non-firing jam; + * 2 for no impact explosion dud; + * 3 for catastrophic failure + */ + public int getFailureType(Shell shell){ + if(isFailure(shell)){ + Random random = new Random(block.getWorld().getSeed() + System.currentTimeMillis()); + double randDouble = random.nextDouble(); + //TODO: compare random double and create an algo to determine type of failure. + } + else return 0; + } + + /** + * Determines if the shell and mortar interaction failed + * @param shell Shell that interacted with this mortar object + * @return True if the interaction failed (did not fire properly) + */ + private boolean isFailure(Shell shell){ + Random random = new Random(block.getWorld().getSeed() + System.currentTimeMillis()); + return random.nextDouble() < failMultiplier*shell.failChance; + } + public static boolean isMortar(Block block){ if(block != null && block.getBlockData().getMaterial() == Material.BREWING_STAND @@ -82,7 +132,8 @@ public class Mortar { } private static boolean isStringMortar(String string){ - if(string.equals(BASIC_MORTAR_NAME) + if(string != null + || string.equals(BASIC_MORTAR_NAME) || string.equals(NORMAL_MORTAR_NAME) || string.equals(REINFORCED_MORTAR_NAME) || string.equals(INCENDIARY_CAPABLE_MORTAR_NAME)){ @@ -90,12 +141,4 @@ public class Mortar { } return false; } - - /** - * Returns the Block that represents this mortar object - * @return Mortar as a Block - */ - public Block getBlock() { - return block; - } } diff --git a/src/xyz/soper/arty/util/MortarHandler.java b/src/xyz/soper/arty/util/MortarHandler.java index f0a3f32..4d65a1b 100644 --- a/src/xyz/soper/arty/util/MortarHandler.java +++ b/src/xyz/soper/arty/util/MortarHandler.java @@ -18,8 +18,18 @@ public class MortarHandler { } public void fireShell(Shell shell, ProjectileSource shooter){ + //TODO: Use linkProjectile method and finalize fireShell method to create a proper non-debug fireShell method. + //As it stands, this method creates a shell with no actual data so the Shell wrapper uses default values. + + + Vector debugVector = new Vector(1, 1, 0).normalize(); - mortar.getBlock().getWorld().playSound(mortar.location, "entity.generic.explode", 1, 1.5F); + mortar.getBlock().getWorld().playSound( + mortar.getBlock().getLocation(), + "entity.generic.explode", + 1, + 1.5F + ); Location mortarLocation = mortar.getBlock().getLocation().add(.5, 1, .5); Arrow shellProjectile = mortar.getBlock().getWorld().spawn(mortarLocation, Arrow.class); shellProjectile.setVelocity(debugVector.multiply(shell.baseVelocity)); @@ -27,7 +37,8 @@ public class MortarHandler { shellProjectile.setCustomName("shell"); mortar.getBlock().getWorld().spawnParticle(Particle.SMOKE_NORMAL, mortarLocation, 500, .5, 0, .5, .5); //TODO: Experiment with spawnParticle offset parameters. This may create more realistic smoke. - if(shooter instanceof Player) ((Player) shooter).sendMessage("DEBUG: Shell fired with a velocity " + shell.baseVelocity); + if(shooter instanceof Player) + ((Player) shooter).sendMessage("DEBUG: Shell fired with a velocity " + shell.baseVelocity); } } \ No newline at end of file