Apply side effects to universe not world

This commit is contained in:
2021-11-28 11:43:17 +00:00
parent 24480bbe52
commit d6fb6adc66
4 changed files with 52 additions and 28 deletions
+22 -14
View File
@@ -47,21 +47,26 @@ main = do
theUpdateStep theUpdateStep
(flip handleEvent) (flip handleEvent)
theCleanup :: World -> IO () theCleanup :: Universe -> IO ()
theCleanup w = SDL.cursorVisible $= True >> cleanUpPreload (_preloadData w) theCleanup uv = let w = _uvWorld uv
in SDL.cursorVisible $= True >> cleanUpPreload (_preloadData w)
firstWorldLoad :: Configuration -> IO World firstWorldLoad :: Configuration -> IO Universe
firstWorldLoad theConfig = do firstWorldLoad theConfig = do
SDL.cursorVisible $= False SDL.cursorVisible $= False
theKeyConfig <- loadKeyConfig theKeyConfig <- loadKeyConfig
pdata <- doPreload >>= applyWorldConfig theConfig pdata <- doPreload >>= applyWorldConfig theConfig
w <- generateWorldFromSeed 0 w <- generateWorldFromSeed 0
return $ w & preloadData .~ pdata return $ Universe ( w & preloadData .~ pdata
& keyConfig .~ theKeyConfig & keyConfig .~ theKeyConfig
& config .~ theConfig & config .~ theConfig
)
theUpdateStep :: World -> IO World theUpdateStep :: Universe -> IO Universe
theUpdateStep = doSideEffects <=< updateRenderSplit theUpdateStep = doSideEffects <=< traverseOf uvWorld theUpdateStep'
theUpdateStep' :: World -> IO World
theUpdateStep' = updateRenderSplit
updateRenderSplit :: World -> IO World updateRenderSplit :: World -> IO World
updateRenderSplit w = do updateRenderSplit w = do
@@ -74,12 +79,13 @@ playSoundUnlessRewinding w
| _rewinding w = return M.empty | _rewinding w = return M.empty
| otherwise = playSoundAndUpdate (_soundData $ _preloadData w) (_playingSounds w) (_toPlaySounds w) | otherwise = playSoundAndUpdate (_soundData $ _preloadData w) (_playingSounds w) (_toPlaySounds w)
doSideEffects :: World -> IO World doSideEffects :: Universe -> IO Universe
doSideEffects w = do doSideEffects u = do
let w = _uvWorld u
let preData = _preloadData w let preData = _preloadData w
--newPlayingSounds <- playSoundAndUpdate (_soundData preData) (_playingSounds w) (_toPlaySounds w) --newPlayingSounds <- playSoundAndUpdate (_soundData preData) (_playingSounds w) (_toPlaySounds w)
newPlayingSounds <- playSoundUnlessRewinding w newPlayingSounds <- playSoundUnlessRewinding w
w' <- _sideEffects w w u' <- _sideEffects w u
endTicks <- SDL.ticks endTicks <- SDL.ticks
let lastFrameTicks = _frameTimer preData let lastFrameTicks = _frameTimer preData
when (_debug_seconds_frame $ _config w) $ void $ renderFoldable when (_debug_seconds_frame $ _config w) $ void $ renderFoldable
@@ -88,11 +94,13 @@ doSideEffects w = do
. translate (-0.5) (-0.8) . scale 0.0005 0.0005 . translate (-0.5) (-0.8) . scale 0.0005 0.0005
. text $ "ms/frame " ++ show (endTicks - lastFrameTicks) . text $ "ms/frame " ++ show (endTicks - lastFrameTicks)
) )
return $ w' & preloadData . frameTimer .~ endTicks return $ u'
& playingSounds .~ newPlayingSounds & uvWorld . preloadData . frameTimer .~ endTicks
& toPlaySounds .~ M.empty & uvWorld . playingSounds .~ newPlayingSounds
& sideEffects .~ return & uvWorld . toPlaySounds .~ M.empty
& rewinding .~ False -- this is probably not the best place for this & uvWorld . sideEffects .~ return
& uvWorld . rewinding .~ False -- this is probably not the best place for this
doPreload :: IO PreloadData doPreload :: IO PreloadData
doPreload = do doPreload = do
+5 -1
View File
@@ -52,6 +52,9 @@ import qualified Data.Map.Strict as M
import SDL (Scancode, MouseButton) import SDL (Scancode, MouseButton)
import Data.Monoid import Data.Monoid
type CRUpdate = Creature -> World -> (Endo World, Maybe Creature) type CRUpdate = Creature -> World -> (Endo World, Maybe Creature)
data Universe = Universe
{ _uvWorld :: World
}
data World = World data World = World
{ _keys :: !(S.Set Scancode) { _keys :: !(S.Set Scancode)
, _mouseButtons :: !(S.Set MouseButton) , _mouseButtons :: !(S.Set MouseButton)
@@ -105,7 +108,7 @@ data World = World
, _closeActiveObjects :: [Either FloorItem Button] , _closeActiveObjects :: [Either FloorItem Button]
, _seenLocations :: IM.IntMap (World -> Point2,String) , _seenLocations :: IM.IntMap (World -> Point2,String)
, _selLocation :: Int , _selLocation :: Int
, _sideEffects :: World -> IO World , _sideEffects :: Universe -> IO Universe
, _inventoryMode :: InventoryMode , _inventoryMode :: InventoryMode
, _distortions :: [Distortion] , _distortions :: [Distortion]
, _worldBounds :: Bounds , _worldBounds :: Bounds
@@ -900,3 +903,4 @@ makeLenses ''ItemDimension
makeLenses ''Vocalization makeLenses ''Vocalization
makeLenses ''UseDelay makeLenses ''UseDelay
makeLenses ''AimParams makeLenses ''AimParams
makeLenses ''Universe
+15 -8
View File
@@ -18,11 +18,12 @@ import Dodge.Event.Keyboard
import Dodge.Data import Dodge.Data
import Dodge.Base import Dodge.Base
import Dodge.Base.Window import Dodge.Base.Window
import Dodge.PreloadData
--import Dodge.Creature.Action --import Dodge.Creature.Action
import Dodge.SoundLogic import Dodge.SoundLogic
import Dodge.Inventory import Dodge.Inventory
--import Geometry --import Geometry
import Preload.Update --import Preload.Update
import qualified IntMapHelp as IM import qualified IntMapHelp as IM
import Control.Lens import Control.Lens
@@ -33,8 +34,14 @@ import Control.Lens
import qualified Data.Set as S import qualified Data.Set as S
import SDL import SDL
handleEvent :: Event -> World -> Maybe World
handleEvent e = case eventPayload e of handleEvent :: Event -> Universe -> Maybe Universe
handleEvent e uv = case handleEvent' e (_uvWorld uv) of
Nothing -> Nothing
Just w -> Just $ uv & uvWorld .~ w
handleEvent' :: Event -> World -> Maybe World
handleEvent' e = case eventPayload e of
KeyboardEvent kev -> handleKeyboardEvent kev KeyboardEvent kev -> handleKeyboardEvent kev
MouseMotionEvent mmev -> handleMouseMotionEvent mmev MouseMotionEvent mmev -> handleMouseMotionEvent mmev
MouseButtonEvent mbev -> handleMouseButtonEvent mbev MouseButtonEvent mbev -> handleMouseButtonEvent mbev
@@ -70,13 +77,13 @@ handleResizeEvent :: WindowSizeChangedEventData -> World -> Maybe World
handleResizeEvent sev w = Just handleResizeEvent sev w = Just
. set (config . windowX) (fromIntegral x) . set (config . windowX) (fromIntegral x)
. set (config . windowY) (fromIntegral y) . set (config . windowY) (fromIntegral y)
$ over sideEffects up $ over sideEffects (sideEffectUpdatePreload divRes x y)
w w
where where
up :: (World -> IO World) -> World -> IO World -- up :: (World -> IO World) -> World -> IO World
up f w' = do -- up f w' = do
pdata <- pdataResizeUpdate (x `div` divRes) (y `div` divRes) x y (_preloadData w') -- pdata <- pdataResizeUpdate (x `div` divRes) (y `div` divRes) x y (_preloadData w')
f $ w' & preloadData .~ pdata -- f $ w' & preloadData .~ pdata
x = fromIntegral x' x = fromIntegral x'
y = fromIntegral y' y = fromIntegral y'
V2 x' y' = windowSizeChangedEventSize sev V2 x' y' = windowSizeChangedEventSize sev
+10 -5
View File
@@ -159,11 +159,16 @@ pauseMenuOptions =
startNewGame :: World -> Maybe World startNewGame :: World -> Maybe World
startNewGame w = Just $ w startNewGame w = Just $ w
& menuLayers .~ [WaitScreen (const "GENERATING...") 1] & menuLayers .~ [WaitScreen (const "GENERATING...") 1]
& sideEffects .~ const (generateWorldFromSeed i <&> keyConfig .~ _keyConfig w <&> config .~ _config w & sideEffects .~ uvWorld (uvWorldSideEffects i w)
<&> preloadData .~ _preloadData w) -- this kills save games etc...
where where
i = fst $ random (_randGen w) i = fst $ random (_randGen w)
uvWorldSideEffects :: Int -> World -> b -> IO World
uvWorldSideEffects i w = const (generateWorldFromSeed i <&> keyConfig .~ _keyConfig w <&> config .~ _config w
<&> preloadData .~ _preloadData w)
-- this kills save games etc...
-- | hacky -- | hacky
scodeToChar :: Scancode -> Char scodeToChar :: Scancode -> Char
scodeToChar = toEnum . (+ 61) . fromIntegral . toNumber scodeToChar = toEnum . (+ 61) . fromIntegral . toNumber
@@ -176,10 +181,10 @@ updateFramebufferSize w = w & sideEffects %~ up
where where
(x,y) = (round $ getWindowX w, round $ getWindowY w) (x,y) = (round $ getWindowX w, round $ getWindowY w)
divRes = w ^. config . resolution_factor divRes = w ^. config . resolution_factor
up :: (World -> IO World) -> World -> IO World up :: (Universe -> IO Universe) -> Universe -> IO Universe
up f w' = do up f w' = do
pdata <- pdataResizeUpdate (x `div` divRes) (y `div` divRes) x y (_preloadData w') pdata <- pdataResizeUpdate (x `div` divRes) (y `div` divRes) x y (_preloadData (_uvWorld w'))
f $ w' & preloadData .~ pdata f $ w' & uvWorld . preloadData .~ pdata
--levelMenu :: Int -> ScreenLayer --levelMenu :: Int -> ScreenLayer
--levelMenu x = OptionScreen --levelMenu x = OptionScreen