Move and updated listeners

This commit is contained in:
Quentin Legot 2021-11-27 17:01:25 +01:00
parent b702a4030f
commit 21e571027f
7 changed files with 49 additions and 31 deletions

View File

@ -41,6 +41,10 @@ public class App extends Application {
return new Game(new Grid(12, 12, players), players); return new Game(new Grid(12, 12, players), players);
} }
public static void updateView() {
App.playerList.get(App.game.getCurrentPlayer()).getView().show();
}
@Override @Override
public void start(Stage stage) { public void start(Stage stage) {
try { try {
@ -52,7 +56,7 @@ public class App extends Application {
for (Player player : game.getPlayers()) { for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, new Window(stage, game, player))); playerList.put(player, new ClientPlayer(player, new Window(stage, game, player)));
} }
playerList.get(game.getCurrentPlayer()).getView().show(); updateView();
} }
public static void launchTerminal() { public static void launchTerminal() {
@ -66,7 +70,7 @@ public class App extends Application {
for (Player player : game.getPlayers()) { for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, new Terminal(game, player))); playerList.put(player, new ClientPlayer(player, new Terminal(game, player)));
} }
playerList.get(game.getCurrentPlayer()).getView().show(); updateView();
} }
public static List<Player> parsePlayers() throws IllegalArgumentException, NoSuchMethodException, public static List<Player> parsePlayers() throws IllegalArgumentException, NoSuchMethodException,

View File

@ -1,13 +1,17 @@
package fr.lnl.game.server.model; package fr.lnl.game.client.listener;
import java.util.List; import java.util.List;
public abstract class AbstractModelListening implements ModelListener { public abstract class AbstractModelListening implements ModelListener {
List<ModelListener> listeners; List<ModelListener> listeners;
public abstract void addListener(ModelListener e); public void addListener(ModelListener e) {
this.listeners.add(e);
}
public abstract void removalListener(ModelListener e); public void removalListener(ModelListener e) {
this.listeners.remove(e);
}
public void fireChange(){ public void fireChange(){
for(ModelListener e : listeners){ for(ModelListener e : listeners){

View File

@ -0,0 +1,24 @@
package fr.lnl.game.client.listener;
import fr.lnl.game.client.App;
public class ButtonListener extends AbstractModelListening{
@Override
public void addListener(ModelListener e) {
this.listeners.add(e);
}
@Override
public void removalListener(ModelListener e) {
this.listeners.remove(e);
}
@Override
public void updateModel(Object event) {
App.game.nextCurrentPlayer();
App.updateView();
}
}

View File

@ -1,4 +1,4 @@
package fr.lnl.game.server.model; package fr.lnl.game.client.listener;
public interface ModelListener { public interface ModelListener {
//à peut-être revoir //à peut-être revoir

View File

@ -1,14 +1,20 @@
package fr.lnl.game.client.view; package fr.lnl.game.client.view;
import fr.lnl.game.client.App; import fr.lnl.game.client.listener.ModelListener;
import fr.lnl.game.client.listener.ButtonListener;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
public class ClientEventHandler implements EventHandler<ActionEvent> { public class ClientEventHandler implements EventHandler<ActionEvent> {
private final ModelListener listener;
public ClientEventHandler(ButtonListener listener) {
this.listener = listener;
}
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
App.game.nextCurrentPlayer(); listener.updateModel(event);
App.playerList.get(App.game.getCurrentPlayer()).getView().show();
} }
} }

View File

@ -1,5 +1,6 @@
package fr.lnl.game.client.view; package fr.lnl.game.client.view;
import fr.lnl.game.client.listener.ButtonListener;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import javafx.scene.Scene; import javafx.scene.Scene;
@ -23,7 +24,7 @@ public class Window extends AbstractView {
Label l = new Label("Hello, JavaFX version " + javafxVersion + ", running on Java " + javaVersion + "."); Label l = new Label("Hello, JavaFX version " + javafxVersion + ", running on Java " + javaVersion + ".");
Label l2 = new Label("I'm " + player.getClass().getSimpleName() + " num " + player.getId() + " my class is " + player.getClassPlayer().toString()); Label l2 = new Label("I'm " + player.getClass().getSimpleName() + " num " + player.getId() + " my class is " + player.getClassPlayer().toString());
Button b = new Button("Click to see others players informations"); Button b = new Button("Click to see others players informations");
b.setOnAction(new ClientEventHandler()); b.setOnAction(new ClientEventHandler(new ButtonListener()));
GridPane grid = new GridPane(); GridPane grid = new GridPane();
grid.add(l, 0, 0); grid.add(l, 0, 0);
grid.add(l2, 0, 1); grid.add(l2, 0, 1);

View File

@ -1,21 +0,0 @@
package fr.lnl.game.server.model;
public class ModelListening extends AbstractModelListening{
@Override
public void addListener(ModelListener e) {
this.listeners.add(e);
}
@Override
public void removalListener(ModelListener e) {
this.listeners.remove(e);
}
@Override
public void updateModel(Object obj) {
}
}