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

Added ranking, removed sorting player on ID

parent 3d577f0e
package com.mygdx.game.controllers;
import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.Constants;
import com.mygdx.game.controllers.commands.cPlayerPieceMove;
import com.mygdx.game.model.Game;
import com.mygdx.game.model.Player;
......@@ -18,9 +17,7 @@ public class GameController {
private StarPiece activePiece; // Previously clicked piece
private Vector3 previousCoordinateClicked; // Previously clicked coordinate
private final PlayerController playerController;
private List<Integer> usedBoardSlots;
private List<List<Float>> playerNameTextFieldCoordinates;
private final List<List<Float>> playerNameTextFieldCoordinates;
public GameController(Game model, PlayView view, PlayerController playerController) {
this.model = model;
......@@ -31,13 +28,13 @@ public class GameController {
this.previousCoordinateClicked = null;
// Get used boardslots and coordinates to place name of players
usedBoardSlots = this.model.getUsedBoardSlots(playerController.getLobby().getPlayers().size());
List<Integer> usedBoardSlots = this.model.getUsedBoardSlots();
playerNameTextFieldCoordinates = this.model.getPlayerNameCoordinates(this.view.hex_side_length);
// Initialize pieces and place playernames
// Initialize pieces, place playernames and place turn-indicator
this.view.initializePieces(this.model.getStartFieldCoordinates());
this.view.placePlayerNames(usedBoardSlots, playerNameTextFieldCoordinates);
this.view.placeRocket(playerNameTextFieldCoordinates.get(usedBoardSlots.get(0)));
this.view.placeTurnIndicator(playerNameTextFieldCoordinates.get(this.model.getPlayerTurnSlot()));
}
public void handleClick(float x, float y) {
......@@ -51,8 +48,8 @@ public class GameController {
if (activePiece != null) {
activePiece.setRotateHead(false);
}
// If clicked piece is owned by player, activate piece
if(this.model.getPieceOwnerId(cubeCoordinates) == playerController.getPlayer().getID()) {
// 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;
......@@ -61,18 +58,16 @@ public class GameController {
} else { // Field does NOT have piece
if (activePiece != null) {
// Try to move piece in model. If successful, move piece in view
if (this.model.movePiece(previousCoordinateClicked, cubeCoordinates, playerController.getPlayer().getID())) {
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)
System.out.println(model.isPlayerFinished(playerController.getPlayer().getID()));
this.view.placeRocket(playerNameTextFieldCoordinates.get(this.model.getPlayerTurnSlot()));
this.view.placeTurnIndicator(playerNameTextFieldCoordinates.get(this.model.getPlayerTurnSlot()));
} else {
System.out.println("Move was not allowed");
// 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)
}
}
}
......@@ -80,11 +75,29 @@ public class GameController {
}
public void playerMovedPiece(Vector3 fromCoordinates, Vector3 toCoordinates, int playerId) {
if (this.model.movePiece(fromCoordinates, toCoordinates, playerId)) {
if (this.model.movePiece(fromCoordinates, toCoordinates, playerId, this)) {
this.view.moveOtherPlayerPiece(fromCoordinates, toCoordinates);
this.view.placeRocket(playerNameTextFieldCoordinates.get(this.model.getPlayerTurnSlot()));
this.view.placeTurnIndicator(playerNameTextFieldCoordinates.get(this.model.getPlayerTurnSlot()));
} else {
System.out.println("ERROR: Could not execute moving of other players piece");
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(List<Integer> placementIds) {
// TODO: New view, pass results
// Remove turnindicator, add home button
}
}
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.*;
......
package com.mygdx.game.model
import com.badlogic.gdx.math.Vector3
import com.mygdx.game.Constants
import com.mygdx.game.controllers.GameController
class Game(gameState: GameState, playerIds: HashSet<Int>) {
class Game(gameState: GameState, playerIds: LinkedHashSet<Int>) {
private var gameState: GameState
private var playerIds: HashSet<Int>
private var playerIds: LinkedHashSet<Int>
private var playerTurnId: Int
private var playerTurnIndex: Int = 0 // TODO: Random initial value
private var playerTurnSlot: Int = 0; // TODO: Random initial value
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)
this.playerTurnId = playerIds.elementAt(playerTurnIndex);
usedBoardSlots = this.gameState.getRules().getUsedBoardSlots(playerIds.size)
playerTurnSlot = usedBoardSlots[0]
playerTurnId = playerIds.elementAt(playerTurnIndex)
}
fun getStartFieldCoordinates(): List<List<Vector3>> {
......@@ -31,13 +35,25 @@ class Game(gameState: GameState, playerIds: HashSet<Int>) {
return this.gameState.getBoardState().fields[cubeCoordinates]?.hasPiece() == true
}
fun movePiece(fromCoordinates: Vector3, toCoordinates: Vector3, playerId: Int): Boolean {
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)
if (pieceMoved) {
if (isPlayerFinished(playerId)) {
playerFinishedIds = playerFinishedIds + playerId
gameController.setPlayerFinished(playerId, playerFinishedIds.size)
if (playerFinishedIds.size == 1) {
this.gameState.setWinner(playerId)
}
if (playerFinishedIds.size == playerIds.size) {
gameController.setGameFinished(playerFinishedIds)
// TODO: End game
}
}
nextPlayer()
}
......@@ -52,15 +68,17 @@ class Game(gameState: GameState, playerIds: HashSet<Int>) {
}
fun getPlayerTurnSlot(): Int {
return gameState.getRules().getUsedBoardSlots(playerIds.size).get(playerTurnIndex)
return usedBoardSlots[playerTurnIndex]
}
fun nextPlayer() {
playerTurnIndex++
if (playerTurnIndex == playerIds.size) {
playerTurnIndex = 0
}
playerTurnId = playerIds.elementAt(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 {
......@@ -69,8 +87,11 @@ class Game(gameState: GameState, playerIds: HashSet<Int>) {
}
fun isPlayerFinished(playerId: Int): Boolean {
if (playerFinishedIds.contains(playerId)) {
return true
}
val boardSlot = gameState.getRules().getUsedBoardSlots(playerIds.size).get(playerIds.indexOf(playerId));
val boardSlot = usedBoardSlots[playerIds.indexOf(playerId)];
val targetFields = this.gameState.getRules().getPlayerTargetFields(boardSlot)
for (targetField: Vector3 in targetFields) {
......@@ -82,8 +103,8 @@ class Game(gameState: GameState, playerIds: HashSet<Int>) {
return true
}
fun getUsedBoardSlots(playerCount: Int): List<Int> {
return gameState.getRules().getUsedBoardSlots(playerCount)
fun getUsedBoardSlots(): List<Int> {
return usedBoardSlots
}
fun getPlayerNameCoordinates(hex_side_length: Float): List<List<Float>> {
......
......@@ -10,7 +10,7 @@ class GameState(gameMode: GameMode) {
private var boardState: AbstractBoard
private var rules: AbstractRules
private var isStarted: Boolean
private var winner: Player?
private var winner: Int?
init {
this.rules = gameMode.getRules()
......@@ -43,11 +43,11 @@ class GameState(gameMode: GameMode) {
return false
}
fun getWinner(): Player? {
fun getWinner(): Int? {
return this.winner
}
fun setWinner(winner: Player) {
fun setWinner(winner: Int) {
this.winner = winner
}
}
\ 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());
}
......
......@@ -32,7 +32,6 @@ class DefaultBoard: AbstractBoard() {
val startFields: List<List<Vector3>> = rules.generateStartFields(playerIds.size)
for ((playerIndex, coordinateSet: List<Vector3>) in startFields.withIndex()) {
for (coordinate: Vector3 in coordinateSet) {
val piece = Piece(playerIds.elementAt(playerIndex))
fields[coordinate]?.setPiece(piece)
......
......@@ -167,7 +167,7 @@ public class CinematicView extends View{
else{
updateSpritesTimer+= dt;
if(0<= timer && timer <= keyframe1){
System.out.println("KEYFRAME 1!!");
//System.out.println("KEYFRAME 1!!");
for(AnimatedSprite lobbyAvatar : lobbyAvatars){
if(updateSpritesTimer >= updateAvatarFrequency) {
lobbyAvatar.updateTexture();
......@@ -176,7 +176,7 @@ public class CinematicView extends View{
}
}
if(keyframe1 <= timer && timer <= keyframe2){
System.out.println("KEYFRAME 2!!");
//System.out.println("KEYFRAME 2!!");
if(onlyOnce1){
cloudSprite.setAlpha(1);
countDownAnimationSprite.updateTexture();
......@@ -198,7 +198,7 @@ public class CinematicView extends View{
rocketImage.setPosition(rocketImage.getX(), rocketImage.getY()+3);
exhaustImage.setPosition(exhaustImage.getX(), exhaustImage.getY()+3);
cam.position.set(rocketImage.getX(), rocketImage.getY(), 0);
System.out.println("KEYFRAME 3!!");
//System.out.println("KEYFRAME 3!!");
}
if(keyframe3 <= timer && timer <= keyframe4){
if(onlyOnce3) {
......@@ -207,7 +207,7 @@ public class CinematicView extends View{
}
rocketImage.setPosition(rocketImage.getX(), rocketImage.getY()+50);
exhaustImage.setPosition(exhaustImage.getX(), exhaustImage.getY()+50);
System.out.println("KEYFRAME 4!!");
//System.out.println("KEYFRAME 4!!");
cloudSpriteAlpha -= dt*2;
cloudSprite.setAlpha(cloudSpriteAlpha);
}
......
package com.mygdx.game.views;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
......@@ -223,7 +224,18 @@ public class MenuView extends View{
@Override
public void handleInput(float dt) {
if (Gdx.input.isKeyPressed(Input.Keys.ENTER)) {
if(!usernameTextField.getText().isEmpty() && usernameTextField.getText().length() >=3 && usernameTextField.getText().length() <= 10){
buttonClickAudio.play(0.70f);
transitionAudio.play(0.50f);
playerController.playerSetName(usernameTextField.getText());
playerController.updateIndexAvatar(currentIndexAvatar);
startFadeout = true;
}
else{
outputLabel.setText("Please enter a valid name. Has to be between 3 and 10 characters.");
}
}
}
private void updateAvatarPortrait(){
......
......@@ -33,6 +33,7 @@ public class PlayView extends View{
int col_width = Gdx.graphics.getWidth() / Help_Guides;
List<Color> PIECE_COLORS = Arrays.asList(Color.PINK, Color.CYAN, Color.LIME, Color.GOLD, Color.LIGHT_GRAY, Color.PURPLE);
List<Color> PODIUM_COLORS = Arrays.asList(Color.GOLD, new Color(192/255F, 192/255F, 192/255F, 1), Color.WHITE);
Texture starPieceBase;
Texture starPieceBaseBorder;
......@@ -188,11 +189,35 @@ public class PlayView extends View{
}
}
public void placeRocket(List<Float> coordinates) {
public void placeTurnIndicator(List<Float> coordinates) {
rocketImage.setPosition(coordinates.get(0) - 60F, coordinates.get(1) + 50F);
rocketExhaustImage.setPosition(rocketImage.getX() - 62F, rocketImage.getY() + 20F - (rocketImage.getHeight()) * rocketImage.getScaleY());
}
public void setPlayerFinished(int playerIndex, int place) {
// Weaken playername-field
TextField playerNameField = playerNameFields.get(playerIndex);
Color color = playerNameField.getColor();
color.a = 0.6F;
playerNameField.setColor(color);
// Create and place place-field
TextField placeField;
if (place <= 3) {
List<String> placeStrings= Arrays.asList("st", "nd", "rd");
placeField = new TextField(place + placeStrings.get(place - 1), skin);
placeField.setColor(PODIUM_COLORS.get(place - 1));
} else {
placeField = new TextField(place + "th", skin);
placeField.setColor(new Color(205/255F, 127/255F, 50/255F, 1));
}
placeField.setSize(115, 65);
placeField.setPosition(playerNameField.getX() + playerNameField.getWidth() - placeField.getWidth(), playerNameField.getY());
stage.addActor(placeField);
}
@Override
public void fadeIn(float dt) {
stage.getBatch().begin();
......
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