Fix MiscLayer not respecting fog of war for spectator (#8992)

* Fix MiscLayer not respecting fog of war for spectator

* Fix MiscLayer not respecting fog of war for spectator - fix1
This commit is contained in:
SomeTroglodyte
2023-03-26 11:17:54 +02:00
committed by GitHub
parent e18a964a28
commit 2adb4ffdde
2 changed files with 93 additions and 74 deletions

View File

@ -46,14 +46,16 @@ open class TileGroup(
@Suppress("LeakingThis") val layerTerrain = TileLayerTerrain(this, groupSize) @Suppress("LeakingThis") val layerTerrain = TileLayerTerrain(this, groupSize)
@Suppress("LeakingThis") val layerFeatures = TileLayerFeatures(this, groupSize) @Suppress("LeakingThis") val layerFeatures = TileLayerFeatures(this, groupSize)
@Suppress("LeakingThis") val layerBorders = TileLayerBorders(this, groupSize) @Suppress("LeakingThis") val layerBorders = TileLayerBorders(this, groupSize)
@Suppress("LeakingThis") val layerMisc = TileLayerMisc(this, groupSize)
@Suppress("LeakingThis") val layerOverlay = TileLayerOverlay(this, groupSize) @Suppress("LeakingThis") val layerOverlay = TileLayerOverlay(this, groupSize)
@Suppress("LeakingThis") val layerUnitArt = TileLayerUnitArt(this, groupSize) @Suppress("LeakingThis") val layerUnitArt = TileLayerUnitArt(this, groupSize)
@Suppress("LeakingThis") val layerUnitFlag = TileLayerUnitFlag(this, groupSize) @Suppress("LeakingThis") val layerUnitFlag = TileLayerUnitFlag(this, groupSize)
@Suppress("LeakingThis") val layerCityButton = TileLayerCityButton(this, groupSize) @Suppress("LeakingThis") val layerCityButton = TileLayerCityButton(this, groupSize)
@Suppress("LeakingThis") val layerMisc = TileLayerMisc(this, groupSize)
init { init {
isTransform = false // performance helper - nothing here is rotated or scaled
this.setSize(groupSize, groupSize) this.setSize(groupSize, groupSize)
this.addActor(layerTerrain) this.addActor(layerTerrain)
this.addActor(layerFeatures) this.addActor(layerFeatures)
this.addActor(layerBorders) this.addActor(layerBorders)
@ -64,8 +66,6 @@ open class TileGroup(
this.addActor(layerCityButton) this.addActor(layerCityButton)
layerTerrain.update(null) layerTerrain.update(null)
isTransform = false // performance helper - nothing here is rotated or scaled
} }
open fun clone() = TileGroup(tile, tileSetStrings) open fun clone() = TileGroup(tile, tileSetStrings)
@ -83,27 +83,33 @@ open class TileGroup(
layerUnitFlag.reset() layerUnitFlag.reset()
} }
private fun setAllLayersVisible(isVisible: Boolean) {
layerTerrain.isVisible = isVisible
layerFeatures.isVisible = isVisible
layerBorders.isVisible = isVisible
layerMisc.isVisible = isVisible
layerOverlay.isVisible = isVisible
layerUnitArt.isVisible = isVisible
layerUnitFlag.isVisible = isVisible
layerCityButton.isVisible = isVisible
}
open fun update(viewingCiv: Civilization? = null) { open fun update(viewingCiv: Civilization? = null) {
layerMisc.removeHexOutline() layerMisc.removeHexOutline()
layerOverlay.hideHighlight() layerOverlay.hideHighlight()
layerOverlay.hideCrosshair() layerOverlay.hideCrosshair()
val layers = listOf(
layerTerrain, layerFeatures, layerBorders, layerMisc,
layerOverlay, layerUnitArt, layerUnitFlag, layerCityButton)
// Show all layers by default // Show all layers by default
layers.forEach { it.isVisible = true } setAllLayersVisible(true)
// Do not update layers if tile is not explored by viewing player // Do not update layers if tile is not explored by viewing player
if (viewingCiv != null && !(isForceVisible || viewingCiv.hasExplored(tile))) { if (viewingCiv != null && !(isForceVisible || viewingCiv.hasExplored(tile))) {
reset()
// If tile has explored neighbors - reveal layers partially // If tile has explored neighbors - reveal layers partially
if (tile.neighbors.any { viewingCiv.hasExplored(it) }) if (tile.neighbors.none { viewingCiv.hasExplored(it) })
reset() // Else - hide all layers
// Else - hide all layers setAllLayersVisible(false)
else
layers.forEach { it.isVisible = false }
return return
} }

View File

@ -56,27 +56,43 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si
} }
} }
private var yieldsInitialized = false private val yields = YieldGroup().apply {
private var yields = YieldGroup().apply { isVisible = false } // Unlike resource or improvement this is created and added only once,
// It's the contents that get updated
isVisible = false
setOrigin(Align.center)
setScale(0.7f)
y = tileGroup.height * 0.25f - height / 2
// Adding YieldGroup to miscLayerGroup
this@TileLayerMisc.addActor(this)
}
/** Array list of all arrows to draw from this tile on the next update. */ /** Array list of all arrows to draw from this tile on the next update. */
private val arrowsToDraw = ArrayList<MapArrow>() private val arrowsToDraw = ArrayList<MapArrow>()
private val arrows = HashMap<Tile, ArrayList<Actor>>() private val arrows = HashMap<Tile, ArrayList<Actor>>()
private var hexOutlineIcon: Actor? = null private var hexOutlineIcon: Actor? = null
private var resourceName: String? = null private var resourceName: String? = null
private var resourceIcon: Actor? = null private var resourceIcon: Actor? = null
private var workedIcon: Actor? = null private var workedIcon: Actor? = null
private var improvementName: String? = null
var improvementIcon: Actor? = null var improvementIcon: Actor? = null
private set // Getter public for BattleTable to display as City Combatant
private val startingLocationIcons = mutableListOf<Actor>() private val startingLocationIcons = mutableListOf<Actor>()
private fun updateArrows() { private fun clearArrows() {
for (actorList in arrows.values) for (actorList in arrows.values)
for (actor in actorList) for (actor in actorList)
actor.remove() actor.remove()
arrows.clear() arrows.clear()
}
private fun updateArrows() {
clearArrows()
val tileScale = 50f * 0.8f // See notes in updateRoadImages. val tileScale = 50f * 0.8f // See notes in updateRoadImages.
for (arrowToAdd in arrowsToDraw) { for (arrowToAdd in arrowsToDraw) {
@ -111,53 +127,58 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si
} }
} }
private fun updateImprovementIcon(viewingCiv: Civilization?, showResourcesAndImprovements: Boolean) { private fun updateImprovementIcon(viewingCiv: Civilization?, show: Boolean) {
// If improvement has changed, force new icon next time it is needed
val improvementToShow = tile().getShownImprovement(viewingCiv)
if (improvementName != improvementToShow) {
improvementName = improvementToShow
improvementIcon?.remove()
improvementIcon = null
}
improvementIcon?.remove() // Get new icon when needed
improvementIcon = null if (improvementName != null && show && improvementIcon == null) {
val icon = ImageGetter.getImprovementPortrait(improvementName!!, dim = false)
icon.center(tileGroup)
icon.x -= 22 // left
icon.y -= 12 // bottom
addActor(icon)
improvementIcon = icon
}
val shownImprovement = tile().getShownImprovement(viewingCiv) improvementIcon?.isVisible = show
if (shownImprovement == null || !showResourcesAndImprovements)
return
val icon = ImageGetter.getImprovementPortrait(shownImprovement, dim = false)
addActor(icon)
icon.center(tileGroup)
icon.x -= 22 // left
icon.y -= 12 // bottom
improvementIcon = icon
} }
private fun updateResourceIcon(viewingCiv: Civilization?, isVisible: Boolean) { private fun updateResourceIcon(viewingCiv: Civilization?, show: Boolean) {
// This could change on any turn, since resources need certain techs to reveal them
val effectiveVisible = when {
tileGroup.isForceVisible -> show
show && viewingCiv == null -> true
show && tile().hasViewableResource(viewingCiv!!) -> true
else -> false
}
// If resource has changed (e.g. tech researched) - add new icon // If resource has changed (e.g. tech researched) - force new icon next time it's needed
if (resourceName != tile().resource) { if (resourceName != tile().resource) {
resourceName = tile().resource resourceName = tile().resource
resourceIcon?.remove() resourceIcon?.remove()
if (resourceName == null) resourceIcon = null
resourceIcon = null
else {
val newResourceIcon = ImageGetter.getResourcePortrait(resourceName!!, 20f, tile().resourceAmount)
newResourceIcon.center(tileGroup)
newResourceIcon.x -= 22 // left
newResourceIcon.y += 10 // top
addActor(newResourceIcon)
resourceIcon = newResourceIcon
}
} }
// This could happen on any turn, since resources need certain techs to reveal them // Get a fresh Icon if and only if necessary
resourceIcon?.isVisible = when { if (resourceName != null && effectiveVisible && resourceIcon == null) {
tileGroup.isForceVisible -> isVisible val icon = ImageGetter.getResourcePortrait(resourceName!!, 20f, tile().resourceAmount)
isVisible && viewingCiv == null -> true icon.center(tileGroup)
isVisible && tile().hasViewableResource(viewingCiv!!) -> true icon.x -= 22 // left
else -> false icon.y += 10 // top
addActor(icon)
resourceIcon = icon
} }
resourceIcon?.isVisible = effectiveVisible
} }
private fun updateStartingLocationIcon(isVisible: Boolean) { private fun updateStartingLocationIcon(show: Boolean) {
// The starting location icons are visible in map editor only, but this method is abused for the // The starting location icons are visible in map editor only, but this method is abused for the
// "Show coordinates on tiles" debug option as well. Calling code made sure this is only called // "Show coordinates on tiles" debug option as well. Calling code made sure this is only called
// with isVisible=false for reset, or for non-WorldMap TileGroups, or with the debug option set. // with isVisible=false for reset, or for non-WorldMap TileGroups, or with the debug option set.
@ -167,7 +188,7 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si
startingLocationIcons.forEach { it.remove() } startingLocationIcons.forEach { it.remove() }
startingLocationIcons.clear() startingLocationIcons.clear()
if (!isVisible || tileGroup.isForMapEditorIcon) if (!show || tileGroup.isForMapEditorIcon)
return return
if (DebugUtils.SHOW_TILE_COORDS) { if (DebugUtils.SHOW_TILE_COORDS) {
@ -239,32 +260,22 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si
} }
// JN updating display of tile yields // JN updating display of tile yields
private fun updateYieldIcon(viewingCiv: Civilization?, showTileYields: Boolean) { private fun updateYieldIcon(viewingCiv: Civilization?, show: Boolean) {
val effectiveVisible = show &&
if (viewingCiv == null) !tileGroup.isForMapEditorIcon && // don't have a map to calc yields
return !(viewingCiv == null && tileGroup.isForceVisible) // main menu background
// Hiding yield icons (in order to update) // Hiding yield icons (in order to update)
if (yieldsInitialized) yields.isVisible = false
yields.isVisible = false if (effectiveVisible) yields.run {
// Update YieldGroup Icon
if (showTileYields) {
// Setting up YieldGroup Icon
if (tileGroup is CityTileGroup) if (tileGroup is CityTileGroup)
yields.setStats(tile().stats.getTileStats(tileGroup.city, viewingCiv)) setStats(tile().stats.getTileStats(tileGroup.city, viewingCiv))
else else
yields.setStats(tile().stats.getTileStats(viewingCiv)) setStats(tile().stats.getTileStats(viewingCiv))
yields.setOrigin(Align.center) toFront()
yields.setScale(0.7f) centerX(tileGroup)
yields.toFront() isVisible = true
yields.centerX(tileGroup)
yields.y = tileGroup.height*0.25f - yields.height/2
yields.isVisible = true
yieldsInitialized = true
// Adding YieldGroup to miscLayerGroup
addActor(yields)
} }
} }
@ -336,7 +347,7 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si
override fun determineVisibility() { override fun determineVisibility() {
isVisible = yields.isVisible isVisible = yields.isVisible
|| resourceIcon?.isVisible == true || resourceIcon?.isVisible == true
|| improvementIcon != null || improvementIcon?.isVisible == true
|| workedIcon != null || workedIcon != null
|| hexOutlineIcon != null || hexOutlineIcon != null
|| arrows.isNotEmpty() || arrows.isNotEmpty()
@ -345,8 +356,10 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si
fun reset() { fun reset() {
updateImprovementIcon(null, false) updateImprovementIcon(null, false)
updateYieldIcon(null, false)
updateResourceIcon(null, false) updateResourceIcon(null, false)
updateStartingLocationIcon(false) updateStartingLocationIcon(false)
clearArrows()
} }
} }