feat: Implemented bed
This commit is contained in:
@ -36,6 +36,7 @@ public class SpriteLoader {
|
||||
FURNACE,
|
||||
CHEST,
|
||||
CRAFTING_TABLE,
|
||||
BED,
|
||||
|
||||
// ENTITIES
|
||||
STEVE,
|
||||
@ -79,6 +80,7 @@ public class SpriteLoader {
|
||||
ITEM_CRAFTING_TABLE,
|
||||
ITEM_CHEST,
|
||||
ITEM_FURNACE,
|
||||
ITEM_BED,
|
||||
|
||||
// Weapons
|
||||
WOODEN_SWORD, //NEWWW
|
||||
@ -143,6 +145,7 @@ public class SpriteLoader {
|
||||
SPRITES_MAP.put(SPRITES.CRAFTING_TABLE, new SimpleSprite("crafting_table.ans"));
|
||||
SPRITES_MAP.put(SPRITES.FURNACE, new Furnace());
|
||||
SPRITES_MAP.put(SPRITES.CHEST, new SimpleSprite("chest.ans"));
|
||||
SPRITES_MAP.put(SPRITES.BED, new Bed());
|
||||
|
||||
// ENTITIES
|
||||
SPRITES_MAP.put(SPRITES.STEVE, new Steve());
|
||||
@ -186,6 +189,7 @@ public class SpriteLoader {
|
||||
SPRITES_MAP.put(SPRITES.ITEM_CRAFTING_TABLE, new SimpleSprite("items/crafting_table.ans"));
|
||||
SPRITES_MAP.put(SPRITES.ITEM_CHEST, new SimpleSprite("items/chest.ans"));
|
||||
SPRITES_MAP.put(SPRITES.ITEM_FURNACE, new SimpleSprite("items/furnace.ans"));
|
||||
SPRITES_MAP.put(SPRITES.ITEM_BED, new SimpleSprite("items/bed.ans"));
|
||||
|
||||
// Weapons
|
||||
SPRITES_MAP.put(SPRITES.WOODEN_SWORD, new SimpleSprite("items/wooden_sword.ans")); //NEWWW
|
||||
|
@ -0,0 +1,14 @@
|
||||
package cz.jzitnik.game.crafting.recipes;
|
||||
|
||||
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
|
||||
|
||||
@CraftingRecipeRegistry(
|
||||
recipe = {
|
||||
"dirt", "_", "_",
|
||||
"_", "_", "_",
|
||||
"_", "_", "_"
|
||||
},
|
||||
result = "bed",
|
||||
amount = 1
|
||||
)
|
||||
public class BedRecipe {}
|
@ -35,11 +35,8 @@ public class ItemBlockSupplier {
|
||||
BlockRegistry annotation = clazz.getAnnotation(BlockRegistry.class);
|
||||
registeredBlocks.put(annotation.value(), blockInstance);
|
||||
|
||||
if (registeredItems.containsKey(annotation.value())) {
|
||||
dropsList.put(annotation.value(), annotation.value());
|
||||
} else {
|
||||
dropsList.put(annotation.value(), annotation.drops());
|
||||
}
|
||||
String dropKey = annotation.drops().isEmpty() ? annotation.value() : annotation.drops();
|
||||
dropsList.put(annotation.value(), dropKey);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -79,7 +76,18 @@ public class ItemBlockSupplier {
|
||||
public static Block getBlock(String key) {
|
||||
try {
|
||||
Block block = registeredBlocks.get(key).newInstance();
|
||||
block.setDrops(List.of(getItem(dropsList.get(key), block)));
|
||||
String dropKey = dropsList.get(key);
|
||||
|
||||
if (dropKey != null && registeredItems.containsKey(dropKey)) {
|
||||
Item dropItem = getItem(dropKey);
|
||||
if (dropKey.equals(block.getBlockId())) {
|
||||
dropItem.setBlock(Optional.of(block));
|
||||
} else {
|
||||
dropItem.setBlock(Optional.of(getBlock(dropKey)));
|
||||
}
|
||||
block.setDrops(List.of(dropItem));
|
||||
}
|
||||
|
||||
return block;
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
@ -90,24 +98,10 @@ public class ItemBlockSupplier {
|
||||
try {
|
||||
Item item = registeredItems.get(key).newInstance();
|
||||
if (registeredBlocks.containsKey(key)) {
|
||||
item.setBlock(Optional.of(registeredBlocks.get(key).newInstance()));
|
||||
}
|
||||
|
||||
return item;
|
||||
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Item getItem(String key, Block block) {
|
||||
try {
|
||||
Item item = registeredItems.get(key).newInstance();
|
||||
if (key.equals(block.getBlockId())) {
|
||||
Block block = registeredBlocks.get(key).newInstance();
|
||||
item.setBlock(Optional.of(block));
|
||||
} else if (registeredBlocks.containsKey(key)) {
|
||||
item.setBlock(Optional.of(registeredBlocks.get(key).newInstance()));
|
||||
block.setDrops(List.of(item));
|
||||
}
|
||||
|
||||
return item;
|
||||
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
@ -0,0 +1,15 @@
|
||||
package cz.jzitnik.game.entities.items.registry.blocks;
|
||||
|
||||
import cz.jzitnik.game.SpriteLoader;
|
||||
import cz.jzitnik.game.annotations.BlockRegistry;
|
||||
import cz.jzitnik.game.entities.Block;
|
||||
import cz.jzitnik.game.sprites.Bed;
|
||||
|
||||
@BlockRegistry("bed")
|
||||
public class BedBlock extends Block {
|
||||
public BedBlock() {
|
||||
super("bed", SpriteLoader.SPRITES.BED, 2);
|
||||
setGhost(true);
|
||||
setSpriteState(Bed.BedState.RIGHT);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package cz.jzitnik.game.entities.items.registry.items;
|
||||
|
||||
import cz.jzitnik.game.SpriteLoader;
|
||||
import cz.jzitnik.game.annotations.ItemRegistry;
|
||||
import cz.jzitnik.game.entities.items.Item;
|
||||
import cz.jzitnik.game.entities.items.ItemType;
|
||||
|
||||
@ItemRegistry("bed")
|
||||
public class BedItem extends Item {
|
||||
public BedItem() {
|
||||
super("bed", "Bed", ItemType.BLOCK, SpriteLoader.SPRITES.ITEM_BED);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package cz.jzitnik.game.handlers.place.handlers;
|
||||
|
||||
import cz.jzitnik.game.annotations.PlaceHandler;
|
||||
import cz.jzitnik.game.entities.Block;
|
||||
import cz.jzitnik.game.Game;
|
||||
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
|
||||
import cz.jzitnik.game.handlers.place.CustomPlaceHandler;
|
||||
import cz.jzitnik.game.sprites.Bed;
|
||||
|
||||
@PlaceHandler("bed")
|
||||
public class BedPlaceHandler implements CustomPlaceHandler {
|
||||
@Override
|
||||
public boolean place(Game game, int x, int y) {
|
||||
var blocks = game.getWorld()[y][x];
|
||||
var blocksLeft = game.getWorld()[y][x-1];
|
||||
var blocksRight = game.getWorld()[y][x+1];
|
||||
var inventory = game.getInventory();
|
||||
|
||||
if (!blocksLeft.stream().allMatch(block -> block.getBlockId().equals("air"))) {
|
||||
if (!blocksRight.stream().allMatch(block -> block.getBlockId().equals("air"))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Block block2 = ItemBlockSupplier.getBlock("bed");
|
||||
block2.setSpriteState(Bed.BedState.RIGHT);
|
||||
blocksRight.add(block2);
|
||||
|
||||
Block block = inventory.getItemInHand().get().getBlock().get();
|
||||
block.setSpriteState(Bed.BedState.LEFT);
|
||||
blocks.add(block);
|
||||
} else {
|
||||
Block block2 = ItemBlockSupplier.getBlock("bed");
|
||||
block2.setSpriteState(Bed.BedState.LEFT);
|
||||
blocksLeft.add(block2);
|
||||
|
||||
Block block = inventory.getItemInHand().get().getBlock().get();
|
||||
block.setSpriteState(Bed.BedState.RIGHT);
|
||||
blocks.add(block);
|
||||
}
|
||||
|
||||
inventory.decreaseItemInHand();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mine(Game game, int x, int y) {
|
||||
var blocks = game.getWorld()[y][x];
|
||||
Block block = blocks.stream().filter(b -> b.getBlockId().equals("bed")).toList().getFirst();
|
||||
|
||||
if (block.getSpriteState().get() == Bed.BedState.LEFT) {
|
||||
var blocks2 = game.getWorld()[y][x+1];
|
||||
blocks2.removeAll(blocks2.stream().filter(i -> !i.getBlockId().equals("air")).toList());
|
||||
}
|
||||
|
||||
if (block.getSpriteState().get() == Bed.BedState.RIGHT) {
|
||||
var blocks2 = game.getWorld()[y][x-1];
|
||||
blocks2.removeAll(blocks2.stream().filter(i -> !i.getBlockId().equals("air")).toList());
|
||||
}
|
||||
|
||||
blocks.removeAll(blocks.stream().filter(i -> !i.getBlockId().equals("air")).toList());
|
||||
}
|
||||
}
|
@ -14,7 +14,7 @@ public class DoorPlaceHandler implements CustomPlaceHandler {
|
||||
var blocks = game.getWorld()[y][x];
|
||||
var blocksTop = game.getWorld()[y-1][x];
|
||||
|
||||
if (!blocksTop.stream().allMatch(Block::isGhost)) {
|
||||
if (!blocksTop.stream().allMatch(block -> block.getBlockId().equals("air"))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -116,7 +116,7 @@ public class CowLogic implements EntityLogicInterface, EntitySpawnInterface, Ent
|
||||
int lstartY = playerY - 15;
|
||||
int lendY = playerY + 15;
|
||||
|
||||
if (countCows(lstartX, lendX, lstartY, lendY, game) < 3 && random.nextInt(100) < 100) {
|
||||
if (countCows(lstartX, lendX, lstartY, lendY, game) < 3 && random.nextInt(100) < 2) {
|
||||
var spawnLocations = cowCanSpawn(lstartX, lendX, playerY, game);
|
||||
if (!spawnLocations.isEmpty()) {
|
||||
for (int i = 0; i < Math.min(4, spawnLocations.size()); i++) {
|
||||
|
25
src/main/java/cz/jzitnik/game/sprites/Bed.java
Normal file
25
src/main/java/cz/jzitnik/game/sprites/Bed.java
Normal file
@ -0,0 +1,25 @@
|
||||
package cz.jzitnik.game.sprites;
|
||||
|
||||
import cz.jzitnik.tui.ResourceLoader;
|
||||
import cz.jzitnik.tui.Sprite;
|
||||
|
||||
public class Bed extends Sprite {
|
||||
public enum BedState {
|
||||
LEFT,
|
||||
RIGHT,
|
||||
}
|
||||
|
||||
public String getSprite() {
|
||||
return getSprite(BedState.LEFT);
|
||||
}
|
||||
|
||||
public String getSprite(Enum e) {
|
||||
return ResourceLoader.loadResource(
|
||||
switch (e) {
|
||||
case BedState.LEFT -> "bed/left.ans";
|
||||
case BedState.RIGHT -> "bed/right.ans";
|
||||
default -> throw new IllegalStateException("Unexpected value: " + e);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user