Current progress on wizards

This commit is contained in:
libraryaddict 2014-12-24 12:56:17 +13:00
parent 34ce818614
commit 0648276720
3 changed files with 238 additions and 61 deletions

View File

@ -1,12 +1,11 @@
package nautilus.game.arcade.game.games.wizards;
import nautilus.game.arcade.ArcadeManager;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
public abstract class Spell
public abstract class Spell implements Listener
{
public interface SpellRightClick
{
@ -23,6 +22,18 @@ public abstract class Spell
public void castSpell(Player player, Block block);
}
protected ArcadeManager manager;
protected WizardBattles wizards;
protected Spells spell;
/**
* Charges him for the cost of the spell
*/
public void charge(Player player)
{
WizardBattles.Wizard wizard = wizards.wizards.get(player.getName());
wizard.mana -= spell.getManaCost(wizard.knownSpells.get(spell));
wizard.cooldowns.put(spell, spell.getSpellCooldown(wizard.knownSpells.get(spell)));
wizards.displayBossbar(player);
}
}

View File

@ -1,6 +1,7 @@
package nautilus.game.arcade.game.games.wizards;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import mineplex.core.common.util.C;
@ -10,29 +11,36 @@ import nautilus.game.arcade.game.games.wizards.spells.*;
public enum Spells
{
DUMB_SPELL(SpellType.ATTACK, "Dumb spell", DumbSpell.class, 10, 10);
DUMB_SPELL(SpellElement.ATTACK, "Dumb spell", new ItemStack(Material.STONE), DumbSpell.class, 10, 10);
private Class<? extends Spell> spellClass;
private Spell spell;
private String spellName;
private int spellCooldown;
private int spellCost;
private SpellType type;
private SpellElement type;
private String[] desc;
private ItemStack item;
private enum SpellType
private enum SpellElement
{
ATTACK(C.cRed), DEFENSE(C.cGray), HEAL(C.cGreen);
String chatColor;
private SpellType(String color)
private SpellElement(String color)
{
this.chatColor = color;
}
@Override
public String toString()
{
return chatColor;
}
}
private Spells(SpellType type, String spellName, Class<? extends Spell> spell, int spellCost, int spellCooldown,
String... desc)
private Spells(SpellElement type, String spellName, ItemStack spellItem, Class<? extends Spell> spell, int spellCost,
int spellCooldown, String... desc)
{
this.item = spellItem;
this.desc = desc;
this.type = type;
this.spellClass = spell;
@ -41,11 +49,6 @@ public enum Spells
this.spellCooldown = spellCooldown;
}
public Spell getSpell()
{
return spell;
}
public String getSpellName()
{
return spellName;
@ -71,33 +74,29 @@ public enum Spells
return wizards.setNonStack(builder.build());
}
public static void registerSpells(ArcadeManager manager)
public SpellElement getElement()
{
for (Spells spell : values())
{
if (spell.spell == null)
{
try
{
spell.spell = spell.spellClass.newInstance();
spell.spell.manager = manager;
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
return this.type;
}
public static void unregisterSpells()
/**
* TODO Make it cost less as you levelup
**/
public int getManaCost(int level)
{
for (Spells spell : values())
{
if (spell.spell != null)
{
spell.spell = null;
}
}
return this.spellCost;
}
/**
* TODO Make it cost less as you levelup
**/
public int getSpellCooldown(int level)
{
return this.spellCooldown;
}
public Class<? extends Spell> getSpellClass()
{
return spellClass;
}
}

View File

@ -1,26 +1,40 @@
package nautilus.game.arcade.game.games.wizards;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import mineplex.core.common.util.C;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilTextTop;
import mineplex.core.common.util.UtilTime;
import mineplex.core.common.util.UtilTime.TimeUnit;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import nautilus.game.arcade.ArcadeManager;
import nautilus.game.arcade.GameType;
import nautilus.game.arcade.events.GameStateChangeEvent;
@ -36,15 +50,16 @@ public class WizardBattles extends SoloGame
{
private int code = 1000;
private class Wizard
class Wizard
{
float manaPerSecond = 2.5F;
float mana;
HashMap<Spells, Integer> knownSpells = new HashMap<Spells, Integer>();
HashMap<Spells, Integer> cooldowns = new HashMap<Spells, Integer>();
NautHashMap<Spells, Integer> knownSpells = new NautHashMap<Spells, Integer>();
NautHashMap<Spells, Integer> cooldowns = new NautHashMap<Spells, Integer>();
}
private HashMap<String, Wizard> wizards = new HashMap<String, Wizard>();
NautHashMap<String, Wizard> wizards = new NautHashMap<String, Wizard>();
private NautHashMap<Spells, Spell> spells = new NautHashMap<Spells, Spell>();
public WizardBattles(ArcadeManager manager, GameType gameType, Kit[] kits, String[] gameDesc)
{
@ -79,6 +94,99 @@ public class WizardBattles extends SoloGame
});
}
@EventHandler
public void onSecond(UpdateEvent event)
{
if ((event.getType() == UpdateType.FASTEST || event.getType() == UpdateType.SEC) && GetState() == GameState.Live)
{
Iterator<Entry<String, Wizard>> itel = wizards.entrySet().iterator();
while (itel.hasNext())
{
Entry<String, Wizard> entry = itel.next();
Player player = Bukkit.getPlayerExact(entry.getKey());
if (event.getType() == UpdateType.SEC)
{
if (!player.isOnline() || !IsAlive(player))
{
itel.remove();
continue;
}
Wizard wizard = wizards.get(player.getName());
wizard.mana += wizard.manaPerSecond;
Iterator<Entry<Spells, Integer>> itel2 = wizard.cooldowns.entrySet().iterator();
while (itel2.hasNext())
{
Entry<Spells, Integer> entry2 = itel2.next();
if (entry2.getValue() > 1)
{
wizard.cooldowns.put(entry2.getKey(), entry2.getValue() - 1);
}
else
{
itel2.remove();
}
}
}
displayBossbar(player);
}
}
}
@EventHandler
public void onSwapItem(PlayerItemHeldEvent event)
{
Player p = event.getPlayer();
ItemStack item = p.getItemInHand();
if (item != null && item.getType() == Material.BLAZE_ROD && item.getItemMeta().hasDisplayName() && IsAlive(p))
{
displayBossbar(p);
}
}
void displayBossbar(Player player)
{
ItemStack item = player.getItemInHand();
if (item != null && item.getType() == Material.BLAZE_ROD && item.getItemMeta().hasDisplayName())
{
Spells type = this.getSpell(item.getItemMeta().getDisplayName());
Wizard wizard = wizards.get(player.getName());
int spellLevel = wizard.knownSpells.get(type);
String display = ChatColor.DARK_PURPLE + "Spell: " + ChatColor.RESET + type.getElement() + type.getSpellName()
+ ChatColor.RESET + ChatColor.BOLD + " :" + ChatColor.RESET + ChatColor.DARK_AQUA + " Mana: "
+ ChatColor.RESET + type.getManaCost(spellLevel) + ChatColor.RESET + ChatColor.BOLD + " :";
double usableTime = 0;// Time in seconds till usable
if (wizard.mana < type.getManaCost(spellLevel))
{
usableTime = (type.getManaCost(spellLevel) - wizard.mana) / wizard.manaPerSecond;
}
int cooldown = wizard.cooldowns.containsKey(type) ? wizard.cooldowns.get(type) : 0;
if (cooldown > 0 && cooldown > usableTime)
{
usableTime = cooldown;
}
if (usableTime > 0)
{
display += ChatColor.RED + " Usable in "
+ UtilTime.convertString((long) (Math.ceil(usableTime) * 1000), 0, TimeUnit.FIT);
}
else
{
display += ChatColor.RED + " Cooldown: " + ChatColor.RESET
+ UtilTime.convertString((long) (type.getSpellCooldown(spellLevel) * 1000), 0, TimeUnit.FIT);
}
double maxSeconds = Math.max(type.getSpellCooldown(spellLevel), type.getManaCost(spellLevel) / wizard.manaPerSecond);
UtilTextTop.displayTextBar(player, (float) (300D - ((usableTime / maxSeconds) * 300)), display);
}
}
@EventHandler
public void onDeath(PlayerDeathEvent event)
{
wizards.remove(event.getEntity().getName());
}
@EventHandler
public void onLive(GameStateChangeEvent event)
{
@ -96,11 +204,29 @@ public class WizardBattles extends SoloGame
{
if (event.GetState() == GameState.Live)
{
Spells.registerSpells(getArcadeManager());
for (Spells spells : Spells.values())
{
try
{
Spell spell = spells.getSpellClass().newInstance();
spell.spell = spells;
spell.wizards = this;
this.spells.put(spells, spell);
Bukkit.getPluginManager().registerEvents(spell, getArcadeManager().GetPlugin());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
else if (event.GetState() == GameState.Dead || event.GetState() == GameState.End)
{
Spells.unregisterSpells();
for (Spell spell : spells.values())
{
HandlerList.unregisterAll(spell);
}
spells.clear();
}
}
@ -146,25 +272,60 @@ public class WizardBattles extends SoloGame
private void onCastSpell(Player player, Object obj)
{
ItemStack item = player.getItemInHand();
if (item != null && item.getType() == Material.BLAZE_ROD && item.getItemMeta().hasDisplayName())
if (GetState() == GameState.Live && IsAlive(player) && item != null && item.getType() == Material.BLAZE_ROD
&& item.getItemMeta().hasDisplayName())
{
Spells spell = getSpell(item.getItemMeta().getDisplayName());
if (spell != null)
{
// TODO If can cast spell
Spell sp = spell.getSpell();
if (obj instanceof Block && sp instanceof SpellRightClickBlock)
Wizard wizard = this.wizards.get(player.getName());
if (wizard.knownSpells.containsKey(spell))
{
((SpellRightClickBlock) sp).castSpell(player, (Block) obj);
int spellLevel = wizard.knownSpells.get(spell);
if (wizard.mana >= spell.getManaCost(spellLevel))
{
if (!wizard.cooldowns.containsKey(spell))
{
Spell sp = spells.get(spell);
if (obj instanceof Block && sp instanceof SpellRightClickBlock)
{
((SpellRightClickBlock) sp).castSpell(player, (Block) obj);
}
else if (obj instanceof Entity && sp instanceof SpellRightClickEntity)
{
((SpellRightClickEntity) sp).castSpell(player, (Entity) obj);
}
else if (sp instanceof SpellRightClick)
{
((SpellRightClick) sp).castSpell(player);
}
else
{
return;
}
wizard.cooldowns.put(spell, spell.getSpellCooldown(spellLevel));
wizard.mana -= spell.getManaCost(spellLevel);
}
else
{
player.playSound(player.getLocation(), Sound.FIZZ, 300, 1);
player.sendMessage(ChatColor.BLUE + "The spell hasn't recharged yet!");
}
}
else
{
player.playSound(player.getLocation(), Sound.FIZZ, 300, 1);
player.sendMessage(ChatColor.BLUE + "The spell sputters and dies.");
player.sendMessage(ChatColor.BLUE + "You do not have enough mana!");
}
}
else if (obj instanceof Entity && sp instanceof SpellRightClickEntity)
else
{
((SpellRightClickEntity) sp).castSpell(player, (Entity) obj);
}
else if (sp instanceof SpellRightClick)
{
((SpellRightClick) sp).castSpell(player);
player.sendMessage(ChatColor.BLUE + "You do not know that spell!");
}
}
}
}
@ -175,15 +336,21 @@ public class WizardBattles extends SoloGame
ItemStack item = event.getItem();
if (item != null)
{
Player p = event.getPlayer();
if (event.getAction().name().contains("RIGHT"))
{
if (item.getType() == Material.BLAZE_ROD)
{
onCastSpell(event.getPlayer(), event.getClickedBlock());
onCastSpell(p, event.getClickedBlock());
}
else if (item.getType() == Material.ENCHANTED_BOOK)
else if (item.getType() == Material.ENCHANTED_BOOK && item.getItemMeta().hasDisplayName())
{
// TODO Learn spell
Spells spell = getSpell(item.getItemMeta().getDisplayName());
if (spell != null)
{
Wizard wiz = wizards.get(p.getName());
wiz.knownSpells.put(spell, (wiz.knownSpells.containsKey(spell) ? wiz.knownSpells.get(spell) : 0) + 1);
}
}
}
else if (event.getAction().name().contains("LEFT"))