Skip to content

Commit

Permalink
- Renamed DialogueAnimation to DialogueAnimationData
Browse files Browse the repository at this point in the history
- Added xoffset + yoffset + scale
  • Loading branch information
Dweblenod committed Mar 30, 2024
1 parent 0a0ac84 commit d723d18
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
15 changes: 9 additions & 6 deletions src/main/java/com/mraof/minestuck/client/gui/DialogueScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mojang.blaze3d.systems.RenderSystem;
import com.mraof.minestuck.entity.dialogue.Dialogue;
import com.mraof.minestuck.entity.dialogue.DialogueAnimation;
import com.mraof.minestuck.entity.dialogue.DialogueAnimationData;
import com.mraof.minestuck.network.DialoguePackets;
import com.mraof.minestuck.network.MSPacketHandler;
import net.minecraft.client.gui.GuiGraphics;
Expand Down Expand Up @@ -31,7 +31,7 @@ public class DialogueScreen extends Screen

private final int dialogueId;
private final Dialogue.DialogueData dialogueData;
private final DialogueAnimation animation;
private final DialogueAnimationData animationData;

private int xOffset;
private int yOffset;
Expand All @@ -52,14 +52,14 @@ public class DialogueScreen extends Screen
this.dialogueId = dialogueId;
this.dialogueData = dialogueData;

this.animation = dialogueData.animation();
this.animationData = dialogueData.animationData();
}

@Override
public void init()
{
yOffset = (this.height / 2) - (GUI_HEIGHT / 2);
xOffset = (this.width / 2) - (GUI_WIDTH / 2) - (animation.spriteWidth() / 2);
xOffset = (this.width / 2) - (GUI_WIDTH / 2) - (animationData.spriteWidth() / 2) - animationData.xOffset();

this.messageLines = font.split(this.dialogueData.message(), GUI_WIDTH - 20);

Expand Down Expand Up @@ -193,11 +193,14 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partia
*/
private void renderAnimation(GuiGraphics guiGraphics)
{
ResourceLocation sprite = animation.getRenderPath(dialogueData.spriteType());
ResourceLocation sprite = animationData.getRenderPath(dialogueData.spriteType());
float scale = animationData.scale();
int moddedWidth = (int) (animationData.spriteWidth() * scale);
int moddedHeight = (int) (animationData.spriteHeight() * scale);

//if there is a .png.mcmeta file associated with the sprite, the animation for it is updated here
AnimatableTexture.setAndUpdate(sprite, animationTick);
guiGraphics.blit(sprite, xOffset + GUI_WIDTH, (this.height / 2) - (animation.spriteHeight() / 2), 0, 0, animation.spriteWidth(), animation.spriteHeight(), animation.spriteWidth(), animation.spriteHeight());
guiGraphics.blit(sprite, xOffset + GUI_WIDTH + animationData.xOffset(), (this.height / 2) - (animationData.spriteHeight() / 2), 0, 0, moddedWidth, moddedHeight, moddedWidth, moddedHeight);

animationTick++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.JsonOps;
import com.mraof.minestuck.entity.dialogue.Dialogue;
import com.mraof.minestuck.entity.dialogue.DialogueAnimation;
import com.mraof.minestuck.entity.dialogue.DialogueAnimationData;
import com.mraof.minestuck.entity.dialogue.DialogueMessage;
import com.mraof.minestuck.entity.dialogue.Trigger;
import com.mraof.minestuck.entity.dialogue.condition.Condition;
Expand Down Expand Up @@ -139,7 +139,7 @@ public Dialogue.NodeSelector buildSelector(ResourceLocation id, BiConsumer<Resou
public static class NodeBuilder implements SimpleDialogueProducer, NodeProducer
{
private final List<Pair<Dialogue.MessageType, MessageProducer>> messages = new ArrayList<>();
private DialogueAnimation animation = DialogueAnimation.DEFAULT_ANIMATION;
private DialogueAnimationData animation = DialogueAnimationData.DEFAULT_ANIMATION;
private ResourceLocation guiPath = Dialogue.DEFAULT_GUI;
private final List<ResponseBuilder> responses = new ArrayList<>();

Expand All @@ -164,12 +164,12 @@ public NodeBuilder addDescription(MessageProducer message)
return this;
}

public NodeBuilder animation(DialogueAnimation.Emotion emotion)
public NodeBuilder animation(DialogueAnimationData.Emotion emotion)
{
return animation(new DialogueAnimation(emotion.getSerializedName(), DialogueAnimation.DEFAULT_SPRITE_HEIGHT, DialogueAnimation.DEFAULT_SPRITE_WIDTH));
return animation(new DialogueAnimationData(emotion.getSerializedName(), DialogueAnimationData.DEFAULT_SPRITE_HEIGHT, DialogueAnimationData.DEFAULT_SPRITE_WIDTH, 0, 0, 1.0F));
}

public NodeBuilder animation(DialogueAnimation animation)
public NodeBuilder animation(DialogueAnimationData animation)
{
this.animation = animation;
return this;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/mraof/minestuck/entity/dialogue/Dialogue.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ public record NodeReference(ResourceLocation dialoguePath, int nodeIndex)
{
}

public record Node(List<Pair<MessageType, DialogueMessage>> messages, DialogueAnimation animation, ResourceLocation guiPath, List<Response> responses)
public record Node(List<Pair<MessageType, DialogueMessage>> messages, DialogueAnimationData animation, ResourceLocation guiPath, List<Response> responses)
{
private static final Codec<Pair<MessageType, DialogueMessage>> MESSAGE_CODEC = Codec.mapPair(MessageType.CODEC.fieldOf("type"), DialogueMessage.CODEC.fieldOf("message")).codec();
private static final MapCodec<List<Pair<MessageType, DialogueMessage>>> MESSAGES_MAP_CODEC = Codec.mapEither(DialogueMessage.CODEC.fieldOf("message"), MESSAGE_CODEC.listOf().fieldOf("messages"))
.xmap(either -> either.map(message -> List.of(Pair.of(MessageType.ENTITY, message)), Function.identity()),
messages -> messages.size() == 1 && messages.get(0).getFirst() == MessageType.ENTITY ? Either.left(messages.get(0).getSecond()): Either.right(messages));
public static final Codec<Node> CODEC = RecordCodecBuilder.create(instance -> instance.group(
MESSAGES_MAP_CODEC.forGetter(Node::messages),
PreservingOptionalFieldCodec.withDefault(DialogueAnimation.CODEC, "animation", DialogueAnimation.DEFAULT_ANIMATION).forGetter(Node::animation),
PreservingOptionalFieldCodec.withDefault(DialogueAnimationData.CODEC, "animation", DialogueAnimationData.DEFAULT_ANIMATION).forGetter(Node::animation),
PreservingOptionalFieldCodec.withDefault(ResourceLocation.CODEC, "gui", DEFAULT_GUI).forGetter(Node::guiPath),
PreservingOptionalFieldCodec.forList(Response.LIST_CODEC, "responses").forGetter(Node::responses)
).apply(instance, Node::new));
Expand Down Expand Up @@ -243,14 +243,14 @@ public record SelectableDialogue(ResourceLocation dialogueId, Condition conditio
).apply(instance, SelectableDialogue::new));
}

public record DialogueData(Component message, ResourceLocation guiBackground, List<ResponseData> responses, DialogueAnimation animation, String spriteType)
public record DialogueData(Component message, ResourceLocation guiBackground, List<ResponseData> responses, DialogueAnimationData animationData, String spriteType)
{
public static DialogueData read(FriendlyByteBuf buffer)
{
Component message = buffer.readComponent();
ResourceLocation guiBackground = buffer.readResourceLocation();
List<ResponseData> responses = buffer.readList(ResponseData::read);
DialogueAnimation animation = DialogueAnimation.read(buffer);
DialogueAnimationData animation = DialogueAnimationData.read(buffer);
String spriteType = buffer.readUtf(25);

return new DialogueData(message, guiBackground, responses, animation, spriteType);
Expand All @@ -261,7 +261,7 @@ public void write(FriendlyByteBuf buffer)
buffer.writeComponent(this.message);
buffer.writeResourceLocation(this.guiBackground);
buffer.writeCollection(this.responses, (byteBuf, responseData) -> responseData.write(byteBuf));
this.animation.write(buffer);
this.animationData.write(buffer);
buffer.writeUtf(this.spriteType, 25);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,42 @@

import java.util.Locale;

public record DialogueAnimation(String emotion, int spriteHeight, int spriteWidth)
public record DialogueAnimationData(String emotion, int spriteHeight, int spriteWidth, int xOffset, int yOffset, float scale)
{
public static final int DEFAULT_SPRITE_WIDTH = 128;
public static final int DEFAULT_SPRITE_HEIGHT = 128;

public static final DialogueAnimation DEFAULT_ANIMATION = new DialogueAnimation(Emotion.GENERIC.getSerializedName(), DEFAULT_SPRITE_HEIGHT, DEFAULT_SPRITE_WIDTH);
public static final DialogueAnimationData DEFAULT_ANIMATION = new DialogueAnimationData(Emotion.GENERIC.getSerializedName(), DEFAULT_SPRITE_HEIGHT, DEFAULT_SPRITE_WIDTH, 0, 0, 1.0F);

public static Codec<DialogueAnimation> CODEC = RecordCodecBuilder.create(instance -> instance.group(
PreservingOptionalFieldCodec.withDefault(Codec.STRING, "emotion", Emotion.GENERIC.getSerializedName()).forGetter(DialogueAnimation::emotion),
PreservingOptionalFieldCodec.withDefault(Codec.INT, "height", DEFAULT_SPRITE_HEIGHT).forGetter(DialogueAnimation::spriteHeight),
PreservingOptionalFieldCodec.withDefault(Codec.INT, "width", DEFAULT_SPRITE_WIDTH).forGetter(DialogueAnimation::spriteWidth)
).apply(instance, DialogueAnimation::new));
public static Codec<DialogueAnimationData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
PreservingOptionalFieldCodec.withDefault(Codec.STRING, "emotion", Emotion.GENERIC.getSerializedName()).forGetter(DialogueAnimationData::emotion),
PreservingOptionalFieldCodec.withDefault(Codec.INT, "height", DEFAULT_SPRITE_HEIGHT).forGetter(DialogueAnimationData::spriteHeight),
PreservingOptionalFieldCodec.withDefault(Codec.INT, "width", DEFAULT_SPRITE_WIDTH).forGetter(DialogueAnimationData::spriteWidth),
PreservingOptionalFieldCodec.withDefault(Codec.INT, "x_offset", 0).forGetter(DialogueAnimationData::xOffset),
PreservingOptionalFieldCodec.withDefault(Codec.INT, "y_offset", 0).forGetter(DialogueAnimationData::yOffset),
PreservingOptionalFieldCodec.withDefault(Codec.FLOAT, "scale", 1.0F).forGetter(DialogueAnimationData::scale)
).apply(instance, DialogueAnimationData::new));

public static DialogueAnimation read(FriendlyByteBuf buffer)
public static DialogueAnimationData read(FriendlyByteBuf buffer)
{
String emotion = buffer.readUtf(25);
int height = buffer.readInt();
int width = buffer.readInt();
int xOffset = buffer.readInt();
int yOffset = buffer.readInt();
float scale = buffer.readFloat();

return new DialogueAnimation(emotion, height, width);
return new DialogueAnimationData(emotion, height, width, xOffset, yOffset, scale);
}

public void write(FriendlyByteBuf buffer)
{
buffer.writeUtf(this.emotion, 25);
buffer.writeInt(this.spriteHeight);
buffer.writeInt(this.spriteWidth);
buffer.writeInt(this.xOffset);
buffer.writeInt(this.yOffset);
buffer.writeFloat(this.scale);
}

/**
Expand Down

0 comments on commit d723d18

Please sign in to comment.