5 Commits

Author SHA1 Message Date
  Rob Hallam 7a182cb7ea feat: remove GameActor reference from GameState once assigned to AI 2 months ago
  Rob Hallam 7ac60058fa refactor: use setter - give GameState a *temporary* ref to GameActor 2 months ago
  Rob Hallam d123fa9810 feat: call GameState::setupGameState() from initialise event handler 2 months ago
  Rob Hallam 87ed62cc7a feat: add setupGameState method for initialising game state 2 months ago
  Rob Hallam 7686cdd652 feat: move GameActor reference into AIPlayer 2 months ago
4 changed files with 36 additions and 19 deletions
Split View
  1. +1
    -1
      app/actors/GameActor.java
  2. +1
    -9
      app/events/Initalize.java
  3. +26
    -7
      app/structures/GameState.java
  4. +8
    -2
      app/structures/basic/AIPlayer.java

+ 1
- 1
app/actors/GameActor.java View File

@@ -65,7 +65,7 @@ public class GameActor extends AbstractActor {
// Initalize a new game state object
gameState = new GameState();
// #hack: give the game state a reference to the game actor so AIPlayer can send messages
gameState.gameActor = this;
gameState.setGameActor(this);
// Get the list of image files to pre-load the UI with
Set<String> images = ImageListForPreLoad.getImageListForPreLoad();


+ 1
- 9
app/events/Initalize.java View File

@@ -23,15 +23,7 @@ public class Initalize implements EventProcessor{
@Override
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);


+ 26
- 7
app/structures/GameState.java View File

@@ -11,17 +11,36 @@ import structures.basic.AIPlayer;
*
*/
public class GameState {
public boolean gameInitalised = false;
public boolean something = false;
public int currentPlayer = 1;
public AIPlayer aiPlayer = new AIPlayer();
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;
}
// bit of a hack but include a reference to the GameActor so AIPlayer can send messages to it
public 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;
}
}

+ 8
- 2
app/structures/basic/AIPlayer.java View File

@@ -8,13 +8,20 @@ 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);
@@ -30,11 +37,10 @@ public class AIPlayer extends Player {
((ObjectNode) message).put("player", "AI");
// then send it to the GameActor:
try {
gameState.gameActor.processMessage("endturnclicked", message);
this.gameActor.processMessage("endturnclicked", message);
} catch (Exception e) {
e.printStackTrace();
}
// gameState.gameActor.receive(message);

}



Loading…
Cancel
Save