Compare commits

..

2 Commits

Author SHA1 Message Date
cb488f9853 feat: Show icons in stats 2026-01-20 12:36:18 +01:00
da45765413 feat: Dialog answering 2026-01-19 20:17:13 +01:00
12 changed files with 228 additions and 80 deletions

View File

@@ -7,7 +7,6 @@ import cz.jzitnik.annotations.EventHandler;
import cz.jzitnik.annotations.injectors.InjectDependency;
import cz.jzitnik.annotations.injectors.InjectState;
import cz.jzitnik.events.RerenderScreen;
import cz.jzitnik.game.GameState;
import cz.jzitnik.game.dialog.Dialog;
import cz.jzitnik.game.dialog.OnEnd;
import cz.jzitnik.states.DialogState;
@@ -16,7 +15,6 @@ import cz.jzitnik.states.TerminalState;
import cz.jzitnik.ui.pixels.AlphaPixel;
import cz.jzitnik.ui.pixels.ColoredPixel;
import cz.jzitnik.ui.pixels.Empty;
import cz.jzitnik.ui.pixels.Pixel;
import cz.jzitnik.utils.DependencyManager;
import cz.jzitnik.utils.TextRenderer;
import cz.jzitnik.utils.events.AbstractEventHandler;
@@ -49,22 +47,34 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
@InjectDependency
private TextRenderer textRenderer;
private static final int WIDTH = 350;
private static final int MARGIN_BOTTOM = 15;
private static final int PADDING = 7;
public static final int PADDING = 7;
private static final int BUTTON_TEXT_PADDING = 4;
private static final int QUESTION_ACTIONS_GAP = 10;
private static final int BUTTON_HEIGHT = 15;
private static final int BUTTON_PADDING = 5;
public static final int BUTTON_HEIGHT = 15;
public static final int BUTTON_PADDING = 5;
private static final float FONT_SIZE = 15f;
public static TerminalSize getSize(TextRenderer textRenderer, Dialog dialog) {
int WIDTH = 355;
public static int calculateButtonHeight(Dialog dialog) {
if (dialog.getOnEnd() instanceof OnEnd.AskQuestion(OnEnd.AskQuestion.Answer[] answers)) {
return answers.length * BUTTON_HEIGHT + (answers.length - 1) * BUTTON_PADDING;
} else {
return 0;
}
}
public static int getYStartButtons(TextRenderer textRenderer, Dialog dialog) {
var textSize = textRenderer.measureText(dialog.getText(), WIDTH - PADDING * 2, FONT_SIZE);
return new TerminalSize(355, PADDING + textSize.height + (
dialog.getOnEnd() instanceof OnEnd.AskQuestion(
OnEnd.AskQuestion.Answer[] answers
) ? answers.length * BUTTON_HEIGHT + (answers.length - 1) * BUTTON_PADDING : 0
return PADDING + textSize.height + BUTTON_PADDING;
}
public static TerminalSize getSize(TextRenderer textRenderer, Dialog dialog) {
var textSize = textRenderer.measureText(dialog.getText(), WIDTH - PADDING * 2, FONT_SIZE);
return new TerminalSize(300, PADDING + textSize.height + (
dialog.getOnEnd() instanceof OnEnd.AskQuestion ? BUTTON_PADDING + calculateButtonHeight(dialog) : 0
) + PADDING);
}
@@ -77,6 +87,7 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
@Override
public void handle(Dialog event) {
boolean onlyLast = dialogState.getCurrentDialog() == event;
dialogState.setCurrentDialog(event);
TerminalSize terminalSize = terminalState.getTerminalScreen().getTerminalSize();
var overrideBuffer = screenBuffer.getGlobalOverrideBuffer();
@@ -98,13 +109,15 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
}
}
dialogState.setRenderInProgress(true);
try {
for (AlphaPixel[][] buf : animation) {
for (int i = onlyLast ? animation.length : 0; i <= animation.length; i++) {
var buf = animation[Math.min(i, animation.length - 1)];
for (int y = 0; y < size.getRows(); y++) {
for (int x = 0; x < size.getColumns(); x++) {
var textPixel = buf[Math.min(Math.max(0, y - PADDING), buf.length - 1)][Math.min(Math.max(0, x - PADDING), buf[0].length - 1)];
if (textPixel instanceof Empty || y < PADDING || x < PADDING || x >= size.getColumns() - PADDING || y >= size.getRows() - PADDING) {
if (y - 2 > textSize.height + QUESTION_ACTIONS_GAP && onEnd instanceof OnEnd.AskQuestion(
if (i == animation.length && y - 2 > textSize.height + QUESTION_ACTIONS_GAP && onEnd instanceof OnEnd.AskQuestion(
OnEnd.AskQuestion.Answer[] answers
)) {
int buttonsY = y - textSize.height - QUESTION_ACTIONS_GAP - 2;
@@ -118,7 +131,7 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
var buttonTextPixel = buttonBuf[Math.min(Math.max(0, localY), buttonBuf.length - 1)][Math.min(Math.max(0, localX), buttonBuf[0].length - 1)];
if (buttonTextPixel instanceof Empty || localY < 0 || localX < 0 || localY >= buttonBuf.length || localX >= buttonBuf[0].length) {
overrideBuffer[start.getRow() + y][start.getColumn() + x] = new ColoredPixel(new TextColor.RGB(255, 255, 255), 1f);
overrideBuffer[start.getRow() + y][start.getColumn() + x] = new ColoredPixel(new TextColor.RGB(255, 255, 255), dialogState.getHoveredButtonIndex() == buttonIndex ? 0.8f : 0.6f);
} else {
overrideBuffer[start.getRow() + y][start.getColumn() + x] = buttonTextPixel;
}
@@ -147,11 +160,29 @@ public class DialogEventHandler extends AbstractEventHandler<Dialog> {
Thread.sleep(1000 / event.getTypingSpeed());
}
dialogState.setRenderInProgress(false);
if (onEnd instanceof OnEnd.Continue(Dialog nextDialog)) {
Thread.sleep(1000);
eventManager.emitEvent(nextDialog);
} else if (onEnd instanceof OnEnd.AskQuestion(OnEnd.AskQuestion.Answer[] answers)) {
for (int y = start.getRow(); y < start.getRow() + size.getRows(); y++) {
for (int x = start.getColumn(); x < start.getColumn() + size.getColumns(); x++) {
screenBuffer.getGlobalOverrideBuffer()[y][x] = new Empty();
}
}
if (nextDialog == null) {
dialogState.setCurrentDialog(null);
eventManager.emitEvent(
new RerenderScreen(
new RerenderScreen.ScreenPart(
start,
new TerminalPosition(start.getColumn() + size.getColumns(), start.getRow() + size.getRows())
)
)
);
} else {
eventManager.emitEvent(nextDialog);
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);

View File

@@ -36,7 +36,10 @@ public class ResourceManager {
APPLE("food/apple.png"),
DOORS("rooms/doors.png");
DOORS("rooms/doors.png"),
STAMINA("ui/stamina.png"),
HEART("ui/heart.png");
private final String path;
}

View File

@@ -1,14 +0,0 @@
package cz.jzitnik.game.setup.config;
import cz.jzitnik.game.ResourceManager;
public class EnemiesConfigSetup {
private class Enemy {
private ResourceManager.Resource texture;
private int initialHealth;
}
public void setup() {
}
}

View File

@@ -7,8 +7,6 @@ import cz.jzitnik.game.mobs.DialogMob;
import cz.jzitnik.game.utils.RoomCords;
import cz.jzitnik.utils.roomtasks.RoomTask;
import java.awt.image.BufferedImage;
public class Pepa extends DialogMob {
public Pepa(ResourceManager resourceManager, RoomCords cords) {
super(resourceManager.getResource(ResourceManager.Resource.PLAYER_FRONT), new RoomTask[]{}, cords,
@@ -16,6 +14,8 @@ public class Pepa extends DialogMob {
"Pepa: Never gonna give you up",
new OnEnd.Continue(new Dialog(
"Pepa: Never gonna let you down",
new OnEnd.Continue(new Dialog(
"Pepa: Never gonna run around",
new OnEnd.Continue(new Dialog(
"Pepa: How it continues?", new OnEnd.AskQuestion(
new OnEnd.AskQuestion.Answer[]{
@@ -24,7 +24,11 @@ public class Pepa extends DialogMob {
}
)))
))
))
)
);
setTasks(new RoomTask[]{
});
}
}

View File

@@ -1,4 +1,4 @@
package cz.jzitnik.game.setup;
package cz.jzitnik.game.setup.scenes;
import com.googlecode.lanterna.input.KeyType;
import cz.jzitnik.annotations.injectors.InjectDependency;

View File

@@ -1,4 +1,4 @@
package cz.jzitnik.game.setup;
package cz.jzitnik.game.setup.scenes;
import cz.jzitnik.screens.Screen;
import cz.jzitnik.screens.scenes.Scene;

View File

@@ -8,4 +8,6 @@ import lombok.Data;
@Data
public class DialogState {
private Dialog currentDialog;
private boolean renderInProgress = false;
private int hoveredButtonIndex = -1;
}

View File

@@ -11,11 +11,18 @@ import cz.jzitnik.annotations.ui.UI;
import cz.jzitnik.events.MouseAction;
import cz.jzitnik.events.RerenderScreen;
import cz.jzitnik.events.handlers.DialogEventHandler;
import cz.jzitnik.game.dialog.OnEnd;
import cz.jzitnik.states.DialogState;
import cz.jzitnik.states.ScreenBuffer;
import cz.jzitnik.states.TerminalState;
import cz.jzitnik.ui.pixels.Empty;
import cz.jzitnik.utils.TextRenderer;
import cz.jzitnik.utils.events.Event;
import cz.jzitnik.utils.events.EventManager;
import lombok.extern.slf4j.Slf4j;
import static cz.jzitnik.events.handlers.DialogEventHandler.*;
@Slf4j
@UI
@Dependency
@@ -29,48 +36,129 @@ public class DialogUI {
@InjectDependency
private TextRenderer textRenderer;
@InjectDependency
private EventManager eventManager;
@InjectState
private ScreenBuffer screenBuffer;
@MouseHandler(MouseHandlerType.CLICK)
public boolean handleClick(MouseAction mouseAction) {
if (dialogState.getCurrentDialog() == null) {
if (dialogState.getCurrentDialog() == null || dialogState.isRenderInProgress()) {
return false;
}
TerminalSize size = DialogEventHandler.getSize(textRenderer, dialogState.getCurrentDialog());
TerminalPosition start = DialogEventHandler.getStart(terminalState.getTerminalScreen().getTerminalSize(), size);
if (!(dialogState.getCurrentDialog().getOnEnd() instanceof OnEnd.AskQuestion(
OnEnd.AskQuestion.Answer[] answers
))) {
setHoveredButtonIndex(-1);
return false;
}
TerminalPosition mouse = mouseAction.getPosition();
TerminalPosition mouseNormalized = new TerminalPosition(mouse.getColumn(), mouse.getRow() * 2);
;
RerenderScreen.ScreenPart part = new RerenderScreen.ScreenPart(
start,
new TerminalPosition(start.getColumn() + size.getColumns(), start.getRow() + size.getRows())
);
if (!part.isWithin(mouseNormalized)) {
setHoveredButtonIndex(-1);
return false;
}
TerminalPosition localPosition = new TerminalPosition(mouseNormalized.getColumn() - start.getColumn(), mouseNormalized.getRow() - start.getRow());
return false;
int buttonsStartY = DialogEventHandler.getYStartButtons(textRenderer, dialogState.getCurrentDialog());
TerminalPosition localPosition = new TerminalPosition(mouseNormalized.getColumn() - start.getColumn(), mouseNormalized.getRow() - start.getRow() - buttonsStartY);
int buttonsHeight = DialogEventHandler.calculateButtonHeight(dialogState.getCurrentDialog());
if (localPosition.getRow() < 0 || localPosition.getRow() >= buttonsHeight) {
setHoveredButtonIndex(-1);
return true;
}
int buttonIndex = localPosition.getRow() / (BUTTON_HEIGHT + BUTTON_PADDING);
int rest = localPosition.getRow() % (BUTTON_HEIGHT + BUTTON_PADDING);
if (buttonIndex < answers.length && rest < BUTTON_HEIGHT && localPosition.getColumn() >= PADDING && localPosition.getColumn() < size.getColumns() - PADDING) {
for (int y = start.getRow(); y < start.getRow() + size.getRows(); y++) {
for (int x = start.getColumn(); x < start.getColumn() + size.getColumns(); x++) {
screenBuffer.getGlobalOverrideBuffer()[y][x] = new Empty();
}
}
eventManager.emitEvent(
new Event[]{
new RerenderScreen(
new RerenderScreen.ScreenPart(
start,
new TerminalPosition(start.getColumn() + size.getColumns(), start.getRow() + size.getRows())
)
),
answers[buttonIndex].dialog(),
});
return true;
}
return true;
}
@MouseHandler(MouseHandlerType.MOVE)
public boolean handleMove(MouseAction mouseAction) {
if (dialogState.getCurrentDialog() == null) {
if (dialogState.getCurrentDialog() == null || dialogState.isRenderInProgress()) {
return false;
}
TerminalSize size = DialogEventHandler.getSize(textRenderer, dialogState.getCurrentDialog());
TerminalPosition start = DialogEventHandler.getStart(terminalState.getTerminalScreen().getTerminalSize(), size);
if (!(dialogState.getCurrentDialog().getOnEnd() instanceof OnEnd.AskQuestion(
OnEnd.AskQuestion.Answer[] answers
))) {
setHoveredButtonIndex(-1);
return false;
}
TerminalPosition mouse = mouseAction.getPosition();
TerminalPosition mouseNormalized = new TerminalPosition(mouse.getColumn(), mouse.getRow() * 2);
RerenderScreen.ScreenPart part = new RerenderScreen.ScreenPart(
start,
new TerminalPosition(start.getColumn() + size.getColumns(), start.getRow() + size.getRows())
);
if (!part.isWithin(mouseNormalized)) {
setHoveredButtonIndex(-1);
return false;
}
TerminalPosition localPosition = new TerminalPosition(mouseNormalized.getColumn() - start.getColumn(), mouseNormalized.getRow() - start.getRow());
log.debug("Position: {}", localPosition);
int buttonsStartY = DialogEventHandler.getYStartButtons(textRenderer, dialogState.getCurrentDialog());
TerminalPosition localPosition = new TerminalPosition(mouseNormalized.getColumn() - start.getColumn(), mouseNormalized.getRow() - start.getRow() - buttonsStartY);
int buttonsHeight = DialogEventHandler.calculateButtonHeight(dialogState.getCurrentDialog());
if (localPosition.getRow() < 0 || localPosition.getRow() >= buttonsHeight) {
setHoveredButtonIndex(-1);
return true;
}
int buttonIndex = localPosition.getRow() / (BUTTON_HEIGHT + BUTTON_PADDING);
int rest = localPosition.getRow() % (BUTTON_HEIGHT + BUTTON_PADDING);
if (buttonIndex < answers.length && rest < BUTTON_HEIGHT && localPosition.getColumn() >= PADDING && localPosition.getColumn() < size.getColumns() - PADDING) {
setHoveredButtonIndex(buttonIndex);
return true;
}
setHoveredButtonIndex(-1);
return true;
}
private void setHoveredButtonIndex(int index) {
if (dialogState.getHoveredButtonIndex() != index) {
dialogState.setHoveredButtonIndex(index);
eventManager.emitEvent(dialogState.getCurrentDialog());
}
}
}

View File

@@ -2,27 +2,34 @@ package cz.jzitnik.ui;
import com.googlecode.lanterna.TextColor;
import cz.jzitnik.annotations.Dependency;
import cz.jzitnik.annotations.injectors.InjectDependency;
import cz.jzitnik.annotations.injectors.InjectState;
import cz.jzitnik.annotations.ui.Render;
import cz.jzitnik.annotations.ui.UI;
import cz.jzitnik.game.GameState;
import cz.jzitnik.game.Player;
import cz.jzitnik.game.ResourceManager;
import cz.jzitnik.states.ScreenBuffer;
import cz.jzitnik.ui.pixels.ColoredPixel;
import cz.jzitnik.ui.pixels.Empty;
import cz.jzitnik.ui.pixels.Pixel;
import cz.jzitnik.utils.RerenderUtils;
import lombok.Getter;
import java.awt.image.BufferedImage;
@Getter
@UI
@Dependency
public class Stats {
public static final int BARS_COUNT = 2;
public static final int BAR_WIDTH = 72;
public static final int ICON_SIZE = 9;
public static final int ICON_BAR_MARGIN = 2;
public static final int BAR_HEIGHT = 8;
public static final int BAR_PADDING = 2;
public static final int HEIGHT = BAR_HEIGHT * BARS_COUNT + (BAR_PADDING * (BARS_COUNT - 1));
public static final int WIDTH = BAR_WIDTH;
public static final int WIDTH = BAR_WIDTH + ICON_SIZE + ICON_BAR_MARGIN;
public static final int OFFSET_X = 5;
public static final int OFFSET_Y = 5;
@@ -36,6 +43,9 @@ public class Stats {
@InjectState
private ScreenBuffer screenBuffer;
@InjectDependency
private ResourceManager resourceManager;
@Render
public void rerender() {
var buffer = screenBuffer.getRenderedBuffer();
@@ -49,20 +59,38 @@ public class Stats {
for (int x = 0; x < BAR_WIDTH; x++) {
for (int y = 0; y < BAR_HEIGHT; y++) {
if (x == 0 || y == 0 || x == BAR_WIDTH - 1 || y == BAR_HEIGHT - 1 || x - 1 < healthAmount) {
buffer[y + OFFSET_Y][x + OFFSET_X] = HEALTH_COLOR;
buffer[y + OFFSET_Y][x + OFFSET_X + ICON_SIZE + ICON_BAR_MARGIN] = HEALTH_COLOR;
} else {
buffer[y + OFFSET_Y][x + OFFSET_X] = new Empty();
buffer[y + OFFSET_Y][x + OFFSET_X + ICON_SIZE + ICON_BAR_MARGIN] = new Empty();
}
}
}
BufferedImage heartImage = resourceManager.getResource(ResourceManager.Resource.HEART);
for (int x = 0; x < heartImage.getWidth(); x++) {
for (int y = 0; y < heartImage.getHeight(); y++) {
var pixelData = RerenderUtils.getPixelData(heartImage.getRGB(x, y));
buffer[y + OFFSET_Y][x + OFFSET_X] = new ColoredPixel(new TextColor.RGB(pixelData.r(), pixelData.g(), pixelData.b()));
}
}
for (int x = 0; x < BAR_WIDTH; x++) {
for (int y = BAR_HEIGHT + BAR_PADDING; y < BAR_PADDING + BAR_HEIGHT * 2; y++) {
if (x == 0 || y == BAR_HEIGHT + BAR_PADDING || x == BAR_WIDTH - 1 || y == BAR_PADDING + BAR_HEIGHT * 2 - 1 || x - 1 < staminaAmount) {
buffer[y + OFFSET_Y][x + OFFSET_X] = STAMINA_COLOR;
buffer[y + OFFSET_Y][x + OFFSET_X + ICON_SIZE + ICON_BAR_MARGIN] = STAMINA_COLOR;
} else {
buffer[y + OFFSET_Y][x + OFFSET_X] = new Empty();
}
buffer[y + OFFSET_Y][x + OFFSET_X + ICON_SIZE + ICON_BAR_MARGIN] = new Empty();
}
}
}
BufferedImage staminaImage = resourceManager.getResource(ResourceManager.Resource.STAMINA);
for (int x = 0; x < staminaImage.getWidth(); x++) {
for (int y = 0; y < staminaImage.getHeight(); y++) {
var pixelData = RerenderUtils.getPixelData(staminaImage.getRGB(x, y));
buffer[y + OFFSET_Y + BAR_HEIGHT + BAR_PADDING][x + OFFSET_X] = new ColoredPixel(new TextColor.RGB(pixelData.r(), pixelData.g(), pixelData.b()));
}
}
}

View File

@@ -95,17 +95,15 @@ public class RerenderUtils {
RoomCords endObjectCords = new RoomCords(startObjectCords.getX() + texture.getWidth() - 1, startObjectCords.getY() + texture.getHeight() - 1);
if (x >= startObjectCords.getX() && x <= endObjectCords.getX() && y >= startObjectCords.getY() && y <= endObjectCords.getY()) {
int pixel = texture.getRGB(x - startObjectCords.getX(), y - startObjectCords.getY());
int alpha = (pixel >> 24) & 0xff;
int r = (pixel >> 16) & 0xff;
int g = (pixel >> 8) & 0xff;
int b = pixel & 0xff;
var pixelData = getPixelData(pixel);
int r, g, b;
if (alpha != 0) {
if (pixelData.alpha != 0) {
if (isSelected) {
r = Math.min(255, (int) (r * factor));
g = Math.min(255, (int) (g * factor));
b = Math.min(255, (int) (b * factor));
pixel = (alpha << 24) | (r << 16) | (g << 8) | b;
r = Math.min(255, (int) (pixelData.r * factor));
g = Math.min(255, (int) (pixelData.g * factor));
b = Math.min(255, (int) (pixelData.b * factor));
pixel = (pixelData.alpha << 24) | (r << 16) | (g << 8) | b;
}
return new PixelResult(pixel, false);
@@ -121,17 +119,15 @@ public class RerenderUtils {
if (x >= startDroppedItemCords.getX() && x <= endDroppedItemCords.getX() && y >= startDroppedItemCords.getY() && y <= endDroppedItemCords.getY()) {
int pixel = texture.getRGB(x - startDroppedItemCords.getX(), y - startDroppedItemCords.getY());
int alpha = (pixel >> 24) & 0xff;
int r = (pixel >> 16) & 0xff;
int g = (pixel >> 8) & 0xff;
int b = pixel & 0xff;
var pixelData = getPixelData(pixel);
int r, g, b;
if (alpha != 0) {
if (pixelData.alpha != 0) {
if (isSelected) {
r = Math.min(255, (int) (r * factor));
g = Math.min(255, (int) (g * factor));
b = Math.min(255, (int) (b * factor));
pixel = (alpha << 24) | (r << 16) | (g << 8) | b;
r = Math.min(255, (int) (pixelData.r * factor));
g = Math.min(255, (int) (pixelData.g * factor));
b = Math.min(255, (int) (pixelData.b * factor));
pixel = (pixelData.alpha << 24) | (r << 16) | (g << 8) | b;
}
return new PixelResult(pixel, false);
@@ -147,17 +143,15 @@ public class RerenderUtils {
RoomCords endObjectCords = new RoomCords(startObjectCords.getX() + texture.getWidth() - 1, startObjectCords.getY() + texture.getHeight() - 1);
if (x >= startObjectCords.getX() && x <= endObjectCords.getX() && y >= startObjectCords.getY() && y <= endObjectCords.getY()) {
int pixel = texture.getRGB(x - startObjectCords.getX(), y - startObjectCords.getY());
int alpha = (pixel >> 24) & 0xff;
int r = (pixel >> 16) & 0xff;
int g = (pixel >> 8) & 0xff;
int b = pixel & 0xff;
var pixelData = getPixelData(pixel);
int r, g, b;
if (alpha != 0) {
if (pixelData.alpha != 0) {
if (isSelected) {
r = Math.min(255, (int) (r * factor));
g = Math.min(255, (int) (g * factor));
b = Math.min(255, (int) (b * factor));
pixel = (alpha << 24) | (r << 16) | (g << 8) | b;
r = Math.min(255, (int) (pixelData.r * factor));
g = Math.min(255, (int) (pixelData.g * factor));
b = Math.min(255, (int) (pixelData.b * factor));
pixel = (pixelData.alpha << 24) | (r << 16) | (g << 8) | b;
}
return new PixelResult(pixel, false);
@@ -197,4 +191,16 @@ public class RerenderUtils {
return doorPositions;
}
public record ColorData(int r, int g, int b, int alpha) {
}
public static ColorData getPixelData(int pixel) {
int alpha = (pixel >> 24) & 0xff;
int r = (pixel >> 16) & 0xff;
int g = (pixel >> 8) & 0xff;
int b = pixel & 0xff;
return new ColorData(r, g, b, alpha);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 B