feat: Implemented wheat and bread

This commit is contained in:
Jakub Žitník 2025-03-21 11:29:59 +01:00
parent bffee19583
commit 064ae511d9
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
10 changed files with 125 additions and 10 deletions

View File

@ -75,6 +75,9 @@ public class SpriteLoader {
HEART,
HUNGER,
// Seeds
WHEAT,
// ITEMS
// Items
@ -158,9 +161,6 @@ public class SpriteLoader {
LAVA_BUCKET,
MILK_BUCKET,
// Seeds
WHEAT,
// Food
ITEM_PORKCHOP,
ITEM_COOKED_PORKCHOP,
@ -169,9 +169,11 @@ public class SpriteLoader {
ITEM_BEEF,
ITEM_STEAK,
ITEM_APPLE,
ITEM_BREAD,
// Seeds
ITEM_WHEAT_SEEDS,
ITEM_WHEAT,
}
public static final HashMap<SPRITES, Sprite> SPRITES_MAP = new HashMap<>();
@ -239,6 +241,7 @@ public class SpriteLoader {
// Seeds
SPRITES_MAP.put(SPRITES.ITEM_WHEAT_SEEDS, new SimpleSprite("items/wheat_seeds.ans"));
SPRITES_MAP.put(SPRITES.ITEM_WHEAT, new SimpleSprite("items/wheat.ans"));
// ENTITIES
SPRITES_MAP.put(SPRITES.STEVE, new Steve());
@ -345,6 +348,7 @@ public class SpriteLoader {
SPRITES_MAP.put(SPRITES.ITEM_BEEF, new SimpleSprite("items/beef.ans"));
SPRITES_MAP.put(SPRITES.ITEM_STEAK, new SimpleSprite("items/steak.ans"));
SPRITES_MAP.put(SPRITES.ITEM_APPLE, new SimpleSprite("items/apple.ans"));
SPRITES_MAP.put(SPRITES.ITEM_BREAD, new SimpleSprite("items/bread.ans"));
}
public static SpriteList<SPRITES> load() {

View File

@ -8,4 +8,5 @@ import java.lang.annotation.ElementType;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Farmable {
String value();
}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes.food;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"wheat", "wheat", "wheat",
"_", "_", "_",
"_", "_", "_"
},
result = "bread",
amount = 1
)
public class BreadRecipe {}

View File

@ -9,7 +9,7 @@ import cz.jzitnik.game.annotations.ResetSpriteStateOnMine;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.logic.services.farmable.FarmableData;
@Farmable
@Farmable("wheat")
@PlaceOnSolidNoHandler
@ResetDataOnMine
@ResetSpriteStateOnMine

View File

@ -0,0 +1,13 @@
package cz.jzitnik.game.entities.items.registry.items.food;
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("bread")
public class BreadItem extends Item {
public BreadItem() {
super("bread", "Bread", ItemType.FOOD, SpriteLoader.SPRITES.ITEM_BREAD, 3);
}
}

View File

@ -0,0 +1,13 @@
package cz.jzitnik.game.entities.items.registry.items.grassy;
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("wheat")
public class WheatItem extends Item {
public WheatItem() {
super("wheat", "Wheat", ItemType.USELESS_ITEM, SpriteLoader.SPRITES.ITEM_WHEAT);
}
}

View File

@ -1,7 +1,6 @@
package cz.jzitnik.game.generation;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import cz.jzitnik.game.Game;
import cz.jzitnik.game.SpriteLoader;
import cz.jzitnik.game.sprites.Steve;
@ -33,10 +32,6 @@ public class Generation {
// Spawn player at a valid starting point
world[terrainHeight[256] - 1][256].add(steveBlock2);
world[terrainHeight[256] - 2][256].add(steveBlock);
game.getInventory().addItem(ItemBlockSupplier.getItem("wooden_hoe"));
game.getInventory().addItem(ItemBlockSupplier.getItem("water_bucket"));
game.getInventory().addItem(ItemBlockSupplier.getItem("wheat_seeds"));
}
private static void initializeWorld(List<Block>[][] world) {

View File

@ -3,7 +3,9 @@ package cz.jzitnik.game.handlers.place;
import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.*;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.entities.items.InventoryItem;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import cz.jzitnik.game.logic.services.farmable.FarmableData;
import cz.jzitnik.game.sprites.TwoBlockSprite;
import java.lang.reflect.Constructor;
@ -14,6 +16,7 @@ import java.util.Random;
public class CustomAnnotationHandler implements CustomPlaceHandler {
private final Class<?> clazz;
private final DefaultPlaceHandler defaultPlaceHandler = new DefaultPlaceHandler();
private final Random random = new Random();
private record BlockDrop(String drops, int percentage) {
}
@ -71,6 +74,29 @@ public class CustomAnnotationHandler implements CustomPlaceHandler {
@Override
public boolean mine(Game game, int x, int y) {
boolean dropDefault = true;
if (clazz.isAnnotationPresent(Farmable.class)) {
var blocks = game.getWorld()[y][x];
var blockx = blocks.stream().filter(block -> !block.getBlockId().equals("air")).findFirst().get();
var data = (FarmableData) blockx.getData();
if (data.getState() == 2) {
dropDefault = false;
var annotation = clazz.getAnnotation(Farmable.class);
var growDrop = annotation.value();
var seedDrop = blockx.getDrops().get(0);
var seedDropAmount = random.nextInt(4);
if (seedDropAmount != 0) {
game.getInventory().addItem(new InventoryItem(seedDropAmount, seedDrop));
}
game.getInventory().addItem(ItemBlockSupplier.getItem(growDrop));
}
}
if (clazz.isAnnotationPresent(ResetDataOnMine.class)) {
resetDataOnMine(game, x, y);
}
@ -82,7 +108,6 @@ public class CustomAnnotationHandler implements CustomPlaceHandler {
blockx.setSpriteState();
}
boolean dropDefault = true;
if (clazz.isAnnotationPresent(CustomDrops.class) || clazz.isAnnotationPresent(CustomDrop.class)) {
var annotations = clazz.isAnnotationPresent(CustomDrops.class)

View File

@ -0,0 +1,25 @@
                                                  
                                                  
                                       
                                
                                  
                             
                             
                             
                            
                            
                       
                             
                        
                            
                           
                           
                          
                           
                                
                                  
                                   
                                 
                                      
                                     
                                        

View File

@ -0,0 +1,25 @@