Unciv/core/src/com/unciv/UnCivGame.kt

145 lines
5.4 KiB
Kotlin
Raw Normal View History

2018-04-11 20:46:00 +00:00
package com.unciv
2018-03-02 13:34:24 +00:00
import com.badlogic.gdx.Game
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.utils.Json
import com.unciv.logic.GameInfo
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.map.TileMap
import com.unciv.models.gamebasics.*
import com.unciv.models.gamebasics.Unit
import com.unciv.models.stats.INamed
2018-04-11 20:46:00 +00:00
import com.unciv.ui.GameSettings
2018-03-02 13:34:24 +00:00
import com.unciv.ui.utils.GameSaver
import com.unciv.ui.utils.getRandom
2018-03-02 13:34:24 +00:00
import com.unciv.ui.worldscreen.WorldScreen
class UnCivGame : Game() {
var gameInfo: GameInfo = GameInfo()
var settings = GameSettings()
var worldScreen: WorldScreen? = null
2018-03-02 13:34:24 +00:00
override fun create() {
setupGameBasics()
Current = this
2018-04-11 20:46:00 +00:00
if (GameSaver.getSave("Autosave").exists()) {
2018-03-02 13:34:24 +00:00
try {
loadGame("Autosave")
2018-03-02 13:34:24 +00:00
} catch (ex: Exception) { // silent fail if we can't read the autosave
startNewGame()
}
}
else startNewGame()
}
2018-03-02 13:34:24 +00:00
fun loadGame(gameName:String){
gameInfo = GameSaver.loadGame( gameName)
2018-03-02 13:34:24 +00:00
worldScreen = WorldScreen()
setWorldScreen()
}
fun startNewGame(saveTutorialState:Boolean = false) {
if(saveTutorialState) {
val tutorials = gameInfo.tutorial
gameInfo = GameInfo()
gameInfo.tutorial = tutorials
}
else gameInfo = GameInfo()
2018-03-02 13:34:24 +00:00
gameInfo.tileMap = TileMap(20)
gameInfo.civilizations.add(CivilizationInfo("Babylon", Vector2.Zero, gameInfo)) // first one is player civ
val freeTiles = gameInfo.tileMap.values.toMutableList()
freeTiles.removeAll(gameInfo.tileMap.getTilesInDistance(Vector2.Zero,6))
val barbarianCivilization = CivilizationInfo()
gameInfo.civilizations.add(barbarianCivilization)// second is barbarian civ
for (civname in listOf("Greece","China","Egypt")) {
val startingLocation = freeTiles.toList().getRandom().position
gameInfo.civilizations.add(CivilizationInfo(civname, startingLocation, gameInfo)) // all the rest whatever
freeTiles.removeAll(gameInfo.tileMap.getTilesInDistance(startingLocation, 6))
}
barbarianCivilization.civName = "Barbarians"
2018-04-20 08:33:56 +00:00
gameInfo.setTransients() // needs to be before placeBarbarianUnit because it depends on the tilemap having its gameinfo set
(1..5).forEach { gameInfo.placeBarbarianUnit(freeTiles.toList().getRandom()) }
2018-03-02 13:34:24 +00:00
worldScreen = WorldScreen()
setWorldScreen()
2018-03-02 13:34:24 +00:00
}
fun setWorldScreen() {
setScreen(worldScreen)
worldScreen!!.update()
Gdx.input.inputProcessor = worldScreen!!.stage
}
private fun <T> getFromJson(tClass: Class<T>, name: String): T {
val jsonText = Gdx.files.internal("jsons/$name.json").readString()
return Json().fromJson(tClass, jsonText)
}
private fun <T : INamed> createHashmap(items: Array<T>): LinkedHashMap<String, T> {
val hashMap = LinkedHashMap<String, T>()
2018-03-02 13:34:24 +00:00
for (item in items)
hashMap[item.name] = item
return hashMap
}
private fun setupGameBasics() {
GameBasics.Buildings += createHashmap(getFromJson(Array<Building>::class.java, "Buildings"))
GameBasics.Terrains += createHashmap(getFromJson(Array<Terrain>::class.java, "Terrains"))
GameBasics.TileResources += createHashmap(getFromJson(Array<TileResource>::class.java, "TileResources"))
GameBasics.TileImprovements += createHashmap(getFromJson(Array<TileImprovement>::class.java, "TileImprovements"))
GameBasics.Helps += createHashmap(getFromJson(Array<BasicHelp>::class.java, "BasicHelp"))
GameBasics.Units += createHashmap(getFromJson(Array<Unit>::class.java, "Units"))
GameBasics.PolicyBranches += createHashmap(getFromJson(Array<PolicyBranch>::class.java, "Policies"))
GameBasics.Civilizations += createHashmap(getFromJson(Array<Civilization>::class.java, "Civilizations"))
2018-03-02 13:34:24 +00:00
// ...Yes. Total Voodoo. I wish I didn't have to do this.
val x = LinkedHashMap<String,com.badlogic.gdx.utils.Array<com.badlogic.gdx.utils.Array<String>>>()
val tutorials = getFromJson(x.javaClass, "Tutorials")
for (tut in tutorials)
GameBasics.Tutorials[tut.key] = tut.value.map{it.joinToString("\r\n")}
2018-03-02 13:34:24 +00:00
val techColumns = getFromJson(Array<TechColumn>::class.java, "Techs")
for (techColumn in techColumns) {
for (tech in techColumn.techs) {
tech.cost = techColumn.techCost
tech.column = techColumn
GameBasics.Technologies[tech.name] = tech
}
}
for (building in GameBasics.Buildings.values) {
if (building.requiredTech == null) continue
val column = building.getRequiredTech().column
if (building.cost == 0)
building.cost = if (building.isWonder) column!!.wonderCost else column!!.buildingCost
}
for (branch in GameBasics.PolicyBranches.values) {
branch.requires = ArrayList()
2018-03-02 13:34:24 +00:00
branch.branch = branch.name
for (policy in branch.policies) {
policy.branch = branch.name
if (policy.requires == null) policy.requires = arrayListOf(branch.name)
2018-03-02 13:34:24 +00:00
}
branch.policies.last().name = branch.name + " Complete"
2018-03-02 13:34:24 +00:00
}
}
companion object {
lateinit var Current: UnCivGame
}
}