Author | SHA1 | Message | Date |
---|---|---|---|
|
7a182cb7ea | feat: remove GameActor reference from GameState once assigned to AI | 1 year ago |
|
7ac60058fa |
refactor: use setter - give GameState a *temporary* ref to GameActor
This reference in GameState can be removed once it is given to AIPlayer in game initialisation (setupGameState) |
1 year ago |
|
d123fa9810 | feat: call GameState::setupGameState() from initialise event handler | 1 year ago |
|
87ed62cc7a |
feat: add setupGameState method for initialising game state
This will be called from the Initialise class, making the game state ready to be played- create player instances, board, tiles, cards etc |
1 year ago |
|
7686cdd652 |
feat: move GameActor reference into AIPlayer
This avoid the ugly hack of keeping a referenec in GameState |
1 year ago |
|
b9543f3819 | feat: handle endturnclicked event (from human or AI player) | 1 year ago |
|
94b9b5f647 |
feat: add AIPlayer with takeTurn() method
Intention is the game hands control to AI player after human ends turn by calling this method |
1 year ago |
|
b6b270e9c2 |
feat: identify this is running the 'end turn' demo
TurnDemo class not really necessary but illustrative |
1 year ago |
|
8ef6a9f4cb |
feat: create addPlayer2Notification basic command
There is a display bug on the frontend with this- it isn't quite aligned with Player 2's RHS avatar |
1 year ago |
|
bef929df9f |
feat: give GameState a reference to GameActor
Can use this on AI turns etc to send messages back to frontend |
1 year ago |
@@ -64,6 +64,8 @@ public class GameActor extends AbstractActor { | |||||
// Initalize a new game state object | // Initalize a new game state object | ||||
gameState = new GameState(); | gameState = new GameState(); | ||||
// #hack: give the game state a reference to the game actor so AIPlayer can send messages | |||||
gameState.setGameActor(this); | |||||
// Get the list of image files to pre-load the UI with | // Get the list of image files to pre-load the UI with | ||||
Set<String> images = ImageListForPreLoad.getImageListForPreLoad(); | Set<String> images = ImageListForPreLoad.getImageListForPreLoad(); | ||||
@@ -392,6 +392,27 @@ public class BasicCommands { | |||||
} | } | ||||
} | } | ||||
/** | |||||
* This command creates a notification box next to the portrait for the player 2 which contains | |||||
* the specified text. It will be displayed for a number of seconds before being removed. | |||||
* object. | |||||
* @param out | |||||
* @param text | |||||
* @param displayTimeSeconds | |||||
*/ | |||||
public static void addPlayer2Notification(ActorRef out, String text, int displayTimeSeconds) { | |||||
try { | |||||
ObjectNode returnMessage = Json.newObject(); | |||||
returnMessage.put("messagetype", "addPlayer2Notification"); | |||||
returnMessage.put("text", text); | |||||
returnMessage.put("seconds", displayTimeSeconds); | |||||
if (altTell!=null) altTell.tell(returnMessage); | |||||
else out.tell(returnMessage, out); | |||||
} catch (Exception e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
/** | /** | ||||
* Plays a projectile fire animation between two tiles | * Plays a projectile fire animation between two tiles | ||||
* @param out | * @param out | ||||
@@ -0,0 +1,18 @@ | |||||
package demo; | |||||
import commands.BasicCommands; | |||||
import akka.actor.ActorRef; | |||||
public class TurnDemo { | |||||
/** | |||||
* This method is used to demonstrate the use of the endTurnClicked event. | |||||
* | |||||
* @param out | |||||
*/ | |||||
public static void executeDemo(ActorRef out) { | |||||
// addPlayer1Notification | |||||
BasicCommands.addPlayer1Notification(out, "Turn Demo", 2); | |||||
try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();} | |||||
} | |||||
} |
@@ -20,7 +20,19 @@ public class EndTurnClicked implements EventProcessor{ | |||||
@Override | @Override | ||||
public void processEvent(ActorRef out, GameState gameState, JsonNode message) { | public void processEvent(ActorRef out, GameState gameState, JsonNode message) { | ||||
// see if we have a "player" field in the message to indicate AI player: | |||||
String player = (message.has("player")) ? message.get("player").asText() : "human"; | |||||
// now we check if the game thinks it is the player who clicked end turn's actual turn: | |||||
if (gameState.currentPlayer == 1 && player.equals("human")) { | |||||
System.out.println("Player 1 clicked end turn"); | |||||
gameState.currentPlayer = 2; | |||||
// hand off control to AI player: | |||||
gameState.aiPlayer.takeTurn(out, gameState); | |||||
System.out.println(String.format("%s", message.toString())); | |||||
} else if (gameState.currentPlayer == 2 && player.equals("AI")) { | |||||
System.out.println("Player 2 clicked end turn"); | |||||
gameState.currentPlayer = 1; | |||||
} | |||||
} | } | ||||
} | } |
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode; | |||||
import akka.actor.ActorRef; | import akka.actor.ActorRef; | ||||
import demo.CommandDemo; | import demo.CommandDemo; | ||||
import demo.TurnDemo; | |||||
import demo.Loaders_2024_Check; | import demo.Loaders_2024_Check; | ||||
import structures.GameState; | import structures.GameState; | ||||
@@ -22,15 +23,10 @@ public class Initalize implements EventProcessor{ | |||||
@Override | @Override | ||||
public void processEvent(ActorRef out, GameState gameState, JsonNode message) { | public void processEvent(ActorRef out, GameState gameState, JsonNode message) { | ||||
// hello this is a change | |||||
gameState.gameInitalised = true; | |||||
gameState.something = true; | |||||
// User 1 makes a change | |||||
CommandDemo.executeDemo(out); // this executes the command demo, comment out this when implementing your solution | |||||
//Loaders_2024_Check.test(out); | |||||
gameState.setupGameState(); | |||||
// start the turn demo | |||||
TurnDemo.executeDemo(out); | |||||
} | } | ||||
} | } | ||||
@@ -1,5 +1,8 @@ | |||||
package structures; | package structures; | ||||
import actors.GameActor; | |||||
import structures.basic.AIPlayer; | |||||
/** | /** | ||||
* This class can be used to hold information about the on-going game. | * This class can be used to hold information about the on-going game. | ||||
* Its created with the GameActor. | * Its created with the GameActor. | ||||
@@ -8,10 +11,36 @@ package structures; | |||||
* | * | ||||
*/ | */ | ||||
public class GameState { | public class GameState { | ||||
public boolean gameInitalised = false; | public boolean gameInitalised = false; | ||||
public boolean something = false; | public boolean something = false; | ||||
public int currentPlayer = 1; | |||||
private GameActor gameActor; | |||||
// note: still a bit of a hack but we remove this from GameState | |||||
// and give it to the AIPlayer on initialisation, and it can then | |||||
// be removed from here | |||||
public AIPlayer aiPlayer; | |||||
public GameState() { | |||||
super(); | |||||
} | |||||
public void setGameActor(GameActor gameActor) { | |||||
this.gameActor = gameActor; | |||||
} | |||||
/** Set up everything the game needs to be played (called via initialise game event) | |||||
* | |||||
*/ | |||||
public void setupGameState() { | |||||
// do game setup | |||||
this.gameInitalised = true; | |||||
// set initial player | |||||
this.currentPlayer = 1; | |||||
// create the AI player: | |||||
this.aiPlayer = new AIPlayer(this.gameActor); | |||||
this.gameActor = null; | |||||
} | |||||
} | } |
@@ -0,0 +1,47 @@ | |||||
package structures.basic; | |||||
// import basic commands for notifications | |||||
import commands.BasicCommands; | |||||
import structures.GameState; | |||||
import com.fasterxml.jackson.databind.JsonNode; | |||||
import com.fasterxml.jackson.databind.ObjectMapper; | |||||
import com.fasterxml.jackson.databind.node.ObjectNode; | |||||
import actors.GameActor; | |||||
import akka.actor.ActorRef; | |||||
public class AIPlayer extends Player { | |||||
private GameActor gameActor; | |||||
public AIPlayer() { | |||||
super(); | |||||
} | |||||
public AIPlayer(GameActor gameActor) { | |||||
super(); | |||||
this.gameActor = gameActor; | |||||
} | |||||
public void takeTurn(ActorRef out, GameState gameState) { | |||||
// addPlayer2Notification | |||||
BasicCommands.addPlayer2Notification(out, "My turn!", 2); | |||||
// addPlayer2Notification | |||||
BasicCommands.addPlayer2Notification(out, "Ending turn", 2); | |||||
// send "endTurnClicked" message to GameActor | |||||
// NOTE: #query -- how do we get the GameActor reference? | |||||
// #hack way- we add a reference to the GameActor in GameState | |||||
// first create the "endTurnClicked" message: | |||||
JsonNode message = new ObjectMapper().createObjectNode(); | |||||
((ObjectNode) message).put("messageType", "endTurnClicked"); | |||||
// add some content to say it is the AI player who 'clicked' end turn: | |||||
((ObjectNode) message).put("player", "AI"); | |||||
// then send it to the GameActor: | |||||
try { | |||||
this.gameActor.processMessage("endturnclicked", message); | |||||
} catch (Exception e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
} |