From 9d5e72cbc1724c72a15cb81e94a01d556b907ff1 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 1 Jan 2022 01:01:01 +0200 Subject: [PATCH] Implement spell dismissal and fixed placed spells not appearing correctly --- .../magic/spell/AbstractDelegatingSpell.java | 2 +- .../ability/magic/spell/CompoundSpell.java | 2 +- .../ability/magic/spell/PlaceableSpell.java | 2 +- .../ability/magic/spell/ThrowableSpell.java | 2 +- .../client/gui/DismissSpellScreen.java | 75 ++++++++++++------- .../unicopia/network/Channel.java | 1 + .../unicopia/network/MsgRemoveSpell.java | 38 ++++++++++ 7 files changed, 93 insertions(+), 29 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java index def71387..eeb6a7e8 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java @@ -30,7 +30,7 @@ public abstract class AbstractDelegatingSpell implements ProjectileSpell { this.type = type; } - protected abstract Collection getDelegates(); + public abstract Collection getDelegates(); @Override public boolean equalsOrContains(UUID id) { diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java index d9743a90..7eb71f16 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java @@ -20,7 +20,7 @@ public class CompoundSpell extends AbstractDelegatingSpell { } @Override - protected Collection getDelegates() { + public Collection getDelegates() { return spells; } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java index eaab3e8a..7ceb0623 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java @@ -45,7 +45,7 @@ public class PlaceableSpell extends AbstractDelegatingSpell { } @Override - protected Collection getDelegates() { + public Collection getDelegates() { return List.of(spell); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java index ef1b699b..72be9d68 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java @@ -30,7 +30,7 @@ public final class ThrowableSpell extends AbstractDelegatingSpell { } @Override - protected Collection getDelegates() { + public Collection getDelegates() { return List.of(spell); } diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java b/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java index fc202c2d..bc8d6629 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java @@ -4,14 +4,20 @@ import java.util.ArrayList; import java.util.List; import com.minelittlepony.common.client.gui.GameGui; -import com.minelittlepony.unicopia.USounds; +import com.minelittlepony.unicopia.ability.magic.spell.AbstractDelegatingSpell; import com.minelittlepony.unicopia.ability.magic.spell.Spell; import com.minelittlepony.unicopia.client.FlowingText; import com.minelittlepony.unicopia.client.particle.SphereModel; import com.minelittlepony.unicopia.entity.player.Pony; +import com.minelittlepony.unicopia.network.Channel; +import com.minelittlepony.unicopia.network.MsgRemoveSpell; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.Drawable; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; +import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.sound.PositionedSoundInstance; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.sound.SoundEvents; @@ -26,11 +32,6 @@ public class DismissSpellScreen extends GameGui { private final List spells; - private final List entries = new ArrayList<>(); - - private double lastMouseX; - private double lastMouseY; - public DismissSpellScreen() { super(new TranslatableText("gui.unicopia.dismiss_spell")); @@ -39,8 +40,6 @@ public class DismissSpellScreen extends GameGui { @Override protected void init() { - entries.clear(); - double azimuth = 0; int ring = 1; @@ -59,7 +58,7 @@ public class DismissSpellScreen extends GameGui { Vector4f pos = SphereModel.convertToCartesianCoord(radius, azimuth, azimuth); pos.add(0, -(float)radius / 2F, 0, 0); - entries.add(new Entry(spell, pos)); + addDrawableChild(new Entry(spell, pos)); } } @@ -68,8 +67,6 @@ public class DismissSpellScreen extends GameGui { fillGradient(matrices, 0, 0, width, height / 2, 0xF0101010, 0x80101010); fillGradient(matrices, 0, height / 2, width, height, 0x80101010, 0xF0101010); - super.render(matrices, mouseX, mouseY, delta); - matrices.push(); matrices.translate(width - mouseX, height - mouseY, 0); @@ -80,9 +77,7 @@ public class DismissSpellScreen extends GameGui { DrawableUtil.drawArc(matrices, 40, 80, 0, Math.PI * 2, 0x00000010, false); DrawableUtil.drawArc(matrices, 160, 1600, 0, Math.PI * 2, 0x00000020, false); - for (Entry entry : entries) { - entry.render(this, matrices, mouseX, mouseY); - } + super.render(matrices, mouseX, mouseY, delta); matrices.push(); DrawableUtil.drawCircle(matrices, 2, 0, Math.PI * 2, 0xFFAAFF99, false); @@ -110,11 +105,12 @@ public class DismissSpellScreen extends GameGui { return true; } - class Entry { + class Entry implements Element, Drawable, Selectable { private final List tooltip = new ArrayList<>(); private final Spell spell; + private final Spell actualSpell; private final Vector4f pos; private boolean lastMouseOver; @@ -122,25 +118,45 @@ public class DismissSpellScreen extends GameGui { public Entry(Spell spell, Vector4f pos) { this.spell = spell; this.pos = pos; + this.actualSpell = getActualSpell(); - MutableText name = spell.getType().getName().shallowCopy(); - name.setStyle(name.getStyle().withColor(spell.getType().getColor())); + MutableText name = actualSpell.getType().getName().shallowCopy(); + int color = actualSpell.getType().getColor(); + name.setStyle(name.getStyle().withColor(color == 0 ? 0xFFAAAAAA : color)); tooltip.add(new TranslatableText("Spell Type: %s", name)); - spell.getType().getTraits().appendTooltip(tooltip); + actualSpell.getType().getTraits().appendTooltip(tooltip); tooltip.add(LiteralText.EMPTY); - tooltip.add(new TranslatableText("Affinity: %s", spell.getAffinity().name()).formatted(spell.getAffinity().getColor())); + tooltip.add(new TranslatableText("Affinity: %s", actualSpell.getAffinity().name()).formatted(actualSpell.getAffinity().getColor())); tooltip.add(LiteralText.EMPTY); - tooltip.addAll(FlowingText.wrap(new TranslatableText(spell.getType().getTranslationKey() + ".lore").formatted(spell.getAffinity().getColor()), 180).toList()); + tooltip.addAll(FlowingText.wrap(new TranslatableText(actualSpell.getType().getTranslationKey() + ".lore").formatted(actualSpell.getAffinity().getColor()), 180).toList()); tooltip.add(LiteralText.EMPTY); tooltip.add(new TranslatableText("[Click to Discard]")); } - public boolean isMouseOver(int mouseX, int mouseY) { + private Spell getActualSpell() { + if (spell instanceof AbstractDelegatingSpell) { + return ((AbstractDelegatingSpell)spell).getDelegates().stream().findFirst().orElse(spell); + } + return spell; + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + remove(this); + pony.getSpellSlot().removeIf(spell -> spell == this.spell, true); + Channel.REMOVE_SPELL.send(new MsgRemoveSpell(spell)); + playClickEffect(); + return true; + } + + @Override + public boolean isMouseOver(double mouseX, double mouseY) { return squareDistance(mouseX, mouseY, pos.getX(), pos.getY()) < 75; } - public void render(DismissSpellScreen screen, MatrixStack matrices, int mouseX, int mouseY) { + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float tickDelta) { Vector4f copy = new Vector4f(pos.getX(), pos.getY(), pos.getZ(), pos.getW()); copy.transform(matrices.peek().getPositionMatrix()); @@ -153,7 +169,7 @@ public class DismissSpellScreen extends GameGui { modelStack.scale(scale, scale, 1); RenderSystem.applyModelViewMatrix(); - screen.client.getItemRenderer().renderGuiItemIcon(spell.getType().getDefualtStack(), (int)copy.getX() - 8, (int)copy.getY() - 8); + client.getItemRenderer().renderGuiItemIcon(actualSpell.getType().getDefualtStack(), (int)copy.getX() - 8, (int)copy.getY() - 8); modelStack.pop(); RenderSystem.applyModelViewMatrix(); @@ -161,13 +177,13 @@ public class DismissSpellScreen extends GameGui { matrices.push(); matrices.translate(pos.getX(), pos.getY(), 0); - int color = spell.getType().getColor() << 2; + int color = actualSpell.getType().getColor() << 2; DrawableUtil.drawArc(matrices, 7, 8, 0, Math.PI * 2, color | 0x00000088, false); if (hovered) { DrawableUtil.drawArc(matrices, 0, 8, 0, Math.PI * 2, color | 0x000000FF, false); - screen.renderTooltip(matrices, tooltip, 0, 0); + renderTooltip(matrices, tooltip, 0, 0); if (!lastMouseOver) { lastMouseOver = true; @@ -179,6 +195,15 @@ public class DismissSpellScreen extends GameGui { matrices.pop(); } + + @Override + public void appendNarrations(NarrationMessageBuilder var1) { + } + + @Override + public SelectionType getType() { + return SelectionType.HOVERED; + } } static void playClickEffect() { diff --git a/src/main/java/com/minelittlepony/unicopia/network/Channel.java b/src/main/java/com/minelittlepony/unicopia/network/Channel.java index c3efbdaa..82d68a35 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/Channel.java +++ b/src/main/java/com/minelittlepony/unicopia/network/Channel.java @@ -13,6 +13,7 @@ public interface Channel { C2SPacketType> CLIENT_PLAYER_ABILITY = SimpleNetworking.clientToServer(new Identifier("unicopia", "player_ability"), MsgPlayerAbility::new); C2SPacketType CLIENT_REQUEST_SPECIES_CHANGE = SimpleNetworking.clientToServer(new Identifier("unicopia", "request_capabilities"), MsgRequestSpeciesChange::new); C2SPacketType MARK_TRAIT_READ = SimpleNetworking.clientToServer(new Identifier("unicopia", "mark_trait_read"), MsgMarkTraitRead::new); + C2SPacketType REMOVE_SPELL = SimpleNetworking.clientToServer(new Identifier("unicopia", "remove_spell"), MsgRemoveSpell::new); S2CPacketType SERVER_PLAYER_CAPABILITIES = SimpleNetworking.serverToClient(new Identifier("unicopia", "player_capabilities"), MsgPlayerCapabilities::new); S2CPacketType SERVER_SPAWN_PROJECTILE = SimpleNetworking.serverToClient(new Identifier("unicopia", "projectile_entity"), MsgSpawnProjectile::new); diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java b/src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java new file mode 100644 index 00000000..f2257412 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgRemoveSpell.java @@ -0,0 +1,38 @@ +package com.minelittlepony.unicopia.network; + +import java.util.UUID; + +import com.minelittlepony.unicopia.ability.magic.spell.Spell; +import com.minelittlepony.unicopia.entity.player.Pony; +import com.minelittlepony.unicopia.util.network.Packet; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.network.ServerPlayerEntity; + +/** + * Sent to the server when a player activates an ability. + */ +public class MsgRemoveSpell implements Packet { + private final UUID id; + + MsgRemoveSpell(PacketByteBuf buffer) { + id = buffer.readUuid(); + } + + public MsgRemoveSpell(Spell spell) { + id = spell.getUuid(); + } + + @Override + public void toBuffer(PacketByteBuf buffer) { + buffer.writeUuid(id); + } + + @Override + public void handle(ServerPlayerEntity sender) { + Pony player = Pony.of(sender); + if (player != null) { + player.getSpellSlot().removeIf(spell -> spell.getUuid().equals(id), true); + } + } +}