diff --git a/build.gradle b/build.gradle index f6e9452..3bb5a0b 100644 --- a/build.gradle +++ b/build.gradle @@ -34,11 +34,11 @@ javafx { } dependencies { - implementation 'junit:junit:4.13.1' + implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.7' + implementation 'junit:junit:4.13.2' + testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") - - implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.7' } test { 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 ac87769..2a748ea 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 @@ -21,11 +21,7 @@ public Meat(final Environment environment) { Player player = environment.getPlayer(); - if (player.getPv() < player.getPvMax()) { - Inventory inventory = player.getInventory(); - + if (player.getPv() < player.getPvMax()) player.setPv(player.getPv() + 1); - 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 05a0374..e191aef 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 @@ -85,16 +85,21 @@ public Player(final Environment environment) } } - @Override public void hit() { } + @Override public void hit() + { + this.setPv(this.getPv() - 1); + } @Override public void spawn(int x, int y) { this.setX(x); this.setY(y); - Image image = View.loadAnImage("sprites/player/player_idle.png", environment.scaleMultiplicatorWidth, environment.scaleMultiplicatorHeight); - this.setRect((int) image.getWidth(), (int) image.getHeight()); - image.cancel(); + Image img = View.loadAnImage("sprites/player/player_idle.png", environment.scaleMultiplicatorWidth, environment.scaleMultiplicatorHeight); + if (!Objects.isNull(img)) { + this.setRect((int) img.getWidth(), (int) img.getHeight()); + img.cancel(); + } else this.setRect(10, 10); this.getGravity().setXInit(x); this.getGravity().setYInit(y); 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 49886e1..e3be515 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 @@ -63,13 +63,17 @@ public void put(StowableObjectType item) if (nbStacksInventory == 0) { Stack stack = new Stack(); stack.nbItemsProperty().addListener((observable, oldValue, newValue) -> { + if (newValue.intValue() < oldValue.intValue()) { + Inventory inventory = player.getInventory(); + inventory.get().get(inventory.getPosCursor()).remove(); + } + if (newValue.intValue() <= 0) { this.value.remove(stack); this.player.setStackSelected(null); } }); stack.setItem(item); - stack.add(); this.value.add(stack); this.player.setStackSelected(stack); } else { @@ -93,7 +97,6 @@ public void put(StowableObjectType item) } }); stack.setItem(item); - stack.add(); this.value.add(stack); this.player.setStackSelected(stack); } 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 4dac440..f597d22 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 @@ -33,7 +33,7 @@ public Stack() { super(); - this.nbItems = new SimpleIntegerProperty(0); + this.nbItems = new SimpleIntegerProperty(1); } public boolean isSameItem(StowableObjectType object) diff --git a/src/main/java/fr/sae/terraria/vue/View.java b/src/main/java/fr/sae/terraria/vue/View.java index 640fe79..9294968 100644 --- a/src/main/java/fr/sae/terraria/vue/View.java +++ b/src/main/java/fr/sae/terraria/vue/View.java @@ -76,13 +76,16 @@ private static Image foundImage(final String path) public static Image loadAnImage(final String path, double scaleMultiplicatorWidth, double scaleMultiplicatorHeight) { Image img = View.foundImage(path); - double width = img.getWidth(); - double height = img.getHeight(); - img.cancel(); - - double widthScaled = width*scaleMultiplicatorWidth; - double heightScaled = height*scaleMultiplicatorHeight; - return new Image(img.getUrl(), widthScaled, heightScaled, false, false, false); + if (!Objects.isNull(img)) { + double width = img.getWidth(); + double height = img.getHeight(); + img.cancel(); + + double widthScaled = width*scaleMultiplicatorWidth; + double heightScaled = height*scaleMultiplicatorHeight; + return new Image(img.getUrl(), widthScaled, heightScaled, false, false, false); + } + return null; } public static ImageView createImageView(final Entity entity, final Image img) diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 0b865f5..fefed6a 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -4,10 +4,6 @@ requires com.google.gson; requires java.desktop; - - opens fr.sae.terraria to javafx.fxml; - exports fr.sae.terraria; - exports fr.sae.terraria.controller; opens fr.sae.terraria.controller to javafx.fxml; exports fr.sae.terraria.modele; opens fr.sae.terraria.modele to javafx.fxml; diff --git a/src/test/java/fr/sae/terraria/modele/entities/player/PlayerTest.java b/src/test/java/fr/sae/terraria/modele/entities/player/PlayerTest.java new file mode 100644 index 0000000..fefc3a1 --- /dev/null +++ b/src/test/java/fr/sae/terraria/modele/entities/player/PlayerTest.java @@ -0,0 +1,212 @@ +package fr.sae.terraria.modele.entities.player; + +import fr.sae.terraria.modele.Environment; +import fr.sae.terraria.modele.TileMaps; +import fr.sae.terraria.modele.entities.Rabbit; +import fr.sae.terraria.modele.entities.blocks.Dirt; +import fr.sae.terraria.modele.entities.items.Coal; +import fr.sae.terraria.modele.entities.items.Meat; +import fr.sae.terraria.modele.entities.items.Vodka; +import javafx.geometry.Rectangle2D; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeAll; + +import java.awt.*; + +import static org.junit.jupiter.api.Assertions.*; + + +public class PlayerTest +{ + private static Environment environment; + private static Player player; + + + public PlayerTest() { super(); } + + @BeforeAll public static void init() + { + environment = new Environment(1., 1.); + player = environment.getPlayer(); + } + + @Test public final void moveTest() + { + double previousPosX; + + previousPosX = player.getX(); + player.moveRight(); + player.move(); + + assertEquals(previousPosX + player.getVelocity(), player.getX(), + "Test: déplacement vers la droite."); + player.idleOnX(); + + + previousPosX = player.getX(); + player.moveLeft(); + player.move(); + + assertEquals(previousPosX - player.getVelocity(), player.getX(), + "Test: déplacement vers la gauche"); + } + + + @Test public final void collideTest() + { + /* + // Le TOP et le BOTTOM est un pet complex à tester dans un JUnit + Dirt dirtLeft = new Dirt(environment, 1, 1); + dirtLeft.setRect(1, 1); + environment.getEntities().add(dirtLeft); + Dirt dirtRight = new Dirt(environment, 5, 1); + dirtRight.setRect(1, 1); + environment.getEntities().add(dirtRight); + player.setRect(1, 1); + player.setVelocity(0); + player.setY(1); + + player.setX(3); + player.moveLeft(); + player.collide(); + + assertTrue(player.isMovingLeft()); + + player.setX(1); + player.moveLeft(); + player.collide(); + + assertTrue(player.isIDLEonX()); + + player.setX(5); + player.moveRight(); + player.collide(); + + assertTrue(player.isIDLEonX()); + */ + } + + @Test public final void hitTest() + { + double previousPv = player.getPv(); + player.hit(); + + assertEquals(previousPv - 1, player.getPv(), + "Vérifie s'il y a bien subit un dégât"); + } + + @Test public final void spawnTest() + { + int spawnLocX = 100; + int spawnLocY = 100; + + player.spawn(spawnLocX, spawnLocY); + + assertEquals(player.getX(), spawnLocX, + "Regarde si la localisation en X est correcte"); + assertEquals(player.getY(), spawnLocY, + "Regarde si la localisation en X est correcte"); + assertEquals(player.getGravity().xInit, spawnLocX, + "Regarde si la localisation au niveau de la gestion de la gravité, en X est correcte"); + assertEquals(player.getGravity().yInit, spawnLocY, + "Regarde si la localisation au niveau de la gestion de la gravité, en X est correcte"); + } + + @Test public final void worldLimitTest() + { + player.setX(2*TileMaps.TILE_DEFAULT_SIZE); + player.moveLeft(); + player.worldLimit(); + + assertTrue(player.isMovingLeft()); + + player.setX(-TileMaps.TILE_DEFAULT_SIZE); + player.moveLeft(); + player.worldLimit(); + + assertTrue(player.isIDLEonX()); + + player.setX(environment.getTileMaps().getWidth()*TileMaps.TILE_DEFAULT_SIZE + TileMaps.TILE_DEFAULT_SIZE); + player.moveRight(); + player.worldLimit(); + + assertTrue(player.isIDLEonX()); + } + + @Test public final void interactWithBlockTest() // TODO: Le nom est probablement mal choisie dans player + { + Rectangle2D blockSelected; + double beforePv; + + Dirt dirt = new Dirt(environment, 1, 1); + dirt.setRect(1, 1); + environment.getEntities().add(dirt); + blockSelected = new Rectangle2D(1, 1, 1, 1); + + beforePv = dirt.getPv(); + player.interactWithBlock(blockSelected); + assertEquals(beforePv - 1, dirt.getPv()); + environment.getEntities().remove(dirt); + + + Rabbit rabbit = new Rabbit(environment, 10, 10); + rabbit.setRect(1, 1); + environment.getEntities().add(rabbit); + blockSelected = new Rectangle2D(10, 10, 1, 1); + + beforePv = rabbit.getPv(); + player.interactWithBlock(blockSelected); + assertEquals(beforePv - 1, rabbit.getPv()); + } + + @Test public final void placeBlockTest() + { + TileMaps tileMaps = environment.getTileMaps(); + player.pickup(new Dirt(environment, 1, 1)); + + assertEquals(tileMaps.getTile(0, 0), TileMaps.SKY); + player.placeBlock(0, 0); + assertEquals(tileMaps.getTile(0, 0), TileMaps.DIRT); + + assertNull(player.getStackSelected()); + } + + @Test public final void drunkTest() + { + player.pickup(new Vodka(environment)); + + assertFalse(player.drunkProperty().get(), + "Vérifie si il n'est pas bourré"); + ((Vodka) player.getStackSelected().getItem()).consumes(); + assertTrue(player.drunkProperty().get(), + "Verife si il est bourré"); + } + + @Test public final void pickupTest() + { + player.getInventory().posCursorProperty().set(0); + + + // Test avec de la viande + player.pickup(new Meat(environment)); + assertTrue(player.getStackSelected().getItem() instanceof Meat, + "Verifie si le stack est bien de la viande"); + assertEquals(player.getStackSelected().getNbItems(), 1, + "Verifie s'il y a bien 1 objet dans le stack"); + ((Meat) player.getStackSelected().getItem()).consumes(); + player.getStackSelected().remove(); + + assertNull(player.getStackSelected()); + + + // Test avec du charbon + player.pickup(new Coal()); + assertTrue(player.getStackSelected().getItem() instanceof Coal, + "Verifie si le stack est bien du charbon"); + assertEquals(player.getStackSelected().getNbItems(), 1, + "Verifie s'il y a bien 1 objet dans le stack"); + player.getStackSelected().remove(); + + assertNull(player.getStackSelected()); + } +}