From 6769c30ebe0a05591c3bd203f3a879928d4b0ab9 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Mon, 26 Aug 2019 15:49:59 +0300 Subject: [PATCH] Added isOnlineMultiplayer toggle in newGameScreen and an input for the player ID --- core/src/com/unciv/UnCivGame.kt | 3 + core/src/com/unciv/logic/GameInfo.kt | 4 +- core/src/com/unciv/logic/GameSaver.kt | 2 +- core/src/com/unciv/{ => logic}/GameStarter.kt | 39 +-------- core/src/com/unciv/logic/map/TileMap.kt | 2 +- .../unciv/models/metadata/GameParameters.kt | 22 +++++ .../{ => models/metadata}/GameSettings.kt | 2 +- .../com/unciv/models/metadata/GameSpeed.kt | 15 ++++ core/src/com/unciv/models/metadata/Player.kt | 10 +++ .../com/unciv/ui/mapeditor/MapEditorScreen.kt | 2 +- .../unciv/ui/newgamescreen/NewGameScreen.kt | 18 ++-- .../NewGameScreenOptionsTable.kt | 20 ++++- .../ui/newgamescreen/PlayerPickerTable.kt | 85 ++++++++++++++----- 13 files changed, 147 insertions(+), 77 deletions(-) rename core/src/com/unciv/{ => logic}/GameStarter.kt (87%) create mode 100644 core/src/com/unciv/models/metadata/GameParameters.kt rename core/src/com/unciv/{ => models/metadata}/GameSettings.kt (95%) create mode 100644 core/src/com/unciv/models/metadata/GameSpeed.kt create mode 100644 core/src/com/unciv/models/metadata/Player.kt diff --git a/core/src/com/unciv/UnCivGame.kt b/core/src/com/unciv/UnCivGame.kt index d858945eba..07fe5faf57 100644 --- a/core/src/com/unciv/UnCivGame.kt +++ b/core/src/com/unciv/UnCivGame.kt @@ -5,6 +5,9 @@ import com.badlogic.gdx.Gdx import com.badlogic.gdx.Input import com.unciv.logic.GameInfo import com.unciv.logic.GameSaver +import com.unciv.logic.GameStarter +import com.unciv.models.metadata.GameParameters +import com.unciv.models.metadata.GameSettings import com.unciv.models.gamebasics.GameBasics import com.unciv.ui.LanguagePickerScreen import com.unciv.ui.utils.ImageGetter diff --git a/core/src/com/unciv/logic/GameInfo.kt b/core/src/com/unciv/logic/GameInfo.kt index bd8975d6c2..de19446842 100644 --- a/core/src/com/unciv/logic/GameInfo.kt +++ b/core/src/com/unciv/logic/GameInfo.kt @@ -2,7 +2,7 @@ package com.unciv.logic import com.badlogic.gdx.graphics.Color import com.unciv.Constants -import com.unciv.GameParameters +import com.unciv.models.metadata.GameParameters import com.unciv.logic.automation.NextTurnAutomation import com.unciv.logic.city.CityConstructions import com.unciv.logic.civilization.CivilizationInfo @@ -21,7 +21,7 @@ class GameInfo { var civilizations = mutableListOf() var difficulty="Chieftain" // difficulty is game-wide, think what would happen if 2 human players could play on different difficulties? var tileMap: TileMap = TileMap() - var gameParameters=GameParameters() + var gameParameters= GameParameters() var turns = 0 var oneMoreTurnMode=false var currentPlayer="" diff --git a/core/src/com/unciv/logic/GameSaver.kt b/core/src/com/unciv/logic/GameSaver.kt index 00fe3b1665..db7a07cd0c 100644 --- a/core/src/com/unciv/logic/GameSaver.kt +++ b/core/src/com/unciv/logic/GameSaver.kt @@ -3,7 +3,7 @@ package com.unciv.logic import com.badlogic.gdx.Gdx import com.badlogic.gdx.files.FileHandle import com.badlogic.gdx.utils.Json -import com.unciv.GameSettings +import com.unciv.models.metadata.GameSettings import com.unciv.ui.utils.ImageGetter import java.io.File diff --git a/core/src/com/unciv/GameStarter.kt b/core/src/com/unciv/logic/GameStarter.kt similarity index 87% rename from core/src/com/unciv/GameStarter.kt rename to core/src/com/unciv/logic/GameStarter.kt index ba59ba3ced..3531b29117 100644 --- a/core/src/com/unciv/GameStarter.kt +++ b/core/src/com/unciv/logic/GameStarter.kt @@ -1,49 +1,16 @@ -package com.unciv +package com.unciv.logic import com.badlogic.gdx.math.Vector2 -import com.unciv.logic.GameInfo -import com.unciv.logic.HexMath +import com.unciv.Constants import com.unciv.logic.civilization.CivilizationInfo -import com.unciv.logic.civilization.PlayerType import com.unciv.logic.map.BFS -import com.unciv.logic.map.MapType import com.unciv.logic.map.TileInfo import com.unciv.logic.map.TileMap +import com.unciv.models.metadata.GameParameters import com.unciv.models.gamebasics.GameBasics -import com.unciv.models.gamebasics.VictoryType -import com.unciv.ui.newgamescreen.Player import java.util.* import kotlin.collections.ArrayList -enum class GameSpeed{ - Quick, - Standard, - Epic; - - fun getModifier(): Float { - when(this) { - Quick -> return 0.67f - Standard -> return 1f - Epic -> return 1.5f - } - } -} - -class GameParameters { // Default values are the default new game - var difficulty = "Prince" - var gameSpeed = GameSpeed.Standard - var mapRadius = 20 - var players = ArrayList().apply { - add(Player().apply { playerType = PlayerType.Human }) - for (i in 1..3) add(Player()) - } - var numberOfCityStates = 0 - var mapType = MapType.Perlin - var noBarbarians = false - var mapFileName: String? = null - var victoryTypes: ArrayList = VictoryType.values().toCollection(ArrayList()) // By default, all victory types -} - class GameStarter{ fun startNewGame(newGameParameters: GameParameters): GameInfo { val gameInfo = GameInfo() diff --git a/core/src/com/unciv/logic/map/TileMap.kt b/core/src/com/unciv/logic/map/TileMap.kt index c1b707cb4b..b760b06e03 100644 --- a/core/src/com/unciv/logic/map/TileMap.kt +++ b/core/src/com/unciv/logic/map/TileMap.kt @@ -1,7 +1,7 @@ package com.unciv.logic.map import com.badlogic.gdx.math.Vector2 -import com.unciv.GameParameters +import com.unciv.models.metadata.GameParameters import com.unciv.logic.GameInfo import com.unciv.logic.HexMath import com.unciv.logic.MapSaver diff --git a/core/src/com/unciv/models/metadata/GameParameters.kt b/core/src/com/unciv/models/metadata/GameParameters.kt new file mode 100644 index 0000000000..35231aaf63 --- /dev/null +++ b/core/src/com/unciv/models/metadata/GameParameters.kt @@ -0,0 +1,22 @@ +package com.unciv.models.metadata + +import com.unciv.logic.civilization.PlayerType +import com.unciv.logic.map.MapType +import com.unciv.models.gamebasics.VictoryType + +class GameParameters { // Default values are the default new game + var difficulty = "Prince" + var gameSpeed = GameSpeed.Standard + var mapRadius = 20 + var players = ArrayList().apply { + add(Player().apply { playerType = PlayerType.Human }) + for (i in 1..3) add(Player()) + } + var numberOfCityStates = 0 + var mapType = MapType.Perlin + var noBarbarians = false + var mapFileName: String? = null + var victoryTypes: ArrayList = VictoryType.values().toCollection(ArrayList()) // By default, all victory types + + var isOnlineMultiplayer = false +} \ No newline at end of file diff --git a/core/src/com/unciv/GameSettings.kt b/core/src/com/unciv/models/metadata/GameSettings.kt similarity index 95% rename from core/src/com/unciv/GameSettings.kt rename to core/src/com/unciv/models/metadata/GameSettings.kt index 5acadb54df..213838b711 100644 --- a/core/src/com/unciv/GameSettings.kt +++ b/core/src/com/unciv/models/metadata/GameSettings.kt @@ -1,4 +1,4 @@ -package com.unciv +package com.unciv.models.metadata import com.unciv.logic.GameSaver import java.util.* diff --git a/core/src/com/unciv/models/metadata/GameSpeed.kt b/core/src/com/unciv/models/metadata/GameSpeed.kt new file mode 100644 index 0000000000..c74ace1db8 --- /dev/null +++ b/core/src/com/unciv/models/metadata/GameSpeed.kt @@ -0,0 +1,15 @@ +package com.unciv.models.metadata + +enum class GameSpeed{ + Quick, + Standard, + Epic; + + fun getModifier(): Float { + when(this) { + Quick -> return 0.67f + Standard -> return 1f + Epic -> return 1.5f + } + } +} \ No newline at end of file diff --git a/core/src/com/unciv/models/metadata/Player.kt b/core/src/com/unciv/models/metadata/Player.kt new file mode 100644 index 0000000000..bffeb41356 --- /dev/null +++ b/core/src/com/unciv/models/metadata/Player.kt @@ -0,0 +1,10 @@ +package com.unciv.models.metadata + +import com.unciv.Constants +import com.unciv.logic.civilization.PlayerType + +class Player { + var playerType: PlayerType = PlayerType.AI + var chosenCiv = Constants.random + var playerId="" +} \ No newline at end of file diff --git a/core/src/com/unciv/ui/mapeditor/MapEditorScreen.kt b/core/src/com/unciv/ui/mapeditor/MapEditorScreen.kt index b9f5ffb0b3..d8cff12d9c 100644 --- a/core/src/com/unciv/ui/mapeditor/MapEditorScreen.kt +++ b/core/src/com/unciv/ui/mapeditor/MapEditorScreen.kt @@ -2,7 +2,7 @@ package com.unciv.ui.mapeditor import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane import com.badlogic.gdx.scenes.scene2d.ui.TextButton -import com.unciv.GameParameters +import com.unciv.models.metadata.GameParameters import com.unciv.logic.MapSaver import com.unciv.logic.map.TileMap import com.unciv.models.gamebasics.tr diff --git a/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt b/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt index 2071817c41..9a2c09382a 100644 --- a/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt +++ b/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt @@ -4,9 +4,9 @@ import com.badlogic.gdx.Gdx import com.badlogic.gdx.scenes.scene2d.ui.SelectBox import com.badlogic.gdx.scenes.scene2d.ui.Skin import com.badlogic.gdx.utils.Array -import com.unciv.GameStarter import com.unciv.UnCivGame import com.unciv.logic.GameInfo +import com.unciv.logic.GameStarter import com.unciv.logic.civilization.PlayerType import com.unciv.models.gamebasics.tr import com.unciv.ui.pickerscreens.PickerScreen @@ -21,12 +21,11 @@ class NewGameScreen: PickerScreen(){ val newGameParameters= UnCivGame.Current.gameInfo.gameParameters - var playerPickerTable = PlayerPickerTable(this,newGameParameters) - init { setDefaultCloseAction() - topTable.add(NewGameScreenOptionsTable(newGameParameters)) + val playerPickerTable = PlayerPickerTable(this, newGameParameters) + topTable.add(NewGameScreenOptionsTable(newGameParameters) { playerPickerTable.update() }) topTable.add(playerPickerTable).pad(10f) topTable.pack() topTable.setFillParent(true) @@ -34,11 +33,10 @@ class NewGameScreen: PickerScreen(){ rightSideButton.enable() rightSideButton.setText("Start game!".tr()) rightSideButton.onClick { - if(newGameParameters.players.none { it.playerType==PlayerType.Human }) - { + if (newGameParameters.players.none { it.playerType == PlayerType.Human }) { val popup = PopupTable(this) popup.addGoodSizedLabel("No human players selected!").row() - popup.addButton("Close"){popup.remove()} + popup.addButton("Close") { popup.remove() } popup.open() return@onClick } @@ -47,11 +45,11 @@ class NewGameScreen: PickerScreen(){ rightSideButton.disable() rightSideButton.setText("Working...".tr()) - thread { // Creating a new game can take a while and we don't want ANRs + thread { + // Creating a new game can take a while and we don't want ANRs try { newGame = GameStarter().startNewGame(newGameParameters) - } - catch (exception:Exception){ + } catch (exception: Exception) { val popup = PopupTable(this) popup.addGoodSizedLabel("It looks like we can't make a map with the parameters you requested!".tr()).row() popup.addGoodSizedLabel("Maybe you put too many players into too small a map?".tr()).row() diff --git a/core/src/com/unciv/ui/newgamescreen/NewGameScreenOptionsTable.kt b/core/src/com/unciv/ui/newgamescreen/NewGameScreenOptionsTable.kt index c982721b08..484e23cb6e 100644 --- a/core/src/com/unciv/ui/newgamescreen/NewGameScreenOptionsTable.kt +++ b/core/src/com/unciv/ui/newgamescreen/NewGameScreenOptionsTable.kt @@ -6,17 +6,18 @@ import com.badlogic.gdx.scenes.scene2d.ui.SelectBox import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener import com.badlogic.gdx.utils.Array -import com.unciv.GameParameters -import com.unciv.GameSpeed import com.unciv.logic.MapSaver import com.unciv.logic.map.MapType import com.unciv.models.gamebasics.GameBasics import com.unciv.models.gamebasics.VictoryType import com.unciv.models.gamebasics.tr +import com.unciv.models.metadata.GameParameters +import com.unciv.models.metadata.GameSpeed import com.unciv.ui.utils.CameraStageBaseScreen import com.unciv.ui.utils.toLabel -class NewGameScreenOptionsTable(val newGameParameters: GameParameters): Table(CameraStageBaseScreen.skin){ +class NewGameScreenOptionsTable(val newGameParameters: GameParameters, val onMultiplayerToggled:()->Unit) + : Table(CameraStageBaseScreen.skin){ init{ addMapTypeSizeAndFile() addDifficultySelectBox() @@ -24,6 +25,7 @@ class NewGameScreenOptionsTable(val newGameParameters: GameParameters): Table(Ca addCityStatesSelectBox() addVictoryTypeCheckboxes() addBarbariansCheckbox() + //addIsOnlineMultiplayerCheckbox() pack() } @@ -39,6 +41,18 @@ class NewGameScreenOptionsTable(val newGameParameters: GameParameters): Table(Ca add(noBarbariansCheckbox).colspan(2).row() } + private fun addIsOnlineMultiplayerCheckbox() { + val isOnlineMultiplayerCheckbox = CheckBox("Online Multiplayer".tr(), CameraStageBaseScreen.skin) + isOnlineMultiplayerCheckbox.isChecked = newGameParameters.isOnlineMultiplayer + isOnlineMultiplayerCheckbox.addListener(object : ChangeListener() { + override fun changed(event: ChangeEvent?, actor: Actor?) { + newGameParameters.isOnlineMultiplayer = isOnlineMultiplayerCheckbox.isChecked + onMultiplayerToggled() + } + }) + add(isOnlineMultiplayerCheckbox).colspan(2).row() + } + private fun addMapTypeSizeAndFile() { add("{Map type}:".tr()) val mapTypes = LinkedHashMap() diff --git a/core/src/com/unciv/ui/newgamescreen/PlayerPickerTable.kt b/core/src/com/unciv/ui/newgamescreen/PlayerPickerTable.kt index 37579600d8..a6b055a92c 100644 --- a/core/src/com/unciv/ui/newgamescreen/PlayerPickerTable.kt +++ b/core/src/com/unciv/ui/newgamescreen/PlayerPickerTable.kt @@ -5,14 +5,17 @@ import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.TextButton +import com.badlogic.gdx.scenes.scene2d.ui.TextField import com.badlogic.gdx.utils.Align -import com.unciv.Constants -import com.unciv.GameParameters +import com.unciv.UnCivGame import com.unciv.logic.civilization.PlayerType import com.unciv.models.gamebasics.GameBasics import com.unciv.models.gamebasics.tr +import com.unciv.models.metadata.GameParameters +import com.unciv.models.metadata.Player import com.unciv.ui.utils.* import com.unciv.ui.worldscreen.optionstable.PopupTable +import java.util.* class PlayerPickerTable(val newGameScreen: NewGameScreen, val newGameParameters: GameParameters): Table() { val playerListTable = Table() @@ -35,16 +38,12 @@ class PlayerPickerTable(val newGameScreen: NewGameScreen, val newGameParameters: } fun getPlayerTable(player: Player): Table { - val table = Table() - table.pad(20f) - table.background = ImageGetter.getBackground(ImageGetter.getBlue().lerp(Color.BLACK, 0.8f)) + val playerTable = Table() + playerTable.pad(20f) + playerTable.background = ImageGetter.getBackground(ImageGetter.getBlue().lerp(Color.BLACK, 0.8f)) - val nationImage = if(player.chosenCiv=="Random") "?".toLabel() - .apply { this.setAlignment(Align.center) }.setFontSize(30) - .setFontColor(Color.BLACK).surroundWithCircle(50f) - else ImageGetter.getNationIndicator(GameBasics.Nations[player.chosenCiv]!!,50f) - table.add(nationImage) - table.add((player.chosenCiv).toLabel()).pad(20f) + val nationTable = getNationTable(player) + playerTable.add(nationTable) val playerTypeTextbutton = TextButton(player.playerType.name, CameraStageBaseScreen.skin) playerTypeTextbutton.onClick { @@ -53,11 +52,57 @@ class PlayerPickerTable(val newGameScreen: NewGameScreen, val newGameParameters: else player.playerType = PlayerType.AI update() } - table.add(playerTypeTextbutton).pad(20f) - table.add(TextButton("Remove".tr(), CameraStageBaseScreen.skin) - .onClick { newGameParameters.players.remove(player); update() }) - table.touchable=Touchable.enabled - table.onClick { + playerTable.add(playerTypeTextbutton).pad(20f) + playerTable.add(TextButton("Remove".tr(), CameraStageBaseScreen.skin) + .onClick { newGameParameters.players.remove(player); update() }).row() + if(newGameParameters.isOnlineMultiplayer && player.playerType==PlayerType.Human) { + val playerIdTable = Table() + playerIdTable.add("Player ID:".toLabel()) + + val playerIdTextfield = TextField("", CameraStageBaseScreen.skin) + playerIdTable.add(playerIdTextfield) + val errorLabel = "Not a valid user id!".toLabel().setFontColor(Color.RED) + errorLabel.isVisible=false + playerIdTable.add(errorLabel) + playerIdTextfield.addListener { + try { + val uuid = UUID.fromString(playerIdTextfield.text) + player.playerId = playerIdTextfield.text + errorLabel.isVisible=false + } catch (ex: Exception) { + errorLabel.isVisible=true + } + true + } + + playerIdTable.row() + + val currentUserId = UnCivGame.Current.settings.userId + val setCurrentUserButton = TextButton("Set current user", CameraStageBaseScreen.skin) + setCurrentUserButton.onClick { + playerIdTextfield.text = currentUserId + errorLabel.isVisible = false + } + playerIdTable.add(setCurrentUserButton) + + playerTable.add(playerIdTable).colspan(playerTable.columns) + + + } + + return playerTable + } + + private fun getNationTable(player: Player): Table { + val nationTable = Table() + val nationImage = if (player.chosenCiv == "Random") "?".toLabel() + .apply { this.setAlignment(Align.center) }.setFontSize(30) + .setFontColor(Color.BLACK).surroundWithCircle(50f) + else ImageGetter.getNationIndicator(GameBasics.Nations[player.chosenCiv]!!, 50f) + nationTable.add(nationImage) + nationTable.add(player.chosenCiv.toLabel()).pad(20f) + nationTable.touchable = Touchable.enabled + nationTable.onClick { val nationsPopup = PopupTable(newGameScreen) val nationListTable = Table() for (nation in GameBasics.Nations.values.filter { !it.isCityState() && it.name != "Barbarians" }) { @@ -70,16 +115,12 @@ class PlayerPickerTable(val newGameScreen: NewGameScreen, val newGameParameters: update() }).pad(10f).width(halfWidth).row() } - nationsPopup.add(ScrollPane(nationListTable)).height(newGameScreen.stage.height*0.8f) + nationsPopup.add(ScrollPane(nationListTable)).height(newGameScreen.stage.height * 0.8f) nationsPopup.open() update() } - return table + return nationTable } } -class Player { - var playerType: PlayerType = PlayerType.AI - var chosenCiv = Constants.random -}