From bed3aa54c07b24a2dac90a0fa074182c4125567a Mon Sep 17 00:00:00 2001 From: chrisjk Date: Sun, 20 Apr 2025 02:04:42 +0200 Subject: [PATCH 1/3] feat: :sparkles: add fow handeling --- assets/ability/effects/fow/fow.png | Bin 236 -> 0 bytes assets/board/fow.png | Bin 0 -> 236 bytes .../chessevolved/components/FowComponent.kt | 13 ++++++ .../entities/BoardSquareFactory.kt | 3 ++ .../systems/FowRenderingSystem.kt | 40 ++++++++++++++++++ .../chessevolved/systems/MovementSystem.kt | 27 ++++++++++++ .../chessevolved/presenters/GamePresenter.kt | 11 +++++ 7 files changed, 94 insertions(+) delete mode 100644 assets/ability/effects/fow/fow.png create mode 100644 assets/board/fow.png create mode 100644 chessevolved_model/src/main/kotlin/io/github/chessevolved/components/FowComponent.kt create mode 100644 chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/FowRenderingSystem.kt diff --git a/assets/ability/effects/fow/fow.png b/assets/ability/effects/fow/fow.png deleted file mode 100644 index 8b899db6cd890a730836d95e07ec079241bfbf78..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 236 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}%RF5iLn2y} zZFX z%DAJ(Z&mY#naz1@VeA2?pC6fF{9q?TzLbLa1ff=wLI%x1iwA)QuNXF6Vpu37#Ma@- zonqNAOZ7>E`@sh52Cb(Is+mn%d<#^31@6i%2)$}!ahsWk=Snv3#J3DyIu7MFH+_wY h4rb(9ykY?QVvd5|sm135@_;U8@O1TaS?83{1OT;3R0{wA diff --git a/assets/board/fow.png b/assets/board/fow.png new file mode 100644 index 0000000000000000000000000000000000000000..0b4651e3ff1b3534a3bd820a57bd3b3451240c33 GIT binary patch literal 236 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}%RF5iLn2y} zZFX<@W`CH0X<6fznk$Tp`4-G(-gf^<(v7;w+^c1{6?=a)EdOP`NT$JGHjZ5&n^Bcv zHFLqwIU($c#_V@_*6=NucD~av{Xj9}Jrf5#1y!!hM+`xW3JxqzxWbsSl+j65g-2kr z*oh(rQ|}Xu{Y{MJ456nOy)LnY$~bt=QrKnduzFQx!8UdYi51tSlx{OliD-CNv1xY7 ilctNe3a&6NVPLpmt6$T5Qu7|r#SEUVelF{r5}E*_jZ^^u literal 0 HcmV?d00001 diff --git a/chessevolved_model/src/main/kotlin/io/github/chessevolved/components/FowComponent.kt b/chessevolved_model/src/main/kotlin/io/github/chessevolved/components/FowComponent.kt new file mode 100644 index 00000000..e04fb11b --- /dev/null +++ b/chessevolved_model/src/main/kotlin/io/github/chessevolved/components/FowComponent.kt @@ -0,0 +1,13 @@ +package io.github.chessevolved.components + +import com.badlogic.ashley.core.Component +import com.badlogic.ashley.core.ComponentMapper + +class FowComponent( + var showFog: Boolean, +) : Component { + companion object { + val mapper: ComponentMapper = + ComponentMapper.getFor(FowComponent::class.java) + } +} diff --git a/chessevolved_model/src/main/kotlin/io/github/chessevolved/entities/BoardSquareFactory.kt b/chessevolved_model/src/main/kotlin/io/github/chessevolved/entities/BoardSquareFactory.kt index 18bfd3c9..d303bf47 100644 --- a/chessevolved_model/src/main/kotlin/io/github/chessevolved/entities/BoardSquareFactory.kt +++ b/chessevolved_model/src/main/kotlin/io/github/chessevolved/entities/BoardSquareFactory.kt @@ -9,6 +9,7 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.scenes.scene2d.Stage import com.badlogic.gdx.scenes.scene2d.ui.Image import io.github.chessevolved.components.ActorComponent +import io.github.chessevolved.components.FowComponent import io.github.chessevolved.components.HighlightComponent import io.github.chessevolved.components.PlayerColorComponent import io.github.chessevolved.components.PositionComponent @@ -58,6 +59,7 @@ class BoardSquareFactory( weatherEvent: WeatherEvent, playerColor: PlayerColor, stage: Stage, + isFowEnabled: Boolean, ): Entity = Entity().apply { add(PositionComponent(position)) @@ -65,6 +67,7 @@ class BoardSquareFactory( add(PlayerColorComponent(playerColor)) add(TextureRegionComponent(getBoardSquareTextureRegion(playerColor))) add(HighlightComponent(Color.WHITE)) + add(FowComponent(isFowEnabled)) add( ActorComponent( getBoardActor(position, stage) { clickedPosition -> inputService.clickBoardSquareAtPosition(clickedPosition) }, diff --git a/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/FowRenderingSystem.kt b/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/FowRenderingSystem.kt new file mode 100644 index 00000000..c9fe5633 --- /dev/null +++ b/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/FowRenderingSystem.kt @@ -0,0 +1,40 @@ +package io.github.chessevolved.systems + +import com.badlogic.ashley.core.Entity +import com.badlogic.ashley.core.Family +import com.badlogic.ashley.systems.IteratingSystem +import com.badlogic.gdx.graphics.Texture +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import io.github.chessevolved.components.FowComponent +import io.github.chessevolved.components.PositionComponent +import io.github.chessevolved.singletons.GameSettings + +class FowRenderingSystem( + private val batch: SpriteBatch, + private val texture: Texture, + private val isFowEnabled: Boolean, +) : IteratingSystem( + Family.all(PositionComponent::class.java, FowComponent::class.java).get(), + ) { + override fun processEntity( + entity: Entity, + deltaTime: Float, + ) { + if (!isFowEnabled) return + + val position = PositionComponent.mapper.get(entity) + val fow = FowComponent.mapper.get(entity) + + if (position != null && fow != null) { + if (fow.showFog && (position.position.y >= (GameSettings.getBoardSize() + 1) / 2)) { + batch.draw( + texture, + position.position.x.toFloat(), + position.position.y.toFloat(), + 1f, + 1f, + ) + } + } + } +} diff --git a/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt b/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt index c59b4c78..d9a1f08f 100644 --- a/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt +++ b/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt @@ -4,11 +4,14 @@ import com.badlogic.ashley.core.Entity import com.badlogic.ashley.core.Family import com.badlogic.ashley.systems.IteratingSystem import io.github.chessevolved.components.ActorComponent +import io.github.chessevolved.components.FowComponent import io.github.chessevolved.components.MovementIntentComponent import io.github.chessevolved.components.MovementRuleComponent import io.github.chessevolved.components.PositionComponent import io.github.chessevolved.components.SelectionComponent import io.github.chessevolved.components.ValidMovesComponent +import io.github.chessevolved.data.Position +import io.github.chessevolved.singletons.EcsEngine class MovementSystem( private val onTurnComplete: () -> Unit, @@ -20,6 +23,8 @@ class MovementSystem( MovementIntentComponent::class.java, ).get(), ) { + private val engine = EcsEngine + override fun processEntity( entity: Entity?, deltaTime: Float, @@ -49,10 +54,32 @@ class MovementSystem( } } + clearFow(targetPosition) + entity?.remove(SelectionComponent::class.java) entity?.remove(ValidMovesComponent::class.java) entity?.remove(MovementIntentComponent::class.java) onTurnComplete() } + + private fun clearFow(center: Position) { + for (x in -1..1) { + for (y in -1..1) { + val currentPosition = Position(center.x + x, center.y + y) + + val boardSquare = + engine.getEntitiesFor( + Family.all(PositionComponent::class.java, FowComponent::class.java).get(), + ).firstOrNull { + PositionComponent.mapper.get(it).position == currentPosition + } + + boardSquare?.let { + val fow = FowComponent.mapper.get(it) + if (fow.showFog) fow.showFog = false + } + } + } + } } diff --git a/chessevolved_presenter/src/main/kotlin/io/github/chessevolved/presenters/GamePresenter.kt b/chessevolved_presenter/src/main/kotlin/io/github/chessevolved/presenters/GamePresenter.kt index 363e41c9..70f91708 100644 --- a/chessevolved_presenter/src/main/kotlin/io/github/chessevolved/presenters/GamePresenter.kt +++ b/chessevolved_presenter/src/main/kotlin/io/github/chessevolved/presenters/GamePresenter.kt @@ -23,9 +23,11 @@ import io.github.chessevolved.singletons.EcsEntityMapper import io.github.chessevolved.singletons.Game import io.github.chessevolved.singletons.Game.subscribeToGameUpdates import io.github.chessevolved.singletons.Game.unsubscribeFromGameUpdates +import io.github.chessevolved.singletons.GameSettings import io.github.chessevolved.singletons.Lobby import io.github.chessevolved.systems.AbilitySystem import io.github.chessevolved.systems.CaptureSystem +import io.github.chessevolved.systems.FowRenderingSystem import io.github.chessevolved.systems.InputSystem import io.github.chessevolved.systems.MovementSystem import io.github.chessevolved.systems.RenderingSystem @@ -57,6 +59,7 @@ class GamePresenter( private val movementSystem: MovementSystem private val renderingSystem: RenderingSystem + private val fowRenderingSystem: FowRenderingSystem private val selectionListener: SelectionEntityListener private val captureSystem: CaptureSystem private val inputSystem: InputSystem @@ -64,6 +67,7 @@ class GamePresenter( private val visualEffectSystem: VisualEffectSystem private var navigatingToEndGame = false + private val isFowEnabled = GameSettings.isFOWEnabled() init { setupGameView() @@ -92,6 +96,11 @@ class GamePresenter( loadRequiredAssets() assetManager.finishLoading() + val fowTexture = assetManager.get("board/fow.png", Texture::class.java) + + fowRenderingSystem = FowRenderingSystem(gameBatch, fowTexture, isFowEnabled) + engine.addSystem(fowRenderingSystem) + visualEffectSystem.initializeAnimations() setupBoard() @@ -103,6 +112,7 @@ class GamePresenter( private fun loadRequiredAssets() { assetManager.load("board/black-tile.png", Texture::class.java) assetManager.load("board/white-tile.png", Texture::class.java) + assetManager.load("board/fow.png", Texture::class.java) PlayerColor.entries.forEach { color -> PieceType.entries.forEach { type -> @@ -143,6 +153,7 @@ class GamePresenter( WeatherEvent.NONE, tileColor, gameStage, + isFowEnabled, ) } } From eced5542f93b8af4a2e73238f779309e17cf6b71 Mon Sep 17 00:00:00 2001 From: chrisjk Date: Sun, 20 Apr 2025 13:13:10 +0200 Subject: [PATCH 2/3] refactor: :recycle: refactor according to review comments --- .../github/chessevolved/entities/BoardSquareFactory.kt | 3 ++- .../io/github/chessevolved/systems/FowRenderingSystem.kt | 9 ++++++--- .../io/github/chessevolved/systems/MovementSystem.kt | 3 +-- .../io/github/chessevolved/presenters/GamePresenter.kt | 5 +---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/chessevolved_model/src/main/kotlin/io/github/chessevolved/entities/BoardSquareFactory.kt b/chessevolved_model/src/main/kotlin/io/github/chessevolved/entities/BoardSquareFactory.kt index d303bf47..e82c3cb9 100644 --- a/chessevolved_model/src/main/kotlin/io/github/chessevolved/entities/BoardSquareFactory.kt +++ b/chessevolved_model/src/main/kotlin/io/github/chessevolved/entities/BoardSquareFactory.kt @@ -18,6 +18,7 @@ import io.github.chessevolved.components.WeatherEventComponent import io.github.chessevolved.data.Position import io.github.chessevolved.enums.PlayerColor import io.github.chessevolved.enums.WeatherEvent +import io.github.chessevolved.singletons.GameSettings import io.github.chessevolved.systems.InputService import ktx.actors.onClick @@ -26,6 +27,7 @@ class BoardSquareFactory( private val assetManager: AssetManager, ) { private val inputService: InputService = InputService() + private val isFowEnabled = GameSettings.isFOWEnabled() private fun getBoardSquareTextureRegion(playerColor: PlayerColor): TextureRegion { val colorStr = playerColor.name.lowercase() @@ -59,7 +61,6 @@ class BoardSquareFactory( weatherEvent: WeatherEvent, playerColor: PlayerColor, stage: Stage, - isFowEnabled: Boolean, ): Entity = Entity().apply { add(PositionComponent(position)) diff --git a/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/FowRenderingSystem.kt b/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/FowRenderingSystem.kt index c9fe5633..32b6e764 100644 --- a/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/FowRenderingSystem.kt +++ b/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/FowRenderingSystem.kt @@ -3,6 +3,7 @@ package io.github.chessevolved.systems import com.badlogic.ashley.core.Entity import com.badlogic.ashley.core.Family import com.badlogic.ashley.systems.IteratingSystem +import com.badlogic.gdx.assets.AssetManager import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.SpriteBatch import io.github.chessevolved.components.FowComponent @@ -11,11 +12,13 @@ import io.github.chessevolved.singletons.GameSettings class FowRenderingSystem( private val batch: SpriteBatch, - private val texture: Texture, - private val isFowEnabled: Boolean, + private val assetManager: AssetManager, ) : IteratingSystem( Family.all(PositionComponent::class.java, FowComponent::class.java).get(), ) { + private val isFowEnabled = GameSettings.isFOWEnabled() + private val fowTexture = assetManager.get("board/fow.png", Texture::class.java) + override fun processEntity( entity: Entity, deltaTime: Float, @@ -28,7 +31,7 @@ class FowRenderingSystem( if (position != null && fow != null) { if (fow.showFog && (position.position.y >= (GameSettings.getBoardSize() + 1) / 2)) { batch.draw( - texture, + fowTexture, position.position.x.toFloat(), position.position.y.toFloat(), 1f, diff --git a/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt b/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt index d9a1f08f..25a02c68 100644 --- a/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt +++ b/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt @@ -11,7 +11,6 @@ import io.github.chessevolved.components.PositionComponent import io.github.chessevolved.components.SelectionComponent import io.github.chessevolved.components.ValidMovesComponent import io.github.chessevolved.data.Position -import io.github.chessevolved.singletons.EcsEngine class MovementSystem( private val onTurnComplete: () -> Unit, @@ -23,7 +22,7 @@ class MovementSystem( MovementIntentComponent::class.java, ).get(), ) { - private val engine = EcsEngine + private val boardFamily = Family.all(PositionComponent::class.java, FowComponent::class.java).get() override fun processEntity( entity: Entity?, diff --git a/chessevolved_presenter/src/main/kotlin/io/github/chessevolved/presenters/GamePresenter.kt b/chessevolved_presenter/src/main/kotlin/io/github/chessevolved/presenters/GamePresenter.kt index 70f91708..519fd27d 100644 --- a/chessevolved_presenter/src/main/kotlin/io/github/chessevolved/presenters/GamePresenter.kt +++ b/chessevolved_presenter/src/main/kotlin/io/github/chessevolved/presenters/GamePresenter.kt @@ -96,9 +96,7 @@ class GamePresenter( loadRequiredAssets() assetManager.finishLoading() - val fowTexture = assetManager.get("board/fow.png", Texture::class.java) - - fowRenderingSystem = FowRenderingSystem(gameBatch, fowTexture, isFowEnabled) + fowRenderingSystem = FowRenderingSystem(gameBatch, assetManager) engine.addSystem(fowRenderingSystem) visualEffectSystem.initializeAnimations() @@ -153,7 +151,6 @@ class GamePresenter( WeatherEvent.NONE, tileColor, gameStage, - isFowEnabled, ) } } From 85aa564a802fe2bf8f70cf7759da27644eade9dd Mon Sep 17 00:00:00 2001 From: chrisjk Date: Sun, 20 Apr 2025 14:47:09 +0200 Subject: [PATCH 3/3] refactor: :recycle: refactor fowrenderingsystem according to review --- .../kotlin/io/github/chessevolved/systems/MovementSystem.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt b/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt index 25a02c68..1613a1e8 100644 --- a/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt +++ b/chessevolved_model/src/main/kotlin/io/github/chessevolved/systems/MovementSystem.kt @@ -11,6 +11,7 @@ import io.github.chessevolved.components.PositionComponent import io.github.chessevolved.components.SelectionComponent import io.github.chessevolved.components.ValidMovesComponent import io.github.chessevolved.data.Position +import io.github.chessevolved.singletons.EcsEngine class MovementSystem( private val onTurnComplete: () -> Unit, @@ -68,9 +69,7 @@ class MovementSystem( val currentPosition = Position(center.x + x, center.y + y) val boardSquare = - engine.getEntitiesFor( - Family.all(PositionComponent::class.java, FowComponent::class.java).get(), - ).firstOrNull { + EcsEngine.getEntitiesFor(boardFamily).firstOrNull { PositionComponent.mapper.get(it).position == currentPosition }