Can now choose a map name and load a map from those existing on the phone!

This commit is contained in:
Yair Morgenstern 2019-02-02 23:26:56 +02:00
parent 3a990434f3
commit 81d1a79e19
6 changed files with 106 additions and 39 deletions

View File

@ -21,8 +21,8 @@ android {
applicationId "com.unciv.app"
minSdkVersion 14
targetSdkVersion 28
versionCode 198
versionName "2.12.5"
versionCode 199
versionName "2.13.0"
}
// Had to add this crap for Travis to build, it wanted to sign the app

View File

@ -20,6 +20,7 @@ class GameParameters{
var humanNations=ArrayList<String>().apply { add("Babylon") }
var numberOfEnemies=3
var mapType= MapType.Perlin
var mapFileName :String?=null
}
class GameStarter{
@ -27,7 +28,7 @@ class GameStarter{
val gameInfo = GameInfo()
gameInfo.gameParameters = newGameParameters
gameInfo.tileMap = TileMap(newGameParameters.mapRadius, newGameParameters.mapType)
gameInfo.tileMap = TileMap(newGameParameters)
gameInfo.tileMap.gameInfo = gameInfo // need to set this transient before placing units in the map
val startingLocations = getStartingLocations(
newGameParameters.numberOfEnemies+newGameParameters.numberOfHumanPlayers, gameInfo.tileMap)

View File

@ -22,6 +22,8 @@ class GameSaver {
val unzippedJson = Gzip.unzip(gzippedString)
return json().fromJson(TileMap::class.java, unzippedJson)
}
fun getMaps() = Gdx.files.local(mapsFolder).list().map { it.name() }
fun getSave(GameName: String): FileHandle {
return Gdx.files.local("$saveFilesFolder/$GameName")

View File

@ -1,6 +1,7 @@
package com.unciv.logic.map
import com.badlogic.gdx.math.Vector2
import com.unciv.GameParameters
import com.unciv.logic.GameInfo
import com.unciv.logic.GameSaver
import com.unciv.logic.HexMath
@ -31,15 +32,15 @@ class TileMap {
get() = tileList
constructor(distance: Int, mapType: MapType) {
constructor(newGameParameters: GameParameters) {
val mapValues:Collection<TileInfo>
if(mapType == MapType.File)
mapValues = GameSaver().loadMap("Test").values
else if(mapType==MapType.Perlin)
mapValues = PerlinNoiseRandomMapGenerator().generateMap(distance).values
if(newGameParameters.mapType == MapType.File)
mapValues = GameSaver().loadMap(newGameParameters.mapFileName!!).values
else if(newGameParameters.mapType==MapType.Perlin)
mapValues = PerlinNoiseRandomMapGenerator().generateMap(newGameParameters.mapRadius).values
else
mapValues = CelluarAutomataRandomMapGenerator(mapType).generateMap(distance).values
mapValues = CelluarAutomataRandomMapGenerator(newGameParameters.mapType).generateMap(newGameParameters.mapRadius).values
tileList.addAll(mapValues)
// tileList.addAll(AlexanderRandomMapGenerator().generateMap(distance,0.8f).values)

View File

@ -2,30 +2,31 @@ package com.unciv.ui
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.ui.Image
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.*
import com.unciv.GameParameters
import com.unciv.UnCivGame
import com.unciv.logic.GameSaver
import com.unciv.logic.map.MapType
import com.unciv.logic.map.TileMap
import com.unciv.models.gamebasics.GameBasics
import com.unciv.models.gamebasics.tile.Terrain
import com.unciv.models.gamebasics.tile.TerrainType
import com.unciv.models.gamebasics.tile.TileResource
import com.unciv.models.gamebasics.tr
import com.unciv.ui.tilegroups.TileGroup
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.center
import com.unciv.ui.utils.onClick
import com.unciv.ui.worldscreen.TileGroupMap
import com.unciv.ui.worldscreen.optionstable.PopupTable
class MapEditorScreen: CameraStageBaseScreen(){
var clearTerrainFeature=false
var selectedTerrain : Terrain?=null
var clearResource=false
var selectedResource:TileResource?=null
var tileMap = TileMap(GameParameters())
var mapName = "My first map"
fun clearSelection(){
clearTerrainFeature=false
@ -48,7 +49,6 @@ class MapEditorScreen: CameraStageBaseScreen(){
}
init{
val tileMap = TileMap(20, MapType.Default)
val scrollPane = getMapHolder(tileMap)
stage.addActor(scrollPane)
@ -56,10 +56,10 @@ class MapEditorScreen: CameraStageBaseScreen(){
val scrollTable = getTileEditorOptions()
stage.addActor(scrollTable)
val saveMapButton = TextButton("Save map",skin)
val saveMapButton = TextButton("Options".tr(),skin)
saveMapButton.onClick {
GameSaver().saveMap("Test",tileMap)
UnCivGame.Current.setWorldScreen()
mapScreenOptionsTable(this)
}
stage.addActor(saveMapButton)
}
@ -140,4 +140,25 @@ class MapEditorScreen: CameraStageBaseScreen(){
scrollTable.setPosition(0f, stage.height - scrollTable.height)
return scrollTable
}
}
class mapScreenOptionsTable(mapEditorScreen: MapEditorScreen):PopupTable(mapEditorScreen){
init{
val mapNameEditor = TextField(mapEditorScreen.mapName, CameraStageBaseScreen.skin)
mapNameEditor.addListener{ mapEditorScreen.mapName=mapNameEditor.text; true }
add(mapNameEditor).row()
val saveMapButton = TextButton("Save".tr(), CameraStageBaseScreen.skin)
saveMapButton.onClick {
GameSaver().saveMap(mapEditorScreen.mapName,mapEditorScreen.tileMap)
UnCivGame.Current.setWorldScreen()
}
add(saveMapButton).row()
val closeOptionsButtton = TextButton("Close".tr(), CameraStageBaseScreen.skin)
closeOptionsButtton.onClick { remove() }
add(closeOptionsButtton).row()
open()
}
}

View File

@ -2,10 +2,7 @@ package com.unciv.ui
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.*
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener
import com.badlogic.gdx.utils.Array
import com.unciv.GameStarter
@ -75,35 +72,46 @@ class NewGameScreen: PickerScreen(){
newGameOptionsTable.add("{Map type}:".tr())
val mapTypes = LinkedHashMap<String, MapType>()
for (type in MapType.values()) {
if(type==MapType.File && !GameSaver().getMap("Test").exists()) continue
if(type==MapType.File && GameSaver().getMaps().isEmpty()) continue
mapTypes[type.toString()] = type
}
val mapFileLabel = Label("{Map file}:".tr(),skin)
val mapFileSelectBox = getMapFileSelectBox()
mapFileLabel.isVisible=false
mapFileSelectBox.isVisible=false
val mapTypeSelectBox = TranslatedSelectBox(mapTypes.keys, newGameParameters.mapType.toString(), skin)
val worldSizeSelectBox = getWorldSizeSelectBox()
val worldSizeLabel = Label("{World size}:".tr(),skin)
mapTypeSelectBox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
newGameParameters.mapType = mapTypes[mapTypeSelectBox.selected.value]!!
if (newGameParameters.mapType == MapType.File) {
worldSizeSelectBox.isVisible = false
worldSizeLabel.isVisible = false
mapFileSelectBox.isVisible = true
mapFileLabel.isVisible=true
newGameParameters.mapFileName = mapFileSelectBox.selected
} else {
worldSizeSelectBox.isVisible = true
worldSizeLabel.isVisible = true
mapFileSelectBox.isVisible = false
mapFileLabel.isVisible=false
newGameParameters.mapFileName = null
}
}
})
newGameOptionsTable.add(mapTypeSelectBox).pad(10f).row()
newGameOptionsTable.add("{World size}:".tr())
val worldSizeToRadius = LinkedHashMap<String, Int>()
worldSizeToRadius["Tiny"] = 10
worldSizeToRadius["Small"] = 15
worldSizeToRadius["Medium"] = 20
worldSizeToRadius["Large"] = 30
worldSizeToRadius["Huge"] = 40
val currentWorldSizeName = worldSizeToRadius.entries.first { it.value==newGameParameters.mapRadius }.key
val worldSizeSelectBox = TranslatedSelectBox(worldSizeToRadius.keys, currentWorldSizeName, skin)
worldSizeSelectBox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
newGameParameters.mapRadius = worldSizeToRadius[worldSizeSelectBox.selected.value]!!
}
})
newGameOptionsTable.add(worldSizeLabel)
newGameOptionsTable.add(worldSizeSelectBox).pad(10f).row()
newGameOptionsTable.add(mapFileLabel)
newGameOptionsTable.add(mapFileSelectBox).pad(10f).row()
newGameOptionsTable.add("{Number of human players}:".tr())
@ -168,6 +176,39 @@ class NewGameScreen: PickerScreen(){
return newGameOptionsTable
}
private fun getMapFileSelectBox(): SelectBox<String> {
val mapFileSelectBox = SelectBox<String>(skin)
val mapNames = Array<String>()
for (mapName in GameSaver().getMaps()) mapNames.add(mapName)
mapFileSelectBox.items = mapNames
mapFileSelectBox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
newGameParameters.mapFileName = mapFileSelectBox.selected!!
}
})
return mapFileSelectBox
}
private fun getWorldSizeSelectBox(): TranslatedSelectBox {
val worldSizeToRadius = LinkedHashMap<String, Int>()
worldSizeToRadius["Tiny"] = 10
worldSizeToRadius["Small"] = 15
worldSizeToRadius["Medium"] = 20
worldSizeToRadius["Large"] = 30
worldSizeToRadius["Huge"] = 40
val currentWorldSizeName = worldSizeToRadius.entries.first { it.value == newGameParameters.mapRadius }.key
val worldSizeSelectBox = TranslatedSelectBox(worldSizeToRadius.keys, currentWorldSizeName, skin)
worldSizeSelectBox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
newGameParameters.mapRadius = worldSizeToRadius[worldSizeSelectBox.selected.value]!!
}
})
return worldSizeSelectBox
}
var newGame:GameInfo?=null
override fun render(delta: Float) {
@ -190,7 +231,8 @@ class TranslatedSelectBox(values : Collection<String>, default:String, skin: Ski
val array = Array<TranslatedString>()
values.forEach{array.add(TranslatedString(it))}
items = array
selected = array.first { it.value==default }
val defaultItem = array.firstOrNull { it.value==default }
selected = if(defaultItem!=null) defaultItem else array.first()
}
}