feat: Screens
Different types of screens have been implemented. For now only ChestScreen.
This commit is contained in:
@@ -5,6 +5,7 @@ import com.googlecode.lanterna.graphics.TextGraphics;
|
|||||||
import cz.jzitnik.annotations.EventHandler;
|
import cz.jzitnik.annotations.EventHandler;
|
||||||
import cz.jzitnik.annotations.injectors.InjectState;
|
import cz.jzitnik.annotations.injectors.InjectState;
|
||||||
import cz.jzitnik.events.RerenderScreen;
|
import cz.jzitnik.events.RerenderScreen;
|
||||||
|
import cz.jzitnik.game.Constants;
|
||||||
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.Empty;
|
import cz.jzitnik.ui.pixels.Empty;
|
||||||
@@ -42,7 +43,7 @@ public class CliHandler extends AbstractEventHandler<RerenderScreen> {
|
|||||||
for (int y = start.getRow(); y <= end.getRow(); y++) {
|
for (int y = start.getRow(); y <= end.getRow(); y++) {
|
||||||
for (int x = start.getColumn(); x <= end.getColumn(); x++) {
|
for (int x = start.getColumn(); x <= end.getColumn(); x++) {
|
||||||
Pixel pixel = buffer[y][x];
|
Pixel pixel = buffer[y][x];
|
||||||
TextColor color = pixel.getClass().equals(Empty.class) ? new TextColor.RGB(4, 4, 16) : pixel.getColor();
|
TextColor color = pixel.getClass().equals(Empty.class) ? Constants.DEFAULT_COLOR : pixel.getColor();
|
||||||
|
|
||||||
drawPixel(tg, x, y, color);
|
drawPixel(tg, x, y, color);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public class KeyboardPressEventHandler extends AbstractEventHandler<KeyboardPres
|
|||||||
@Override
|
@Override
|
||||||
public void handle(KeyboardPressEvent event) {
|
public void handle(KeyboardPressEvent event) {
|
||||||
if (gameState.getScreen() != null) {
|
if (gameState.getScreen() != null) {
|
||||||
gameState.getScreen().handleKeybordAction(event);
|
gameState.getScreen().handleKeyboardAction(event);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ public class MouseActionEventHandler extends AbstractEventHandler<MouseAction> {
|
|||||||
if (object.isEmpty()) return;
|
if (object.isEmpty()) return;
|
||||||
|
|
||||||
((Interactable) object.get()).interact(dm);
|
((Interactable) object.get()).interact(dm);
|
||||||
|
object.get().setSelected(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public class RoomChangeEventHandler extends AbstractEventHandler<RoomChangeEvent
|
|||||||
case LEFT -> playerCords.updateCords(155, 60);
|
case LEFT -> playerCords.updateCords(155, 60);
|
||||||
case RIGHT -> playerCords.updateCords(30, 50);
|
case RIGHT -> playerCords.updateCords(30, 50);
|
||||||
case TOP -> playerCords.updateCords(90, 110);
|
case TOP -> playerCords.updateCords(90, 110);
|
||||||
case BOTTOM -> playerCords.updateCords(85, 10);
|
case BOTTOM -> playerCords.updateCords(90, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
gameState.setCurrentRoom(newRoom);
|
gameState.setCurrentRoom(newRoom);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import cz.jzitnik.annotations.injectors.InjectDependency;
|
|||||||
import cz.jzitnik.annotations.injectors.InjectState;
|
import cz.jzitnik.annotations.injectors.InjectState;
|
||||||
import cz.jzitnik.events.FullRoomDraw;
|
import cz.jzitnik.events.FullRoomDraw;
|
||||||
import cz.jzitnik.events.TerminalResizeEvent;
|
import cz.jzitnik.events.TerminalResizeEvent;
|
||||||
|
import cz.jzitnik.game.GameState;
|
||||||
import cz.jzitnik.states.ScreenBuffer;
|
import cz.jzitnik.states.ScreenBuffer;
|
||||||
import cz.jzitnik.ui.pixels.Empty;
|
import cz.jzitnik.ui.pixels.Empty;
|
||||||
import cz.jzitnik.ui.pixels.Pixel;
|
import cz.jzitnik.ui.pixels.Pixel;
|
||||||
@@ -27,6 +28,9 @@ public class TerminalResizeEventHandler extends AbstractEventHandler<TerminalRes
|
|||||||
@InjectState
|
@InjectState
|
||||||
private ScreenBuffer screenBuffer;
|
private ScreenBuffer screenBuffer;
|
||||||
|
|
||||||
|
@InjectState
|
||||||
|
private GameState gameState;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(TerminalResizeEvent event) {
|
public void handle(TerminalResizeEvent event) {
|
||||||
TerminalSize size = event.getNewSize();
|
TerminalSize size = event.getNewSize();
|
||||||
@@ -41,6 +45,10 @@ public class TerminalResizeEventHandler extends AbstractEventHandler<TerminalRes
|
|||||||
}
|
}
|
||||||
screenBuffer.setRenderedBuffer(buffer);
|
screenBuffer.setRenderedBuffer(buffer);
|
||||||
|
|
||||||
|
if (gameState.getScreen() != null) {
|
||||||
|
gameState.getScreen().fullRender();
|
||||||
|
} else {
|
||||||
eventManager.emitEvent(new FullRoomDraw(true));
|
eventManager.emitEvent(new FullRoomDraw(true));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
src/main/java/cz/jzitnik/game/Constants.java
Normal file
7
src/main/java/cz/jzitnik/game/Constants.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package cz.jzitnik.game;
|
||||||
|
|
||||||
|
import com.googlecode.lanterna.TextColor;
|
||||||
|
|
||||||
|
public class Constants {
|
||||||
|
public static final TextColor DEFAULT_COLOR = new TextColor.RGB(4, 4, 16);
|
||||||
|
}
|
||||||
@@ -3,14 +3,12 @@ package cz.jzitnik.game;
|
|||||||
import cz.jzitnik.game.objects.GameObject;
|
import cz.jzitnik.game.objects.GameObject;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
|
||||||
public class GameRoom {
|
public class GameRoom {
|
||||||
private GameRoom left;
|
private GameRoom left;
|
||||||
private GameRoom right;
|
private GameRoom right;
|
||||||
@@ -23,4 +21,41 @@ public class GameRoom {
|
|||||||
public void addObject(GameObject gameObject) {
|
public void addObject(GameObject gameObject) {
|
||||||
objects.addFirst(gameObject);
|
objects.addFirst(gameObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setLeft(GameRoom left) {
|
||||||
|
setLeft(left, true);
|
||||||
|
}
|
||||||
|
public void setLeft(GameRoom left, boolean addReference) {
|
||||||
|
this.left = left;
|
||||||
|
if (addReference) {
|
||||||
|
left.setRight(this, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void setRight(GameRoom right) {
|
||||||
|
setRight(right, true);
|
||||||
|
}
|
||||||
|
public void setRight(GameRoom right, boolean addReference) {
|
||||||
|
this.right = right;
|
||||||
|
if (addReference) {
|
||||||
|
right.setLeft(this, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void setUp(GameRoom up) {
|
||||||
|
setUp(up, true);
|
||||||
|
}
|
||||||
|
public void setUp(GameRoom up, boolean addReference) {
|
||||||
|
this.up = up;
|
||||||
|
if (addReference) {
|
||||||
|
up.setDown(this, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void setDown(GameRoom down) {
|
||||||
|
setDown(down, true);
|
||||||
|
}
|
||||||
|
public void setDown(GameRoom down, boolean addReference) {
|
||||||
|
this.down = down;
|
||||||
|
if (addReference) {
|
||||||
|
down.setUp(this, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,9 +20,7 @@ public class GameSetup {
|
|||||||
GameRoom topRightRoom = new GameRoom(ResourceManager.Resource.ROOM3);
|
GameRoom topRightRoom = new GameRoom(ResourceManager.Resource.ROOM3);
|
||||||
|
|
||||||
mainRoom.setRight(rightRoom);
|
mainRoom.setRight(rightRoom);
|
||||||
rightRoom.setLeft(mainRoom);
|
|
||||||
rightRoom.setUp(topRightRoom);
|
rightRoom.setUp(topRightRoom);
|
||||||
topRightRoom.setDown(rightRoom);
|
|
||||||
|
|
||||||
Chest chest = new Chest(resourceManager, new RoomCords(100, 45));
|
Chest chest = new Chest(resourceManager, new RoomCords(100, 45));
|
||||||
mainRoom.addObject(chest);
|
mainRoom.addObject(chest);
|
||||||
|
|||||||
@@ -23,6 +23,6 @@ public final class Chest extends GameObject implements Interactable {
|
|||||||
gameState.setInteracting(this);
|
gameState.setInteracting(this);
|
||||||
ChestScreen chestScreen = dm.getDependencyOrThrow(ChestScreen.class);
|
ChestScreen chestScreen = dm.getDependencyOrThrow(ChestScreen.class);
|
||||||
gameState.setScreen(chestScreen);
|
gameState.setScreen(chestScreen);
|
||||||
chestScreen.initialRender();
|
chestScreen.fullRender();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,47 @@
|
|||||||
package cz.jzitnik.screens;
|
package cz.jzitnik.screens;
|
||||||
|
|
||||||
|
import com.googlecode.lanterna.TerminalSize;
|
||||||
|
import com.googlecode.lanterna.input.KeyStroke;
|
||||||
|
import com.googlecode.lanterna.input.KeyType;
|
||||||
import cz.jzitnik.annotations.Dependency;
|
import cz.jzitnik.annotations.Dependency;
|
||||||
|
import cz.jzitnik.annotations.injectors.InjectDependency;
|
||||||
|
import cz.jzitnik.annotations.injectors.InjectState;
|
||||||
|
import cz.jzitnik.events.FullRoomDraw;
|
||||||
import cz.jzitnik.events.KeyboardPressEvent;
|
import cz.jzitnik.events.KeyboardPressEvent;
|
||||||
import cz.jzitnik.events.MouseAction;
|
import cz.jzitnik.events.MouseAction;
|
||||||
|
import cz.jzitnik.events.RerenderScreen;
|
||||||
|
import cz.jzitnik.game.GameState;
|
||||||
|
import cz.jzitnik.states.ScreenBuffer;
|
||||||
|
import cz.jzitnik.states.TerminalState;
|
||||||
|
import cz.jzitnik.ui.pixels.Empty;
|
||||||
|
import cz.jzitnik.utils.events.EventManager;
|
||||||
|
|
||||||
@Dependency
|
@Dependency
|
||||||
public final class ChestScreen extends Screen {
|
public final class ChestScreen extends Screen {
|
||||||
@Override
|
@InjectDependency
|
||||||
public void initialRender() {
|
private EventManager eventManager;
|
||||||
|
|
||||||
|
@InjectState
|
||||||
|
private ScreenBuffer screenBuffer;
|
||||||
|
|
||||||
|
@InjectState
|
||||||
|
private TerminalState terminalState;
|
||||||
|
|
||||||
|
@InjectState
|
||||||
|
private GameState gameState;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fullRender() {
|
||||||
|
var buffer = screenBuffer.getRenderedBuffer();
|
||||||
|
TerminalSize terminalSize = terminalState.getTerminalScreen().getTerminalSize();
|
||||||
|
|
||||||
|
for (int x = 0; x < terminalSize.getColumns(); x++) {
|
||||||
|
for (int y = 0; y < terminalSize.getRows(); y++) {
|
||||||
|
buffer[y][x] = new Empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
eventManager.emitEvent(RerenderScreen.full(terminalSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -17,7 +50,14 @@ public final class ChestScreen extends Screen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleKeybordAction(KeyboardPressEvent event) {
|
public void handleKeyboardAction(KeyboardPressEvent event) {
|
||||||
|
KeyStroke keyStroke = event.getKeyStroke();
|
||||||
|
|
||||||
|
if (keyStroke.getKeyType() == KeyType.Escape || keyStroke.getCharacter() == 'e') {
|
||||||
|
gameState.setScreen(null);
|
||||||
|
gameState.setInteracting(null);
|
||||||
|
|
||||||
|
eventManager.emitEvent(new FullRoomDraw(true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import cz.jzitnik.events.KeyboardPressEvent;
|
|||||||
import cz.jzitnik.events.MouseAction;
|
import cz.jzitnik.events.MouseAction;
|
||||||
|
|
||||||
public sealed abstract class Screen permits ChestScreen {
|
public sealed abstract class Screen permits ChestScreen {
|
||||||
public abstract void initialRender();
|
public abstract void fullRender();
|
||||||
public abstract void handleMouseAction(MouseAction event);
|
public abstract void handleMouseAction(MouseAction event);
|
||||||
public abstract void handleKeybordAction(KeyboardPressEvent event);
|
public abstract void handleKeyboardAction(KeyboardPressEvent event);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user