using StringAction for string-encoded actions used throughout the app

This commit is contained in:
martin 2019-05-22 22:15:48 +02:00 committed by Yair Morgenstern
parent 29647896ce
commit 4330508d2e
3 changed files with 23 additions and 5 deletions

View file

@ -7,6 +7,7 @@ import com.unciv.logic.automation.UnitAutomation
import com.unciv.logic.automation.WorkerAutomation import com.unciv.logic.automation.WorkerAutomation
import com.unciv.logic.civilization.CivilizationInfo import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.map.action.MapUnitAction import com.unciv.logic.map.action.MapUnitAction
import com.unciv.logic.map.action.StringAction
import com.unciv.models.gamebasics.GameBasics import com.unciv.models.gamebasics.GameBasics
import com.unciv.models.gamebasics.tech.TechEra import com.unciv.models.gamebasics.tech.TechEra
import com.unciv.models.gamebasics.tile.TerrainType import com.unciv.models.gamebasics.tile.TerrainType
@ -38,8 +39,8 @@ class MapUnit {
var mapUnitAction : MapUnitAction? = null var mapUnitAction : MapUnitAction? = null
var action: String? // work, automation, fortifying, I dunno what. var action: String? // work, automation, fortifying, I dunno what.
// getter and setter for compatibility: make sure string-based actions still work // getter and setter for compatibility: make sure string-based actions still work
get() = mapUnitAction?.name get() = (mapUnitAction as? StringAction)?.action ?: mapUnitAction?.let { "" } // null if no action is assigned.
set(value) { mapUnitAction = value?.let{ MapUnitAction(this, value) } } set(value) { mapUnitAction = value?.let{ StringAction(this, value) } } // wrap traditional string-encoded actions into StringAction
var attacksThisTurn = 0 var attacksThisTurn = 0
var promotions = UnitPromotions() var promotions = UnitPromotions()

View file

@ -3,13 +3,12 @@ package com.unciv.logic.map.action
import com.unciv.logic.map.MapUnit import com.unciv.logic.map.MapUnit
open class MapUnitAction( open class MapUnitAction(
@Transient var unit: MapUnit = MapUnit(), @Transient var unit: MapUnit = MapUnit()
var name: String = ""
) { ) {
/** return true if this action is possible in the given conditions */ /** return true if this action is possible in the given conditions */
open fun isAvailable(): Boolean = true open fun isAvailable(): Boolean = true
open fun doPreTurnAction() {} open fun doPreTurnAction() {}
open fun shouldStopOnEnemyInSight(): Boolean = name.startsWith("moveTo") open fun shouldStopOnEnemyInSight(): Boolean = false
} }

View file

@ -0,0 +1,18 @@
package com.unciv.logic.map.action
import com.unciv.logic.map.MapUnit
/**
* this class represents all actions that are identified by string only.
* this is the traditional way of handling actions in UnCiv: by coding relevant information
* into a string. This class is here to maintain compatibility to that method, preventing from a huge
* refactoring going on here.
*/
class StringAction(
unit: MapUnit = MapUnit(),
val action: String = "" // traditional string-encoded action like "moveTo x,y"
): MapUnitAction(unit) {
override fun shouldStopOnEnemyInSight(): Boolean = action.startsWith("moveTo")
}