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

Fixed concurrentModificationException and placed playernames

parent 4c0db6d9
package com.mygdx.game;
public class Constants {
public static final int PLAYER_NAME_TEXT_FIELD_WIDTH = 340;
public static final int PLAYER_NAME_TEXT_FIELD_HEIGHT = 80;
}
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;
import com.mygdx.game.views.PlayView;
import com.mygdx.game.views.tokens.StarPiece;
import java.util.List;
public class GameController {
private final Game model;
......@@ -24,7 +27,13 @@ public class GameController {
this.activePiece = null;
this.previousCoordinateClicked = null;
// Get used boardslots and coordinates to place name of players
List<Integer> playerIndexes = this.model.getUsedBoardSlots(playerController.getLobby().getPlayers().size());
List<List<Float>> playerNameTextFieldCoordinates = this.model.getPlayerNameCoordinates(this.view.hex_side_length);
// Initialize pieces and place playernames
this.view.initializePieces(this.model.getStartFieldCoordinates());
this.view.placePlayerNames(playerIndexes, playerNameTextFieldCoordinates);
}
public void handleClick(float x, float y) {
......
package com.mygdx.game.model
import com.badlogic.gdx.math.Vector3
import com.mygdx.game.Constants
class Game(gameState: GameState, playerIds: HashSet<Int>) {
......@@ -73,4 +74,12 @@ class Game(gameState: GameState, playerIds: HashSet<Int>) {
return true
}
fun getUsedBoardSlots(playerCount: Int): List<Int> {
return gameState.getRules().getUsedBoardSlots(playerCount)
}
fun getPlayerNameCoordinates(hex_side_length: Float): List<List<Float>> {
return gameState.getRules().getPlayerNameCoordinates(hex_side_length)
}
}
\ No newline at end of file
......@@ -5,10 +5,12 @@ import com.badlogic.gdx.math.Vector3
abstract class AbstractRules {
abstract var startFields: List<List<Vector3>> // List containing list of startfields for each player
abstract var moveRange: Int // How far a piece can be moved
abstract var jumpRange: Int // How far a piece can jump (over other pieces)
abstract var moveRange: Int // How far a piece can be moved
abstract var jumpRange: Int // How many pieces a piece can jump over
abstract fun getPlayerStartfields(playerIndex: Int) : List<Vector3> // Returns a list of startfields for a given player
abstract fun getPlayerTargetFields(playerIndex: Int) : List<Vector3> // Returns a list of targetfields for a given player
abstract fun generateStartFields(playerCount: Int = 6): List<List<Vector3>> // Returns a list with lists of startfields
abstract fun getPlayerStartfields(playerIndex: Int) : List<Vector3> // Returns a list of startfields for a given player
abstract fun getPlayerTargetFields(playerIndex: Int) : List<Vector3> // Returns a list of targetfields for a given player
abstract fun generateStartFields(playerCount: Int = 6): List<List<Vector3>> // Returns a list with lists of startfields
abstract fun getUsedBoardSlots(playerCount: Int): List<Int> // Returns a list with used boardslots
abstract fun getPlayerNameCoordinates(hex_side_length: Float): List<List<Float>> // Returns a list with lists of coordinates for playernames
}
\ No newline at end of file
package com.mygdx.game.model.gamemodes.rules
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.math.Vector3
import com.mygdx.game.Constants
import com.mygdx.game.model.AbstractRules
import com.mygdx.game.model.Field
class DefaultRules: AbstractRules() {
override var startFields = generateStartFields()
......@@ -30,29 +31,29 @@ class DefaultRules: AbstractRules() {
override fun generateStartFields(playerCount: Int): List<List<Vector3>> {
val startFields: MutableList<List<Vector3>> = mutableListOf()
val allStartFields: List<List<Vector3>> = listOf(
listOf( Vector3(1F,4F,-5F), Vector3(2F,3F,-5F), Vector3(3F,2F,-5F), Vector3(4F,1F,-5F),
Vector3(2F,4F,-6F), Vector3(3F,3F,-6F), Vector3(4F,2F,-6F), Vector3(3F,4F,-7F),
Vector3(4F,3F,-7F), Vector3(4F,4F,-8F)
listOf(Vector3(1F, 4F, -5F), Vector3(2F, 3F, -5F), Vector3(3F, 2F, -5F), Vector3(4F, 1F, -5F),
Vector3(2F, 4F, -6F), Vector3(3F, 3F, -6F), Vector3(4F, 2F, -6F), Vector3(3F, 4F, -7F),
Vector3(4F, 3F, -7F), Vector3(4F, 4F, -8F)
),
listOf( Vector3(5F,-1F,-4F), Vector3(5F,-2F,-3F), Vector3(5F,-3F,-2F), Vector3(5F,-4F,-1F),
Vector3(6F,-2F,-4F), Vector3(6F,-3F,-3F), Vector3(6F,-4F,-2F), Vector3(7F,-3F,-4F),
Vector3(7F,-4F,-3F), Vector3(8F,-4F,-4F)
listOf(Vector3(5F, -1F, -4F), Vector3(5F, -2F, -3F), Vector3(5F, -3F, -2F), Vector3(5F, -4F, -1F),
Vector3(6F, -2F, -4F), Vector3(6F, -3F, -3F), Vector3(6F, -4F, -2F), Vector3(7F, -3F, -4F),
Vector3(7F, -4F, -3F), Vector3(8F, -4F, -4F)
),
listOf( Vector3(4F,-5F,1F), Vector3(3F,-5F,2F), Vector3(2F,-5F,3F), Vector3(1F,-5F,4F),
Vector3(4F,6F,2F), Vector3(3F,-6F,3F), Vector3(2F,-6F,4F), Vector3(4F,-7F,3F),
Vector3(3F,-7F,4F), Vector3(4F,-8F,4F)
listOf(Vector3(4F, -5F, 1F), Vector3(3F, -5F, 2F), Vector3(2F, -5F, 3F), Vector3(1F, -5F, 4F),
Vector3(4F, 6F, 2F), Vector3(3F, -6F, 3F), Vector3(2F, -6F, 4F), Vector3(4F, -7F, 3F),
Vector3(3F, -7F, 4F), Vector3(4F, -8F, 4F)
),
listOf( Vector3(-4F,-1F,5F), Vector3(-3F,-2F,5F), Vector3(-2F,-3F,5F), Vector3(-1F,-4F,5F),
Vector3(-4F,-2F,6F), Vector3(-3F,-3F,6F), Vector3(-2F,-4F,6F), Vector3(-4F,-3F,7F),
Vector3(-3F,-4F,7F), Vector3(-4F,-4F,8F)
listOf(Vector3(-4F, -1F, 5F), Vector3(-3F, -2F, 5F), Vector3(-2F, -3F, 5F), Vector3(-1F, -4F, 5F),
Vector3(-4F, -2F, 6F), Vector3(-3F, -3F, 6F), Vector3(-2F, -4F, 6F), Vector3(-4F, -3F, 7F),
Vector3(-3F, -4F, 7F), Vector3(-4F, -4F, 8F)
),
listOf( Vector3(-5F,4F,1F), Vector3(-5F,3F,2F), Vector3(-5F,2F,3F), Vector3(-5F,1F,4F),
Vector3(-6F,4F,2F), Vector3(-6F,3F,3F), Vector3(-6F,2F,4F), Vector3(-7F,4F,3F),
Vector3(-7F, 3F, 4F), Vector3(-8F,4F,4F)
listOf(Vector3(-5F, 4F, 1F), Vector3(-5F, 3F, 2F), Vector3(-5F, 2F, 3F), Vector3(-5F, 1F, 4F),
Vector3(-6F, 4F, 2F), Vector3(-6F, 3F, 3F), Vector3(-6F, 2F, 4F), Vector3(-7F, 4F, 3F),
Vector3(-7F, 3F, 4F), Vector3(-8F, 4F, 4F)
),
listOf( Vector3(-1F,5F,-4F), Vector3(-2F,5F,-3F), Vector3(-3F,5F,-2F), Vector3(-4F,5F,-1F),
Vector3(-2F,6F,-4F), Vector3(-3F,6F,-3F), Vector3(-4F,6F,-2F), Vector3(-3F,7F,-4F),
Vector3(-4F,7F,-3F), Vector3(-4F,8F,-4F)
listOf(Vector3(-1F, 5F, -4F), Vector3(-2F, 5F, -3F), Vector3(-3F, 5F, -2F), Vector3(-4F, 5F, -1F),
Vector3(-2F, 6F, -4F), Vector3(-3F, 6F, -3F), Vector3(-4F, 6F, -2F), Vector3(-3F, 7F, -4F),
Vector3(-4F, 7F, -3F), Vector3(-4F, 8F, -4F)
)
)
......@@ -75,5 +76,30 @@ class DefaultRules: AbstractRules() {
return startFields
}
override fun getUsedBoardSlots(playerCount: Int): List<Int> {
return when (playerCount) {
2 -> {
listOf(0, 3)
}
4 -> {
listOf(1, 2, 4, 5)
}
else -> {
listOf(0, 1, 2, 3, 4, 5)
}
}
}
override fun getPlayerNameCoordinates(hex_side_length: Float): List<List<Float>> {
return listOf(
listOf(Gdx.graphics.width / 2f - Constants.PLAYER_NAME_TEXT_FIELD_WIDTH - 2.5f * hex_side_length, Gdx.graphics.height - Constants.PLAYER_NAME_TEXT_FIELD_HEIGHT - 1.5f * hex_side_length),
listOf(Gdx.graphics.width / 2f + Constants.PLAYER_NAME_TEXT_FIELD_WIDTH + 4f * hex_side_length, Gdx.graphics.height / 2f + 4 * hex_side_length),
listOf(Gdx.graphics.width / 2f + Constants.PLAYER_NAME_TEXT_FIELD_WIDTH + 4f * hex_side_length, Gdx.graphics.height / 2f - 5 * hex_side_length),
listOf(Gdx.graphics.width / 2f - Constants.PLAYER_NAME_TEXT_FIELD_WIDTH - 2.5f * hex_side_length, 1.5f * hex_side_length),
listOf(Gdx.graphics.width / 2f - Constants.PLAYER_NAME_TEXT_FIELD_WIDTH - 12f * hex_side_length, Gdx.graphics.height / 2f - 5 * hex_side_length),
listOf(Gdx.graphics.width / 2f - Constants.PLAYER_NAME_TEXT_FIELD_WIDTH - 12f * hex_side_length, Gdx.graphics.height / 2f + 4 * hex_side_length)
)
}
}
......@@ -25,9 +25,15 @@ import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.utils.Align;
import com.mygdx.game.controllers.GameController;
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.Lobby;
import com.mygdx.game.model.Player;
import com.mygdx.game.model.gamemodes.rules.DefaultRules;
import com.mygdx.game.views.enums.CharacterAssets;
import com.mygdx.game.views.enums.CinematicAssets;
import com.mygdx.game.views.enums.LobbyAssets;
......@@ -199,7 +205,7 @@ public class LobbyView extends View{
lobbyCreateTextField.setPosition(lobbyListRefreshButton.getX(), lobbyListRefreshButton.getY()+row_height*1.25f);
lobbyCreateSizeSelectBox =new SelectBox<>(skin);
lobbyCreateSizeSelectBox.setItems(2, 4, 6, 8);
lobbyCreateSizeSelectBox.setItems(2, 4, 6);
lobbyCreateSizeSelectBox.setPosition(lobbyListBackButton.getX(), lobbyCreateTextField.getY());
lobbyCreateSizeSelectBox.setSize(lobbyListBackButton.getWidth(), lobbyListBackButton.getHeight());
......
......@@ -9,7 +9,9 @@ import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.utils.Scaling;
import com.mygdx.game.Constants;
import com.mygdx.game.controllers.GameController;
import com.mygdx.game.controllers.PlayerController;
import com.mygdx.game.controllers.UtilsKt;
......@@ -20,8 +22,10 @@ import com.mygdx.game.views.tokens.StarPiece;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.soap.Text;
public class PlayView extends View{
......@@ -39,8 +43,9 @@ public class PlayView extends View{
Texture starPieceHead;
Texture starPieceHeadBorder;
HashMap<Vector3, StarPiece> pieces;
ConcurrentHashMap<Vector3, StarPiece> pieces; // Concurrent to avoid ConcurrentModificationException due to render-function
GameController gameController;
List<TextField> playerNameFields;
float scale_factor_piece;
public float hex_side_length;
......@@ -88,7 +93,20 @@ public class PlayView extends View{
starPieceHead = (Texture) assetManager.get(PlayAssets.STAR_PIECE_HEAD.path, PlayAssets.STAR_PIECE_HEAD.classType);
starPieceHeadBorder = (Texture) assetManager.get(PlayAssets.STAR_PIECE_HEAD_BORDER.path, PlayAssets.STAR_PIECE_HEAD_BORDER.classType);
this.pieces = new HashMap<>();
this.pieces = new ConcurrentHashMap<>();
/*for (Player player : playerController.getLobby().getPlayers()) {
String playerName = player.getPlayerName();
TextField textField = getPlayerNameTextField(player.getPlayerName(), index);
TextField textField = new TextField(player.getPlayerName(), skin);
textField.setSize(300, 80);
textField.setPosition(Gdx.graphics.getWidth() / 2F - textField.getWidth() - (2 * hex_side_length), Gdx.graphics.getHeight() - (3 * hex_side_length));
stage.addActor(textField);
playerNameFields.add(textField);
}*/
}
public void setGameController(GameController gameController) {
......@@ -205,6 +223,21 @@ public class PlayView extends View{
return startFieldCoordinatesPixel;
}
public void placePlayerNames(List<Integer> playerIndexes, List<List<Float>> coordinateList) {
List<Player> players = playerController.getLobby().getPlayers();
playerNameFields = new ArrayList<>();
for (int i = 0; i < players.size(); i++) {
TextField textField = new TextField(players.get(i).getPlayerName(), skin);
textField.setSize(Constants.PLAYER_NAME_TEXT_FIELD_WIDTH, Constants.PLAYER_NAME_TEXT_FIELD_HEIGHT);
textField.setPosition(coordinateList.get(playerIndexes.get(i)).get(0), coordinateList.get(playerIndexes.get(i)).get(1));
stage.addActor(textField);
playerNameFields.add(textField);
}
}
public void movePiece(StarPiece piece, Vector3 toCoordinates) {
Float[] pixelCoordinates = UtilsKt.cubeToPixel(toCoordinates, hex_side_length);
......@@ -224,7 +257,7 @@ public class PlayView extends View{
piece.setY(Gdx.graphics.getHeight() / 2F - pixelCoordinates[1] - ((starPieceBase.getHeight() * scale_factor_piece) / 2F));
// Replace key
pieces.put(toCoordinates, pieces.remove(fromCoordinates)); // TODO: May cause concurrency issues. If so, create new hashmap and assign it to this.pieces
pieces.put(toCoordinates, pieces.remove(fromCoordinates));
}
private void debugDraw(SpriteBatch sb, String text, int xPos, int yPos){
......
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