Skip to content
Snippets Groups Projects
Commit f328d873 authored by Anders H. Rebner's avatar Anders H. Rebner
Browse files

Fixed concurrentModificationException and placed playernames

parent 4c0db6d9
No related branches found
No related tags found
1 merge request!14WIP: Default game
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; package com.mygdx.game.controllers;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.Constants;
import com.mygdx.game.controllers.commands.cPlayerPieceMove; import com.mygdx.game.controllers.commands.cPlayerPieceMove;
import com.mygdx.game.model.Game; import com.mygdx.game.model.Game;
import com.mygdx.game.model.Player; import com.mygdx.game.model.Player;
import com.mygdx.game.views.PlayView; import com.mygdx.game.views.PlayView;
import com.mygdx.game.views.tokens.StarPiece; import com.mygdx.game.views.tokens.StarPiece;
import java.util.List;
public class GameController { public class GameController {
private final Game model; private final Game model;
...@@ -24,7 +27,13 @@ public class GameController { ...@@ -24,7 +27,13 @@ public class GameController {
this.activePiece = null; this.activePiece = null;
this.previousCoordinateClicked = 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.initializePieces(this.model.getStartFieldCoordinates());
this.view.placePlayerNames(playerIndexes, playerNameTextFieldCoordinates);
} }
public void handleClick(float x, float y) { public void handleClick(float x, float y) {
......
package com.mygdx.game.model package com.mygdx.game.model
import com.badlogic.gdx.math.Vector3 import com.badlogic.gdx.math.Vector3
import com.mygdx.game.Constants
class Game(gameState: GameState, playerIds: HashSet<Int>) { class Game(gameState: GameState, playerIds: HashSet<Int>) {
...@@ -73,4 +74,12 @@ class Game(gameState: GameState, playerIds: HashSet<Int>) { ...@@ -73,4 +74,12 @@ class Game(gameState: GameState, playerIds: HashSet<Int>) {
return true 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
...@@ -6,9 +6,11 @@ abstract class AbstractRules { ...@@ -6,9 +6,11 @@ abstract class AbstractRules {
abstract var startFields: List<List<Vector3>> // List containing list of startfields for each player 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 moveRange: Int // How far a piece can be moved
abstract var jumpRange: Int // How far a piece can jump (over other pieces) 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 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 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 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 package com.mygdx.game.model.gamemodes.rules
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.math.Vector3 import com.badlogic.gdx.math.Vector3
import com.mygdx.game.Constants
import com.mygdx.game.model.AbstractRules import com.mygdx.game.model.AbstractRules
import com.mygdx.game.model.Field
class DefaultRules: AbstractRules() { class DefaultRules: AbstractRules() {
override var startFields = generateStartFields() override var startFields = generateStartFields()
...@@ -75,5 +76,30 @@ class DefaultRules: AbstractRules() { ...@@ -75,5 +76,30 @@ class DefaultRules: AbstractRules() {
return startFields 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; ...@@ -25,9 +25,15 @@ import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField; import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Align;
import com.mygdx.game.controllers.GameController;
import com.mygdx.game.controllers.PlayerController; 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.Lobby;
import com.mygdx.game.model.Player; 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.CharacterAssets;
import com.mygdx.game.views.enums.CinematicAssets; import com.mygdx.game.views.enums.CinematicAssets;
import com.mygdx.game.views.enums.LobbyAssets; import com.mygdx.game.views.enums.LobbyAssets;
...@@ -199,7 +205,7 @@ public class LobbyView extends View{ ...@@ -199,7 +205,7 @@ public class LobbyView extends View{
lobbyCreateTextField.setPosition(lobbyListRefreshButton.getX(), lobbyListRefreshButton.getY()+row_height*1.25f); lobbyCreateTextField.setPosition(lobbyListRefreshButton.getX(), lobbyListRefreshButton.getY()+row_height*1.25f);
lobbyCreateSizeSelectBox =new SelectBox<>(skin); lobbyCreateSizeSelectBox =new SelectBox<>(skin);
lobbyCreateSizeSelectBox.setItems(2, 4, 6, 8); lobbyCreateSizeSelectBox.setItems(2, 4, 6);
lobbyCreateSizeSelectBox.setPosition(lobbyListBackButton.getX(), lobbyCreateTextField.getY()); lobbyCreateSizeSelectBox.setPosition(lobbyListBackButton.getX(), lobbyCreateTextField.getY());
lobbyCreateSizeSelectBox.setSize(lobbyListBackButton.getWidth(), lobbyListBackButton.getHeight()); lobbyCreateSizeSelectBox.setSize(lobbyListBackButton.getWidth(), lobbyListBackButton.getHeight());
......
...@@ -9,7 +9,9 @@ import com.badlogic.gdx.math.Vector3; ...@@ -9,7 +9,9 @@ import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.utils.Scaling; import com.badlogic.gdx.utils.Scaling;
import com.mygdx.game.Constants;
import com.mygdx.game.controllers.GameController; import com.mygdx.game.controllers.GameController;
import com.mygdx.game.controllers.PlayerController; import com.mygdx.game.controllers.PlayerController;
import com.mygdx.game.controllers.UtilsKt; import com.mygdx.game.controllers.UtilsKt;
...@@ -20,8 +22,10 @@ import com.mygdx.game.views.tokens.StarPiece; ...@@ -20,8 +22,10 @@ import com.mygdx.game.views.tokens.StarPiece;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.soap.Text;
public class PlayView extends View{ public class PlayView extends View{
...@@ -39,8 +43,9 @@ public class PlayView extends View{ ...@@ -39,8 +43,9 @@ public class PlayView extends View{
Texture starPieceHead; Texture starPieceHead;
Texture starPieceHeadBorder; Texture starPieceHeadBorder;
HashMap<Vector3, StarPiece> pieces; ConcurrentHashMap<Vector3, StarPiece> pieces; // Concurrent to avoid ConcurrentModificationException due to render-function
GameController gameController; GameController gameController;
List<TextField> playerNameFields;
float scale_factor_piece; float scale_factor_piece;
public float hex_side_length; public float hex_side_length;
...@@ -88,7 +93,20 @@ public class PlayView extends View{ ...@@ -88,7 +93,20 @@ public class PlayView extends View{
starPieceHead = (Texture) assetManager.get(PlayAssets.STAR_PIECE_HEAD.path, PlayAssets.STAR_PIECE_HEAD.classType); 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); 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) { public void setGameController(GameController gameController) {
...@@ -205,6 +223,21 @@ public class PlayView extends View{ ...@@ -205,6 +223,21 @@ public class PlayView extends View{
return startFieldCoordinatesPixel; 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) { public void movePiece(StarPiece piece, Vector3 toCoordinates) {
Float[] pixelCoordinates = UtilsKt.cubeToPixel(toCoordinates, hex_side_length); Float[] pixelCoordinates = UtilsKt.cubeToPixel(toCoordinates, hex_side_length);
...@@ -224,7 +257,7 @@ public class PlayView extends View{ ...@@ -224,7 +257,7 @@ public class PlayView extends View{
piece.setY(Gdx.graphics.getHeight() / 2F - pixelCoordinates[1] - ((starPieceBase.getHeight() * scale_factor_piece) / 2F)); piece.setY(Gdx.graphics.getHeight() / 2F - pixelCoordinates[1] - ((starPieceBase.getHeight() * scale_factor_piece) / 2F));
// Replace key // 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){ private void debugDraw(SpriteBatch sb, String text, int xPos, int yPos){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment