Refactor Falling Blocks and Fast Food

This commit is contained in:
Thanos Paravantis 2016-07-05 18:14:31 +03:00
parent ba02c94e08
commit f64234e4d1
3 changed files with 82 additions and 75 deletions

View File

@ -39,7 +39,7 @@ public class ChallengeBlockLobbers extends Challenge
private static final int MAP_HEIGHT = 1;
private static final int MAP_MIN_SIZE = 11;
private static final int WOOL_DATA_RANGE = 16;
private static final int INVENTORY_HOTBAR_SIZE = 9;
private static final int INVENTORY_HOTBAR_SIZE = 8;
private static final double FALLING_BLOCK_HEIGHT_ADD = 0.4;
private static final double FALLING_BLOCK_VECTOR_MULTIPLY = 1.5;

View File

@ -41,32 +41,23 @@ import nautilus.game.arcade.game.games.mineware.challenge.NumberTracker;
*/
public class ChallengeFallingBlocks extends Challenge implements NumberTracker
{
// The spawn chances of a falling block.
private double _spawnChance = 20.0;
private static final int MAP_SPAWN_SHIFT = 2;
private static final int MAP_HEIGHT = 1;
private static final byte STONE_DATA = 5;
// The amount of times the spawn chance will increment after each wave.
private double _incrementRate = 5.0;
private static final double SPAWN_CHANCE = 0.2;
private static final double SPAWN_CHANCE_INCREMENT = 0.05;
private static final double SPAWN_HEIGHT = 13.0;
private static final int NEXT_WAVE = 5;
private static final float WAVE_SOUND_VOLUME = 1.0F;
private static final float WAVE_SOUND_PITCH = 1.5F;
private static final double SPAWN_CHANCE_MAX = 100;
// The map height where all blocks will spawn.
private double _waveHeight = 13.0;
private static final int PLAYER_CAMP_MAX_HEIGHT = 3;
private static final byte ADDITIONAL_BLOCK_DATA = 2;
private static final float BLOCK_HITBOX_GROW = 0.7F;
// The amount of seconds until the next anvil wave appears.
private long _nextWave = 5;
// The remaining time until the next anvil wave appears.
private int _time;
// The number of waves completed until the challenge is ended.
private int _wavesCompleted;
// After each wave, the block spawn chance increases.
private double _modifiedSpawnChance;
// The remaining number of blocks before they disappear.
private Set<Block> _remaining = new HashSet<>();
// The array of materials used to generate random falling blocks.
private Material[] _materials = {
private static final Material[] MATERIALS = {
Material.GRASS,
Material.DIRT,
Material.STONE,
@ -82,16 +73,15 @@ public class ChallengeFallingBlocks extends Challenge implements NumberTracker
Material.EMERALD_ORE,
Material.FURNACE };
// The array of materials used to generate random floor.
private Material[] _floor = { Material.GRASS, Material.DIRT, Material.STONE, Material.COBBLESTONE };
// The array of sounds used to play when a new wave is being deployed.
private Sound[] _sounds = { Sound.DIG_GRASS, Sound.DIG_GRAVEL, Sound.DIG_SAND, Sound.DIG_SNOW, Sound.DIG_STONE, Sound.DIG_WOOD, Sound.DIG_WOOL };
private static final Material[] FLOOR = { Material.GRASS, Material.DIRT, Material.STONE, Material.COBBLESTONE };
private static final Sound[] SOUNDS = { Sound.DIG_GRASS, Sound.DIG_GRAVEL, Sound.DIG_SAND, Sound.DIG_SNOW, Sound.DIG_STONE, Sound.DIG_WOOD, Sound.DIG_WOOL };
private int _modifiedNextWave;
private double _modifiedSpawnChance;
private int _wavesCompleted;
private Set<Block> _remaining = new HashSet<>();
private int _arenaStartSize;
private Map<Player, Integer> _waveTracker = new HashMap<>();
private boolean _trackedWave = false;
public ChallengeFallingBlocks(BawkBawkBattles host)
@ -106,20 +96,22 @@ public class ChallengeFallingBlocks extends Challenge implements NumberTracker
Settings.setUseMapHeight();
}
@Override
public ArrayList<Location> createSpawns()
{
ArrayList<Location> spawns = new ArrayList<Location>();
int size = getArenaSize() - 2;
int size = getArenaSize() - MAP_SPAWN_SHIFT;
for (Location location : circle(getCenter(), size, 1, true, false, 0))
{
spawns.add(location.add(0.5, 1.1, 0.5));
spawns.add(location.add(0.5, MAP_HEIGHT, 0.5));
}
return spawns;
}
@Override
public void createMap()
{
@ -127,7 +119,7 @@ public class ChallengeFallingBlocks extends Challenge implements NumberTracker
for (Location location : circle(getCenter(), _arenaStartSize, 1, false, false, 0))
{
Material material = _floor[UtilMath.r(_floor.length)];
Material material = UtilMath.randomElement(FLOOR);
Block block = location.getBlock();
setBlock(block, material);
@ -135,7 +127,7 @@ public class ChallengeFallingBlocks extends Challenge implements NumberTracker
{
if (UtilMath.random.nextBoolean())
{
setData(block, (byte) 5);
setData(block, STONE_DATA);
}
}
@ -146,6 +138,9 @@ public class ChallengeFallingBlocks extends Challenge implements NumberTracker
@Override
public void onStart()
{
_modifiedNextWave = NEXT_WAVE;
_modifiedSpawnChance = SPAWN_CHANCE;
initializeWaveTracker();
startWavesTask();
startWaveTimerTask();
@ -154,10 +149,9 @@ public class ChallengeFallingBlocks extends Challenge implements NumberTracker
@Override
public void onEnd()
{
_time = (int) _nextWave;
_wavesCompleted = 0;
_modifiedSpawnChance = _spawnChance;
_trackedWave = false;
_arenaStartSize = 0;
for (Block block : _remaining)
{
@ -271,7 +265,7 @@ public class ChallengeFallingBlocks extends Challenge implements NumberTracker
createWave();
}
}.runTaskTimer(Host.getArcadeManager().getPlugin(), 20 * _nextWave, 20 * _nextWave);
}.runTaskTimer(Host.getArcadeManager().getPlugin(), NEXT_WAVE * TICK_MULTIPLIER, NEXT_WAVE * TICK_MULTIPLIER);
}
private void startWaveTimerTask()
@ -287,13 +281,13 @@ public class ChallengeFallingBlocks extends Challenge implements NumberTracker
return;
}
if (_time <= 0)
if (_modifiedNextWave == 0)
{
_time = (int) _nextWave;
_modifiedNextWave = NEXT_WAVE;
}
int wave = _wavesCompleted + 1;
String time = C.cWhite + C.Bold + _time;
String time = C.cWhite + C.Bold + _modifiedNextWave;
if (wave > 1)
{
@ -304,32 +298,33 @@ public class ChallengeFallingBlocks extends Challenge implements NumberTracker
UtilTextBottom.display(C.cYellow + C.Bold + "First Wave: " + time, UtilServer.getPlayers());
}
_time--;
_modifiedNextWave--;
}
}.runTaskTimer(Host.getArcadeManager().getPlugin(), 0L, 20L);
}.runTaskTimer(Host.getArcadeManager().getPlugin(), 0L, TICK_MULTIPLIER);
}
private void createWave()
{
_trackedWave = false;
if (_modifiedSpawnChance < 100.0)
_modifiedSpawnChance += _incrementRate;
if (_modifiedSpawnChance < SPAWN_CHANCE_MAX)
_modifiedSpawnChance += SPAWN_CHANCE_INCREMENT;
_wavesCompleted++;
Sound nextSound = _sounds[UtilMath.r(_sounds.length)];
Sound nextSound = UtilMath.randomElement(SOUNDS);
for (Player player : getPlayersAlive())
{
player.playSound(player.getLocation(), nextSound, 1.0F, 1.5F);
player.playSound(player.getLocation(), nextSound, WAVE_SOUND_VOLUME, WAVE_SOUND_PITCH);
}
Location center = getCenter().add(0, _waveHeight, 0);
Location center = getCenter().add(0, SPAWN_HEIGHT, 0);
for (Location location : UtilShapes.getCircle(center, false, _arenaStartSize))
{
if (Math.random() * 100 <= _modifiedSpawnChance)
if (Math.random() <= _modifiedSpawnChance)
{
createFallingBlock(location);
}
@ -339,17 +334,19 @@ public class ChallengeFallingBlocks extends Challenge implements NumberTracker
{
Location camp = player.getLocation();
if (camp.getY() >= getCenter().getY() + 1 && camp.getY() <= getCenter().getY() + 3)
if (camp.getY() >= getCenter().getY() + 1 && camp.getY() <= getCenter().getY() + PLAYER_CAMP_MAX_HEIGHT)
{
createFallingBlock(new Location(Host.WorldData.World, camp.getX(), getCenter().getY() + _waveHeight, camp.getZ()));
createFallingBlock(new Location(Host.WorldData.World, camp.getX(), getCenter().getY() + SPAWN_HEIGHT, camp.getZ()));
}
}
}
@SuppressWarnings("deprecation")
private void createFallingBlock(Location location)
{
Material material = UtilMath.randomElement(_materials);
Material material = UtilMath.randomElement(MATERIALS);
World world = location.getWorld();
FallingBlock block = world.spawnFallingBlock(location, material, (byte) 0);
@ -357,10 +354,10 @@ public class ChallengeFallingBlocks extends Challenge implements NumberTracker
if ((material == Material.SMOOTH_BRICK || material == Material.DIRT) && UtilMath.random.nextBoolean())
{
block = world.spawnFallingBlock(location, material, (byte) 2);
block = world.spawnFallingBlock(location, material, ADDITIONAL_BLOCK_DATA);
}
Host.Manager.GetProjectile().AddThrow(block, null, Host, -1, true, false, false, true, 0.7F);
Host.Manager.GetProjectile().AddThrow(block, null, Host, -1, true, false, false, true, BLOCK_HITBOX_GROW);
}
@Override

View File

@ -35,7 +35,20 @@ import nautilus.game.arcade.game.games.mineware.challenge.ChallengeType;
*/
public class ChallengeFastFood extends Challenge
{
private Material[] _food = {
private static final int MAP_SPAWN_SHIFT = 3;
private static final int MAP_HEIGHT = 1;
private static final int INVENTORY_HOTBAR_SIZE = 8;
private static final int RAW_FISH_DATA_RANGE = 3;
private static final int RANDOM_FOOD_AMOUNT = 5;
private static final int FOOD_THROW_COOLDOWN = 100;
private static final double FOOD_THROW_EYE_LOCATION_HEIGHT_SUBTRACT = 0.5;
private static final double FOOD_THROW_CREATE_DIRT_CHANCE = 0.3;
private static final int LONG_GRASS_DATA_RANGE = 2;
private static final int RED_ROSE_DATA_RANGE = 8;
private static final Material[] FOOD = {
Material.APPLE,
Material.BREAD,
Material.GRILLED_PORK,
@ -50,7 +63,7 @@ public class ChallengeFastFood extends Challenge
Material.BAKED_POTATO,
Material.PUMPKIN_PIE };
private Material[] _flowers = { Material.LONG_GRASS, Material.YELLOW_FLOWER, Material.RED_ROSE };
private static final Material[] FLOWERS = { Material.LONG_GRASS, Material.YELLOW_FLOWER, Material.RED_ROSE };
private int _itemSeperator = 0;
@ -70,11 +83,11 @@ public class ChallengeFastFood extends Challenge
public ArrayList<Location> createSpawns()
{
ArrayList<Location> spawns = new ArrayList<Location>();
int size = getArenaSize() - 3;
int size = getArenaSize() - MAP_SPAWN_SHIFT;
for (Location location : circle(getCenter(), size, 1, true, false, 0))
{
spawns.add(location.add(0.5, 1.1, 0.5));
spawns.add(location.add(0.5, MAP_HEIGHT, 0.5));
}
return spawns;
@ -98,7 +111,7 @@ public class ChallengeFastFood extends Challenge
for (Player player : getPlayersAlive())
{
for (int i = 0; i < 9; i++)
for (int i = 0; i <= INVENTORY_HOTBAR_SIZE; i++)
{
player.getInventory().setItem(i, getRandomFood());
}
@ -124,7 +137,7 @@ public class ChallengeFastFood extends Challenge
if (!isPlayerValid(player))
return;
if (UtilEvent.isAction(event, ActionType.L) && !Data.isDone(player))
if (UtilEvent.isAction(event, ActionType.L))
{
if (event.getItem() != null)
{
@ -160,8 +173,7 @@ public class ChallengeFastFood extends Challenge
if (!item.isValid() || item.isDead() || item.isOnGround() || item.getItemStack().getType() == Material.INK_SACK)
continue;
UtilParticle.PlayParticle(ParticleType.INSTANT_SPELL, item.getLocation(), 0, 0, 0, 0, 1, ViewDist.NORMAL,
UtilServer.getPlayers());
UtilParticle.PlayParticle(ParticleType.INSTANT_SPELL, item.getLocation(), 0, 0, 0, 0, 1, ViewDist.NORMAL, UtilServer.getPlayers());
}
}
}
@ -170,25 +182,25 @@ public class ChallengeFastFood extends Challenge
private ItemStack getRandomFood()
{
Material foodMaterial = UtilMath.randomElement(_food);
Material foodMaterial = UtilMath.randomElement(FOOD);
byte data = 0;
if (foodMaterial == Material.RAW_FISH)
{
data = (byte) (UtilMath.r(3) + 1);
data = (byte) (UtilMath.r(RAW_FISH_DATA_RANGE) + 1);
}
else if (foodMaterial == Material.COOKED_FISH)
{
data = (byte) UtilMath.r(1);
}
ItemStack itemStack = new ItemStack(foodMaterial, 5, (byte) data);
ItemStack itemStack = new ItemStack(foodMaterial, RANDOM_FOOD_AMOUNT, (byte) data);
return itemStack;
}
private void changeItemSlot(Player player)
{
for (int i = 0; i < 9; i++)
for (int i = 0; i <= INVENTORY_HOTBAR_SIZE; i++)
{
if (player.getInventory().getItem(i) != null)
{
@ -206,7 +218,7 @@ public class ChallengeFastFood extends Challenge
@SuppressWarnings("deprecation")
private void throwItemInGround(Player player, ItemStack item)
{
if (!Recharge.Instance.use(player, "Food Throw", 100, false, false))
if (!Recharge.Instance.use(player, "Food Throw", FOOD_THROW_COOLDOWN, false, false))
return;
player.getWorld().playSound(player.getLocation(), Sound.EAT, 0.5F, 1.1F);
@ -215,9 +227,9 @@ public class ChallengeFastFood extends Challenge
_itemSeperator++;
ItemStack toThrow = ItemStackFactory.Instance.CreateStack(item.getType(), item.getData().getData(), 1, Integer.toString(_itemSeperator));
double randomMultiply = 0.5 + (1 - 0.5) * UtilMath.random.nextDouble();
double randomMultiply = UtilMath.random.nextDouble();
Item thrownItem = player.getWorld().dropItem(player.getEyeLocation().subtract(0, 0.5, 0), toThrow);
Item thrownItem = player.getWorld().dropItem(player.getEyeLocation().subtract(0, FOOD_THROW_EYE_LOCATION_HEIGHT_SUBTRACT, 0), toThrow);
thrownItem.setVelocity(player.getLocation().getDirection().normalize().multiply(randomMultiply));
growGrassTask(thrownItem);
@ -239,9 +251,7 @@ public class ChallengeFastFood extends Challenge
if (item.isOnGround())
{
double random = Math.random() * 100;
if (random < 30.0)
if (Math.random() < FOOD_THROW_CREATE_DIRT_CHANCE)
{
Location drop = item.getLocation();
Block block = drop.getBlock();
@ -262,16 +272,16 @@ public class ChallengeFastFood extends Challenge
if (block.isEmpty() && !below.isEmpty())
{
Material flower = UtilMath.randomElement(_flowers);
Material flower = UtilMath.randomElement(FLOWERS);
setBlock(block, flower);
if (flower == Material.LONG_GRASS)
{
setData(block, (byte) (UtilMath.r(2) + 1));
setData(block, (byte) (UtilMath.r(LONG_GRASS_DATA_RANGE) + 1));
}
else if (flower == Material.RED_ROSE)
{
setData(block, (byte) UtilMath.r(8));
setData(block, (byte) UtilMath.r(RED_ROSE_DATA_RANGE));
}
blockBreakEffect(block, false);