feat: Zombies fall when block is broken under them
This commit is contained in:
@ -0,0 +1,15 @@
|
||||
package cz.jzitnik.game.annotations;
|
||||
|
||||
import cz.jzitnik.game.core.CustomMobFallingAfterMineLogicHandler;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@RequireAnnotation(BlockRegistry.class)
|
||||
public @interface CustomMobFallingAfterMineLogic {
|
||||
Class<? extends CustomMobFallingAfterMineLogicHandler> value();
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package cz.jzitnik.game.core;
|
||||
|
||||
import cz.jzitnik.game.Game;
|
||||
|
||||
public interface CustomMobFallingAfterMineLogicHandler {
|
||||
void handle(Game game, int x, int y);
|
||||
}
|
@ -1,10 +1,13 @@
|
||||
package cz.jzitnik.game.entities.items.registry.mobs;
|
||||
|
||||
import cz.jzitnik.game.SpriteLoader;
|
||||
import cz.jzitnik.game.annotations.CustomMobFallingAfterMineLogic;
|
||||
import cz.jzitnik.game.annotations.EntityRegistry;
|
||||
import cz.jzitnik.game.entities.Block;
|
||||
import cz.jzitnik.game.mobs.services.zombie.ZombieData;
|
||||
import cz.jzitnik.game.mobs.services.zombie.ZombieFallingAfterMineLogic;
|
||||
|
||||
@CustomMobFallingAfterMineLogic(ZombieFallingAfterMineLogic.class)
|
||||
@EntityRegistry("zombie")
|
||||
public class Zombie extends Block {
|
||||
public Zombie() {
|
||||
|
@ -30,6 +30,8 @@ public class Generation {
|
||||
game.getPlayer().setPlayerBlock1(steveBlock);
|
||||
game.getPlayer().setPlayerBlock2(steveBlock2);
|
||||
|
||||
game.getInventory().addItem(ItemBlockSupplier.getItem("sand"));
|
||||
|
||||
PopulateWorld.populateWorld(world, terrainHeight);
|
||||
Trees.plantTrees(world, terrainHeight);
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
package cz.jzitnik.game.handlers.events.handlers.mobs;
|
||||
|
||||
import cz.jzitnik.game.Game;
|
||||
import cz.jzitnik.game.annotations.CustomMobFallingAfterMineLogic;
|
||||
import cz.jzitnik.game.annotations.MineEventHandler;
|
||||
import cz.jzitnik.game.entities.Block;
|
||||
import cz.jzitnik.tui.ScreenRenderer;
|
||||
|
||||
public class MobMineHandler {
|
||||
@ -10,7 +12,23 @@ public class MobMineHandler {
|
||||
var world = game.getWorld();
|
||||
|
||||
var mobs = world[y - 1][x].stream().filter(block -> block.isMob() && !block.getBlockId().equals("steve")).toList();
|
||||
world[y-1][x].removeAll(mobs);
|
||||
world[y][x].addAll(mobs);
|
||||
|
||||
for (Block mob : mobs) {
|
||||
if (mob.getClass().isAnnotationPresent(CustomMobFallingAfterMineLogic.class)) {
|
||||
var annotation = mob.getClass().getAnnotation(CustomMobFallingAfterMineLogic.class);
|
||||
var handlerClass = annotation.value();
|
||||
try {
|
||||
var instance = handlerClass.getDeclaredConstructor().newInstance();
|
||||
|
||||
instance.handle(game, x, y);
|
||||
continue;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
world[y - 1][x].remove(mob);
|
||||
world[y][x].add(mob);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
package cz.jzitnik.game.mobs.services.zombie;
|
||||
|
||||
import cz.jzitnik.game.Game;
|
||||
import cz.jzitnik.game.core.CustomMobFallingAfterMineLogicHandler;
|
||||
import cz.jzitnik.game.entities.items.registry.mobs.Zombie;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
// I know this code is shit, but I don't want to deal with it
|
||||
@Slf4j
|
||||
public class ZombieFallingAfterMineLogic implements CustomMobFallingAfterMineLogicHandler {
|
||||
@Override
|
||||
public void handle(Game game, int x, int y) {
|
||||
log.debug("Zombie is falling");
|
||||
|
||||
var world = game.getWorld();
|
||||
|
||||
var mobsTop = world[y - 2][x].stream().filter(block ->
|
||||
block.getClass().equals(Zombie.class) && block.getSpriteState().isPresent() &&
|
||||
(block.getSpriteState().get().equals(cz.jzitnik.game.sprites.Zombie.ZombieState.TOP) ||
|
||||
block.getSpriteState().get().equals(cz.jzitnik.game.sprites.Zombie.ZombieState.TOP_HURT))).toList();
|
||||
var mobsBottom = world[y - 1][x].stream().filter(block ->
|
||||
block.getClass().equals(Zombie.class) && block.getSpriteState().isPresent() &&
|
||||
(block.getSpriteState().get().equals(cz.jzitnik.game.sprites.Zombie.ZombieState.BOTTOM_HURT) ||
|
||||
block.getSpriteState().get().equals(cz.jzitnik.game.sprites.Zombie.ZombieState.BOTTOM))).toList();
|
||||
world[y - 2][x].removeAll(mobsTop);
|
||||
world[y -1][x].addAll(mobsTop);
|
||||
|
||||
world[y - 1][x].removeAll(mobsBottom);
|
||||
world[y][x].addAll(mobsBottom);
|
||||
}
|
||||
}
|
@ -12,12 +12,14 @@ import cz.jzitnik.game.mobs.*;
|
||||
import cz.jzitnik.game.sprites.Zombie;
|
||||
import cz.jzitnik.game.sprites.Zombie.ZombieState;
|
||||
import cz.jzitnik.tui.ScreenMovingCalculationProvider;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jline.terminal.Terminal;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static cz.jzitnik.game.sprites.Zombie.ZombieState.*;
|
||||
|
||||
@Slf4j
|
||||
@EntitySpawn
|
||||
@EntityLogic("zombie")
|
||||
@EntityHurtAnimationHandler("zombie")
|
||||
@ -29,6 +31,7 @@ public class ZombieLogic
|
||||
|
||||
@Override
|
||||
public void nextIteration(EntityLogicProvider.EntityLogicMobDTO entityLogicMobDTO) {
|
||||
log.debug("Running zombie logic");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user