Unify save states

This commit is contained in:
2021-10-19 14:20:56 +01:00
parent 4c3a18d7d7
commit 1f8b4b8565
6 changed files with 25 additions and 18 deletions
+4 -3
View File
@@ -78,10 +78,8 @@ data World = World
, _pathGraph :: ~(Gr Point2 Float)
, _pathGraphP :: ~[(Point2,Point2)]
, _pathPoints :: ~(IM.IntMap (IM.IntMap [(Int,Point2)]))
, _storedLevel :: Maybe World
, _quicksaveLevel :: Maybe World
, _savedWorlds :: M.Map SaveSlot World
, _menuLayers :: [ScreenLayer]
-- , _worldState :: M.Map WorldState Bool
, _worldTriggers :: S.Set WorldTrigger
, _carteDisplay :: !Bool
, _carteCenter :: !Point2
@@ -103,6 +101,9 @@ data World = World
, _frameClock :: Int
, _worldBounds :: Bounds
}
data SaveSlot = QuicksaveSlot | LevelStartSlot
deriving (Eq,Ord)
data OptionScreenFlag = NormalOptions | GameOverOptions
data ScreenLayer
= OptionScreen
+1 -3
View File
@@ -52,10 +52,8 @@ defaultWorld = World
, _playingSounds = M.empty
, _corpses = Zone IM.empty
, _decorations = IM.empty
, _storedLevel = Nothing
, _quicksaveLevel = Nothing
, _savedWorlds = M.empty
, _menuLayers = []
-- , _worldState = M.empty
, _worldTriggers = S.empty
, _clickMousePos = V2 0 0
, _pathGraph = Data.Graph.Inductive.Graph.empty
+1 -1
View File
@@ -33,7 +33,7 @@ handleKeyboardEvent kev w = case keyboardEventKeyMotion kev of
handlePressedKey :: Bool -> Scancode -> World -> Maybe World
handlePressedKey True _ w = Just w
handlePressedKey _ ScancodeF5 w = Just $ doQuicksave w
handlePressedKey _ ScancodeF9 w = Just $ maybe w doQuicksave (_quicksaveLevel w)
handlePressedKey _ ScancodeF9 w = Just $ loadSaveSlot QuicksaveSlot w
handlePressedKey _ ScancodeSemicolon w = Just $ gotoTerminal w
handlePressedKey _ scode w
| null (_menuLayers w) = handlePressedKeyInGame scode w
+1 -2
View File
@@ -50,13 +50,12 @@ initialWorld = defaultWorld
, _mousePos = V2 0 0
, _testString = testStringInit
, _yourID = 0
, _worldEvents = storeLevel . soundStart BackgroundSound (V2 0 0) foamSprayFadeOutS Nothing
, _worldEvents = saveWorldInEmptySlot LevelStartSlot . soundStart BackgroundSound (V2 0 0) foamSprayFadeOutS Nothing
. foldr ((.) . makeStartCloudAt) id [V3 x y 5 | x <- [-5,-3..5] , y <- [-5,-3..5]]
, _pressPlates = IM.empty
, _buttons = IM.empty
, _toPlaySounds = M.empty
, _decorations = IM.empty
, _storedLevel = Nothing
, _menuLayers = [TerminalScreen 300 rezText']
}
testStringInit :: World -> [String]
+1 -1
View File
@@ -151,7 +151,7 @@ pauseMenu = OptionScreen
pauseMenuOptions :: [MenuOption]
pauseMenuOptions =
[ Toggle ScancodeN startNewGame (const "NEW LEVEL")
, Toggle ScancodeR (Just . loadStoredLevel) (const "RESTART")
, Toggle ScancodeR (Just . loadSaveSlot LevelStartSlot) (const "RESTART")
, Toggle ScancodeO (pushScreen optionMenu ) (const "OPTIONS")
, Toggle ScancodeC (pushScreen displayControls) (const "CONTROLS")
, InvisibleToggle ScancodeEscape (const Nothing)
+17 -8
View File
@@ -1,25 +1,34 @@
module Dodge.Save
( storeLevel
, loadStoredLevel
( saveWorldInEmptySlot
, loadSaveSlot
, doQuicksave
)
where
import Dodge.Data
import qualified Data.Map.Strict as M
import Data.Maybe
import Control.Lens
import qualified Data.Set as S
storeLevel :: World -> World
storeLevel w = case _storedLevel w of
Nothing -> w & storedLevel ?~ clearKeys w
clearSaves :: World -> World
clearSaves = savedWorlds .~ M.empty
saveWorldInEmptySlot :: SaveSlot -> World -> World
saveWorldInEmptySlot slot w = case M.lookup slot $ _savedWorlds w of
Nothing -> w & savedWorlds %~ M.insert slot (clearKeys w)
_ -> w
loadStoredLevel :: World -> World
loadStoredLevel w = fromMaybe w $ _storedLevel w <&> quicksaveLevel .~ _quicksaveLevel w
saveWorldInSlot :: SaveSlot -> World -> World
saveWorldInSlot slot w = w & savedWorlds %~ M.insert slot (clearSaves $ clearKeys w)
loadSaveSlot :: SaveSlot -> World -> World
loadSaveSlot slot w = fromMaybe w
$ M.lookup slot (_savedWorlds w)
<&> savedWorlds .~ _savedWorlds w -- keep saves from current point, not from save point
doQuicksave :: World -> World
doQuicksave w = w & quicksaveLevel ?~ clearKeys w
doQuicksave = saveWorldInSlot QuicksaveSlot
clearKeys :: World -> World
clearKeys = (keys .~ S.empty) . (mouseButtons .~ S.empty)