Battle tabl now shows results of battle, added Barbarian civ

This commit is contained in:
Yair Morgenstern
2018-04-01 15:01:48 +03:00
parent f71c605210
commit bd1a191b15
9 changed files with 84 additions and 15 deletions

View File

@ -0,0 +1,13 @@
package com.unciv.logic
import com.unciv.logic.map.MapUnit
/**
* Created by LENOVO on 3/26/2018.
*/
class Battle(){
fun calculateDamage(attacker:MapUnit, defender:MapUnit): Int {
return (attacker.strength*attacker.health*5) / (defender.strength*defender.health)
}
}

View File

@ -7,18 +7,27 @@ import com.unciv.models.gamebasics.TileImprovement
import java.text.DecimalFormat
enum class UnitType{
Civilian,
Melee,
Ranged
}
class MapUnit {
@Transient
lateinit var civInfo: CivilizationInfo
@JvmField var owner: String? = null
@JvmField var name: String? = null
@JvmField var maxMovement: Int = 0
@JvmField var currentMovement: Float = 0f
@JvmField var action: String? = null // work, automation, fortifying, I dunno what.
var owner: String? = null
var name: String? = null
var maxMovement: Int = 0
var currentMovement: Float = 0f
lateinit var unitType:UnitType
var health:Int = 10
var strength:Int = 1
var rangedStrength:Int = 0
var action: String? = null // work, automation, fortifying, I dunno what.
val movementString: String
get() = DecimalFormat("0.#").format(currentMovement.toDouble()) + "/" + maxMovement
fun getMovementString(): String = DecimalFormat("0.#").format(currentMovement.toDouble()) + "/" + maxMovement
fun doPreTurnAction(tile: TileInfo) {
if (currentMovement == 0f) return // We've already done stuff this turn, and can't do any more stuff

View File

@ -154,7 +154,7 @@ class TileInfo {
if (roadStatus !== RoadStatus.None && !isCityCenter) SB.appendln(roadStatus)
if (improvement != null) SB.appendln(improvement!!)
if (improvementInProgress != null) SB.appendln("$improvementInProgress in ${this.turnsToImprovement} turns")
if (unit != null) SB.appendln(unit!!.name + "(" + unit!!.movementString + ")")
if (unit != null) SB.appendln(unit!!.name + "(" + unit!!.getMovementString() + ")")
return SB.toString()
}

View File

@ -115,7 +115,8 @@ class TileMap {
val unit = GameBasics.Units[unitName]!!.mapUnit
unit.owner = civInfo.civName
unit.civInfo = civInfo
getTilesInDistance(position, 2).first { it.unit == null }.unit = unit // And if there's none, then kill me.
val tilesInDistance = getTilesInDistance(position, 2)
tilesInDistance.first { it.unit == null }.unit = unit // And if there's none, then kill me.
}
fun getViewableTiles(position: Vector2, sightDistance: Int): MutableList<TileInfo> {

View File

@ -44,7 +44,11 @@ class UnCivGame : Game() {
gameInfo = GameInfo()
gameInfo.tileMap = TileMap(20)
gameInfo.civilizations.add(CivilizationInfo("Babylon", Vector2.Zero, gameInfo))
val barbarians = CivilizationInfo()
barbarians.civName = "Barbarians"
gameInfo.civilizations.add(barbarians)
gameInfo.setTransients()
gameInfo.tileMap.placeUnitNearTile(Vector2.Zero,"Scout",barbarians)
worldScreen = WorldScreen()
setWorldScreen()

View File

@ -2,8 +2,11 @@ package com.unciv.ui.worldscreen
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.scenes.scene2d.ui.Label
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.unciv.logic.Battle
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.map.MapUnit
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.pickerscreens.PolicyPickerScreen
import com.unciv.ui.pickerscreens.TechPickerScreen
@ -25,6 +28,7 @@ class WorldScreen : CameraStageBaseScreen() {
internal val optionsTable: WorldScreenOptionsTable
private val notificationsScroll: NotificationsScroll
internal val unitTable = UnitTable(this)
internal val battleTable = BattleTable(this)
init {
val gameInfo = game.gameInfo
@ -41,6 +45,7 @@ class WorldScreen : CameraStageBaseScreen() {
optionsTable = WorldScreenOptionsTable(this, civInfo)
Label("", CameraStageBaseScreen.skin).style.font.data.setScale(game.settings.labelScale)
tileMapHolder.addTiles()
stage.addActor(tileMapHolder)
@ -50,6 +55,7 @@ class WorldScreen : CameraStageBaseScreen() {
stage.addActor(techButton)
stage.addActor(notificationsScroll)
stage.addActor(unitTable)
stage.addActor(battleTable)
update()
@ -66,7 +72,8 @@ class WorldScreen : CameraStageBaseScreen() {
}
updateTechButton()
if (tileMapHolder.selectedTile != null) tileInfoTable.updateTileTable(tileMapHolder.selectedTile!!)
if (tileMapHolder.selectedTile != null)
tileInfoTable.updateTileTable(tileMapHolder.selectedTile!!)
tileMapHolder.updateTiles()
civTable.update(this)
notificationsScroll.update()
@ -74,6 +81,13 @@ class WorldScreen : CameraStageBaseScreen() {
notificationsScroll.setPosition(stage.width - notificationsScroll.width - 5f,
nextTurnButton.y - notificationsScroll.height - 5f)
unitTable.update()
if(tileMapHolder.selectedTile!=null
&& tileMapHolder.selectedTile!!.unit!=null
&& tileMapHolder.selectedTile!!.unit!!.owner!=civInfo.civName
&& unitTable.selectedUnitTile!=null)
battleTable.simulateBattle(unitTable.getSelectedUnit(), tileMapHolder.selectedTile!!.unit!!)
else battleTable.clear()
}
private fun updateTechButton() {
@ -130,3 +144,26 @@ class WorldScreen : CameraStageBaseScreen() {
}
}
class BattleTable(val worldScreen: WorldScreen): Table() {
fun simulateBattle(attacker:MapUnit,defender:MapUnit){
clear()
add(Label(attacker.name, CameraStageBaseScreen.skin))
add(Label(defender.name, CameraStageBaseScreen.skin))
row()
val battle = Battle()
val damageToAttacker = battle.calculateDamage(attacker,defender)
add(Label(attacker.health.toString()+"/10 -> "
+(attacker.health-damageToAttacker)+"/10",
CameraStageBaseScreen.skin))
val damageToDefender = battle.calculateDamage(defender,attacker)
add(Label(defender.health.toString()+"/10 -> "
+(defender.health-damageToDefender)+"/10",
CameraStageBaseScreen.skin))
pack()
setPosition(worldScreen.stage.width/2-width/2,
5f)
}
}

View File

@ -40,7 +40,7 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
if(selectedUnitTile!=null && selectedUnitTile!!.unit==null) selectedUnitTile=null // The unit that was there no longer exists
if(selectedUnitTile!=null) {
unitLabel.setText(getSelectedUnit().name+" "+getSelectedUnit().movementString)
unitLabel.setText(getSelectedUnit().name+" "+getSelectedUnit().getMovementString())
for (button in UnitActions().getUnitActions(selectedUnitTile!!,worldScreen))
unitActionsTable.add(button).colspan(2).pad(5f)
.size(button.width * worldScreen.buttonScale, button.height * worldScreen.buttonScale).row()
@ -61,7 +61,8 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
currentlyExecutingAction = null
}
if(selectedTile.unit!=null) selectedUnitTile = selectedTile
if(selectedTile.unit!=null && selectedTile.unit!!.civInfo == worldScreen.civInfo)
selectedUnitTile = selectedTile
}
private fun getDistanceToTiles(): HashMap<TileInfo, Float> {