Commit 9aebe1bd authored by Anders H. Rebner's avatar Anders H. Rebner
Browse files

Model ready to use playerIDs

parent 2bb82a83
......@@ -54,8 +54,6 @@ public class UniCheckersClient extends ApplicationAdapter {
Gdx.input.setInputProcessor(stage);
// Initialize Models..
// Temporary model with default board and rules
Game model = new Game(new GameState(new GameMode(new DefaultRules(), new DefaultBoard())));
// Initialize controllers..
NetworkController networkController = new NetworkController();
......
......@@ -2,6 +2,7 @@ package com.mygdx.game.controllers;
import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.model.Game;
import com.mygdx.game.model.Player;
import com.mygdx.game.views.PlayView;
import com.mygdx.game.views.tokens.StarPiece;
......@@ -12,10 +13,12 @@ public class GameController {
private StarPiece activePiece; // Previously clicked piece
private Vector3 previousCoordinateClicked; // Previously clicked coordinate
private PlayerController playerController;
public GameController(Game model, PlayView view) {
public GameController(Game model, PlayView view, PlayerController playerController) {
this.model = model;
this.view = view;
this.activePiece = null;
this.previousCoordinateClicked = null;
......
......@@ -21,7 +21,7 @@ public class NetworkController {
this.client = new Client();
client.start();
try {
String IP4_LAN_ADDRESS = "192.168.0.136"; //"192.168.87.20";
String IP4_LAN_ADDRESS = "192.168.0.122"; //.136 //"192.168.87.20";
client.connect(10000, IP4_LAN_ADDRESS, 54555, 54777);
} catch (IOException e) {
e.printStackTrace();
......
......@@ -2,44 +2,61 @@ package com.mygdx.game.model
import com.badlogic.gdx.math.Vector3
//Logic of the game, holds a GameState, operations
//(((Controller???))) Model can change its own data, but has to be told by controller
class Game(gameState: GameState) {
class Game(gameState: GameState, playerIds: HashSet<Int>) {
private var gameState: GameState
private var playerIds: HashSet<Int>
private var playerTurnId: Int
private var playerTurnIndex: Int = 0 // TODO: Random initial value
init {
this.gameState = gameState
this.playerIds = playerIds
this.gameState.getBoardState().placePiecesAtStart(this.gameState.getRules(), this.playerIds)
this.playerTurnId = playerIds.elementAt(playerTurnIndex);
}
fun getStartFieldCoordinates(): List<List<Vector3>>? {
return this.gameState.getRules()?.generateStartFields()
return this.gameState.getRules().generateStartFields()
}
fun fieldExists(cubeCoordinates: Vector3): Boolean {
return this.gameState.getBoardState()?.fieldExists(cubeCoordinates) ?: false
return this.gameState.getBoardState().fieldExists(cubeCoordinates)
}
fun fieldHasPiece(cubeCoordinates: Vector3): Boolean {
return this.gameState.getBoardState()?.fields?.get(cubeCoordinates)?.hasPiece() == true
return this.gameState.getBoardState().fields[cubeCoordinates]?.hasPiece() == true
}
fun movePiece(fromCoordinates: Vector3, toCoordinates: Vector3): Boolean {
// TODO: Check if valid move
return this.gameState.getBoardState()?.movePiece(fromCoordinates, toCoordinates) ?: false
val pieceMoved: Boolean = this.gameState.getBoardState().movePiece(fromCoordinates, toCoordinates)
if (pieceMoved) {
nextPlayer()
}
return pieceMoved
}
fun nextPlayer() {
playerTurnIndex++
if (playerTurnIndex > playerIds.size) {
playerTurnIndex = 0
}
playerTurnId = playerIds.elementAt(playerTurnIndex)
}
fun isPlayerFinished(player: Player): Boolean {
val targetFields = this.gameState.getRules()?.getPlayerTargetFields(player.id)
val targetFields = this.gameState.getRules().getPlayerTargetFields(player.id)
if (targetFields != null) {
for (targetField: Vector3 in targetFields) {
if (this.gameState.getBoardState()?.fields?.get(targetField)?.getPiece()?.getOwner()?.id != player.id) {
return false
}
for (targetField: Vector3 in targetFields) {
if (this.gameState.getBoardState().fields[targetField]?.getPiece()?.getOwnerId() != player.id) {
return false
}
}
return true
}
}
\ No newline at end of file
package com.mygdx.game.model//Holds a ruleset and a board
class GameMode(rules:AbstractRules? = null, board:AbstractBoard? = null) {
class GameMode(rules:AbstractRules, board:AbstractBoard) {
private var rules: AbstractRules?
private var board: AbstractBoard?
private var rules: AbstractRules
private var board: AbstractBoard
init {
this.rules = rules
......@@ -14,7 +14,7 @@ class GameMode(rules:AbstractRules? = null, board:AbstractBoard? = null) {
this.board = board
}
fun getBoard(): AbstractBoard? {
fun getBoard(): AbstractBoard {
return this.board
}
......@@ -22,7 +22,7 @@ class GameMode(rules:AbstractRules? = null, board:AbstractBoard? = null) {
this.rules = rules
}
fun getRules(): AbstractRules? {
fun getRules(): AbstractRules {
return this.rules
}
}
\ No newline at end of file
......@@ -5,36 +5,26 @@ import com.mygdx.game.model.gamemodes.rules.DefaultRules
/*
Holds the state of the game and all data related to this.
*/
class GameState(gameMode: GameMode? = null) {
class GameState(gameMode: GameMode) {
private var boardState:AbstractBoard?
private var rules:AbstractRules?
private var boardState: AbstractBoard
private var rules: AbstractRules
private var isStarted: Boolean
private var winner: Player?
init {
if (gameMode == null) {
this.rules = null
this.boardState = null
} else {
this.rules = gameMode.getRules()
this.boardState = gameMode.getBoard()
this.rules?.let { this.boardState?.placePiecesAtStart(this.rules!!, it.generateStartFields()) }
}
this.rules = gameMode.getRules()
this.boardState = gameMode.getBoard()
this.isStarted = false
this.winner = null
}
fun setBoardState(boardState: AbstractBoard) {
this.boardState = boardState
this.rules?.let { this.boardState!!.placePiecesAtStart(this.rules!!, it.generateStartFields()) }
}
fun getBoardState(): AbstractBoard? {
fun getBoardState(): AbstractBoard {
return boardState
}
fun getRules(): AbstractRules? {
fun getRules(): AbstractRules {
return rules
}
......
//Wich players are int the game, holds a Game
//Controller?
\ No newline at end of file
//Controller?
// TODO: remove? Replaced by Lobby.java
\ No newline at end of file
package com.mygdx.game.model
class Piece(owner: Player) {
class Piece(ownerId: Int) {
private var owner: Player
private var ownerId: Int = ownerId
fun getOwnerId(): Int {
return this.ownerId
}
/*private var owner: Player
init {
this.owner = owner
......@@ -10,6 +16,6 @@ class Piece(owner: Player) {
fun getOwner(): Player {
return this.owner
}
}*/
}
\ No newline at end of file
......@@ -8,6 +8,6 @@ abstract class AbstractBoard {
abstract fun computeFields() // Computes all fields on the board
abstract fun fieldExists(coordinates: Vector3): Boolean
abstract fun placePiecesAtStart(rules: AbstractRules, startFieldCoordinates: List<List<Vector3>>)
abstract fun placePiecesAtStart(rules: AbstractRules, playerIds: HashSet<Int>)
abstract fun movePiece(fromCoordinates: Vector3, toCoordinates: Vector3): Boolean
}
\ No newline at end of file
......@@ -27,16 +27,17 @@ class DefaultBoard: AbstractBoard() {
}
}
override fun placePiecesAtStart(rules: AbstractRules, startFieldCoordinates: List<List<Vector3>>) {
var playerId = 0
override fun placePiecesAtStart(rules: AbstractRules, playerIds: HashSet<Int>) {
var playerIndex = 0
for (coordinateSet: List<Vector3> in startFieldCoordinates) {
val player = Player(playerId++)
for (coordinateSet: List<Vector3> in rules.generateStartFields()) {
for (coordinate: Vector3 in coordinateSet) {
val piece = Piece(player)
//val piece = Piece(playerIds.elementAt(playerIndex)) // TODO: use this
val piece = Piece(playerIndex)
fields[coordinate]?.setPiece(piece)
}
playerIndex++
}
}
......
......@@ -276,9 +276,9 @@ public class CinematicView extends View{
stage.clear();
startFadeout = false;
Game model = new Game(new GameState(new GameMode(new DefaultRules(), new DefaultBoard())));
Game model = new Game(new GameState(new GameMode(new DefaultRules(), new DefaultBoard())), playerController.getLobby().getPlayersID());
PlayView playView = new PlayView(gvm, playerController, assetManager, stage, skin, lobbyAvatars);
GameController gameController = new GameController(model, playView);
GameController gameController = new GameController(model, playView, playerController);
playView.setGameController(gameController);
gvm.set(playView);
......
......@@ -13,6 +13,7 @@ import com.badlogic.gdx.utils.Scaling;
import com.mygdx.game.controllers.GameController;
import com.mygdx.game.controllers.PlayerController;
import com.mygdx.game.controllers.UtilsKt;
import com.mygdx.game.model.Player;
import com.mygdx.game.views.enums.PlayAssets;
import com.mygdx.game.views.tokens.AnimatedSprite;
import com.mygdx.game.views.tokens.StarPiece;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment