Improve chicken spawning on chicken shooting

This commit is contained in:
Thanos Paravantis 2016-04-27 18:37:42 +03:00
parent f8ff99fc58
commit ddad17855f

View File

@ -37,6 +37,7 @@ import nautilus.game.arcade.game.games.mineware.challenge.NumberTracker;
public class ChallengeChickenShooting extends Challenge implements NumberTracker
{
private ArrayList<Chicken> _chickens = new ArrayList<Chicken>();
private ArrayList<Location> _chickenSpawns = new ArrayList<Location>();
private HashMap<Player, Integer> _score = new HashMap<Player, Integer>();
public ChallengeChickenShooting(BawkBawkBattles host)
@ -102,6 +103,8 @@ public class ChallengeChickenShooting extends Challenge implements NumberTracker
}
}
}
initializeChickenSpawns();
}
@Override
@ -111,25 +114,7 @@ public class ChallengeChickenShooting extends Challenge implements NumberTracker
Host.DamagePvE = true;
ItemStack bow = new ItemBuilder(Material.BOW)
.setUnbreakable(true)
.addEnchantment(Enchantment.ARROW_INFINITE, 1)
.addItemFlags(ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE)
.build();
ItemStack arrow = new ItemStack(Material.ARROW);
for (Player player : getPlayersAlive())
{
// It's better to add the items without setItem() method, to minimize for-loops.
PlayerInventory inv = player.getInventory();
inv.setItem(Settings.getLockedSlot(), bow);
inv.setItem(31, arrow);
_score.put(player, 0);
}
addBowAndArrow();
}
@Override
@ -198,20 +183,14 @@ public class ChallengeChickenShooting extends Challenge implements NumberTracker
}
event.AddMod("Ensure Death", null, 10, false);
int score = showAndIncrementScore(player, entity);
if (score == 6)
{
setCompleted(player);
}
checkCompleted(player, entity);
}
@EventHandler
public void onUpdate(UpdateEvent event)
{
UpdateType speed;
int alive = getPlayersIn(false).size();
int alive = getPlayersIn(true).size();
if (alive >= 40)
speed = UpdateType.FASTEST;
@ -236,13 +215,42 @@ public class ChallengeChickenShooting extends Challenge implements NumberTracker
if (!isChallengeValid())
return;
Player player = event.getPlayer();
if (_score.containsKey(player))
{
_score.remove(player);
_score.remove(event.getPlayer());
}
private void initializeChickenSpawns()
{
_chickenSpawns = (ArrayList<Location>) circle(getCenter(), getArenaSize() - 2, 1, false, false, 15);
}
private void addBowAndArrow()
{
ItemStack bow = new ItemBuilder(Material.BOW)
.setUnbreakable(true)
.addEnchantment(Enchantment.ARROW_INFINITE, 1)
.addItemFlags(ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE)
.build();
ItemStack arrow = new ItemStack(Material.ARROW);
for (Player player : getPlayersAlive())
{
PlayerInventory inv = player.getInventory();
inv.setItem(Settings.getLockedSlot(), bow);
inv.setItem(31, arrow);
_score.put(player, 0);
}
}
private void checkCompleted(Player player, LivingEntity entity)
{
int score = showAndIncrementScore(player, entity);
if (score == 6)
{
setCompleted(player);
}
}
private int showAndIncrementScore(Player player, LivingEntity entity)
@ -252,12 +260,40 @@ public class ChallengeChickenShooting extends Challenge implements NumberTracker
Location location = player.getEyeLocation();
location.add(UtilAlg.getTrajectory(location, entity.getEyeLocation()).multiply(Math.min(7, entity.getLocation().distance(location))));
displayCount(player, location, (score >= 6 ? C.cDGreen : score >= 4 ? C.cGreen : score >= 2 ? C.cRed : C.cDRed) + score);
displayCount(player, location, selectScoreColor(score));
_score.put(player, score);
return score;
}
private String selectScoreColor(int score)
{
if (score == 1)
{
return C.cWhiteB + score;
}
else if (score == 2)
{
return C.cGrayB + score;
}
else if (score == 3)
{
return C.cGreenB + score;
}
else if (score == 4)
{
return C.cYellowB + score;
}
else if (score == 5)
{
return C.cGoldB + score;
}
else
{
return C.cRedB + score;
}
}
private void removeChickensOnGround()
{
Iterator<Chicken> iterator = _chickens.iterator();
@ -276,9 +312,9 @@ public class ChallengeChickenShooting extends Challenge implements NumberTracker
private void spawnChicken()
{
if (_chickens.size() < 11 + (getPlayersAlive().size() * 2))
if (_chickens.size() < 10 + (getPlayersIn(true).size() * 3))
{
Location spawn = getRandomLocation();
Location spawn = UtilMath.randomElement(_chickenSpawns);
Host.CreatureAllow = true;
Chicken chicken = (Chicken) spawn.getWorld().spawnEntity(spawn, EntityType.CHICKEN);
@ -287,17 +323,15 @@ public class ChallengeChickenShooting extends Challenge implements NumberTracker
chicken.setMaxHealth(0.1);
chicken.setHealth(0.1);
if (Math.random() < 0.3)
{
chicken.setBaby();
}
_chickens.add(chicken);
}
}
private Location getRandomLocation()
{
int random = UtilMath.r(getArenaSize() * 2) - (getArenaSize());
return getCenter().add(random, 15, random);
}
@Override
public Number getData(Player player)
{