Compare commits

...

2 Commits

Author SHA1 Message Date
c6cc5faba2 feat: Idk 2025-09-24 12:27:17 +02:00
bfafe8577a feat: Zombie logic 2025-09-24 10:58:55 +02:00
16 changed files with 327 additions and 33 deletions

View File

@ -31,6 +31,7 @@ import lombok.Setter;
import org.jline.terminal.Terminal; import org.jline.terminal.Terminal;
import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -72,6 +73,10 @@ public class Game {
Generation.generateWorld(this); Generation.generateWorld(this);
} }
public boolean isNight(){
return daytime > 200 && daytime < 400;
}
/** /**
* Returns the current coordinates of the player (bottom half). * Returns the current coordinates of the player (bottom half).
* *
@ -96,6 +101,12 @@ public class Game {
return null; return null;
} }
public Point getPlayerCordsPoint() {
int[] cords = getPlayerCords();
return new Point(cords[0], cords[1]);
}
/** /**
* Moves the player one block to the right if possible. * Moves the player one block to the right if possible.
* *
@ -610,7 +621,7 @@ public class Game {
* @return true if any block is not a ghost block. * @return true if any block is not a ghost block.
*/ */
public boolean isSolid(List<Block> blocks) { public boolean isSolid(List<Block> blocks) {
return !blocks.stream().allMatch(Block::isGhost); return !blocks.stream().allMatch(block -> block.isGhost() || block.isMob());
} }
/** /**

View File

@ -0,0 +1,12 @@
package cz.jzitnik.game.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@RequireAnnotation(BlockRegistry.class)
public @interface FireImmune {
}

View File

@ -0,0 +1,12 @@
package cz.jzitnik.game.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@RequireAnnotation(BlockRegistry.class)
public @interface LightBurningMob {
}

View File

@ -3,10 +3,12 @@ package cz.jzitnik.game.entities.items.registry.mobs;
import cz.jzitnik.game.SpriteLoader; import cz.jzitnik.game.SpriteLoader;
import cz.jzitnik.game.annotations.CustomMobFallingAfterMineLogic; import cz.jzitnik.game.annotations.CustomMobFallingAfterMineLogic;
import cz.jzitnik.game.annotations.EntityRegistry; import cz.jzitnik.game.annotations.EntityRegistry;
import cz.jzitnik.game.annotations.LightBurningMob;
import cz.jzitnik.game.entities.Block; import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.mobs.services.zombie.ZombieData; import cz.jzitnik.game.mobs.services.zombie.ZombieData;
import cz.jzitnik.game.mobs.services.zombie.ZombieFallingAfterMineLogic; import cz.jzitnik.game.mobs.services.zombie.ZombieFallingAfterMineLogic;
@LightBurningMob
@CustomMobFallingAfterMineLogic(ZombieFallingAfterMineLogic.class) @CustomMobFallingAfterMineLogic(ZombieFallingAfterMineLogic.class)
@EntityRegistry("zombie") @EntityRegistry("zombie")
public class Zombie extends Block { public class Zombie extends Block {

View File

@ -27,11 +27,11 @@ public class Generation {
int[] terrainHeight = PopulateWorld.generateTerrain(); int[] terrainHeight = PopulateWorld.generateTerrain();
game.getInventory().addItem(ItemBlockSupplier.getItem("lava_bucket"));
game.getPlayer().setPlayerBlock1(steveBlock); game.getPlayer().setPlayerBlock1(steveBlock);
game.getPlayer().setPlayerBlock2(steveBlock2); game.getPlayer().setPlayerBlock2(steveBlock2);
game.getInventory().addItem(ItemBlockSupplier.getItem("sand"));
PopulateWorld.populateWorld(world, terrainHeight); PopulateWorld.populateWorld(world, terrainHeight);
Trees.plantTrees(world, terrainHeight); Trees.plantTrees(world, terrainHeight);

View File

@ -0,0 +1,71 @@
package cz.jzitnik.game.logic.services.burning;
import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.CustomLogic;
import cz.jzitnik.game.annotations.LightBurningMob;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.entities.items.Item;
import cz.jzitnik.game.logic.CustomLogicInterface;
import cz.jzitnik.tui.ScreenRenderer;
@CustomLogic
public class LightBurningMobLogic implements CustomLogicInterface {
private static final int RADIUS = 30;
@Override
public void nextIteration(Game game, ScreenRenderer screenRenderer) {
if (!game.isNight()) {
return;
}
int[] data = game.getPlayerCords();
var world = game.getWorld();
int playerX = data[0];
int playerY = data[1];
int startX = Math.max(0, playerX - RADIUS);
int startY = Math.max(0, playerY - RADIUS);
int endX = Math.min(world[0].length - 1, playerX + RADIUS);
int endY = Math.min(world.length - 1, playerY + RADIUS);
for (int y = startY; y <= endY; y++) {
for (int x = startX; x <= endX; x++) {
var blocks = world[y][x];
for (Block block : blocks) {
if (block.getClass().isAnnotationPresent(LightBurningMob.class)) {
int dealDamage = game.getInventory().getItemInHand().map(Item::getDealDamage).orElse(1);
if (block.getHp() - dealDamage <= 0) {
// Mob is killed
world[y][x].remove(block);
} else {
block.decreaseHp(dealDamage);
block.setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
.setHurtAnimation(true, block.getSpriteState().get()));
if (block.getLinkedMobTexture() != null) {
block.getLinkedMobTexture().setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
.setHurtAnimation(true, block.getLinkedMobTexture().getSpriteState().get()));
}
}
new Thread(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
block.setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
.setHurtAnimation(false, block.getSpriteState().get()));
if (block.getLinkedMobTexture() != null) {
block.getLinkedMobTexture().setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
.setHurtAnimation(false, block.getLinkedMobTexture().getSpriteState().get()));
}
}).start();
}
}
}
}
}
}

View File

@ -0,0 +1,4 @@
package cz.jzitnik.game.logic.services.burning;
public class MobsBurning {
}

View File

@ -32,7 +32,7 @@ public class FireSpreadingLogic implements CustomLogicInterface {
var blocks = world[y][x]; var blocks = world[y][x];
for (Block block : blocks) { for (Block block : blocks) {
if (block.isOnFire() && block.getClass().getAnnotation(Flamable.class).value()) { if ((block.isOnFire() && !block.isMob()) && block.getClass().getAnnotation(Flamable.class).value()) {
int maxTime = random.nextInt(30) + 15; int maxTime = random.nextInt(30) + 15;
block.setBurningTime2(block.getBurningTime2() + 1); block.setBurningTime2(block.getBurningTime2() + 1);
if (block.getBurningTime2() >= maxTime) { if (block.getBurningTime2() >= maxTime) {

View File

@ -4,8 +4,10 @@ import java.util.Random;
import cz.jzitnik.game.Game; import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.CustomLogic; import cz.jzitnik.game.annotations.CustomLogic;
import cz.jzitnik.game.annotations.FireImmune;
import cz.jzitnik.game.annotations.Flamable; import cz.jzitnik.game.annotations.Flamable;
import cz.jzitnik.game.entities.Block; import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.entities.items.Item;
import cz.jzitnik.game.logic.CustomLogicInterface; import cz.jzitnik.game.logic.CustomLogicInterface;
import cz.jzitnik.tui.ScreenRenderer; import cz.jzitnik.tui.ScreenRenderer;
@ -48,13 +50,46 @@ public class LavaFireLogic implements CustomLogicInterface {
} }
for (Block block : world[y][x]) { for (Block block : world[y][x]) {
if (block.getClass().isAnnotationPresent(Flamable.class) && !block.isOnFire()) { if ((block.getClass().isAnnotationPresent(Flamable.class) || block.isMob()) && !block.isOnFire()) {
int maxTime = random.nextInt(8) + 5; int maxTime = random.nextInt(8) + 5;
if (!block.isMob()) {
block.setBurningTime(block.getBurningTime() + 1); block.setBurningTime(block.getBurningTime() + 1);
if (block.getBurningTime() >= maxTime) { if (block.getBurningTime() >= maxTime) {
block.setOnFire(true); block.setOnFire(true);
block.setBurningTime(0); block.setBurningTime(0);
} }
} else if (!block.getClass().isAnnotationPresent(FireImmune.class)) {
int dealDamage = game.getInventory().getItemInHand().map(Item::getDealDamage).orElse(1);
if (block.getHp() - dealDamage <= 0) {
// Mob is killed
world[y][x].remove(block);
} else {
block.decreaseHp(dealDamage);
block.setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
.setHurtAnimation(true, block.getSpriteState().get()));
if (block.getLinkedMobTexture() != null) {
block.getLinkedMobTexture().setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
.setHurtAnimation(true, block.getLinkedMobTexture().getSpriteState().get()));
}
}
new Thread(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
block.setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
.setHurtAnimation(false, block.getSpriteState().get()));
if (block.getLinkedMobTexture() != null) {
block.getLinkedMobTexture().setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
.setHurtAnimation(false, block.getLinkedMobTexture().getSpriteState().get()));
}
}).start();
}
} }
} }
} }

View File

@ -0,0 +1,22 @@
package cz.jzitnik.game.logic.services.mobs;
import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.CustomLogic;
import cz.jzitnik.game.entities.items.registry.mobs.Zombie;
import cz.jzitnik.game.logic.CustomLogicInterface;
import cz.jzitnik.tui.ScreenRenderer;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@CustomLogic
public class ZombieHurtLogic implements CustomLogicInterface {
@Override
public void nextIteration(Game game, ScreenRenderer screenRenderer) {
var world = game.getWorld();
var playerCords = game.getPlayerCordsPoint();
if (world[playerCords.y][playerCords.x].stream().anyMatch(block -> block.getClass().equals(Zombie.class))) {
game.getPlayer().dealDamage(game, screenRenderer);
}
}
}

View File

@ -7,6 +7,5 @@ import lombok.Setter;
@Setter @Setter
public class ZombieData { public class ZombieData {
private int lastDirection = 1; // 1 = right, -1 = left private int lastDirection = 1; // 1 = right, -1 = left
private int movementCooldown = 0;
private int jumpAttempts = 0; private int jumpAttempts = 0;
} }

View File

@ -1,13 +1,140 @@
package cz.jzitnik.game.mobs.services.zombie; package cz.jzitnik.game.mobs.services.zombie;
import cz.jzitnik.game.annotations.EntityLogic; import cz.jzitnik.game.annotations.EntityLogic;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.mobs.EntityLogicInterface; import cz.jzitnik.game.mobs.EntityLogicInterface;
import cz.jzitnik.game.mobs.EntityLogicProvider; import cz.jzitnik.game.mobs.EntityLogicProvider;
import cz.jzitnik.game.sprites.Zombie;
import lombok.extern.slf4j.Slf4j;
import java.awt.*;
import java.util.List;
@Slf4j
@EntityLogic("zombie") @EntityLogic("zombie")
public class ZombieMoveLogic implements EntityLogicInterface { public class ZombieMoveLogic implements EntityLogicInterface {
@Override @Override
public void nextIteration(EntityLogicProvider.EntityLogicMobDTO entityLogicMobDTO) { public void nextIteration(EntityLogicProvider.EntityLogicMobDTO entityLogicMobDTO) {
int zombieX = entityLogicMobDTO.getX();
int zombieY = entityLogicMobDTO.getY();
var game = entityLogicMobDTO.getGame();
var zombie = entityLogicMobDTO.getMob();
// Skip updates for top half
if (zombie.getSpriteState().get() == Zombie.ZombieState.TOP ||
zombie.getSpriteState().get() == Zombie.ZombieState.TOP_HURT) {
log.debug("Skipping top half of zombie at ({},{})", zombieX, zombieY);
return;
}
log.debug("Zombie tick @ ({},{})", zombieX, zombieY);
var world = game.getWorld();
var zombieData = (ZombieData) zombie.getData();
// Player coordinates
Point playerCords = game.getPlayerCordsPoint();
int playerX = playerCords.x;
int playerY = playerCords.y;
log.debug("Player at ({},{})", playerX, playerY);
// Decide direction
int direction = Integer.compare(playerX, zombieX);
zombieData.setLastDirection(direction);
log.debug("Zombie direction: {}", direction);
if (direction == 0) {
return;
}
boolean updated = false;
int newZombieX = zombieX;
int newZombieY = zombieY;
// Check bounds
if (zombieX + direction < 0 || zombieX + direction >= world[0].length) {
log.debug("Zombie cannot move, out of bounds at X: {}", zombieX + direction);
} else {
// Blocks ahead
List<Block> blocksAhead = world[zombieY][zombieX + direction];
log.debug("Blocks ahead solid? {}", game.isSolid(blocksAhead));
if (!game.isSolid(blocksAhead)) {
log.debug("Path clear, moving to ({},{})", zombieX + direction, zombieY);
moveZombie(zombie, zombieX, zombieY, zombieX + direction, zombieY, world);
newZombieX = zombieX + direction;
updated = true;
zombieData.setJumpAttempts(0);
} else {
log.debug("Path blocked, checking jump options");
List<Block> blocksAboveAhead = world[zombieY - 1][zombieX + direction];
List<Block> blocksTwoAboveAhead = world[zombieY - 2][zombieX + direction];
log.debug("Blocks above ahead solid? {}", game.isSolid(blocksAboveAhead));
log.debug("Blocks two above ahead solid? {}", game.isSolid(blocksTwoAboveAhead));
if (!game.isSolid(blocksAboveAhead) && game.isSolid(blocksAhead) && !game.isSolid(blocksTwoAboveAhead)) {
if (zombieData.getJumpAttempts() < 2) {
log.debug("Jumping to ({},{})", zombieX + direction, zombieY - 1);
moveZombie(zombie, zombieX, zombieY, zombieX + direction, zombieY - 1, world);
newZombieX = zombieX + direction;
newZombieY = zombieY - 1;
updated = true;
zombieData.setJumpAttempts(zombieData.getJumpAttempts() + 1);
} else {
log.debug("Max jump attempts reached: {}", zombieData.getJumpAttempts());
}
} else {
log.debug("Cannot jump over obstacle");
}
}
}
// Apply gravity
while (updated) {
if (newZombieY + 1 >= world.length) {
log.debug("Gravity blocked, bottom of world reached");
updated = false;
break;
}
if (!game.isSolid(world[newZombieY + 1][newZombieX])) {
if (newZombieY - zombieY < 3) {
log.debug("Gravity moving zombie down from ({},{}) to ({},{})", newZombieX, newZombieY, newZombieX, newZombieY + 1);
moveZombie(zombie, newZombieX, newZombieY, newZombieX, newZombieY + 1, world);
newZombieY++;
} else {
log.debug("Gravity limit reached, stopping");
updated = false;
}
} else {
log.debug("Block below solid, stopping gravity");
updated = false;
}
}
}
private void moveZombie(Block zombie, int oldX, int oldY, int newX, int newY, List<Block>[][] world) {
Block linked = zombie.getLinkedMobTexture();
boolean isTop = zombie.getSpriteState().isPresent() &&
(zombie.getSpriteState().get().equals(Zombie.ZombieState.TOP) ||
zombie.getSpriteState().get().equals(Zombie.ZombieState.TOP_HURT));
if (isTop) {
log.debug("Moving TOP block from ({},{}) to ({},{})", oldX, oldY, newX, newY);
world[oldY][oldX].remove(zombie);
world[oldY + 1][oldX].remove(linked);
world[newY][newX].add(zombie);
world[newY + 1][newX].add(linked);
} else {
log.debug("Moving BOTTOM block from ({},{}) to ({},{})", oldX, oldY, newX, newY);
world[oldY][oldX].remove(zombie);
world[oldY - 1][oldX].remove(linked);
world[newY][newX].add(zombie);
world[newY - 1][newX].add(linked);
}
} }
} }

View File

@ -16,6 +16,10 @@ public class ZombieSpawnLogic implements EntitySpawnInterface {
@Override @Override
public void spawn(int playerX, int playerY, Game game, Terminal terminal) { public void spawn(int playerX, int playerY, Game game, Terminal terminal) {
if (!game.isNight()) {
return;
}
int[] view = ScreenMovingCalculationProvider.calculate(playerX, playerY, terminal.getHeight(), terminal.getWidth(), game.getWorld()[0].length, game.getWorld().length); int[] view = ScreenMovingCalculationProvider.calculate(playerX, playerY, terminal.getHeight(), terminal.getWidth(), game.getWorld()[0].length, game.getWorld().length);
int startX = view[0]; int startX = view[0];
int endX = view[1]; int endX = view[1];

View File

@ -84,13 +84,9 @@ public class InputHandlerThread extends Thread {
switch (key) { switch (key) {
case '1', '2', '3', '4', '5', '6', '7', '8', '9' -> game.changeSlot(key - 49, screenRenderer); case '1', '2', '3', '4', '5', '6', '7', '8', '9' -> game.changeSlot(key - 49, screenRenderer);
case 'a' -> { case 'a' -> new Thread(() -> game.movePlayerLeft(screenRenderer, terminal)).start();
game.movePlayerLeft(screenRenderer, terminal); case 'd' -> new Thread(() -> game.movePlayerRight(screenRenderer, terminal)).start();
} case ' ' ->{
case 'd' -> {
game.movePlayerRight(screenRenderer, terminal);
}
case ' ' -> {
game.movePlayerUp(screenRenderer); game.movePlayerUp(screenRenderer);
screenRenderer.render(game); screenRenderer.render(game);
} }

View File

@ -6,6 +6,7 @@ import lombok.AllArgsConstructor;
import org.jline.terminal.MouseEvent; import org.jline.terminal.MouseEvent;
import org.jline.terminal.Terminal; import org.jline.terminal.Terminal;
import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -171,17 +172,14 @@ public class MouseHandler {
} }
if (game.isMineable(blockX, blockY, terminal)) { if (game.isMineable(blockX, blockY, terminal)) {
List<Integer> list = new ArrayList<>();
list.add(blockX);
list.add(blockY);
if (screenRenderer.getSelectedBlock().isPresent() if (screenRenderer.getSelectedBlock().isPresent()
&& screenRenderer.getSelectedBlock().get().get(0).equals(blockX) && screenRenderer.getSelectedBlock().get().x == blockX
&& screenRenderer.getSelectedBlock().get().get(1).equals(blockY)) { && screenRenderer.getSelectedBlock().get().y == blockY) {
return; return;
} }
screenRenderer.setSelectedBlock(Optional.of(list)); screenRenderer.setSelectedBlock(Optional.of(new Point(blockX, blockY)));
} else { } else {
if (screenRenderer.getSelectedBlock().isEmpty()) { if (screenRenderer.getSelectedBlock().isEmpty()) {
return; return;

View File

@ -15,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
import org.jline.terminal.Terminal; import org.jline.terminal.Terminal;
import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -45,7 +46,7 @@ public class ScreenRenderer {
*/ */
@Getter @Getter
@Setter @Setter
private Optional<List<Integer>> selectedBlock = Optional.empty(); private Optional<Point> selectedBlock = Optional.empty();
/** /**
* Finds the coordinates of the player's lower or upper body in the world array. * Finds the coordinates of the player's lower or upper body in the world array.
@ -53,7 +54,7 @@ public class ScreenRenderer {
* @param world The 2D world array of blocks. * @param world The 2D world array of blocks.
* @return Integer array of coordinates [x, y], or null if player not found. * @return Integer array of coordinates [x, y], or null if player not found.
*/ */
private int[] getPlayerCords(List<Block>[][] world) { private Point getPlayerCords(List<Block>[][] world) {
for (int i = 0; i < world.length; i++) { for (int i = 0; i < world.length; i++) {
for (int j = 0; j < world[i].length; j++) { for (int j = 0; j < world[i].length; j++) {
var steve = world[i][j].stream().filter(x -> x.getBlockId().equals("steve")).findFirst(); var steve = world[i][j].stream().filter(x -> x.getBlockId().equals("steve")).findFirst();
@ -61,9 +62,9 @@ public class ScreenRenderer {
var steveData = (SteveData) steve.get().getData(); var steveData = (SteveData) steve.get().getData();
if (steveData.isTop()) { if (steveData.isTop()) {
return new int[] { j, i + 1 }; return new Point(j, i + 1);
} else { } else {
return new int[] { j, i }; return new Point(j, i);
} }
} }
} }
@ -98,7 +99,7 @@ public class ScreenRenderer {
* *
* @param game Current game state to render. * @param game Current game state to render.
*/ */
public synchronized void render(Game game) { public void render(Game game) {
if (rendering) { if (rendering) {
return; return;
} }
@ -126,12 +127,12 @@ public class ScreenRenderer {
case WORLD -> { case WORLD -> {
// World // World
int[] cords = getPlayerCords(world); Point cords = getPlayerCords(world);
if (cords == null) if (cords == null)
return; return;
int playerX = cords[0]; int playerX = cords.x;
int playerY = cords[1]; int playerY = cords.y;
int terminalWidth = terminal.getWidth(); int terminalWidth = terminal.getWidth();
int terminalHeight = terminal.getHeight(); int terminalHeight = terminal.getHeight();
@ -168,8 +169,8 @@ public class ScreenRenderer {
.map(block -> getTexture(block, spriteList, game)) .map(block -> getTexture(block, spriteList, game))
.toList()); .toList());
if (selectedBlock.isPresent() && selectedBlock.get().get(0) == x if (selectedBlock.isPresent() && selectedBlock.get().x == x
&& selectedBlock.get().get(1) == y) { && selectedBlock.get().y == y) {
StringBuilder stringBuilder = getStringBuilder(); StringBuilder stringBuilder = getStringBuilder();
sprites.add(stringBuilder.toString()); sprites.add(stringBuilder.toString());