Add charge-up mechanic for more powerful spells

- Implements spell charge-up in ItemWand
- Assigns chargeup values in json files for spells that should have one
- Adds a charge meter to the crosshair in first-person
- Updates some javadoc comments appropriately
This commit is contained in:
Electroblob77
2020-06-11 22:20:04 +01:00
parent 72971b5f16
commit 7d650ebbc3
84 changed files with 204 additions and 125 deletions
@@ -44,7 +44,8 @@ import java.util.Map.Entry;
public class GuiSpellDisplay {
private static final ResourceLocation INDEX = new ResourceLocation(Wizardry.MODID, "textures/gui/spell_hud/_index.json");
private static final ResourceLocation CHARGE_METER = new ResourceLocation(Wizardry.MODID, "textures/gui/spell_charge_meter.png");
/** A map which stores all loaded HUD skin objects. This gets wiped on resource pack reload and repopulated with
* mappings as specified by {@code _index.json} (these stack between resource packs). The keys in the map correspond
* to the keys in {@code _index.json}, and are sorted in that order, with skins belonging to resource packs sorted
@@ -55,6 +56,11 @@ public class GuiSpellDisplay {
private static final Gson gson = new Gson();
private static final Random random = new Random();
/** Width of the charge meter. */
private static final int CHARGE_METER_WIDTH = 25;
/** Height of the charge meter. */
private static final int CHARGE_METER_HEIGHT = 9;
/** Width and height of the spell icon (very unlikely to change!) */
private static final int SPELL_ICON_SIZE = 32;
/** Number of ticks the spell switching animation plays for. */
@@ -99,12 +105,7 @@ public class GuiSpellDisplay {
@SubscribeEvent
public static void draw(RenderGameOverlayEvent.Post event){
if(event.getType() != RenderGameOverlayEvent.ElementType.TEXT
&& event.getType() != RenderGameOverlayEvent.ElementType.HOTBAR) return;
Minecraft mc = Minecraft.getMinecraft();
EntityPlayer player = mc.player;
EntityPlayer player = Minecraft.getMinecraft().player;
if(player.isSpectator()) return; // Spectators shouldn't have the spell HUD!
@@ -117,12 +118,74 @@ public class GuiSpellDisplay {
wand = player.getHeldItemOffhand();
mainHand = false;
// If the player isn't holding a spellcasting item that shows the HUD, then nothing else needs to be done.
if(!(wand.getItem() instanceof ISpellCastingItem && ((ISpellCastingItem)wand.getItem()).showSpellHUD(player, wand))) return;
if(!(wand.getItem() instanceof ISpellCastingItem && ((ISpellCastingItem)wand.getItem()).showSpellHUD(player, wand)))
return;
}
int width = event.getResolution().getScaledWidth();
int height = event.getResolution().getScaledHeight();
switch(event.getType()){
case CROSSHAIRS:
renderChargeMeter(player, wand, width, height, event.getPartialTicks());
break;
case HOTBAR:
renderSpellHUD(player, wand, mainHand, width, height, event.getPartialTicks(), false);
break;
case TEXT:
renderSpellHUD(player, wand, mainHand, width, height, event.getPartialTicks(), true);
break;
}
}
/**
* Renders the spell charge meter around the crosshairs.
* @param player A reference to the client player
* @param wand The wand the HUD is for
* @param width The width of the screen
* @param height The height of the screen
* @param partialTicks The current partial tick time
*/
private static void renderChargeMeter(EntityPlayer player, ItemStack wand, int width, int height, float partialTicks){
if(Minecraft.getMinecraft().gameSettings.showDebugInfo) return; // Don't show charge meter in the debug screen
if(Minecraft.getMinecraft().gameSettings.thirdPersonView != 0) return; // Don't show in third person
Spell spell = WandHelper.getCurrentSpell(wand);
if(spell.getChargeup() <= 0) return;
// WHY WHY WHY are these methods named so misleadingly?! Sort yourselves out MCP!
// (getItemInUseCount returns the max count MINUS the use count, and getItemInUseMaxCount returns the use count)
if(player.getItemInUseMaxCount() == 0) return; // Not charging
float charge = (player.getItemInUseMaxCount() + partialTicks) / spell.getChargeup();
if(charge > 1) return; // Done charging
Minecraft.getMinecraft().renderEngine.bindTexture(CHARGE_METER);
int x1 = width/2 - CHARGE_METER_WIDTH/2;
int y = height/2 - CHARGE_METER_HEIGHT/2;
int w = (int)(CHARGE_METER_WIDTH/2 * charge);
int u = CHARGE_METER_WIDTH - w;
DrawingUtils.drawTexturedRect(x1, y, 0, 0, w, CHARGE_METER_HEIGHT, 32, 32);
DrawingUtils.drawTexturedRect(x1 + u, y, u, 0, w, CHARGE_METER_HEIGHT, 32, 32);
}
/**
* Renders the main spell HUD in the corner of the screen.
* @param player A reference to the client player
* @param wand The wand the HUD is for
* @param mainHand True if the wand is in the player's main hand, false if it is in their offhand
* @param width The width of the screen
* @param height The height of the screen
* @param partialTicks The current partial tick time
* @param textLayer True to render the text layer, false to render the background (hotbar layer)
*/
private static void renderSpellHUD(EntityPlayer player, ItemStack wand, boolean mainHand, int width, int height, float partialTicks, boolean textLayer){
boolean flipX = Wizardry.settings.spellHUDPosition.flipX;
boolean flipY = Wizardry.settings.spellHUDPosition.flipY;
@@ -130,16 +193,16 @@ public class GuiSpellDisplay {
// ............. | This bit is true if the wand is on the left, false if it is on the right
flipX = flipX == ((mainHand ? player.getPrimaryHand() : player.getPrimaryHand().opposite()) == EnumHandSide.LEFT);
}
Skin skin = skins.get(Wizardry.settings.spellHUDSkin);
if(skin == null){
Wizardry.logger.info("The spell HUD skin '" + Wizardry.settings.spellHUDSkin + "' specified in the config"
+ " did not match any of the loaded skins; using the default skin as a fallback.");
skin = skins.get(Settings.DEFAULT_HUD_SKIN_KEY);
if(skin == null){
Wizardry.logger.warn("The default spell HUD skin is missing! A resource pack must have overridden it"
+ " with an invalid JSON file (default.json), please try again without any resource packs.");
@@ -148,7 +211,7 @@ public class GuiSpellDisplay {
}
GlStateManager.pushMatrix();
// 'Origin' of the spell hud (bottom left corner of the actual texture, always in the corner of the screen)
int x = flipX ? width : 0;
int y = flipY ? 0: height;
@@ -169,45 +232,46 @@ public class GuiSpellDisplay {
y = MathHelper.ceil(y/scale);
}
// TODO: Maybe convert this to use ISpellCastingItem
Spell spell = WandHelper.getCurrentSpell(wand);
int cooldown = WandHelper.getCurrentCooldown(wand);
int maxCooldown = WandHelper.getCurrentMaxCooldown(wand);
if(event.getType() == RenderGameOverlayEvent.ElementType.TEXT){
if(textLayer){
float animationProgress = Math.signum(switchTimer) * ((SPELL_SWITCH_TIME - Math.abs(switchTimer) +
event.getPartialTicks()) / SPELL_SWITCH_TIME);
partialTicks) / SPELL_SWITCH_TIME);
String prevSpellName = getFormattedSpellName(WandHelper.getPreviousSpell(wand), player, WandHelper.getPreviousCooldown(wand));
String spellName = getFormattedSpellName(spell, player, cooldown);
String nextSpellName = getFormattedSpellName(WandHelper.getNextSpell(wand), player, WandHelper.getNextCooldown(wand));
skin.drawText(x, y, flipX, flipY, prevSpellName, spellName, nextSpellName, animationProgress);
}else if(event.getType() == RenderGameOverlayEvent.ElementType.HOTBAR){
}else{
boolean discovered = true;
if(!player.isCreative() && WizardData.get(player) != null){
discovered = WizardData.get(player).hasSpellBeenDiscovered(spell);
}
ResourceLocation icon = discovered ? spell.getIcon() : Spells.none.getIcon();
float progress = 1;
// Doesn't really matter what progress is when in creative, but we might as well avoid the calculation.
if(!player.isCreative() && !spell.isContinuous){
// Subtracted partial tick time to make it smoother
progress = maxCooldown == 0 ? 1 : (maxCooldown - (float)cooldown + event.getPartialTicks()) / maxCooldown;
progress = maxCooldown == 0 ? 1 : (maxCooldown - (float)cooldown + partialTicks) / maxCooldown;
}
skin.drawBackground(x, y, flipX, flipY, icon, progress, player.isCreative(), player.isPotionActive(WizardryPotions.arcane_jammer));
}
GlStateManager.popMatrix();
}
/**
* Gets the name of the given spell, with formatting added according to its cooldown and whether the given player
* has discovered it.
@@ -134,9 +134,9 @@ public abstract class SpellCastEvent extends Event {
/**
* SpellCastEvent.Pre is fired just before a spell is cast. Use this event to change the spell modifiers and
* generally alter the behaviour of the spell, or stop it from being cast entirely. For example, wizardry uses this
* event to cancel spells cast by entities that have the arcane jammer effect. Note that for wands, this is called
* <i>before</i> mana, tier and cooldowns are checked. Also note that this event is only fired once for continuous
* spells, when they start casting.<br>
* event to cancel spells cast by entities that have the arcane jammer effect. For wands, this is called
* <i>before</i> mana, tier and cooldowns are checked, and before charge-up. Also note that this event is only fired
* once for continuous spells, when they start casting.<br>
* <br>
* This event is {@link Cancelable}. If this event is canceled, the spell is not cast, mana is not consumed, and the
* right-click action that caused it (if any) returns a result of FAIL, meaning that the right-click is passed to
@@ -117,7 +117,8 @@ public interface ISpellCastingItem {
/**
* Casts the given spell using the given item stack. <b>This method does not perform any checks</b>; these are done
* in {@link ISpellCastingItem#canCast(ItemStack, Spell, EntityPlayer, EnumHand, int, SpellModifiers)}. This method
* also performs any post-casting logic, such as mana costs and cooldowns.
* also performs any post-casting logic, such as mana costs and cooldowns. This method does not handle charge-up
* times.
* <p></p>
* <i>N.B. Continuous spell casting from outside of the items requires a bit of extra legwork, see
* {@link WizardData} for an example.</i>
@@ -355,18 +355,19 @@ public class ItemWand extends Item implements IWorkbenchItem, ISpellCastingItem,
SpellModifiers modifiers = this.calculateModifiers(stack, player, spell);
if(canCast(stack, spell, player, hand, 0, modifiers)){
// Now we can cast continuous spells with scrolls!
if(spell.isContinuous){
if(spell.isContinuous || spell.getChargeup() > 0){
// Spells that need the mouse to be held (continuous, charge-up or both)
if(!player.isHandActive()){
player.setActiveHand(hand);
// Store the modifiers for use each tick
// Store the modifiers for use later
if(WizardData.get(player) != null) WizardData.get(player).itemCastingModifiers = modifiers;
// Return the player's held item so spells can change it if they wish (e.g. possession)
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
return new ActionResult<>(EnumActionResult.SUCCESS, stack);
}
}else{
// All other (instant) spells
if(cast(stack, spell, player, hand, 0, modifiers)){
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
return new ActionResult<>(EnumActionResult.SUCCESS, stack);
}
}
}
@@ -374,7 +375,8 @@ public class ItemWand extends Item implements IWorkbenchItem, ISpellCastingItem,
return new ActionResult<>(EnumActionResult.FAIL, stack);
}
// For continuous spells. The count argument actually decrements by 1 each tick.
// For continuous spells and spells with a charge-up time. The count argument actually decrements by 1 each tick.
// N.B. The first time this gets called is the tick AFTER onItemRightClick is called, not the same tick
@Override
public void onUsingTick(ItemStack stack, EntityLivingBase user, int count){
@@ -384,8 +386,6 @@ public class ItemWand extends Item implements IWorkbenchItem, ISpellCastingItem,
Spell spell = WandHelper.getCurrentSpell(stack);
if(!spell.isContinuous) return;
SpellModifiers modifiers;
if(WizardData.get(player) != null){
@@ -394,15 +394,29 @@ public class ItemWand extends Item implements IWorkbenchItem, ISpellCastingItem,
modifiers = this.calculateModifiers(stack, (EntityPlayer)user, spell); // Fallback to the old way, should never be used
}
int castingTick = stack.getMaxItemUseDuration() - count;
int useTick = stack.getMaxItemUseDuration() - count;
// Continuous spells (these must check if they can be cast each tick since the mana changes)
// Don't call canCast when castingTick == 0 because we already did it in onItemRightClick
if(castingTick == 0 || canCast(stack, spell, player, player.getActiveHand(), castingTick, modifiers)){
cast(stack, spell, player, player.getActiveHand(), castingTick, modifiers);
if(spell.isContinuous){
// Continuous spell charge-up is simple, just don't do anything until it's charged
if(useTick >= spell.getChargeup()){
// castingTick needs to be relative to when the spell actually started
int castingTick = useTick - spell.getChargeup();
// Continuous spells (these must check if they can be cast each tick since the mana changes)
// Don't call canCast when castingTick == 0 because we already did it in onItemRightClick - even
// with charge-up times, because we don't want to trigger events twice
if(castingTick == 0 || canCast(stack, spell, player, player.getActiveHand(), castingTick, modifiers)){
cast(stack, spell, player, player.getActiveHand(), castingTick, modifiers);
}else{
// Stops the casting if it was interrupted, either by events or because the wand ran out of mana
player.stopActiveHand();
}
}
}else{
// Stops the casting if it was interrupted, either by events or because the wand ran out of mana
player.stopActiveHand();
// Non-continuous spells need to check they actually have a charge-up since ALL spells call setActiveHand
if(spell.getChargeup() > 0 && useTick == spell.getChargeup()){
// Once the spell is charged, it's exactly the same as in onItemRightClick
cast(stack, spell, player, player.getActiveHand(), 0, modifiers);
}
}
}
}
@@ -450,8 +464,6 @@ public class ItemWand extends Item implements IWorkbenchItem, ISpellCastingItem,
WizardryPacketHandler.net.sendToDimension(msg, world.provider.getDimension());
}
caster.setActiveHand(hand);
// Mana cost
int cost = (int)(spell.getCost() * modifiers.get(SpellModifiers.COST) + 0.1f); // Weird floaty rounding
// As of wizardry 4.2 mana cost is only divided over two intervals each second
@@ -461,6 +473,8 @@ public class ItemWand extends Item implements IWorkbenchItem, ISpellCastingItem,
}
caster.setActiveHand(hand);
// Cooldown
if(!spell.isContinuous && !caster.isCreative()){ // Spells only have a cooldown in survival
WandHelper.setCurrentCooldown(stack, (int)(spell.getCooldown() * modifiers.get(WizardryItems.cooldown_upgrade)));