Compare commits

...

Author SHA1 Message Date
  Rob Hallam 7a182cb7ea feat: remove GameActor reference from GameState once assigned to AI 1 year ago
  Rob Hallam 7ac60058fa refactor: use setter - give GameState a *temporary* ref to GameActor 1 year ago
  Rob Hallam d123fa9810 feat: call GameState::setupGameState() from initialise event handler 1 year ago
  Rob Hallam 87ed62cc7a feat: add setupGameState method for initialising game state 1 year ago
  Rob Hallam 7686cdd652 feat: move GameActor reference into AIPlayer 1 year ago
  Rob Hallam b9543f3819 feat: handle endturnclicked event (from human or AI player) 1 year ago
  Rob Hallam 94b9b5f647 feat: add AIPlayer with takeTurn() method 1 year ago
  Rob Hallam b6b270e9c2 feat: identify this is running the 'end turn' demo 1 year ago
  Rob Hallam 8ef6a9f4cb feat: create addPlayer2Notification basic command 1 year ago
  Rob Hallam bef929df9f feat: give GameState a reference to GameActor 1 year ago
7 changed files with 139 additions and 14 deletions
Split View
  1. +2
    -0
      app/actors/GameActor.java
  2. +21
    -0
      app/commands/BasicCommands.java
  3. +18
    -0
      app/demo/TurnDemo.java
  4. +13
    -1
      app/events/EndTurnClicked.java
  5. +5
    -9
      app/events/Initalize.java
  6. +33
    -4
      app/structures/GameState.java
  7. +47
    -0
      app/structures/basic/AIPlayer.java

+ 2
- 0
app/actors/GameActor.java View File

@@ -64,6 +64,8 @@ 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.setGameActor(this);
// Get the list of image files to pre-load the UI with
Set<String> images = ImageListForPreLoad.getImageListForPreLoad();


+ 21
- 0
app/commands/BasicCommands.java View File

@@ -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
* @param out


+ 18
- 0
app/demo/TurnDemo.java View File

@@ -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();}
}
}

+ 13
- 1
app/events/EndTurnClicked.java View File

@@ -20,7 +20,19 @@ public class EndTurnClicked implements EventProcessor{
@Override
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;
}
}
}

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

@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import akka.actor.ActorRef;
import demo.CommandDemo;
import demo.TurnDemo;
import demo.Loaders_2024_Check;
import structures.GameState;
@@ -22,15 +23,10 @@ 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);
}
}


+ 33
- 4
app/structures/GameState.java View File

@@ -1,5 +1,8 @@
package structures;
import actors.GameActor;
import structures.basic.AIPlayer;
/**
* This class can be used to hold information about the on-going game.
* Its created with the GameActor.
@@ -8,10 +11,36 @@ package structures;
*
*/
public class GameState {
public boolean gameInitalised = 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;
}
}

+ 47
- 0
app/structures/basic/AIPlayer.java View File

@@ -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();
}

}

}

Loading…
Cancel
Save