Skip to content
Snippets Groups Projects
Commit bde2f61d authored by Bariatti Francesco's avatar Bariatti Francesco
Browse files

GUI can load a state

parent 35bdc544
No related branches found
No related tags found
No related merge requests found
package controller;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.control.Label;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import model.GameState;
import model.Move;
import model.Player;
import model.Tile;
import org.json.JSONException;
import org.json.JSONObject;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import view.TileView;
import java.io.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
......@@ -27,6 +29,7 @@ import java.util.ResourceBundle;
public class Controller implements Initializable
{
private static final String introMusicFile = "./resource/pingu_theme.wav";
private static final String clickSoundFile = "./resource/clic.wav";
private GameState gameState;
private Process gameProcess;
......@@ -45,7 +48,7 @@ public class Controller implements Initializable
private BorderPane mainPane;
@FXML
private Label scoreRed, scoreBlue, turnLabel;
private Label scoreRedLabel, scoreBlueLabel, turnLabel;
@Override
public void initialize(URL location, ResourceBundle resources)
......@@ -57,12 +60,9 @@ public class Controller implements Initializable
for (int i = 0; i < fxTiles.length; i++)
{
board[i] = new Tile(i, Tile.PenguinPresence.NO_PENGUIN);
TileClickHandler clickHandler = new TileClickHandler(i);
fxTiles[i].setOnMouseClicked(clickHandler);
labels[i].setOnMouseClicked(clickHandler);
boardView[i] = new TileView(fxTiles[i], board[i], labels[i]);
boardView[i] = new TileView(fxTiles[i], labels[i], board[i]);
}
this.selectedTile = -1;
// this.selectedTile = -1;
this.gameState = new GameState();
try
{
......@@ -75,60 +75,82 @@ public class Controller implements Initializable
gameProcess.destroy();
}
});
}
catch (IOException e)
UpdateThread upT = new UpdateThread(gameProcess, this.gameState, this.board, this.boardView, scoreRedLabel, scoreBlueLabel, turnLabel);
upT.setDaemon(true);
upT.start();
} catch (IOException e)
{
Alert alert = new Alert(Alert.AlertType.ERROR, "Can't run penguin program", ButtonType.OK);
alert.showAndWait();
e.printStackTrace();
System.exit(1);
}
// SENDING STATE TO THE GAME
// TODO: 5/10/16 Creating a state and sending it to the game
String state = "{}";
gameInput.println(state);
//Choice: who will start?
//PLAY START SOUND
new Thread(() -> {
try
{
AudioPlayer.player.start(new AudioStream(new FileInputStream(introMusicFile)));
} catch (IOException e)
{
e.printStackTrace();
}
}).start();
//DO WE WANT TO LOAD A STATE?
TextInputDialog dialog = new TextInputDialog();
dialog.setHeaderText("Do you want to load a state?");
dialog.setContentText("Enter JSON (or leave blank for random)");
dialog.showAndWait();
String result = dialog.getResult();
if (result == null) //If the user clicked exit
System.exit(0);
try
{
new JSONObject(result); //Verify if it is valid json (if not, an exception is thrown)
gameInput.println(result);
Platform.runLater(() -> startGame());
} catch (JSONException e)
{
System.out.println("TODO: RANDOM STATE");
System.exit(0);
}
}
private void startGame()
{
ChoiceDialog<Player> playerChoice = new ChoiceDialog<>(Player.Red, Player.Red, Player.Blue);
playerChoice.setTitle("Penguin game!");
playerChoice.setHeaderText("Choose your color (red starts)");
playerChoice.showAndWait();
humanPlayer = playerChoice.getResult();
if (humanPlayer == null)
{
System.exit(0);
}
if (humanPlayer.equals(Player.Red))
gameInput.println("h");
else
gameInput.println("c");
this.selectedTile = -1;
for (TileView t : boardView)
{
if (humanPlayer.equals(Player.Red))
gameInput.println("h");
else
gameInput.println("c");
}
GameClickHandler gch = new GameClickHandler(t.getModelTile().getNumber());
t.getFxTile().setOnMouseClicked(gch);
t.getFishLabel().setOnMouseClicked(gch);
UpdateThread upT = new UpdateThread(gameProcess, this.gameState, this.board, this.boardView, humanPlayer, scoreRed, scoreBlue, turnLabel);
upT.setDaemon(true);
upT.start();
try
{
AudioPlayer.player.start(new AudioStream(new FileInputStream(introMusicFile)));
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* Event handler that will listen for a click on a tile.
* Event handler that will listen for a click on a tile during the game and move/select the penguin.
* It is associated with a TileView
*/
private class TileClickHandler implements EventHandler<MouseEvent>
private class GameClickHandler implements EventHandler<MouseEvent>
{
private int tileNumber;
private final String clickSoundFile = "./resource/clic.wav";
public TileClickHandler(int tileNumber)
public GameClickHandler(int tileNumber)
{
this.tileNumber = tileNumber;
}
......@@ -242,15 +264,5 @@ public class Controller implements Initializable
}
}
private class MyHexEnteredHandler implements EventHandler<MouseEvent>
{
@Override
public void handle(MouseEvent event)
{
Polygon poly = (Polygon) event.getSource();
poly.setFill(Color.GOLD);
}
}
}
......@@ -21,19 +21,27 @@ public class UpdateThread extends Thread
Player humanPlayer;
Label scoreRed, scoreBlue, turnLabel;
public UpdateThread(Process program, GameState gameState, Tile[] board, TileView[] boardView, Player humanPlayer, Label scoreRed, Label scoreBlue, Label turnLabel)
public UpdateThread(Process program, GameState gameState, Tile[] board, TileView[] boardView, Label scoreRed, Label scoreBlue, Label turnLabel)
{
this.reader = new BufferedReader(new InputStreamReader(program.getInputStream()));
this.writer = new PrintWriter(new OutputStreamWriter(program.getOutputStream()), true);
this.gameState = gameState;
this.board = board;
this.boardView = boardView;
this.humanPlayer = humanPlayer;
this.scoreRed = scoreRed;
this.scoreBlue = scoreBlue;
this.turnLabel = turnLabel;
}
public Player getHumanPlayer()
{
return humanPlayer;
}
public void setHumanPlayer(Player humanPlayer)
{
this.humanPlayer = humanPlayer;
}
public void run()
{
......
......@@ -17,7 +17,7 @@ public class TileView
private boolean selected, highlighted;
private Move highlightMove = null; // How do we get here from the selected tile (used when highlighting)
public TileView(Polygon fxTile, Tile modelTile, Label fishLabel)
public TileView(Polygon fxTile, Label fishLabel, Tile modelTile)
{
this.fxTile = fxTile;
......@@ -63,6 +63,10 @@ public class TileView
public boolean isSelected() { return selected; }
public Polygon getFxTile() { return fxTile; }
public Label getFishLabel() { return fishLabel; }
public void setSelected(boolean selected) { this.selected = selected; }
public boolean isHighlighted() { return highlighted; }
......
......@@ -997,12 +997,12 @@
<Font size="22.0" />
</font>
</Label>
<Label fx:id="scoreRed" layoutX="556.0" layoutY="152.0" text="0">
<Label fx:id="scoreRedLabel" layoutX="556.0" layoutY="152.0" text="0">
<font>
<Font size="22.0" />
</font>
</Label>
<Label fx:id="scoreBlue" layoutX="556.0" layoutY="183.0" text="0">
<Label fx:id="scoreBlueLabel" layoutX="556.0" layoutY="183.0" text="0">
<font>
<Font size="22.0" />
</font>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment