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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 layerFeatures = TileLayerFeatures(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 layerUnitArt = TileLayerUnitArt(this, groupSize)
@Suppress("LeakingThis") val layerUnitFlag = TileLayerUnitFlag(this, groupSize)
@Suppress("LeakingThis") val layerCityButton = TileLayerCityButton(this, groupSize)
@Suppress("LeakingThis") val layerMisc = TileLayerMisc(this, groupSize)
init {
isTransform = false // performance helper - nothing here is rotated or scaled
this.setSize(groupSize, groupSize)
this.addActor(layerTerrain)
this.addActor(layerFeatures)
this.addActor(layerBorders)
@ -64,8 +66,6 @@ open class TileGroup(
this.addActor(layerCityButton)
layerTerrain.update(null)
isTransform = false // performance helper - nothing here is rotated or scaled
}
open fun clone() = TileGroup(tile, tileSetStrings)
@ -83,27 +83,33 @@ open class TileGroup(
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) {
layerMisc.removeHexOutline()
layerOverlay.hideHighlight()
layerOverlay.hideCrosshair()
val layers = listOf(
layerTerrain, layerFeatures, layerBorders, layerMisc,
layerOverlay, layerUnitArt, layerUnitFlag, layerCityButton)
// Show all layers by default
layers.forEach { it.isVisible = true }
setAllLayersVisible(true)
// Do not update layers if tile is not explored by viewing player
if (viewingCiv != null && !(isForceVisible || viewingCiv.hasExplored(tile))) {
reset()
// If tile has explored neighbors - reveal layers partially
if (tile.neighbors.any { viewingCiv.hasExplored(it) })
reset()
// Else - hide all layers
else
layers.forEach { it.isVisible = false }
if (tile.neighbors.none { viewingCiv.hasExplored(it) })
// Else - hide all layers
setAllLayersVisible(false)
return
}

View File

@ -56,27 +56,43 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si
}
}
private var yieldsInitialized = false
private var yields = YieldGroup().apply { isVisible = false }
private val yields = YieldGroup().apply {
// 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. */
private val arrowsToDraw = ArrayList<MapArrow>()
private val arrows = HashMap<Tile, ArrayList<Actor>>()
private var hexOutlineIcon: Actor? = null
private var resourceName: String? = null
private var resourceIcon: Actor? = null
private var workedIcon: Actor? = null
private var improvementName: String? = null
var improvementIcon: Actor? = null
private set // Getter public for BattleTable to display as City Combatant
private val startingLocationIcons = mutableListOf<Actor>()
private fun updateArrows() {
private fun clearArrows() {
for (actorList in arrows.values)
for (actor in actorList)
actor.remove()
arrows.clear()
}
private fun updateArrows() {
clearArrows()
val tileScale = 50f * 0.8f // See notes in updateRoadImages.
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()
improvementIcon = null
// Get new icon when needed
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)
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
improvementIcon?.isVisible = show
}
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) {
resourceName = tile().resource
resourceIcon?.remove()
if (resourceName == 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
}
resourceIcon = null
}
// This could happen on any turn, since resources need certain techs to reveal them
resourceIcon?.isVisible = when {
tileGroup.isForceVisible -> isVisible
isVisible && viewingCiv == null -> true
isVisible && tile().hasViewableResource(viewingCiv!!) -> true
else -> false
// Get a fresh Icon if and only if necessary
if (resourceName != null && effectiveVisible && resourceIcon == null) {
val icon = ImageGetter.getResourcePortrait(resourceName!!, 20f, tile().resourceAmount)
icon.center(tileGroup)
icon.x -= 22 // left
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
// "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.
@ -167,7 +188,7 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si
startingLocationIcons.forEach { it.remove() }
startingLocationIcons.clear()
if (!isVisible || tileGroup.isForMapEditorIcon)
if (!show || tileGroup.isForMapEditorIcon)
return
if (DebugUtils.SHOW_TILE_COORDS) {
@ -239,32 +260,22 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si
}
// JN updating display of tile yields
private fun updateYieldIcon(viewingCiv: Civilization?, showTileYields: Boolean) {
if (viewingCiv == null)
return
private fun updateYieldIcon(viewingCiv: Civilization?, show: Boolean) {
val effectiveVisible = show &&
!tileGroup.isForMapEditorIcon && // don't have a map to calc yields
!(viewingCiv == null && tileGroup.isForceVisible) // main menu background
// Hiding yield icons (in order to update)
if (yieldsInitialized)
yields.isVisible = false
if (showTileYields) {
// Setting up YieldGroup Icon
yields.isVisible = false
if (effectiveVisible) yields.run {
// Update YieldGroup Icon
if (tileGroup is CityTileGroup)
yields.setStats(tile().stats.getTileStats(tileGroup.city, viewingCiv))
setStats(tile().stats.getTileStats(tileGroup.city, viewingCiv))
else
yields.setStats(tile().stats.getTileStats(viewingCiv))
yields.setOrigin(Align.center)
yields.setScale(0.7f)
yields.toFront()
yields.centerX(tileGroup)
yields.y = tileGroup.height*0.25f - yields.height/2
yields.isVisible = true
yieldsInitialized = true
// Adding YieldGroup to miscLayerGroup
addActor(yields)
setStats(tile().stats.getTileStats(viewingCiv))
toFront()
centerX(tileGroup)
isVisible = true
}
}
@ -336,7 +347,7 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si
override fun determineVisibility() {
isVisible = yields.isVisible
|| resourceIcon?.isVisible == true
|| improvementIcon != null
|| improvementIcon?.isVisible == true
|| workedIcon != null
|| hexOutlineIcon != null
|| arrows.isNotEmpty()
@ -345,8 +356,10 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si
fun reset() {
updateImprovementIcon(null, false)
updateYieldIcon(null, false)
updateResourceIcon(null, false)
updateStartingLocationIcon(false)
clearArrows()
}
}