Commit 7894b4ca authored by morkolai's avatar morkolai
Browse files

resolved merge conflicts from merge with default-game

parents b29a247a 5e4090d7
package com.mygdx.game;
import java.util.HashMap;
public class Constants {
public static final int PLAYER_NAME_TEXT_FIELD_WIDTH = 340;
public static final int PLAYER_NAME_TEXT_FIELD_HEIGHT = 80;
public static final HashMap<Integer, String> AVAILABLEAVATARSHASHMAP = new HashMap<Integer, String>() {{
put(0, "RegularGuy");
put(1, "HipsterGirl");
put(2, "HighSchoolGuy");
put(3, "RocketGuy");
put(4, "AsianGirl");
put(5, "Grandma");
}};
}
......@@ -10,22 +10,15 @@ import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.game.controllers.GameController;
import com.mygdx.game.controllers.NetworkController;
import com.mygdx.game.controllers.PlayerController;
import com.mygdx.game.model.DefaultBoard;
import com.mygdx.game.model.Game;
import com.mygdx.game.model.GameMode;
import com.mygdx.game.model.GameState;
import com.mygdx.game.model.gamemodes.rules.DefaultRules;
import com.mygdx.game.views.GameViewManager;
import com.mygdx.game.views.PlayView;
import com.mygdx.game.views.LoadingView;
import java.util.ArrayList;
public class UniCheckersClient extends ApplicationAdapter {
PlayerController playerController;
GameController gameController;
private GameViewManager gvm;
private SpriteBatch sb;
......@@ -46,9 +39,7 @@ public class UniCheckersClient extends ApplicationAdapter {
stage = new Stage(viewport);
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 Models.. // TODO: remove?
// Initialize controllers..
NetworkController networkController = new NetworkController();
......@@ -61,13 +52,7 @@ public class UniCheckersClient extends ApplicationAdapter {
skin = new Skin(Gdx.files.internal("UISkins/glassy/skin/glassy-ui.json"));
gvm = new GameViewManager();
//gvm.push(new LoadingView(gvm, playerController, assetManager, stage, skin));
//gvm.push(new CinematicView(gvm, playerController, assetManager, stage, skin));
PlayView playView = new PlayView(gvm, playerController, assetManager, stage, skin, null);
gameController = new GameController(model, playView);
playView.setGameController(gameController);
gvm.push(playView);
gvm.push(new LoadingView(gvm, playerController, assetManager, stage, skin));
}
@Override
......
package com.mygdx.game.controllers;
import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.controllers.commands.cPlayerPieceMove;
import com.mygdx.game.model.Game;
import com.mygdx.game.views.Constants;
import com.mygdx.game.model.Player;
import com.mygdx.game.views.PlayView;
import com.mygdx.game.views.tokens.StarPiece;
import java.util.List;
public class GameController {
private final Game model;
......@@ -13,18 +16,29 @@ public class GameController {
private StarPiece activePiece; // Previously clicked piece
private Vector3 previousCoordinateClicked; // Previously clicked coordinate
private final PlayerController playerController;
private final List<List<Float>> playerNameTextFieldCoordinates;
public GameController(Game model, PlayView view) {
public GameController(Game model, PlayView view, PlayerController playerController) {
this.model = model;
this.view = view;
this.playerController = playerController;
this.activePiece = null;
this.previousCoordinateClicked = null;
// Get used boardslots and coordinates to place name of players
List<Integer> usedBoardSlots = this.model.getUsedBoardSlots();
playerNameTextFieldCoordinates = this.model.getPlayerNameCoordinates(this.view.hex_side_length);
// Initialize pieces, place playernames and place turn-indicator
this.view.initializePieces(this.model.getStartFieldCoordinates());
this.view.placePlayerNames(usedBoardSlots, playerNameTextFieldCoordinates);
this.view.placeTurnIndicator(playerNameTextFieldCoordinates.get(this.model.getPlayerTurnSlot()));
}
public void handleClick(float x, float y) {
Vector3 cubeCoordinates = UtilsKt.pixelToCube(x, y, Constants.HEX_SIDE_LENGTH);
Vector3 cubeCoordinates = UtilsKt.pixelToCube(x, y, this.view.hex_side_length);
// If field exists at clicked coordinates
if (this.model.fieldExists(cubeCoordinates)) {
......@@ -34,26 +48,57 @@ public class GameController {
if (activePiece != null) {
activePiece.setRotateHead(false);
}
// TODO: Check if the piece is owned by this player
activePiece = this.view.getPiece(cubeCoordinates);
System.out.print(this.model.getGameState().getGameMode().getPossibleMoves(cubeCoordinates));
activePiece.setRotateHead(true);
previousCoordinateClicked = cubeCoordinates;
// If clicked piece is owned by player and player is not finished, activate piece
if(this.model.getPieceOwnerId(cubeCoordinates) == playerController.getPlayer().getID() && !this.model.isPlayerFinished(playerController.getPlayer().getID())) {
activePiece = this.view.getPiece(cubeCoordinates);
activePiece.setRotateHead(true);
previousCoordinateClicked = cubeCoordinates;
}
} else { // Field does NOT have piece
if (activePiece != null) {
// TODO: Check if it is the players turn (could be done in model)
// Try to move piece in model, if successful, move piece in view
if (this.model.movePiece(previousCoordinateClicked, cubeCoordinates)) {
// Try to move piece in model. If successful, move piece in view
if (this.model.movePiece(previousCoordinateClicked, cubeCoordinates, playerController.getPlayer().getID(), this)) {
this.view.movePiece(activePiece, cubeCoordinates);
activePiece.setRotateHead(false);
activePiece = null;
playerController.getNetWorkController().sendToServer(new cPlayerPieceMove(previousCoordinateClicked, cubeCoordinates, playerController.getLobby().getID(), playerController.getPlayer().getID()));
previousCoordinateClicked = null;
// TODO: Check if all pieces of the player are in goal? (model.isPlayerFinished)
this.view.placeTurnIndicator(playerNameTextFieldCoordinates.get(this.model.getPlayerTurnSlot()));
} else {
// TODO: Give feedback (move was not valid/not executed)
System.out.println("Move was not allowed"); // TODO: Give feedback (move was not valid/not executed)
}
}
}
}
}
public void playerMovedPiece(Vector3 fromCoordinates, Vector3 toCoordinates, int playerId) {
if (this.model.movePiece(fromCoordinates, toCoordinates, playerId, this)) {
this.view.moveOtherPlayerPiece(fromCoordinates, toCoordinates);
this.view.placeTurnIndicator(playerNameTextFieldCoordinates.get(this.model.getPlayerTurnSlot()));
} else {
System.out.println("ERROR: Could not execute moving of other players piece"); // TODO: Give feedback
}
}
public void setPlayerFinished(int playerId, int place) {
int playerIndex;
List<Player> players = playerController.getLobby().getPlayers();
for (Player player: players) {
if (player.getID() == playerId) {
playerIndex = players.indexOf(player);
this.view.setPlayerFinished(playerIndex, place);
}
}
}
public void setGameFinished() {
this.view.removeTurnIndicator();
this.view.placeHomeButton();
}
}
package com.mygdx.game.controllers;
import com.badlogic.gdx.math.Vector3;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.mygdx.game.controllers.commands.Command;
......@@ -9,6 +10,7 @@ import com.mygdx.game.model.Player;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class NetworkController {
......@@ -21,7 +23,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.136"; //122 //"192.168.87.20";
client.connect(10000, IP4_LAN_ADDRESS, 54555, 54777);
} catch (IOException e) {
e.printStackTrace();
......@@ -50,6 +52,7 @@ public class NetworkController {
kryo.register(cSetPlayerReady.class, 26);
kryo.register(cSetPlayerName.class, 27);
kryo.register(cStartGame.class, 28);
kryo.register(Vector3.class, 29);
}
public Client getClient() { return client; }
......
package com.mygdx.game.controllers;
import com.badlogic.gdx.utils.Null;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.mygdx.game.controllers.commands.*;
......@@ -20,6 +19,7 @@ public class PlayerController {
private Player player; //This is the current player
private Lobby lobby; //Current lobby
private ArrayList<Lobby> lobbies; //List of all lobbies
private GameController gameController;
public PlayerController(NetworkController networkController){
net = networkController;
......@@ -103,5 +103,11 @@ public class PlayerController {
public Command getLastCommand(){ return receivedCommands.getFirst(); }
public void setGameController(GameController gameController) {
this.gameController = gameController;
}
public GameController getGameController() {
return gameController;
}
}
package com.mygdx.game.controllers.commands;
import com.badlogic.gdx.math.Vector3;
import com.esotericsoftware.kryonet.Connection;
import com.mygdx.game.controllers.NetworkController;
import com.mygdx.game.controllers.PlayerController;
import java.util.ArrayList;
import java.util.List;
public class cPlayerPieceMove extends Command {
Vector3 fromCoordinates;
Vector3 toCordinates;
int lobby_id;
int playerId;
public cPlayerPieceMove() {
super("cPlayerPieceMove");
}
public cPlayerPieceMove(Vector3 fromCoordinates, Vector3 toCoordinates, int lobbyId, int playerId) {
super("cPlayerPieceMove");
this.fromCoordinates = fromCoordinates;
this.toCordinates = toCoordinates;
this.lobby_id = lobbyId;
this.playerId = playerId;
ArrayList<Object> data = new ArrayList<Object>();
data.add(fromCoordinates);
data.add(toCoordinates);
data.add(playerId);
this.data = data;
}
@Override
public void execute(PlayerController playerController, Connection connection){
//SERVER LOGIC
System.out.println("Execute on client. Update model on both-, with views, animations and assets on client side.");
if (data instanceof ArrayList) {
List<Object> receivedData = (ArrayList<Object>) data;
Vector3 fromCoordinates = (Vector3) receivedData.get(0);
Vector3 toCoordinates = (Vector3) receivedData.get(1);
int playerId = (int) receivedData.get(2);
playerController.getGameController().playerMovedPiece(fromCoordinates, toCoordinates, playerId);
System.out.printf("Received move from other player. Player ID = %d \n", playerId);
}
}
}
package com.mygdx.game.model
import com.badlogic.gdx.math.Vector3
import com.mygdx.game.controllers.GameController
//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: LinkedHashSet<Int>) {
private var gameState: GameState
private var playerIds: LinkedHashSet<Int>
private var playerTurnId: Int
private var playerTurnIndex: Int = 0 // TODO: Random from server
private var playerTurnSlot: Int
private var playerFinishedIds: List<Int> = listOf()
private var usedBoardSlots: List<Int>
init {
this.gameState = gameState
this.playerIds = playerIds
this.gameState.getBoardState().placePiecesAtStart(this.gameState.getRules(), this.playerIds)
usedBoardSlots = this.gameState.getRules().getUsedBoardSlots(playerIds.size)
playerTurnSlot = usedBoardSlots[0]
playerTurnId = playerIds.elementAt(playerTurnIndex)
}
fun getStartFieldCoordinates(): List<List<Vector3>>? {
return this.gameState.getRules()?.generateStartFields()
fun getStartFieldCoordinates(): List<List<Vector3>> {
return this.gameState.getRules().generateStartFields(playerIds.size)
}
fun fieldExists(cubeCoordinates: Vector3): Boolean {
return this.gameState.getBoardState()?.fieldExists(cubeCoordinates) ?: false
return this.gameState.getBoardState().fieldExists(cubeCoordinates)
}
fun fieldHasPiece(cubeCoordinates: Vector3): Boolean {
print(this.gameState.getBoardState()?.fields?.get(cubeCoordinates)?.hasPiece())
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
}
fun movePiece(fromCoordinates: Vector3, toCoordinates: Vector3, playerId: Int, gameController: GameController): Boolean {
// Check if players turn
if (playerId == playerTurnId) {
// TODO: Check if valid move
val pieceMoved: Boolean = this.gameState.getBoardState().movePiece(fromCoordinates, toCoordinates)
fun isPlayerFinished(player: Player): Boolean {
val targetFields = this.gameState.getRules()?.getPlayerTargetFields(player.id)
if (pieceMoved) {
if (isPlayerFinished(playerId)) {
playerFinishedIds = playerFinishedIds + playerId
gameController.setPlayerFinished(playerId, playerFinishedIds.size)
if (targetFields != null) {
for (targetField: Vector3 in targetFields) {
if (this.gameState.getBoardState()?.fields?.get(targetField)?.getPiece()?.getOwner()?.id != player.id) {
return false
// First player finished, he/she wins
if (playerFinishedIds.size == 1) {
this.gameState.setWinner(playerId)
}
// All players are finished
if (playerFinishedIds.size == playerIds.size) {
gameController.setGameFinished()
this.gameState.setFinished()
}
}
nextPlayer()
}
return pieceMoved
} else {
return false
}
}
fun getPlayerTurnId(): Int {
return playerTurnId
}
fun getPlayerTurnSlot(): Int {
return usedBoardSlots[playerTurnIndex]
}
private fun nextPlayer() {
do {
playerTurnIndex++
if (playerTurnIndex == playerIds.size) {
playerTurnIndex = 0
}
playerTurnId = playerIds.elementAt(playerTurnIndex)
} while (playerFinishedIds.contains(playerTurnId) && playerFinishedIds.size < playerIds.size)
}
fun getPieceOwnerId(coordinates: Vector3): Int {
return gameState.getBoardState().fields[coordinates]?.getPiece()?.getOwnerId() ?: -1
}
fun isPlayerFinished(playerId: Int): Boolean {
if (playerFinishedIds.contains(playerId)) {
return true
}
val boardSlot = usedBoardSlots[playerIds.indexOf(playerId)];
val targetFields = this.gameState.getRules().getPlayerTargetFields(boardSlot)
for (targetField: Vector3 in targetFields) {
if (this.gameState.getBoardState().fields[targetField]?.getPiece()?.getOwnerId() != playerId) {
return false
}
}
return true
}
fun getGameState(): GameState? {
return this.gameState
}
fun getUsedBoardSlots(): List<Int> {
return usedBoardSlots
}
fun getPlayerNameCoordinates(hex_side_length: Float): List<List<Float>> {
return gameState.getRules().getPlayerNameCoordinates(hex_side_length)
}
}
\ No newline at end of file
......@@ -4,10 +4,10 @@ import com.badlogic.gdx.math.Vector3
//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
......@@ -18,7 +18,7 @@ class GameMode(rules:AbstractRules? = null, board:AbstractBoard? = null) {
this.board = board
}
fun getBoard(): AbstractBoard? {
fun getBoard(): AbstractBoard {
return this.board
}
......@@ -26,7 +26,7 @@ class GameMode(rules:AbstractRules? = null, board:AbstractBoard? = null) {
this.rules = rules
}
fun getRules(): AbstractRules? {
fun getRules(): AbstractRules {
return this.rules
}
......@@ -119,7 +119,7 @@ class GameMode(rules:AbstractRules? = null, board:AbstractBoard? = null) {
fun getPossibleMoves(position: Vector3): ArrayList<Vector3> {
var possibleMoves: ArrayList<Vector3> = arrayListOf()
zigzagAlgorithm(position, possibleMoves, this.rules?.moveRange ?: 1, this.rules?.jumpRange ?: 1)
zigzagAlgorithm(position, possibleMoves, this.rules.moveRange , this.rules?.jumpRange)
return possibleMoves
}
}
......
......@@ -5,38 +5,28 @@ 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 gameMode: GameMode?
private var boardState:AbstractBoard?
private var rules:AbstractRules?
private var gameMode: GameMode
private var boardState: AbstractBoard
private var rules: AbstractRules
private var isStarted: Boolean
private var winner: Player?
private var winner: Int?
private var isFinished: Boolean = false
init {
this.gameMode = gameMode
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
}
......@@ -53,17 +43,18 @@ class GameState(gameMode: GameMode? = null) {
}
fun isFinished(): Boolean {
if (this.winner != null) {
return true
}
return false
return isFinished
}
fun getWinner(): Player? {
fun getWinner(): Int? {
return this.winner
}
fun setWinner(winner: Player) {
fun setWinner(winner: Int) {
this.winner = winner
}
fun setFinished() {
isFinished = true
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.mygdx.game.model;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
public class Lobby {
......@@ -61,8 +62,8 @@ public class Lobby {
return players;
}
public HashSet<Integer> getPlayersID(){
HashSet set = new HashSet();
public LinkedHashSet<Integer> getPlayersID(){
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
for(Player player : players){
set.add((Integer) player.getID());
}
......
//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