45 lines
No EOL
1.9 KiB
Haskell
45 lines
No EOL
1.9 KiB
Haskell
module RPGEngine.Input.LevelSelection
|
|
( handleInputLevelSelection
|
|
) where
|
|
|
|
import RPGEngine.Input.Core (InputHandler, composeInputHandlers, handleKey, ListSelector (..))
|
|
|
|
import RPGEngine.Data (Game (..), State (..), Direction (..))
|
|
import Graphics.Gloss.Interface.IO.Game (Key(..))
|
|
import Graphics.Gloss.Interface.IO.Interact (SpecialKey(..), KeyState(..))
|
|
import RPGEngine.Config (levelFolder)
|
|
import RPGEngine.Parse (parse)
|
|
|
|
------------------------------ Exported ------------------------------
|
|
|
|
handleInputLevelSelection :: InputHandler Game
|
|
handleInputLevelSelection = composeInputHandlers [
|
|
handleKey (SpecialKey KeySpace) Down selectLevel,
|
|
|
|
handleKey (SpecialKey KeyUp) Down $ moveSelector North,
|
|
handleKey (SpecialKey KeyDown) Down $ moveSelector South
|
|
]
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
-- Select a level and load it in
|
|
selectLevel :: Game -> Game
|
|
selectLevel game@Game{ state = LevelSelection list selector } = newGame
|
|
where newGame = parse $ levelFolder ++ (list !! index)
|
|
index = selection selector
|
|
selectLevel g = g{ state = Error "Something went wrong while selecting a level"}
|
|
|
|
-- TODO Lift this code from ActionSelection
|
|
-- Move the selector either up or down
|
|
moveSelector :: Direction -> Game -> Game
|
|
moveSelector dir game@Game{ state = state@(LevelSelection list selector) } = newGame
|
|
where newGame = game{ state = newState }
|
|
newState = state{ selector = newSelector }
|
|
newSelector | constraint = selector{ selection = newSelection }
|
|
| otherwise = selector
|
|
constraint = 0 <= newSelection && newSelection < length list
|
|
newSelection = selection selector + diff
|
|
diff | dir == North = -1
|
|
| dir == South = 1
|
|
| otherwise = 0
|
|
moveSelector _ g = g{ state = Error "Something went wrong while moving the selector up or down"} |