diff --git a/src/main/java/fr/sae/terraria/controller/MenuController.java b/src/main/java/fr/sae/terraria/controller/MenuController.java index f3ddf98..5d523b4 100644 --- a/src/main/java/fr/sae/terraria/controller/MenuController.java +++ b/src/main/java/fr/sae/terraria/controller/MenuController.java @@ -16,10 +16,20 @@ public class MenuController implements Initializable { @FXML public HBox recipeRock; + @FXML public HBox recipeTorch; + @FXML public HBox recipeWoodPickaxe; @FXML public HBox recipeStonePickaxe; @FXML public HBox recipeIronPickaxe; + @FXML public HBox recipeWoodAxe; + @FXML public HBox recipeStoneAxe; + @FXML public HBox recipeIronAxe; + + @FXML public HBox recipeWoodSword; + @FXML public HBox recipeStoneSword; + @FXML public HBox recipeIronSword; + public Environment environment = null; public Player player = null; @@ -27,28 +37,59 @@ public class MenuController implements Initializable @Override public void initialize(URL location, ResourceBundle resources) { // Craft de la roche à partir de 3 pierres - this.recipeRock.addEventFilter(Event.ANY, ev -> { - if (ev.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED")) + this.recipeRock.addEventFilter(Event.ANY, event -> { + if (event.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED")) this.player.pickup(Craft.rock(this.environment)); }); - this.recipeWoodPickaxe.addEventFilter(Event.ANY, ev -> { - if (ev.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED")) + this.recipeTorch.addEventFilter(Event.ANY, event -> { + if (event.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED")) + this.player.pickup(Craft.torch(this.environment)); + }); + + + // LES PIOCHES + this.recipeWoodPickaxe.addEventFilter(Event.ANY, event -> { + if (event.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED")) this.player.pickup(Craft.pickaxe(this.environment, MaterialSet.WOOD)); }); - this.recipeStonePickaxe.addEventFilter(Event.ANY, ev -> { - if (ev.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED")) + this.recipeStonePickaxe.addEventFilter(Event.ANY, event -> { + if (event.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED")) this.player.pickup(Craft.pickaxe(this.environment, MaterialSet.STONE)); }); - this.recipeIronPickaxe.addEventFilter(Event.ANY, ev -> { - if (ev.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED")) + this.recipeIronPickaxe.addEventFilter(Event.ANY, event -> { + if (event.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED")) this.player.pickup(Craft.pickaxe(this.environment, MaterialSet.IRON)); }); - this.recipeWoodPickaxe.addEventFilter(Event.ANY, ev -> { - if (ev.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED")) - this.player.pickup(Craft.pickaxe(this.environment, MaterialSet.WOOD)); + + + // LES HACHES + this.recipeWoodAxe.addEventFilter(Event.ANY, event -> { + + }); + + this.recipeStoneAxe.addEventFilter(Event.ANY, event -> { + + }); + + this.recipeIronAxe.addEventFilter(Event.ANY, event -> { + + }); + + + // LES EPEES + this.recipeWoodSword.addEventFilter(Event.ANY, event -> { + + }); + + this.recipeStoneSword.addEventFilter(Event.ANY, event -> { + + }); + + this.recipeIronSword.addEventFilter(Event.ANY, event -> { + }); } } diff --git a/src/main/java/fr/sae/terraria/modele/Environment.java b/src/main/java/fr/sae/terraria/modele/Environment.java index 672b5aa..f9a1282 100644 --- a/src/main/java/fr/sae/terraria/modele/Environment.java +++ b/src/main/java/fr/sae/terraria/modele/Environment.java @@ -4,12 +4,9 @@ import fr.sae.terraria.modele.entities.Rabbit; import fr.sae.terraria.modele.entities.Slime; import fr.sae.terraria.modele.entities.blocks.Block; -import fr.sae.terraria.modele.entities.blocks.BlockSet; import fr.sae.terraria.modele.entities.blocks.Tree; -import fr.sae.terraria.modele.entities.entity.CollideObjectType; import fr.sae.terraria.modele.entities.entity.Entity; import fr.sae.terraria.modele.entities.entity.ReproductiveObjectType; -import fr.sae.terraria.modele.entities.items.Meat; import fr.sae.terraria.modele.entities.items.Vodka; import fr.sae.terraria.modele.entities.player.Player; import fr.sae.terraria.modele.entities.tools.MaterialSet; @@ -122,8 +119,6 @@ private void gameLoop() KeyFrame keyFrame = new KeyFrame(Duration.seconds(Terraria.TARGET_FPS), (ev -> { // TODO TEST if (!caught[0]) { - this.player.pickup(new Block(BlockSet.TORCH, this)); - this.player.pickup(new Meat(this)); this.player.pickup(new Tool(ToolSet.PICKAXE, MaterialSet.IRON)); this.player.pickup(new Vodka(this)); this.player.pickup(new Tool(ToolSet.SWORD, MaterialSet.IRON)); @@ -180,7 +175,7 @@ public static Clip playSound(String path, boolean loop) } catch (Exception e) { e.printStackTrace(); } if (!Objects.isNull(clip)) { - clip.loop((loop) ? Clip.LOOP_CONTINUOUSLY : 0); + clip.loop(loop ? Clip.LOOP_CONTINUOUSLY : 0); clip.start(); } diff --git a/src/main/java/fr/sae/terraria/modele/entities/blocks/Block.java b/src/main/java/fr/sae/terraria/modele/entities/blocks/Block.java index 1f74001..e50455e 100644 --- a/src/main/java/fr/sae/terraria/modele/entities/blocks/Block.java +++ b/src/main/java/fr/sae/terraria/modele/entities/blocks/Block.java @@ -76,6 +76,11 @@ private void loots() } else if (Block.isRock(this)) { for (int loot = 0; loot < Block.ROCK_NB_LOOTS; loot++) player.pickup(Item.STONE); + + if (Math.random() > .5) + player.pickup(Item.COAL); + if (Math.random() < .05) + player.pickup(Item.IRON); } else if (Block.isTallGrass(this)) { for (int loot = (int) (Math.random()*3)+1; loot <= TallGrass.LOOTS_FIBRE_MAX; loot++) player.pickup(Item.FIBER); diff --git a/src/main/java/fr/sae/terraria/modele/entities/items/Meat.java b/src/main/java/fr/sae/terraria/modele/entities/items/Meat.java index 831aeb4..37d8772 100644 --- a/src/main/java/fr/sae/terraria/modele/entities/items/Meat.java +++ b/src/main/java/fr/sae/terraria/modele/entities/items/Meat.java @@ -19,8 +19,9 @@ public Meat(final Environment environment) @Override public void consumes() { - Player player = environment.getPlayer(); + Environment.playSound("sound/eat.wav", false); + Player player = environment.getPlayer(); if (player.getPv() < player.getPvMax()) player.setPv(player.getPv() + 1); } diff --git a/src/main/java/fr/sae/terraria/modele/entities/items/Vodka.java b/src/main/java/fr/sae/terraria/modele/entities/items/Vodka.java index de241fa..bb4871c 100644 --- a/src/main/java/fr/sae/terraria/modele/entities/items/Vodka.java +++ b/src/main/java/fr/sae/terraria/modele/entities/items/Vodka.java @@ -28,6 +28,8 @@ public Vodka(final Environment environment) @Override public void consumes() { + Environment.playSound("sound/eat.wav", false); + Player player = environment.getPlayer(); Inventory inventory = player.getInventory(); inventory.get().get(inventory.getPosCursor()).remove(); diff --git a/src/main/java/fr/sae/terraria/modele/entities/player/Player.java b/src/main/java/fr/sae/terraria/modele/entities/player/Player.java index 0701a27..6a9dcf1 100644 --- a/src/main/java/fr/sae/terraria/modele/entities/player/Player.java +++ b/src/main/java/fr/sae/terraria/modele/entities/player/Player.java @@ -3,6 +3,7 @@ import fr.sae.terraria.modele.Environment; import fr.sae.terraria.modele.TileMaps; import fr.sae.terraria.modele.entities.entity.*; +import fr.sae.terraria.modele.entities.items.Item; import fr.sae.terraria.modele.entities.player.inventory.Inventory; import fr.sae.terraria.modele.entities.player.inventory.Stack; import fr.sae.terraria.vue.View; diff --git a/src/main/java/fr/sae/terraria/modele/entities/player/craft/Craft.java b/src/main/java/fr/sae/terraria/modele/entities/player/craft/Craft.java index 2d5b093..906c1df 100644 --- a/src/main/java/fr/sae/terraria/modele/entities/player/craft/Craft.java +++ b/src/main/java/fr/sae/terraria/modele/entities/player/craft/Craft.java @@ -7,6 +7,7 @@ import fr.sae.terraria.modele.entities.player.craft.recipes.Ingredient; import fr.sae.terraria.modele.entities.player.craft.recipes.PickaxeRecipe; import fr.sae.terraria.modele.entities.player.craft.recipes.RockRecipe; +import fr.sae.terraria.modele.entities.player.craft.recipes.TorchRecipe; import fr.sae.terraria.modele.entities.player.inventory.Inventory; import fr.sae.terraria.modele.entities.player.inventory.Stack; import fr.sae.terraria.modele.entities.tools.MaterialSet; @@ -33,6 +34,23 @@ public static Block rock(final Environment environment) return (!Objects.isNull(rockIngredients)) ? new Block(BlockSet.ROCK, environment) : null; } + public static Block torch(final Environment environment) + { + Inventory inventory = environment.getPlayer().getInventory(); + + Ingredient torchIngredients = TorchRecipe.apply(inventory); + if (!Objects.isNull(torchIngredients)) for (int i = 0; i < torchIngredients.get().length; i++) { + Stack stack = torchIngredients.get()[i]; + + if (Item.isStick(stack.getItem())) + stack.removeQuantity(TorchRecipe.NB_STICKS); + if (Item.isCoal(stack.getItem())) + stack.removeQuantity(TorchRecipe.NB_COALS); + } + + return (!Objects.isNull(torchIngredients)) ? new Block(BlockSet.TORCH, environment) : null; + } + public static Tool pickaxe(final Environment environment, final MaterialSet material) { Inventory inventory = environment.getPlayer().getInventory(); diff --git a/src/main/java/fr/sae/terraria/modele/entities/player/craft/recipes/IngredientSet.java b/src/main/java/fr/sae/terraria/modele/entities/player/craft/recipes/IngredientSet.java index 6a608fe..a307d78 100644 --- a/src/main/java/fr/sae/terraria/modele/entities/player/craft/recipes/IngredientSet.java +++ b/src/main/java/fr/sae/terraria/modele/entities/player/craft/recipes/IngredientSet.java @@ -40,4 +40,10 @@ public static boolean irons(final Ingredient ingredient, final Stack stack, fina boolean haveEnoughIrons = Item.isIron(stack.getItem()) && stack.haveEnoughQuantity(nbStones); return haveEnoughIrons && IngredientSet.putStackIntoIngredientsList(ingredient, stack); } + + public static boolean coals(final Ingredient ingredient, final Stack stack, final int nbCoals) + { + boolean haveEnoughCoals = Item.isCoal(stack.getItem()) && stack.haveEnoughQuantity(nbCoals); + return haveEnoughCoals && IngredientSet.putStackIntoIngredientsList(ingredient, stack); + } } diff --git a/src/main/java/fr/sae/terraria/modele/entities/player/craft/recipes/RockRecipe.java b/src/main/java/fr/sae/terraria/modele/entities/player/craft/recipes/RockRecipe.java index 27178a3..ac1e260 100644 --- a/src/main/java/fr/sae/terraria/modele/entities/player/craft/recipes/RockRecipe.java +++ b/src/main/java/fr/sae/terraria/modele/entities/player/craft/recipes/RockRecipe.java @@ -13,7 +13,7 @@ public class RockRecipe public static Ingredient apply(final Inventory inventory) { Ingredient ingredient = new Ingredient(RockRecipe.NB_INGREDIENTS); - boolean haveEnoughStone ; + boolean haveEnoughStone; int i = 0; do { diff --git a/src/main/java/fr/sae/terraria/modele/entities/player/craft/recipes/TorchRecipe.java b/src/main/java/fr/sae/terraria/modele/entities/player/craft/recipes/TorchRecipe.java new file mode 100644 index 0000000..b66ec31 --- /dev/null +++ b/src/main/java/fr/sae/terraria/modele/entities/player/craft/recipes/TorchRecipe.java @@ -0,0 +1,33 @@ +package fr.sae.terraria.modele.entities.player.craft.recipes; + +import fr.sae.terraria.modele.entities.player.inventory.Inventory; +import fr.sae.terraria.modele.entities.player.inventory.Stack; + + +public class TorchRecipe +{ + private static final int NB_INGREDIENTS = 2; + public static final int NB_COALS = 1; + public static final int NB_STICKS = 1; + + + public static Ingredient apply(final Inventory inventory) + { + Ingredient ingredient = new Ingredient(TorchRecipe.NB_INGREDIENTS); + boolean haveEnoughCoal = false; + boolean haveEnoughSticks = false; + + int i = 0; + do { + Stack stack = inventory.get().get(i); + + if (!haveEnoughCoal) + haveEnoughCoal = IngredientSet.coals(ingredient, stack, TorchRecipe.NB_COALS); + if (!haveEnoughSticks) + haveEnoughSticks = IngredientSet.sticks(ingredient, stack, TorchRecipe.NB_COALS); + i++; + } while (i < inventory.get().size() && (!haveEnoughCoal || !haveEnoughSticks)); + + return (haveEnoughCoal && haveEnoughSticks) ? ingredient : null; + } +} diff --git a/src/main/java/fr/sae/terraria/modele/entities/player/inventory/Inventory.java b/src/main/java/fr/sae/terraria/modele/entities/player/inventory/Inventory.java index 76f4575..74714cb 100644 --- a/src/main/java/fr/sae/terraria/modele/entities/player/inventory/Inventory.java +++ b/src/main/java/fr/sae/terraria/modele/entities/player/inventory/Inventory.java @@ -1,10 +1,12 @@ package fr.sae.terraria.modele.entities.player.inventory; import fr.sae.terraria.modele.entities.entity.StowableObjectType; +import fr.sae.terraria.modele.entities.items.Item; import fr.sae.terraria.modele.entities.player.Player; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.collections.FXCollections; +import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; import javafx.scene.input.KeyCode; diff --git a/src/main/java/fr/sae/terraria/modele/entities/player/inventory/Stack.java b/src/main/java/fr/sae/terraria/modele/entities/player/inventory/Stack.java index 00b5916..0eaff13 100644 --- a/src/main/java/fr/sae/terraria/modele/entities/player/inventory/Stack.java +++ b/src/main/java/fr/sae/terraria/modele/entities/player/inventory/Stack.java @@ -104,7 +104,8 @@ public boolean removeQuantity(int quantity) return false; } - @Override public String toString(){ + @Override public String toString() + { if (item instanceof Block) return String.valueOf(((Block) item).getTypeOfBlock()); return item.toString(); diff --git a/src/main/resources/fr/sae/terraria/sound/hit.wav b/src/main/resources/fr/sae/terraria/sound/hit.wav new file mode 100644 index 0000000..8b0a14f Binary files /dev/null and b/src/main/resources/fr/sae/terraria/sound/hit.wav differ diff --git a/src/main/resources/fr/sae/terraria/vue/menu.fxml b/src/main/resources/fr/sae/terraria/vue/menu.fxml index 38fd511..ab8eee4 100644 --- a/src/main/resources/fr/sae/terraria/vue/menu.fxml +++ b/src/main/resources/fr/sae/terraria/vue/menu.fxml @@ -13,16 +13,15 @@ + xmlns="http://javafx.com/javafx/11" xmlns:fx="http://javafx.com/fxml/1"> - + - + @@ -31,11 +30,9 @@ - + - + @@ -44,75 +41,59 @@
- - - + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - - + + + + - - - - + + + + - - - - + + + + - - - + + + + - + + + +
@@ -121,17 +102,16 @@ - + - +
- +
diff --git a/src/test/java/fr/sae/terraria/modele/entities/player/craft/CraftTest.java b/src/test/java/fr/sae/terraria/modele/entities/player/craft/CraftTest.java new file mode 100644 index 0000000..1d8e340 --- /dev/null +++ b/src/test/java/fr/sae/terraria/modele/entities/player/craft/CraftTest.java @@ -0,0 +1,110 @@ +package fr.sae.terraria.modele.entities.player.craft; + +import fr.sae.terraria.modele.Environment; +import fr.sae.terraria.modele.entities.blocks.Block; +import fr.sae.terraria.modele.entities.items.Item; +import fr.sae.terraria.modele.entities.player.Player; +import fr.sae.terraria.modele.entities.player.craft.recipes.IngredientSet; +import fr.sae.terraria.modele.entities.player.craft.recipes.PickaxeRecipe; +import fr.sae.terraria.modele.entities.player.craft.recipes.RockRecipe; +import fr.sae.terraria.modele.entities.player.inventory.Stack; +import fr.sae.terraria.modele.entities.tools.MaterialSet; +import fr.sae.terraria.modele.entities.tools.Tool; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.Objects; + +import static org.junit.jupiter.api.Assertions.*; + + +public class CraftTest +{ + private static Environment environment; + private static Player player; + + + public CraftTest() { super(); } + + @BeforeAll public static void init() + { + environment = new Environment(1., 1.); + player = environment.getPlayer(); + } + + @Test public final void stockTest() + { + player.pickup(Item.STONE); + player.pickup(Item.STONE); + player.pickup(Item.STONE); + player.pickup(Item.STONE); + + boolean haveEnoughStones = false; + for (Stack stack : player.getInventory().get()) + if (Item.isStone(stack.getItem()) && stack.haveEnoughQuantity(RockRecipe.NB_STONES)) + haveEnoughStones = true; + assertTrue(haveEnoughStones); + + Block block = Craft.rock(environment); + player.pickup(block); + + boolean haveRock = false; + for (Stack stack : player.getInventory().get()) + if (stack.getItem() instanceof Block && Block.isRock((Block) stack.getItem())) + haveRock = true; + assertTrue(haveRock); + + boolean dontHaveStones = true; + for (Stack stack : player.getInventory().get()) + if (Item.isStone(stack.getItem())) { + dontHaveStones = false; + break; + } + assertTrue(dontHaveStones); + } + + @Test public final void pickaxeTest() + { + player.pickup(Item.WOOD); + player.pickup(Item.WOOD); + player.pickup(Item.WOOD); + player.pickup(Item.STICK); + player.pickup(Item.STICK); + + boolean haveEnoughWoods = false; + boolean haveEnoughSticks = false; + for (Stack stack : player.getInventory().get()) { + if (Item.isWood(stack.getItem()) && stack.haveEnoughQuantity(PickaxeRecipe.WoodRecipe.NB_WOODS)) + haveEnoughWoods = true; + if (Item.isStick(stack.getItem()) && stack.haveEnoughQuantity(PickaxeRecipe.NB_STICKS)) + haveEnoughSticks = true; + } + + assertTrue(haveEnoughWoods); + assertTrue(haveEnoughSticks); + Tool woodPickaxe = Craft.pickaxe(environment, MaterialSet.WOOD); + assertTrue(!Objects.isNull(woodPickaxe) && Tool.isPickaxe(woodPickaxe) && MaterialSet.isWood(woodPickaxe.getMaterial())); + player.pickup(woodPickaxe); + + boolean havePickaxe = false; + for (Stack stack : player.getInventory().get()) + if (Tool.isPickaxe((Tool) stack.getItem())) + havePickaxe = true; + assertTrue(havePickaxe); + + + boolean dontHaveWoods = true; + boolean dontHaveSticks = true; + for (Stack stack : player.getInventory().get()) { + System.out.println(stack.getItem()); + if (!Item.isWood(stack.getItem()) && stack.haveEnoughQuantity(PickaxeRecipe.WoodRecipe.NB_WOODS)) { + dontHaveWoods = false; + break; + } else if (!Item.isStick(stack.getItem()) && stack.haveEnoughQuantity(PickaxeRecipe.NB_STICKS)) { + dontHaveSticks = false; + break; + } + } + assertTrue(dontHaveSticks && dontHaveWoods); + } +}