feat: Fully working chest
This commit is contained in:
@@ -51,6 +51,8 @@ public class PlayerMoveEventHandler extends AbstractEventHandler<PlayerMoveEvent
|
|||||||
Player player = gameState.getPlayer();
|
Player player = gameState.getPlayer();
|
||||||
RoomCords playerCords = player.getPlayerCords();
|
RoomCords playerCords = player.getPlayerCords();
|
||||||
|
|
||||||
|
int moveStep = 3;
|
||||||
|
|
||||||
int originalPlayerX = playerCords.getX();
|
int originalPlayerX = playerCords.getX();
|
||||||
int originalPlayerY = playerCords.getY();
|
int originalPlayerY = playerCords.getY();
|
||||||
switch (keyStroke.getCharacter()) {
|
switch (keyStroke.getCharacter()) {
|
||||||
@@ -61,7 +63,7 @@ public class PlayerMoveEventHandler extends AbstractEventHandler<PlayerMoveEvent
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
playerCords.updateCords(player.getPlayerCords().getX(), playerCords.getY() - 5);
|
playerCords.updateCords(player.getPlayerCords().getX(), playerCords.getY() - moveStep);
|
||||||
}
|
}
|
||||||
case 'a' -> {
|
case 'a' -> {
|
||||||
if (originalPlayerX <= 30) {
|
if (originalPlayerX <= 30) {
|
||||||
@@ -70,7 +72,7 @@ public class PlayerMoveEventHandler extends AbstractEventHandler<PlayerMoveEvent
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
playerCords.updateCords(player.getPlayerCords().getX() - 5, playerCords.getY());
|
playerCords.updateCords(player.getPlayerCords().getX() - moveStep, playerCords.getY());
|
||||||
}
|
}
|
||||||
case 's' -> {
|
case 's' -> {
|
||||||
if (originalPlayerY >= 110) {
|
if (originalPlayerY >= 110) {
|
||||||
@@ -79,7 +81,7 @@ public class PlayerMoveEventHandler extends AbstractEventHandler<PlayerMoveEvent
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
playerCords.updateCords(player.getPlayerCords().getX(), playerCords.getY() + 5);
|
playerCords.updateCords(player.getPlayerCords().getX(), playerCords.getY() + moveStep);
|
||||||
}
|
}
|
||||||
case 'd' -> {
|
case 'd' -> {
|
||||||
if (originalPlayerX >= 155) {
|
if (originalPlayerX >= 155) {
|
||||||
@@ -88,7 +90,7 @@ public class PlayerMoveEventHandler extends AbstractEventHandler<PlayerMoveEvent
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
playerCords.updateCords(player.getPlayerCords().getX() + 5, playerCords.getY());
|
playerCords.updateCords(player.getPlayerCords().getX() + moveStep, playerCords.getY());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int newPlayerX = playerCords.getX();
|
int newPlayerX = playerCords.getX();
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ public class ResourceManager {
|
|||||||
|
|
||||||
CHEST("chest.png"),
|
CHEST("chest.png"),
|
||||||
|
|
||||||
|
WOODEN_SWORD("tools/wooden_sword.png"),
|
||||||
|
|
||||||
DOORS("rooms/doors.png");
|
DOORS("rooms/doors.png");
|
||||||
|
|
||||||
private final String path;
|
private final String path;
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ public class WoodenSword extends GameItem {
|
|||||||
super(
|
super(
|
||||||
"Wooden sword",
|
"Wooden sword",
|
||||||
new Sword(5),
|
new Sword(5),
|
||||||
resourceManager.getResource(ResourceManager.Resource.PLAYER_RIGHT) // TODO: Change to actual resource
|
resourceManager.getResource(ResourceManager.Resource.WOODEN_SWORD)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import cz.jzitnik.game.utils.RoomCords;
|
|||||||
import cz.jzitnik.states.ScreenBuffer;
|
import cz.jzitnik.states.ScreenBuffer;
|
||||||
import cz.jzitnik.states.TerminalState;
|
import cz.jzitnik.states.TerminalState;
|
||||||
import cz.jzitnik.ui.pixels.ColoredPixel;
|
import cz.jzitnik.ui.pixels.ColoredPixel;
|
||||||
|
import cz.jzitnik.ui.pixels.Empty;
|
||||||
import cz.jzitnik.ui.pixels.Pixel;
|
import cz.jzitnik.ui.pixels.Pixel;
|
||||||
import cz.jzitnik.utils.DependencyManager;
|
import cz.jzitnik.utils.DependencyManager;
|
||||||
import cz.jzitnik.utils.RerenderUtils;
|
import cz.jzitnik.utils.RerenderUtils;
|
||||||
@@ -25,6 +26,7 @@ import cz.jzitnik.utils.events.EventManager;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -55,12 +57,19 @@ public final class Chest extends GameObject implements Interactable, UIClickHand
|
|||||||
this.dependencyManager = dm;
|
this.dependencyManager = dm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int actualDisplayStartX;
|
||||||
|
private int actualDisplayStartY;
|
||||||
|
private int chestUISizeX;
|
||||||
|
private int chestUISizeY;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void interact(DependencyManager dm) {
|
public void interact(DependencyManager dm) {
|
||||||
dm.inject(this);
|
dm.inject(this);
|
||||||
|
|
||||||
log.debug("Interacted with chest");
|
log.debug("Interacted with chest");
|
||||||
|
render(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void render(boolean clear) {
|
||||||
GameRoom currentRoom = gameState.getCurrentRoom();
|
GameRoom currentRoom = gameState.getCurrentRoom();
|
||||||
Player player = gameState.getPlayer();
|
Player player = gameState.getPlayer();
|
||||||
BufferedImage playerTexture = RerenderUtils.getPlayer(resourceManager, player);
|
BufferedImage playerTexture = RerenderUtils.getPlayer(resourceManager, player);
|
||||||
@@ -77,8 +86,8 @@ public final class Chest extends GameObject implements Interactable, UIClickHand
|
|||||||
int chestUIStartX = getCords().getX();
|
int chestUIStartX = getCords().getX();
|
||||||
int chestUIStartY = getCords().getY();
|
int chestUIStartY = getCords().getY();
|
||||||
|
|
||||||
int chestUISizeY = 16 + 4;
|
chestUISizeY = 16 + 4;
|
||||||
int chestUISizeX = 8 + itemCount * 16 + (itemCount - 1);
|
chestUISizeX = 4 + itemCount * 16 + (itemCount - 1) * 2;
|
||||||
|
|
||||||
int chestGUIStartY = chestUIStartY - chestUISizeY - 1;
|
int chestGUIStartY = chestUIStartY - chestUISizeY - 1;
|
||||||
int chestGUIStartX = chestUIStartX + (chest.getWidth() / 2) - (chestUISizeX / 2);
|
int chestGUIStartX = chestUIStartX + (chest.getWidth() / 2) - (chestUISizeX / 2);
|
||||||
@@ -86,14 +95,46 @@ public final class Chest extends GameObject implements Interactable, UIClickHand
|
|||||||
TerminalPosition guiStart = new TerminalPosition(chestGUIStartX / 2, chestGUIStartY);
|
TerminalPosition guiStart = new TerminalPosition(chestGUIStartX / 2, chestGUIStartY);
|
||||||
TerminalPosition guiEnd = new TerminalPosition((chestGUIStartX + chestUISizeX - 1) / 2, ((chestGUIStartY + chestUISizeY - 1) / 2));
|
TerminalPosition guiEnd = new TerminalPosition((chestGUIStartX + chestUISizeX - 1) / 2, ((chestGUIStartY + chestUISizeY - 1) / 2));
|
||||||
|
|
||||||
|
actualDisplayStartX = chestGUIStartX + start.getX() + 2;
|
||||||
|
actualDisplayStartY = chestGUIStartY + start.getY() + 2;
|
||||||
|
|
||||||
|
if (clear) {
|
||||||
|
int chestUISizeX = 4 + (itemCount + 1) * 16 + (itemCount) * 2;
|
||||||
|
int chestGUIStartX2 = chestUIStartX + (chest.getWidth() / 2) - (chestUISizeX / 2);
|
||||||
|
|
||||||
|
for (int y = chestGUIStartY; y < chestGUIStartY + chestUISizeY; y++) {
|
||||||
|
for (int x = chestGUIStartX2; x < chestGUIStartX2 + chestUISizeX; x++) {
|
||||||
|
log.debug("{} {}", x, y);
|
||||||
|
|
||||||
|
int intPixel = RerenderUtils.getPixel(currentRoom, room, null, new HashSet<>(), player, playerTexture, x, y).pixel(); // This will never reach doors so no need to load texture
|
||||||
|
int red = (intPixel >> 16) & 0xff;
|
||||||
|
int green = (intPixel >> 8) & 0xff;
|
||||||
|
int blue = intPixel & 0xff;
|
||||||
|
|
||||||
|
Pixel pixel = new ColoredPixel(new TextColor.RGB(red, green, blue));
|
||||||
|
|
||||||
|
buffer[y + start.getY()][x + start.getX()] = pixel;
|
||||||
|
overrideBuffer[y][x] = new Empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
guiStart = new TerminalPosition(chestGUIStartX2 / 2, chestGUIStartY);
|
||||||
|
guiEnd = new TerminalPosition((chestGUIStartX2 + chestUISizeX - 1) / 2, ((chestGUIStartY + chestUISizeY - 1) / 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!items.isEmpty()) {
|
||||||
for (int y = chestGUIStartY; y < chestGUIStartY + chestUISizeY; y++) {
|
for (int y = chestGUIStartY; y < chestGUIStartY + chestUISizeY; y++) {
|
||||||
for (int x = chestGUIStartX; x < chestGUIStartX + chestUISizeX; x++) {
|
for (int x = chestGUIStartX; x < chestGUIStartX + chestUISizeX; x++) {
|
||||||
Pixel pixel = new ColoredPixel(TextColor.ANSI.RED);
|
int objectX = x - chestGUIStartX;
|
||||||
|
int objectY = y - chestGUIStartY;
|
||||||
|
|
||||||
|
Pixel pixel = getPixel(objectX, objectY, chestUISizeX, chestUISizeY);
|
||||||
|
|
||||||
buffer[y + start.getY()][x + start.getX()] = pixel;
|
buffer[y + start.getY()][x + start.getX()] = pixel;
|
||||||
overrideBuffer[y][x] = pixel;
|
overrideBuffer[y][x] = pixel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RerenderUtils.rerenderPart(guiStart.getColumn(), guiEnd.getColumn(), guiStart.getRow(), guiEnd.getRow(), start.getX(), start.getY(), currentRoom, room, player, playerTexture, screenBuffer, resourceManager);
|
RerenderUtils.rerenderPart(guiStart.getColumn(), guiEnd.getColumn(), guiStart.getRow(), guiEnd.getRow(), start.getX(), start.getY(), currentRoom, room, player, playerTexture, screenBuffer, resourceManager);
|
||||||
|
|
||||||
@@ -102,8 +143,71 @@ public final class Chest extends GameObject implements Interactable, UIClickHand
|
|||||||
eventManager.emitEvent(RerenderScreen.full(terminalState.getTerminalScreen().getTerminalSize())); // TODO: Make this not rerender full screen
|
eventManager.emitEvent(RerenderScreen.full(terminalState.getTerminalScreen().getTerminalSize())); // TODO: Make this not rerender full screen
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Add some padding on the item
|
||||||
|
private Pixel getPixel(int objectX, int objectY, int chestUISizeX, int chestUISizeY) {
|
||||||
|
if (objectX >= 0 && objectX <= 1 || objectX >= chestUISizeX - 2 && objectX <= chestUISizeX - 1 || objectY >= 0 && objectY <= 1 || objectY >= chestUISizeY - 2 && objectY <= chestUISizeY - 1) {
|
||||||
|
return new ColoredPixel(new TextColor.RGB(0, 0, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
int screenX = objectX - 2;
|
||||||
|
int screenY = objectY - 2;
|
||||||
|
int part = screenX / 18;
|
||||||
|
int rest = screenX % 18;
|
||||||
|
|
||||||
|
if (rest == 16 || rest == 17) {
|
||||||
|
return new ColoredPixel(new TextColor.RGB(0, 0, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
GameItem item = items.get(part);
|
||||||
|
BufferedImage texture = item.getTexture();
|
||||||
|
log.debug("{} {}", rest, screenY);
|
||||||
|
int pixel = texture.getRGB(rest, screenY);
|
||||||
|
|
||||||
|
int alpha = (pixel >> 24) & 0xff;
|
||||||
|
|
||||||
|
if (alpha == 0) {
|
||||||
|
return new ColoredPixel(
|
||||||
|
new TextColor.RGB(255, 255, 255)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
int red = (pixel >> 16) & 0xff;
|
||||||
|
int green = (pixel >> 8) & 0xff;
|
||||||
|
int blue = pixel & 0xff;
|
||||||
|
|
||||||
|
return new ColoredPixel(
|
||||||
|
new TextColor.RGB(red, green, blue)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleClick(MouseAction mouseAction) {
|
public void handleClick(MouseAction mouseAction) {
|
||||||
log.debug("Test");
|
int mouseX = mouseAction.getPosition().getColumn();
|
||||||
|
int mouseY = mouseAction.getPosition().getRow();
|
||||||
|
|
||||||
|
log.debug("Mouse: x: {}, y: {}", mouseX, mouseY);
|
||||||
|
|
||||||
|
int screenX = mouseX - actualDisplayStartX;
|
||||||
|
int screenY = (mouseY * 2) - actualDisplayStartY;
|
||||||
|
|
||||||
|
log.debug("Screen: x: {}, y: {}", screenX, screenY);
|
||||||
|
|
||||||
|
int part = screenX / 18;
|
||||||
|
|
||||||
|
if (screenX < 0 || screenY < 0 || screenX > chestUISizeX || screenY > chestUISizeY || part >= items.size()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rest = screenX % 18;
|
||||||
|
if (rest == 17 || rest == 16) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
GameItem item = items.get(part);
|
||||||
|
items.remove(part);
|
||||||
|
|
||||||
|
// TODO: Add item to inventory
|
||||||
|
|
||||||
|
render(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
src/main/resources/textures/tools/wooden_sword.png
Normal file
BIN
src/main/resources/textures/tools/wooden_sword.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 471 B |
Reference in New Issue
Block a user