-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract game engine sublibrary (#1714)
Towards #1043 Extracts `Swarm.Game.*` modules to their own sublibrary. There was already pretty good separation along this boundary; just had to move three functions into a new module `Swarm.Util.Content`.
- Loading branch information
Showing
90 changed files
with
300 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
-- | | ||
-- SPDX-License-Identifier: BSD-3-Clause | ||
-- | ||
-- Utilities for accessing content of the world, | ||
-- by single cells or in bulk for rendering. | ||
module Swarm.Util.Content where | ||
|
||
import Control.Applicative ((<|>)) | ||
import Control.Lens ((^.)) | ||
import Data.Map qualified as M | ||
import Data.Text qualified as T | ||
import Swarm.Game.Display | ||
import Swarm.Game.Entity.Cosmetic | ||
import Swarm.Game.Entity.Cosmetic.Assignment (terrainAttributes) | ||
import Swarm.Game.Scenario.Topography.Area qualified as EA | ||
import Swarm.Game.Scenario.Topography.Cell (PCell (..)) | ||
import Swarm.Game.Scenario.Topography.EntityFacade | ||
import Swarm.Game.Terrain (TerrainType, getTerrainWord) | ||
import Swarm.Game.Universe | ||
import Swarm.Game.World | ||
import Swarm.Util.Erasable (erasableToMaybe, maybeToErasable) | ||
|
||
-- | Get the terrain and entity at a single cell | ||
getContentAt :: MultiWorld Int e -> Cosmic Coords -> (TerrainType, Maybe e) | ||
getContentAt w coords = (underlyingCellTerrain, underlyingCellEntity) | ||
where | ||
underlyingCellEntity = lookupCosmicEntity coords w | ||
underlyingCellTerrain = lookupCosmicTerrain coords w | ||
|
||
-- * Rendering | ||
|
||
-- | Get a rectangle of cells for rendering | ||
getMapRectangle :: | ||
(d -> e) -> | ||
(Coords -> (TerrainType, Maybe d)) -> | ||
BoundsRectangle -> | ||
EA.Grid (PCell e) | ||
getMapRectangle paintTransform contentFunc coords = | ||
EA.Grid $ map renderRow [yTop .. yBottom] | ||
where | ||
(Coords (yTop, xLeft), Coords (yBottom, xRight)) = coords | ||
|
||
drawCell f rowIndex colIndex = | ||
Cell | ||
terrain | ||
(f <$> maybeToErasable erasableEntity) | ||
[] | ||
where | ||
(terrain, erasableEntity) = contentFunc $ Coords (rowIndex, colIndex) | ||
|
||
renderRow rowIndex = map (drawCell paintTransform rowIndex) [xLeft .. xRight] | ||
|
||
-- | Get the color used to render a single cell | ||
getTerrainEntityColor :: | ||
M.Map WorldAttr PreservableColor -> | ||
PCell EntityFacade -> | ||
Maybe PreservableColor | ||
getTerrainEntityColor aMap (Cell terr cellEnt _) = | ||
(entityColor =<< erasableToMaybe cellEnt) <|> terrainFallback | ||
where | ||
terrainFallback = M.lookup (TerrainAttr $ T.unpack $ getTerrainWord terr) terrainAttributes | ||
entityColor (EntityFacade _ d) = case d ^. displayAttr of | ||
AWorld n -> M.lookup (WorldAttr $ T.unpack n) aMap | ||
_ -> Nothing |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.