Added isOnlineMultiplayer toggle in newGameScreen and an input for the player ID

This commit is contained in:
Yair Morgenstern 2019-08-26 15:49:59 +03:00
parent 8015ca1e1e
commit 6769c30ebe
13 changed files with 147 additions and 77 deletions

View File

@ -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

View File

@ -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<CivilizationInfo>()
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=""

View File

@ -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

View File

@ -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<Player>().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> = VictoryType.values().toCollection(ArrayList()) // By default, all victory types
}
class GameStarter{
fun startNewGame(newGameParameters: GameParameters): GameInfo {
val gameInfo = GameInfo()

View File

@ -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

View File

@ -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<Player>().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> = VictoryType.values().toCollection(ArrayList()) // By default, all victory types
var isOnlineMultiplayer = false
}

View File

@ -1,4 +1,4 @@
package com.unciv
package com.unciv.models.metadata
import com.unciv.logic.GameSaver
import java.util.*

View File

@ -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
}
}
}

View File

@ -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=""
}

View File

@ -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

View File

@ -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()

View File

@ -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<String, MapType>()

View File

@ -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
}