Skip to content

Commit

Permalink
Merge pull request #73 from AnthonyClayton12/master
Browse files Browse the repository at this point in the history
Add button for user to choose their kingdom color
  • Loading branch information
Sesu8642 authored Apr 8, 2024
2 parents a93fa61 + 3a96acc commit 288358e
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public EditorController(EventBus eventBus) {
public void generateEmptyGameState() {
gameState = new GameState();
ArrayList<Player> players = new ArrayList<>();
players.add(new Player(GameController.PLAYER_COLORS[0], Type.LOCAL_PLAYER));
for (int i = 1; i < GameController.PLAYER_COLORS.length; i++) {
players.add(new Player(GameController.PLAYER_COLORS[i], Type.LOCAL_BOT));
players.add(new Player(GameController.COLOR_BANK[0], Type.LOCAL_PLAYER));
for (int i = 1; i < GameController.COLOR_BANK.length; i++) {
players.add(new Player(GameController.COLOR_BANK[i], Type.LOCAL_BOT));
}
GameStateHelper.initializeMap(gameState, players, 0, 0, 0F, null);
eventBus.post(new GameStateChangeEvent(gameState));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

package de.sesu8642.feudaltactics.ingame;

import com.badlogic.gdx.graphics.Color;

/** Parameter class for map generation. Immutable class. */
public class MapParameters {
private int humanPlayerNo;
private int botPlayerNo;
private Long seed;
private int landMass;
private float density;
private Color userColor;

/**
* Constructor.
Expand All @@ -18,28 +21,32 @@ public class MapParameters {
* @param seed map seed to use for generating the map
* @param landMass number of tiles to generate
* @param density map density to use for generation
* @param userColor color user selects for their kingdom
*/
public MapParameters(int humanPlayerNo, int botPlayerNo, Long seed, int landMass, float density) {
public MapParameters(int humanPlayerNo, int botPlayerNo, Long seed, int landMass, float density, Color userColor) {
this.humanPlayerNo = humanPlayerNo;
this.botPlayerNo = botPlayerNo;
this.seed = seed;
this.landMass = landMass;
this.density = density;
this.userColor = userColor;
}

/**
* Constructor. Assumes one human player vs. 5 bots.
*
* @param seed map seed to use for generating the map
* @param landMass number of tiles to generate
* @param density map density to use for generation
* @param seed map seed to use for generating the map
* @param landMass number of tiles to generate
* @param density map density to use for generation
* @param userColor color user selects for their kingdom
*/
public MapParameters(Long seed, int landMass, float density) {
public MapParameters(Long seed, int landMass, float density, Color userColor) {
this.humanPlayerNo = 1;
this.botPlayerNo = 5;
this.seed = seed;
this.landMass = landMass;
this.density = density;
this.userColor = userColor;
}

public int getHumanPlayerNo() {
Expand All @@ -62,6 +69,10 @@ public float getDensity() {
return density;
}

public Color getUserColor() {
return userColor;
}

@Override
public String toString() {
return String.format("MapParameters [humanPlayerNo=%s, botPlayerNo=%s, seed=%s, landMass=%s, density=%s]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

package de.sesu8642.feudaltactics.ingame;

import com.badlogic.gdx.graphics.Color;

import de.sesu8642.feudaltactics.lib.ingame.GameController;
import de.sesu8642.feudaltactics.lib.ingame.botai.Intelligence;

/** Value object: preferences for a new game. */
Expand All @@ -10,18 +13,21 @@ public class NewGamePreferences {
private Intelligence botIntelligence;
private MapSizes mapSize;
private Densities density;
private UserColors userColor;

/**
* Constructor.
*
* @param botIntelligence intelligence of the bot players for the game
* @param mapSize size of the map for this game
* @param density density of the map for this game
* @param userColor color user selects for their kingdom
*/
public NewGamePreferences(Intelligence botIntelligence, MapSizes mapSize, Densities density) {
public NewGamePreferences(Intelligence botIntelligence, MapSizes mapSize, Densities density, UserColors userColor) {
this.botIntelligence = botIntelligence;
this.mapSize = mapSize;
this.density = density;
this.userColor = userColor;
}

public Intelligence getBotIntelligence() {
Expand All @@ -48,6 +54,14 @@ public void setDensity(Densities density) {
this.density = density;
}

public UserColors getUserColor() {
return userColor;
}

public void setUserColor(UserColors userColor) {
this.userColor = userColor;
}

/** Map sizes that can be generated. */
public enum MapSizes {
SMALL(50), MEDIUM(150), LARGE(250), XLARGE(500), XXLARGE(1000);
Expand Down Expand Up @@ -79,4 +93,20 @@ public float getDensityFloat() {
}
}

/** User colors that can be generated. */
public enum UserColors {
BLUE(GameController.BLUE), ORANGE(GameController.ORANGE), GREEN(GameController.GREEN),
YELLOW(GameController.YELLOW), PINK(GameController.PINK), WHITE(GameController.WHITE);

private Color kingdomColor;

private UserColors(Color kingdomColor) {
this.kingdomColor = kingdomColor;
}

public Color getKingdomColor() {
return this.kingdomColor;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import de.sesu8642.feudaltactics.ingame.NewGamePreferences.Densities;
import de.sesu8642.feudaltactics.ingame.NewGamePreferences.MapSizes;
import de.sesu8642.feudaltactics.ingame.NewGamePreferences.UserColors;
import de.sesu8642.feudaltactics.ingame.dagger.NewGamePrefsPrefStore;
import de.sesu8642.feudaltactics.lib.ingame.botai.Intelligence;

Expand All @@ -21,6 +22,7 @@ public class NewGamePreferencesDao {
private static final String NEW_GAME_PREFERENCES_DENSITY_NAME = "density";
private static final String NEW_GAME_PREFERENCES_MAP_SIZE_NAME = "mapSize";
private static final String NEW_GAME_PREFERENCES_BOT_INTELLIGENCE_NAME = "botIntelligence";
private static final String NEW_GAME_PREFERENCES_USER_COLOR_NAME = "userColor";

private final Preferences prefStore;

Expand All @@ -38,6 +40,7 @@ public void saveNewGamePreferences(NewGamePreferences prefs) {
prefStore.putInteger(NEW_GAME_PREFERENCES_BOT_INTELLIGENCE_NAME, prefs.getBotIntelligence().ordinal());
prefStore.putInteger(NEW_GAME_PREFERENCES_MAP_SIZE_NAME, prefs.getMapSize().ordinal());
prefStore.putInteger(NEW_GAME_PREFERENCES_DENSITY_NAME, prefs.getDensity().ordinal());
prefStore.putInteger(NEW_GAME_PREFERENCES_USER_COLOR_NAME, prefs.getUserColor().ordinal());
prefStore.flush();
}

Expand All @@ -51,7 +54,8 @@ public NewGamePreferences getNewGamePreferences() {
.getInteger(NEW_GAME_PREFERENCES_BOT_INTELLIGENCE_NAME, 0)];
MapSizes mapSize = MapSizes.values()[prefStore.getInteger(NEW_GAME_PREFERENCES_MAP_SIZE_NAME, 0)];
Densities density = Densities.values()[prefStore.getInteger(NEW_GAME_PREFERENCES_DENSITY_NAME, 0)];
return new NewGamePreferences(botIntelligence, mapSize, density);
UserColors userColor = UserColors.values()[prefStore.getInteger(NEW_GAME_PREFERENCES_USER_COLOR_NAME, 0)];
return new NewGamePreferences(botIntelligence, mapSize, density, userColor);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ private void resetGame() {
eventBus.post(new RegenerateMapEvent(parameterInputStage.getBotIntelligence(),
new MapParameters(parameterInputStage.getSeedParam(),
parameterInputStage.getMapSizeParam().getAmountOfTiles(),
parameterInputStage.getMapDensityParam().getDensityFloat())));
parameterInputStage.getMapDensityParam().getDensityFloat(),
parameterInputStage.getUserColor().getKingdomColor())));
centerMap();
activateStage(IngameStages.PARAMETERS);
}
Expand Down Expand Up @@ -486,16 +487,18 @@ private void addParameterInputListeners() {
() -> parameterInputStage.seedTextField.setText(String.valueOf(System.currentTimeMillis()))));

Stream.of(parameterInputStage.seedTextField, parameterInputStage.randomButton, parameterInputStage.sizeSelect,
parameterInputStage.densitySelect)
parameterInputStage.densitySelect, parameterInputStage.colorSelect)
.forEach(actor -> actor.addListener(new ExceptionLoggingChangeListener(() -> {
eventBus.post(new RegenerateMapEvent(parameterInputStage.getBotIntelligence(),
new MapParameters(parameterInputStage.getSeedParam(),
parameterInputStage.getMapSizeParam().getAmountOfTiles(),
parameterInputStage.getMapDensityParam().getDensityFloat())));
parameterInputStage.getMapDensityParam().getDensityFloat(),
parameterInputStage.getUserColor().getKingdomColor())));
centerMap();
newGamePrefDao
.saveNewGamePreferences(new NewGamePreferences(parameterInputStage.getBotIntelligence(),
parameterInputStage.getMapSizeParam(), parameterInputStage.getMapDensityParam()));
parameterInputStage.getMapSizeParam(), parameterInputStage.getMapDensityParam(),
parameterInputStage.getUserColor()));
})));

parameterInputStage.playButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import de.sesu8642.feudaltactics.ingame.NewGamePreferences.Densities;
import de.sesu8642.feudaltactics.ingame.NewGamePreferences.MapSizes;
import de.sesu8642.feudaltactics.ingame.NewGamePreferences.UserColors;
import de.sesu8642.feudaltactics.lib.ingame.botai.Intelligence;
import de.sesu8642.feudaltactics.menu.common.dagger.MenuViewport;
import de.sesu8642.feudaltactics.menu.common.ui.ResizableResettableStage;
Expand Down Expand Up @@ -53,6 +54,7 @@ public class ParameterInputStage extends ResizableResettableStage {
private Skin skin;

private Table rootTable;
SelectBox<String> colorSelect;
SelectBox<String> sizeSelect;
SelectBox<String> densitySelect;
SelectBox<String> difficultySelect;
Expand All @@ -77,20 +79,28 @@ public ParameterInputStage(@MenuViewport Viewport viewport, TextureAtlas texture

private void initUi() {
// note about checktyle: widgets are declared in the order they appear in the UI
Label colorLabel = new Label("Kingdom\nColor", skin);
colorSelect = new SelectBox<>(skin);
String[] kingdomColors = { "Blue", "Orange", "Green", "Yellow", "Pink", "White" };
colorSelect.setItems(kingdomColors);

Label difficultyLabel = new Label("CPU\nDifficulty", skin);
difficultySelect = new SelectBox<>(skin);
String[] difficulties = { "Easy", "Medium", "Hard", "Very hard" };
difficultySelect.setItems(difficulties);

Label sizeLabel = new Label("Map\nSize", skin);
sizeSelect = new SelectBox<>(skin);
// xxlarge is temporarily disabled because of performance problems
String[] sizes = { "Small", "Medium ", "Large", "XLarge", /* "XXLarge" */
};
sizeSelect.setItems(sizes);

Label densityLabel = new Label("Map\nDensity", skin);
densitySelect = new SelectBox<>(skin);
String[] densities = { "Dense", "Medium ", "Loose" };
densitySelect.setItems(densities);

Label seedLabel = new Label("Seed", skin);
seedTextField = new TextField(String.valueOf(System.currentTimeMillis()), skin);
seedTextField.setTextFieldFilter(new DigitsOnlyFilter());
Expand All @@ -99,6 +109,7 @@ private void initUi() {
randomButton.getStyle().imageUp = new SpriteDrawable(textureAtlas.createSprite("die"));
randomButton.getStyle().imageDown = new SpriteDrawable(textureAtlas.createSprite("die_pressed"));
randomButton.getImageCell().expand().fill();

playButton = new TextButton("Play", skin);

/*
Expand All @@ -115,9 +126,12 @@ private void initUi() {
rootTable.columnDefaults(0).pad(0, OUTER_PADDING_PX, 0, OUTER_PADDING_PX);
rootTable.add().expandY();
rootTable.row();
rootTable.add(colorLabel);
rootTable.add(colorSelect).colspan(2).fillX();
rootTable.add().expandX();
rootTable.row();
rootTable.add(difficultyLabel);
rootTable.add(difficultySelect).colspan(2).fillX();
rootTable.add().expandX();
rootTable.row();
rootTable.add(sizeLabel);
rootTable.add(sizeSelect).colspan(2).fillX();
Expand Down Expand Up @@ -169,6 +183,10 @@ public Intelligence getBotIntelligence() {
return Intelligence.values()[difficultySelect.getSelectedIndex()];
}

public UserColors getUserColor() {
return UserColors.values()[colorSelect.getSelectedIndex()];
}

@Override
public void updateOnResize(int width, int height) {
// VERY IMPORTANT!!! makes everything scale correctly on startup and going
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Queue;
import com.google.common.eventbus.EventBus;

import de.sesu8642.feudaltactics.events.GameStateChangeEvent;
Expand All @@ -30,9 +31,14 @@ public class GameController {

private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());

public static final Color[] PLAYER_COLORS = { new Color(0.2F, 0.45F, 0.8F, 1), new Color(0.75F, 0.5F, 0F, 1),
new Color(1F, 0.67F, 0.67F, 1), new Color(1F, 1F, 0F, 1), new Color(1F, 1F, 1F, 1),
new Color(0F, 1F, 0F, 1) };
/** All colors available to bots and user in the game. */
public static final Color BLUE = new Color(0.2F, 0.45F, 0.8F, 1);
public static final Color ORANGE = new Color(0.75F, 0.5F, 0F, 1);
public static final Color PINK = new Color(1F, 0.67F, 0.67F, 1);
public static final Color YELLOW = new Color(1F, 1F, 0F, 1);
public static final Color WHITE = new Color(1F, 1F, 1F, 1);
public static final Color GREEN = new Color(0F, 1F, 0F, 1);
public static final Color[] COLOR_BANK = { BLUE, ORANGE, PINK, YELLOW, WHITE, GREEN };

private final EventBus eventBus;
private final ExecutorService botTurnExecutor;
Expand Down Expand Up @@ -102,7 +108,10 @@ public void generateGameState(Intelligence botIntelligence, MapParameters mapPar
ArrayList<Player> players = new ArrayList<>();
int remainingHumanPlayers = mapParams.getHumanPlayerNo();
int remainingBotPlayers = mapParams.getBotPlayerNo();
for (Color color : PLAYER_COLORS) {

Color[] playerColors = determinePlayerColors(mapParams.getUserColor());

for (Color color : playerColors) {
if (remainingHumanPlayers > 0) {
remainingHumanPlayers--;
players.add(new Player(color, Type.LOCAL_PLAYER));
Expand All @@ -117,6 +126,31 @@ public void generateGameState(Intelligence botIntelligence, MapParameters mapPar
eventBus.post(new GameStateChangeEvent(gameState));
}

/**
* Set the color order depending on the user color choice.
*
* @param userColor The color the user has chosen for their kingdom.
* @return Array with the user color in the zero index.
*/
private Color[] determinePlayerColors(Color userColor) {
Color[] colors = new Color[COLOR_BANK.length];
Queue<Color> colorQueue = new Queue<>();

for (Color color : COLOR_BANK) {
if (color.equals(userColor)) {
colorQueue.addFirst(color);
} else {
colorQueue.addLast(color);
}
}

for (int i = 0; i < colorQueue.size; i++) {
colors[i] = colorQueue.get(i);
}

return colors;
}

/**
* Prints debug info about a tile.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void initUi(MenuStage stage) {
eventBus.post(new ScreenTransitionTriggerEvent(ScreenTransitionTarget.INGAME_SCREEN));
eventBus.post(new RegenerateMapEvent(savedPrefs.getBotIntelligence(),
new MapParameters(System.currentTimeMillis(), savedPrefs.getMapSize().getAmountOfTiles(),
savedPrefs.getDensity().getDensityFloat())));
savedPrefs.getDensity().getDensityFloat(), savedPrefs.getUserColor().getKingdomColor())));
}));
// tutorial button
buttons.get(1).addListener(new ExceptionLoggingChangeListener(
Expand Down

0 comments on commit 288358e

Please sign in to comment.