Files
src
build.gradle
gradle.properties
gradlew
gradlew.bat
gradlew.ps1
LICENSE
settings.gradle
Code
97 2,340
src/main/java/com/kodari/cardgame/CardGameClient.java
package com.kodari.cardgame;
import java.util.List;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.RotationAxis;
public final class CardGameClient implements ClientModInitializer {
private static final int PANEL_WIDTH = 440;
private static final int PANEL_HEIGHT = 96;
private static final int PANEL_COLOR = 0xE6101726;
private static final int PANEL_SHADOW = 0x90000000;
private static CardItem activationCard;
private static int activationTicks = -1;
private static AbilityType feedbackAbility;
private static int feedbackTicks = -1;
@Override
public void onInitializeClient() {
ClientPlayNetworking.registerGlobalReceiver(CardGameMod.CARD_ACTIVATION_PACKET,
(client, player, handler, buffer, responseSender) -> {
int cardOrdinal = buffer.readVarInt();
client.execute(() -> {
if (cardOrdinal < 0 || cardOrdinal >= CardType.values().length) {
return;
}
activationCard = cardForType(CardType.values()[cardOrdinal]);
activationTicks = 0;
});
});
ClientPlayNetworking.registerGlobalReceiver(CardGameMod.ABILITY_FEEDBACK_PACKET,
(client, player, handler, buffer, responseSender) -> {
int abilityOrdinal = buffer.readVarInt();
client.execute(() -> {
if (abilityOrdinal < 0 || abilityOrdinal >= AbilityType.values().length) {
return;
}
feedbackAbility = AbilityType.values()[abilityOrdinal];
feedbackTicks = 0;
});
});
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (activationTicks >= 0 && ++activationTicks > 52) {
activationTicks = -1;
activationCard = null;
}
if (feedbackTicks >= 0 && ++feedbackTicks > 24) {
feedbackTicks = -1;
feedbackAbility = null;
}
});
HudRenderCallback.EVENT.register(CardGameClient::renderHud);
}
private static void renderHud(DrawContext context, float tickDelta) {
MinecraftClient client = MinecraftClient.getInstance();
ClientPlayerEntity player = client.player;
if (player == null || client.world == null || client.currentScreen != null) {
return;
}
context.getMatrices().push();
if (activationTicks >= 0) {
float shake = (float) Math.sin((activationTicks + tickDelta) * 2.8f) * (1.0f - Math.min(activationTicks / 18.0f, 1.0f)) * 1.7f;
context.getMatrices().translate(shake, -shake * 0.35f, 0.0f);
}
renderActivationAnimation(context, tickDelta);
renderAbilityFeedback(context, tickDelta);
CardItem card = activeCard(player);
if (card == null) {
context.getMatrices().pop();
return;
}
int x = (context.getScaledWindowWidth() - PANEL_WIDTH) / 2;
int y = context.getScaledWindowHeight() - 116;
int accent = accentColor(card.getCardType());
context.fill(x + 3, y + 4, x + PANEL_WIDTH + 3, y + PANEL_HEIGHT + 4, PANEL_SHADOW);
context.fill(x - 3, y + 3, x + PANEL_WIDTH + 3, y + PANEL_HEIGHT - 3, withAlpha(0x24324A, 0.45f));
context.fill(x, y, x + PANEL_WIDTH, y + PANEL_HEIGHT, PANEL_COLOR);
context.fill(x, y, x + PANEL_WIDTH, y + 2, accent);
context.fill(x, y + PANEL_HEIGHT - 2, x + PANEL_WIDTH, y + PANEL_HEIGHT, accent);
context.fill(x, y, x + 2, y + PANEL_HEIGHT, accent);
context.fill(x + PANEL_WIDTH - 2, y, x + PANEL_WIDTH, y + PANEL_HEIGHT, accent);
context.fill(x + 7, y + 7, x + 132, y + PANEL_HEIGHT - 7, 0xA91E2838);
context.fill(x + 10, y + 10, x + 13, y + PANEL_HEIGHT - 10, withAlpha(accent, 0.7f));
context.drawItem(new ItemStack(card), x + 16, y + 20);
context.drawText(client.textRenderer, Text.translatable("hud.cardgame.active"), x + 42, y + 12, accent, false);
context.drawText(client.textRenderer, card.getName(), x + 42, y + 27, 0xFFFFFFFF, false);
context.drawText(client.textRenderer, Text.translatable("hud.cardgame.deactivate"), x + 16, y + 74, 0xFF9DA8B8, false);
List<Item> abilities = CardGameMod.abilitiesFor(card);
for (int index = 0; index < abilities.size(); index++) {
int column = index % 4;
int row = index / 4;
renderAbility(context, client, player, (AbilityItem) abilities.get(index),
x + 140 + column * 74, y + 8 + row * 39, tickDelta, accent);
}
context.getMatrices().pop();
}
private static void renderActivationAnimation(DrawContext context, float tickDelta) {
if (activationCard == null || activationTicks < 0) {
return;
}
float age = activationTicks + tickDelta;
float progress = Math.min(age / 46.0f, 1.0f);
float fade = progress < 0.18f ? progress / 0.18f : 1.0f - (progress - 0.18f) / 0.82f;
fade = Math.max(0.0f, Math.min(1.0f, fade));
float entrance = Math.min(progress / 0.23f, 1.0f);
float ease = 1.0f - (float) Math.pow(1.0f - entrance, 3.0);
float scale = 1.8f + ease * 3.5f;
int centerX = context.getScaledWindowWidth() / 2;
int centerY = context.getScaledWindowHeight() / 2 - 14 + Math.round((1.0f - ease) * 26.0f);
int accent = accentColor(activationCard.getCardType());
float pulse = (float) Math.sin(progress * Math.PI * 5.0) * 0.5f + 0.5f;
int width = context.getScaledWindowWidth();
int height = context.getScaledWindowHeight();
context.fill(0, 0, width, height, withAlpha(0x050914, fade * 0.34f));
context.fill(centerX - 72, centerY - 72, centerX + 72, centerY + 72, withAlpha(0x0C1425, fade * 0.86f));
context.fill(centerX - 66, centerY - 66, centerX + 66, centerY - 64, withAlpha(accent, fade * 0.85f));
context.fill(centerX - 66, centerY + 64, centerX + 66, centerY + 66, withAlpha(accent, fade * 0.85f));
context.fill(centerX - 66, centerY - 66, centerX - 64, centerY + 66, withAlpha(accent, fade * 0.85f));
context.fill(centerX + 64, centerY - 66, centerX + 66, centerY + 66, withAlpha(accent, fade * 0.85f));
for (int ring = 0; ring < 3; ring++) {
int radius = 48 + ring * 17 + Math.round(pulse * 5.0f);
int alpha = fade * (0.22f + ring * 0.08f) > 1.0f ? 255 : Math.round(fade * (0.22f + ring * 0.08f) * 255.0f);
for (int spoke = 0; spoke < 16; spoke++) {
double angle = (Math.PI * 2.0 * spoke / 16.0) + progress * (ring % 2 == 0 ? 1.2 : -0.8);
int px = centerX + Math.round((float) Math.cos(angle) * radius);
int py = centerY + Math.round((float) Math.sin(angle) * radius);
context.fill(px - 2, py - 2, px + 3, py + 3, alpha << 24 | (accent & 0xFFFFFF));
}
}
context.getMatrices().push();
context.getMatrices().translate(centerX, centerY, 300.0);
context.getMatrices().scale(scale, scale, 1.0f);
context.getMatrices().multiply(RotationAxis.POSITIVE_Z.rotationDegrees((float) Math.sin(progress * Math.PI * 2.0) * 7.0f));
context.fill(-10, -10, 10, 10, withAlpha(0x000000, fade * 0.7f));
context.drawItem(new ItemStack(activationCard), -8, -8);
context.getMatrices().pop();
MinecraftClient client = MinecraftClient.getInstance();
drawCentered(context, client, activationCard.getName(), centerX, centerY + 82, withAlpha(0xFFFFFF, fade), false);
drawCentered(context, client, Text.literal("CARD AWAKENED"), centerX, centerY + 96, withAlpha(accent, fade), false);
}
private static void renderAbilityFeedback(DrawContext context, float tickDelta) {
if (feedbackAbility == null || feedbackTicks < 0) {
return;
}
MinecraftClient client = MinecraftClient.getInstance();
float age = feedbackTicks + tickDelta;
float fade = age < 4.0f ? age / 4.0f : 1.0f - Math.max(0.0f, (age - 16.0f) / 8.0f);
fade = Math.max(0.0f, Math.min(1.0f, fade));
int accent = accentColor(cardForAbility(feedbackAbility));
int centerX = context.getScaledWindowWidth() / 2;
int y = context.getScaledWindowHeight() / 2 + 70 - Math.round(Math.min(age, 8.0f) * 2.0f);
Text abilityName = abilityName(feedbackAbility);
int width = Math.max(150, client.textRenderer.getWidth(abilityName) + 54);
context.fill(centerX - width / 2 + 3, y + 3, centerX + width / 2 + 3, y + 27, withAlpha(0x000000, fade * 0.65f));
context.fill(centerX - width / 2, y, centerX + width / 2, y + 24, withAlpha(0x101A2A, fade * 0.94f));
context.fill(centerX - width / 2, y, centerX + width / 2, y + 2, withAlpha(accent, fade));
drawCentered(context, client, abilityName, centerX, y + 7, withAlpha(0xFFFFFF, fade), false);
context.fill(centerX - width / 2, y + 22, centerX - width / 2 + Math.round(width * Math.min(age / 24.0f, 1.0f)), y + 24, withAlpha(accent, fade));
}
private static void drawCentered(DrawContext context, MinecraftClient client, Text text, int centerX, int y, int color,
boolean shadow) {
context.drawText(client.textRenderer, text, centerX - client.textRenderer.getWidth(text) / 2, y, color, shadow);
}
private static int withAlpha(int rgb, float opacity) {
int alpha = Math.round(Math.max(0.0f, Math.min(1.0f, opacity)) * 255.0f);
return alpha << 24 | (rgb & 0xFFFFFF);
}
private static CardItem cardForType(CardType type) {
return switch (type) {
case FIRE -> CardGameMod.FIRE_CARD;
case ICE -> CardGameMod.ICE_CARD;