Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #36 from NaulaN/develop
Browse files Browse the repository at this point in the history
-= Sprint n°4 finish =-
  • Loading branch information
naulan-chrzaszcz authored Jun 17, 2022
2 parents 6824ada + 089f865 commit 224a0e5
Show file tree
Hide file tree
Showing 87 changed files with 2,496 additions and 1,539 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ javafx {
}

dependencies {
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 {
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/fr/sae/terraria/Terraria.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

public class Terraria extends Application
{
// Constants
// Constantes
public static final String SRC_PATH = "src/main/resources/fr/sae/terraria/";
public static final double TARGET_FPS = .017;
public static final int DISPLAY_RENDERING_WIDTH = 465;
Expand All @@ -47,7 +47,7 @@ private FXMLLoader loadFXML(String path)
public void start(Stage stage) throws IOException
{
GameController gameController = new GameController(stage);
MenuController menuController = new MenuController(stage);
MenuController menuController = new MenuController();

FXMLLoader fxmlLoader = this.loadFXML("vue/game.fxml");
fxmlLoader.setController(gameController);
Expand Down Expand Up @@ -77,29 +77,31 @@ public void start(Stage stage) throws IOException
stage.addEventFilter(KeyEvent.KEY_RELEASED, key -> timePressedKey[0] = 1);
stage.sizeToScene();

// Sync les changements du joueur entre les contrôleurs.
// Synchronise les changements du joueur entre les contrôleurs.
stage.sceneProperty().addListener(((obs, oldScene, newScene) -> {
if (switchScene.get()) {
if (!Objects.isNull(menuController.player)) {
gameController.player = menuController.player;
menuController.loop.stop();
gameController.environment.getLoop().play();
}
} else {
if (!Objects.isNull(gameController.player)) {
menuController.player = gameController.player;
menuController.loop.play();
menuController.environment = gameController.environment;
gameController.environment.getLoop().stop();
}
}
}));
stage.widthProperty().addListener((obs, oldV, newV) -> {
gameController.scaleMultiplicatorWidth = (newV.intValue() / Terraria.DISPLAY_RENDERING_WIDTH);
menuController.scaleMultiplicatorWidth = (newV.intValue() / Terraria.DISPLAY_RENDERING_WIDTH);
});
stage.heightProperty().addListener((obs, oldV, newV) -> {
gameController.scaleMultiplicatorHeight = ((newV.intValue()-gameController.title.getPrefHeight()) / Terraria.DISPLAY_RENDERING_HEIGHT);
menuController.scaleMultiplicatorHeight = ((newV.intValue()-gameController.title.getPrefHeight()) / Terraria.DISPLAY_RENDERING_HEIGHT);
});
gameController.environment.getPlayer().pvProperty().addListener((obs, oldV, newV) ->{
if (newV.longValue() == 0){
stage.close();
}
});

stage.show();
Expand Down
74 changes: 24 additions & 50 deletions src/main/java/fr/sae/terraria/controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import fr.sae.terraria.Terraria;
import fr.sae.terraria.modele.Environment;
import fr.sae.terraria.modele.GenerateEntity;
import fr.sae.terraria.modele.TileMaps;
import fr.sae.terraria.modele.entities.entity.*;
import fr.sae.terraria.modele.entities.entity.ConsumableObjectType;
import fr.sae.terraria.modele.entities.player.Player;
import fr.sae.terraria.modele.entities.player.inventory.Inventory;
import fr.sae.terraria.vue.Camera;
Expand All @@ -20,7 +21,6 @@
import javafx.stage.Stage;

import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;


Expand Down Expand Up @@ -86,6 +86,9 @@ private void addKeysEventListener(final Stage stage)
this.environment.getGameClock().setMinutes(1_000);
if (key.isShiftDown() && key.isControlDown() && key.getCode().equals(KeyCode.J))
this.environment.getGameClock().setMinutes(400);
// Fait apparaitre un slime
if (key.isShiftDown() && key.isControlDown() && key.getCode().equals(KeyCode.P))
GenerateEntity.slime(this.environment);

key.consume();
});
Expand All @@ -98,28 +101,15 @@ private void addKeysEventListener(final Stage stage)

stage.addEventFilter(MouseEvent.MOUSE_CLICKED, mouse -> {
this.player.getMouseInput().put(mouse.getButton(), true);
mouse.consume();
});

stage.addEventFilter(MouseEvent.MOUSE_RELEASED, mouse -> {
this.player.getMouseInput().put(mouse.getButton(), false);
mouse.consume();
});

stage.addEventFilter(ScrollEvent.SCROLL, scroll -> {
inventory.setScroll((int) scroll.getDeltaY());
scroll.consume();
});

stage.addEventFilter(MouseEvent.MOUSE_CLICKED, click -> {
final double scaleMultiplicativeWidth = (this.root.getPrefWidth() / Terraria.DISPLAY_RENDERING_WIDTH);
final double scaleMultiplicativeHeight = ((this.root.getPrefHeight()-this.title.getPrefHeight()) / Terraria.DISPLAY_RENDERING_HEIGHT);
final int tileWidth = (int) (TileMaps.TILE_DEFAULT_SIZE * scaleMultiplicativeWidth);
final int tileHeight = (int) (TileMaps.TILE_DEFAULT_SIZE * scaleMultiplicativeHeight);
// La position correcte sur le Pane
double mouseX = click.getSceneX()+((Rectangle) this.displayTiledMap.getParent().getClip()).getX();
double mouseY = (click.getSceneY()-this.title.getPrefHeight())+((Rectangle) this.displayTiledMap.getParent().getClip()).getY();
// Le bloc où la souris à clicker
double mouseX = mouse.getSceneX()+((Rectangle) this.displayTiledMap.getParent().getClip()).getX();
double mouseY = (mouse.getSceneY()-this.title.getPrefHeight())+((Rectangle) this.displayTiledMap.getParent().getClip()).getY();
// Le bloc où la souris à clickée
int xBlock = (int) (mouseX/tileWidth);
int yBlock = (int) (mouseY/tileHeight);
Rectangle2D rectangle = new Rectangle2D(mouseX, mouseY, scaleMultiplicativeWidth, scaleMultiplicativeHeight);
Expand All @@ -131,42 +121,26 @@ private void addKeysEventListener(final Stage stage)
int distanceBetweenBlockPlayerAxisY = Math.abs(yPlayer - yBlock);

boolean isOneBlockDistance = distanceBetweenBlockPlayerAxisY >= 0 && distanceBetweenBlockPlayerAxisY <= Player.BREAK_BLOCK_DISTANCE && distanceBetweenBlockPlayerAxisX >= 0 && distanceBetweenBlockPlayerAxisX <= Player.BREAK_BLOCK_DISTANCE;
if (this.player.getStackSelected() instanceof EatableObjectType) {
((EatableObjectType) this.player.getStackSelected()).eat();
if (this.player.getStackSelected() != null && this.player.getStackSelected().getItem() instanceof ConsumableObjectType) {
((ConsumableObjectType) this.player.getStackSelected().getItem()).consumes();
} else if (isOneBlockDistance) {
if (click.getButton().equals(MouseButton.PRIMARY))
this.breakBlock(rectangle);
if (click.getButton().equals(MouseButton.SECONDARY))
this.placeBlock(xBlock, yBlock);
if (mouse.getButton().equals(MouseButton.PRIMARY))
this.player.interactWithBlock(rectangle);
if (mouse.getButton().equals(MouseButton.SECONDARY))
this.player.placeBlock(xBlock, yBlock);
}
});
}

private void breakBlock(final Rectangle2D rectangle)
{
// Commence a cherché l'entité ciblée
for (Entity entity : this.environment.getEntities()) if (entity.getRect().collideRect(rectangle)) {
if (entity instanceof BreakableObjectType)
((BreakableObjectType) entity).breaks();
if (entity instanceof CollapsibleObjectType) // TODO TEMP, à déplacer
((CollapsibleObjectType) entity).hit();

// Quand tous c'est bien déroulés, aprés avoir trouvé l'entité et l'objet sur l'écran, il arrête de chercher d'autre entité d'où le break
break;
}
}

private void placeBlock(int xBlock, int yBlock)
{
boolean haveAnItemOnHand = !Objects.isNull(this.player.getStackSelected());
boolean goodPlace = this.environment.getTileMaps().getTile(xBlock, yBlock) == TileMaps.SKY;
mouse.consume();
});

if (haveAnItemOnHand && goodPlace) {
if (!(this.player.getStackSelected().getItem() instanceof PlaceableObjectType) && !(this.player.getStackSelected() instanceof EatableObjectType))
return;
stage.addEventFilter(MouseEvent.MOUSE_RELEASED, mouse -> {
this.player.getMouseInput().put(mouse.getButton(), false);
mouse.consume();
});

if (this.player.getStackSelected().getItem() instanceof PlaceableObjectType)
((PlaceableObjectType) this.player.getStackSelected().getItem()).place(xBlock, yBlock);
}
stage.addEventFilter(ScrollEvent.SCROLL, scroll -> {
inventory.setScroll((int) scroll.getDeltaY());
scroll.consume();
});
}
}
104 changes: 71 additions & 33 deletions src/main/java/fr/sae/terraria/controller/MenuController.java
Original file line number Diff line number Diff line change
@@ -1,57 +1,95 @@
package fr.sae.terraria.controller;

import fr.sae.terraria.Terraria;
import fr.sae.terraria.modele.Environment;
import fr.sae.terraria.modele.entities.player.Player;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import fr.sae.terraria.modele.entities.player.craft.Craft;
import fr.sae.terraria.modele.entities.tools.MaterialSet;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;


public class MenuController implements Initializable
{
@FXML public Pane root;
@FXML public Pane displayInventory;
@FXML public Pane displayLexique;
@FXML public HBox HBoxText;
@FXML public HBox recipeRock;
@FXML public HBox recipeTorch;

public Timeline loop;
private Stage stage;
public Player player = null;
public double scaleMultiplicatorWidth;
public double scaleMultiplicatorHeight;
@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;

public MenuController(final Stage stage)
{
super();
this.stage = stage;
}

@Override public void initialize(URL location, ResourceBundle resources)
{
this.scaleMultiplicatorWidth = (this.root.getPrefWidth() / Terraria.DISPLAY_RENDERING_WIDTH);
this.scaleMultiplicatorHeight = ((this.root.getPrefHeight()-this.HBoxText.getPrefHeight()) / Terraria.DISPLAY_RENDERING_HEIGHT);
// Craft de la roche à partir de 3 pierres
this.recipeRock.addEventFilter(Event.ANY, event -> {
if (event.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED"))
this.player.pickup(Craft.rock(this.environment));
});

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, event -> {
if (event.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED"))
this.player.pickup(Craft.pickaxe(this.environment, MaterialSet.STONE));
});

this.recipeIronPickaxe.addEventFilter(Event.ANY, event -> {
if (event.getEventType().getName().equalsIgnoreCase("MOUSE_PRESSED"))
this.player.pickup(Craft.pickaxe(this.environment, MaterialSet.IRON));
});


// 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.loop = new Timeline();
this.loop.setCycleCount(Animation.INDEFINITE);
});

KeyFrame keyFrame = new KeyFrame(Duration.seconds(Terraria.TARGET_FPS), (ev -> {
if (!Objects.isNull(this.player)) {
System.out.println(this.player.getInventory());
}
}));
this.recipeIronSword.addEventFilter(Event.ANY, event -> {

this.loop.getKeyFrames().add(keyFrame);
this.loop.play();
});
}
}
19 changes: 18 additions & 1 deletion src/main/java/fr/sae/terraria/modele/Clock.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.sae.terraria.modele;

import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;


Expand All @@ -11,12 +12,13 @@ public class Clock

private final SimpleIntegerProperty minutes;
private final SimpleIntegerProperty days;
private final SimpleDoubleProperty opacityNightFilter;


public Clock()
{
super();

opacityNightFilter = new SimpleDoubleProperty(.0);
this.minutes = new SimpleIntegerProperty(Clock.EIGHT_AM_INGAME);
this.days = new SimpleIntegerProperty(1);
}
Expand All @@ -29,15 +31,30 @@ public void updates(int ticks)
this.days.setValue(getDays()+1);
this.minutes.setValue(0);
} else this.minutes.setValue(getMinutes()+1);
updateOpacity();
}
}

public SimpleIntegerProperty minutesProperty() { return this.minutes; }
public SimpleIntegerProperty daysProperty() { return this.days; }

private void updateOpacity()
{
if (this.getMinutes() > Clock.MINUTES_IN_A_DAY/2)
this.opacityNightFilter.set(((this.getMinutes()*(2. /* Compensation temps */))/Clock.MINUTES_IN_A_DAY) - (1.1 /* Décallage */));
else this.opacityNightFilter.set(((Clock.MINUTES_IN_A_DAY - (this.getMinutes()*(4. /* Compensation temps */)))/Clock.MINUTES_IN_A_DAY) - (.1 /* Décallage */));
}

public int getMinutes() { return this.minutes.get(); }
public int getDays() { return this.days.get(); }

public double getOpacityNightFilter() {
return opacityNightFilter.get();
}

public SimpleDoubleProperty opacityNightFilterProperty() {
return opacityNightFilter;
}

public void setMinutes(int newMinutes) { this.minutes.set(newMinutes); }
}
Loading

0 comments on commit 224a0e5

Please sign in to comment.