diff --git a/lib/Input.hs b/lib/Input.hs deleted file mode 100644 index 9f63d99..0000000 --- a/lib/Input.hs +++ /dev/null @@ -1,10 +0,0 @@ --- Go to the next stage of the Game --- setNextState :: Game -> Game --- setNextState game = game{ state = newState } --- where newState = nextState $ state game - --- -- Get the next state based on the current state --- nextState :: State -> State --- nextState Menu {} = defaultLvlSelect --- nextState Pause {} = Playing --- nextState _ = Menu diff --git a/lib/RPGEngine.hs b/lib/RPGEngine.hs index a2855cf..db19c09 100644 --- a/lib/RPGEngine.hs +++ b/lib/RPGEngine.hs @@ -6,8 +6,11 @@ module RPGEngine ) where import RPGEngine.Config ( bgColor, winDimensions, winOffsets ) -import RPGEngine.Render ( initWindow, render, initGame ) +import RPGEngine.Render ( initWindow, render ) import RPGEngine.Input ( handleAllInput ) +import RPGEngine.Input.Playing ( checkPlaying, spawnPlayer ) +import RPGEngine.Data (Game (..), State (..), Layout, Level (..), Physical (..)) +import RPGEngine.Data.Default (defaultLevel, defaultPlayer) import Graphics.Gloss ( play ) @@ -19,4 +22,59 @@ playRPGEngine :: String -> Int -> IO() playRPGEngine title fps = do play window bgColor fps initGame render handleAllInput step where window = initWindow title winDimensions winOffsets - step _ g = g -- TODO Do something with step? Check health etc. \ No newline at end of file + step _ = checkPlaying -- TODO Do something with step? Check health etc. + +-- TODO revert this +-- Initialize the game +initGame :: Game +-- initGame = Game { +-- state = Menu{ base = StateBase{ +-- renderer = renderMenu, +-- inputHandler = handleInputMenu +-- }} +-- } +initGame = Game{ + state = initState +} + where initState = Playing{ + levels = [defaultLevel, otherLevel], + count = 0, + level = defaultLevel, + player = spawnPlayer defaultLevel defaultPlayer, + restart = initState + } + +-- TODO remove this +otherLayout :: Layout +otherLayout = [ + [Blocked, Blocked, Blocked], + [Blocked, Entrance, Blocked], + [Blocked, Walkable, Blocked], + [Blocked, Exit, Blocked], + [Blocked, Blocked, Blocked] + ] + +-- TODO remove this +otherLevel :: Level +otherLevel = Level { + layout = otherLayout, + index = [ + (0, 0, Blocked), + (1, 0, Blocked), + (2, 0, Blocked), + (0, 1, Blocked), + (1, 1, Entrance), + (2, 1, Blocked), + (0, 2, Blocked), + (1, 2, Walkable), + (2, 2, Blocked), + (0, 3, Blocked), + (1, 3, Exit), + (2, 3, Blocked), + (0, 4, Blocked), + (1, 4, Blocked), + (2, 4, Blocked) + ], + items = [], + entities = [] +} \ No newline at end of file diff --git a/lib/RPGEngine/Data.hs b/lib/RPGEngine/Data.hs index a47cded..2c6cfa1 100644 --- a/lib/RPGEngine/Data.hs +++ b/lib/RPGEngine/Data.hs @@ -16,32 +16,23 @@ data Game = Game { ------------------------------- State -------------------------------- --- Code reusability -data StateBase = StateBase { - renderer :: Renderer State, - inputHandler :: InputHandler Game -} - -- Main menu -data State = Menu { base :: StateBase } +data State = Menu -- Select the level you want to play - | LevelSelection { base :: StateBase, - levelList :: [FilePath], + | LevelSelection { levelList :: [FilePath], selector :: ListSelector } -- Playing a level - | Playing { base :: StateBase, - levels :: [Level], + | Playing { levels :: [Level], + count :: Int, level :: Level, player :: Player, restart :: State } -- Paused while playing a level - | Paused { base :: StateBase, - continue :: State } + | Paused { continue :: State } -- Won a level - | Win { base :: StateBase } + | Win -- Lost a level - | Lose { base :: StateBase, - restart :: State } + | Lose { restart :: State } ------------------------------- Level -------------------------------- diff --git a/lib/RPGEngine/Data/Default.hs b/lib/RPGEngine/Data/Default.hs index 7129a12..f877f7f 100644 --- a/lib/RPGEngine/Data/Default.hs +++ b/lib/RPGEngine/Data/Default.hs @@ -1,11 +1,8 @@ module RPGEngine.Data.Default -- Everything is exported where -import RPGEngine.Data (Entity (..), Game (..), Item (..), Layout, Player (..), Level (..), StateBase (..), State (..), Physical (..), Direction (..)) +import RPGEngine.Data (Entity (..), Game (..), Item (..), Layout, Player (..), Level (..), State (..), Physical (..), Direction (..)) import RPGEngine.Input.Core (ListSelector(..)) -import RPGEngine.Render.LevelSelection (renderLevelSelection) -import RPGEngine.Input.Playing (spawnPlayer) -import RPGEngine.Render.Menu (renderMenu) ------------------------------ Defaults ------------------------------ @@ -36,9 +33,9 @@ defaultItem = Item { defaultLayout :: Layout defaultLayout = [ - [Blocked, Blocked, Blocked], - [Blocked, Entrance, Blocked], - [Blocked, Blocked, Blocked] + [Blocked, Blocked, Blocked, Blocked, Blocked], + [Blocked, Entrance, Walkable, Exit, Blocked], + [Blocked, Blocked, Blocked, Blocked, Blocked] ] defaultLevel :: Level @@ -52,8 +49,14 @@ defaultLevel = Level { (1, 1, Entrance), (1, 2, Blocked), (2, 0, Blocked), - (2, 1, Blocked), - (2, 2, Blocked) + (2, 1, Walkable), + (2, 2, Blocked), + (3, 0, Blocked), + (3, 1, Exit), + (3, 2, Blocked), + (4, 0, Blocked), + (4, 1, Blocked), + (4, 2, Blocked) ], items = [], entities = [] @@ -64,4 +67,10 @@ defaultPlayer = Player { playerHp = Prelude.Nothing, -- Compares to infinity inventory = [], position = (0, 0) +} + +defaultSelector :: ListSelector +defaultSelector = ListSelector { + selection = 0, + selected = False } \ No newline at end of file diff --git a/lib/RPGEngine/Data/Game.hs b/lib/RPGEngine/Data/Game.hs index 37fe826..bd1cae4 100644 --- a/lib/RPGEngine/Data/Game.hs +++ b/lib/RPGEngine/Data/Game.hs @@ -1,5 +1,7 @@ module RPGEngine.Data.Game ( isLegalMove +, isPlayerAtExit +, isPlayerDead ) where import RPGEngine.Data @@ -14,9 +16,21 @@ import RPGEngine.Data.Level (findAt, directionOffsets) -- Check if a move is legal by checking what is located at the new position. isLegalMove :: Direction -> Game -> Bool -isLegalMove dir g@Game{ state = Playing { level = lvl, player = p@Player{ position = (x, y) }}} = legality +isLegalMove dir g@Game{ state = Playing{ level = lvl, player = p@Player{ position = (x, y) }}} = legality where legality = physical `elem` [Walkable, Entrance, Exit] physical = findAt newPos lvl newPos = (x + xD, y + yD) (xD, yD) = directionOffsets dir -isLegalMove _ _ = False \ No newline at end of file +isLegalMove _ _ = False + +-- Check if a player is standing on an exit +isPlayerAtExit :: Game -> Bool +isPlayerAtExit g@Game{ state = Playing{ player = player, level = level }} = atExit + where playerPos = position player + atPos = findAt playerPos level + atExit = atPos == Exit +isPlayerAtExit _ = False + +-- Check if the players health is <= 0, which means the player is dead. +isPlayerDead :: Game -> Bool +isPlayerDead _ = False \ No newline at end of file diff --git a/lib/RPGEngine/Data/Level.hs b/lib/RPGEngine/Data/Level.hs index 86ef84d..f41a0c1 100644 --- a/lib/RPGEngine/Data/Level.hs +++ b/lib/RPGEngine/Data/Level.hs @@ -5,7 +5,7 @@ where import GHC.IO (unsafePerformIO) import System.Directory (getDirectoryContents) import RPGEngine.Input.Core (ListSelector(..)) -import RPGEngine.Data (Level (..), Physical (..), Direction (..), Entity (..), Game (..), Item (..), Player (..), StateBase (..), State (..), X, Y, Layout) +import RPGEngine.Data (Level (..), Physical (..), Direction (..), Entity (..), Game (..), Item (..), Player (..), State (..), X, Y, Layout) import RPGEngine.Config (levelFolder) ------------------------------ Exported ------------------------------ diff --git a/lib/RPGEngine/Input.hs b/lib/RPGEngine/Input.hs index 485affb..1314c9e 100644 --- a/lib/RPGEngine/Input.hs +++ b/lib/RPGEngine/Input.hs @@ -4,12 +4,23 @@ module RPGEngine.Input ( handleAllInput ) where -import RPGEngine.Input.Core -import RPGEngine.Data +import RPGEngine.Input.Core ( InputHandler, composeInputHandlers, handleAnyKey ) + +import RPGEngine.Data ( Game(..), State(..) ) +import RPGEngine.Input.Menu ( handleInputMenu ) +import RPGEngine.Input.LevelSelection (handleInputLevelSelection) +import RPGEngine.Input.Playing ( handleInputPlaying ) +import RPGEngine.Input.Paused ( handleInputPaused ) +import RPGEngine.Input.Win ( handleInputWin ) +import RPGEngine.Input.Lose ( handleInputLose ) ------------------------------ Exported ------------------------------ -- Handle all input of all states of the game. handleAllInput :: InputHandler Game -handleAllInput ev g@Game{ state = state } = handleInput ev g - where handleInput = inputHandler $ base state \ No newline at end of file +handleAllInput ev g@Game{ state = Menu } = handleInputMenu ev g +handleAllInput ev g@Game{ state = LevelSelection{} } = handleInputLevelSelection ev g +handleAllInput ev g@Game{ state = Playing{} } = handleInputPlaying ev g +handleAllInput ev g@Game{ state = Paused{} } = handleInputPaused ev g +handleAllInput ev g@Game{ state = Win } = handleInputWin ev g +handleAllInput ev g@Game{ state = Lose{} } = handleInputLose ev g \ No newline at end of file diff --git a/lib/RPGEngine/Input/Core.hs b/lib/RPGEngine/Input/Core.hs index 9044c1d..780a87b 100644 --- a/lib/RPGEngine/Input/Core.hs +++ b/lib/RPGEngine/Input/Core.hs @@ -6,6 +6,8 @@ module RPGEngine.Input.Core , handle , handleKey , handleAnyKey + +, SpecialKey(..) ) where import Graphics.Gloss.Interface.Pure.Game diff --git a/lib/RPGEngine/Input/LevelSelection.hs b/lib/RPGEngine/Input/LevelSelection.hs index d2f3578..33bdce8 100644 --- a/lib/RPGEngine/Input/LevelSelection.hs +++ b/lib/RPGEngine/Input/LevelSelection.hs @@ -1,18 +1,13 @@ -module RPGEngine.Input.LevelSelection +module RPGEngine.Input.LevelSelection ( handleInputLevelSelection ) where -import RPGEngine.Input.Core - ( composeInputHandlers, handleKey, InputHandler, ListSelector (..) ) +import RPGEngine.Input.Core (InputHandler, composeInputHandlers, handleKey, ListSelector (..)) -import RPGEngine.Config ( levelFolder ) -import RPGEngine.Data ( Game (..), Direction (..), State (..), StateBase (..) ) - -import Graphics.Gloss.Interface.IO.Game - ( Key(SpecialKey), SpecialKey(KeySpace) ) +import RPGEngine.Data (Game (..), State (..), Direction (..)) +import Graphics.Gloss.Interface.IO.Game (Key(..)) import Graphics.Gloss.Interface.IO.Interact (SpecialKey(..)) -import RPGEngine.Render.Playing (renderPlaying) -import RPGEngine.Input.Playing (handleInputPlaying) +import RPGEngine.Config (levelFolder) import RPGEngine.Parse (parse) ------------------------------ Exported ------------------------------ @@ -46,4 +41,4 @@ moveSelector dir game@Game{ state = state@LevelSelection{ levelList = list, sele diff | dir == North = -1 | dir == South = 1 | otherwise = 0 -moveSelector _ g = g +moveSelector _ g = g \ No newline at end of file diff --git a/lib/RPGEngine/Input/Lose.hs b/lib/RPGEngine/Input/Lose.hs index f9c6d0e..007a25f 100644 --- a/lib/RPGEngine/Input/Lose.hs +++ b/lib/RPGEngine/Input/Lose.hs @@ -2,12 +2,11 @@ module RPGEngine.Input.Lose ( handleInputLose ) where -import RPGEngine.Input.Core ( InputHandler ) +import RPGEngine.Input.Core (InputHandler) -import RPGEngine.Data ( Game ) +import RPGEngine.Data (Game) ------------------------------ Exported ------------------------------ --- TODO handleInputLose :: InputHandler Game handleInputLose = undefined \ No newline at end of file diff --git a/lib/RPGEngine/Input/Menu.hs b/lib/RPGEngine/Input/Menu.hs index 6903d0d..9dd27d8 100644 --- a/lib/RPGEngine/Input/Menu.hs +++ b/lib/RPGEngine/Input/Menu.hs @@ -2,35 +2,22 @@ module RPGEngine.Input.Menu ( handleInputMenu ) where -import RPGEngine.Input.Core ( InputHandler, composeInputHandlers, handleAnyKey, ListSelector (..) ) - -import RPGEngine.Data ( Game (..), State (..), StateBase (..) ) -import RPGEngine.Render.LevelSelection (renderLevelSelection) -import RPGEngine.Input.LevelSelection (handleInputLevelSelection) +import RPGEngine.Input.Core (InputHandler, composeInputHandlers, handleAnyKey) +import RPGEngine.Data (Game (state), State (..)) +import RPGEngine.Data.Default (defaultSelector) import RPGEngine.Data.Level (getLevelList) ------------------------------ Exported ------------------------------ handleInputMenu :: InputHandler Game handleInputMenu = composeInputHandlers [ - handleAnyKey selectLevel + handleAnyKey (\game -> game{ state = startLevelSelection }) ] ---------------------------------------------------------------------- -selectLevel :: Game -> Game -selectLevel g@Game{ state = state } = g{ state = defaultLevelSelection } - -defaultLevelSelection :: State -defaultLevelSelection = LevelSelection { base = base, selector = defaultSelector, levelList = levels } - where base = StateBase { - renderer = renderLevelSelection, - inputHandler = handleInputLevelSelection - } - levels = getLevelList - -defaultSelector :: ListSelector -defaultSelector = ListSelector { - selection = 0, - selected = False +startLevelSelection :: State +startLevelSelection = LevelSelection { + levelList = getLevelList, + selector = defaultSelector } \ No newline at end of file diff --git a/lib/RPGEngine/Input/Paused.hs b/lib/RPGEngine/Input/Paused.hs index 5420296..7ef6c63 100644 --- a/lib/RPGEngine/Input/Paused.hs +++ b/lib/RPGEngine/Input/Paused.hs @@ -1,16 +1,17 @@ -module RPGEngine.Input.Paused +module RPGEngine.Input.Paused ( handleInputPaused ) where -import RPGEngine.Input.Core ( InputHandler, handleAnyKey ) - -import RPGEngine.Data ( Game (..), State (..) ) +import RPGEngine.Input.Core (InputHandler, handleAnyKey) +import RPGEngine.Data (Game (..), State (continue, Paused)) ------------------------------ Exported ------------------------------ handleInputPaused :: InputHandler Game handleInputPaused = handleAnyKey continueGame +---------------------------------------------------------------------- + continueGame :: Game -> Game continueGame g@Game{ state = Paused{ continue = state }} = newGame where newGame = g{ state = state } diff --git a/lib/RPGEngine/Input/Playing.hs b/lib/RPGEngine/Input/Playing.hs index c02a548..e8b6ab0 100644 --- a/lib/RPGEngine/Input/Playing.hs +++ b/lib/RPGEngine/Input/Playing.hs @@ -1,28 +1,17 @@ -module RPGEngine.Input.Playing +module RPGEngine.Input.Playing ( handleInputPlaying +, checkPlaying , spawnPlayer ) where -import RPGEngine.Input.Core - ( composeInputHandlers, handleKey, InputHandler ) +import RPGEngine.Input.Core (InputHandler, handleKey, composeInputHandlers) -import RPGEngine.Data - ( Player(Player, position), - Direction(West, North, East, South), - Physical(Entrance), - Y, - X, - Level(Level, layout), - State(..), - StateBase(StateBase, renderer, inputHandler), - Game(..) ) -import RPGEngine.Data.Level ( findFirstOf, directionOffsets ) -import RPGEngine.Data.Game ( isLegalMove ) -import RPGEngine.Input.Paused ( handleInputPaused ) -import RPGEngine.Render.Paused ( renderPaused ) +import RPGEngine.Data (Game (..), Layout(..), Level(..), Physical(..), Player(..), State(..), X, Y, Direction (..)) +import RPGEngine.Data.Game (isLegalMove, isPlayerDead, isPlayerAtExit) +import RPGEngine.Data.Level (directionOffsets, findFirstOf) +import Data.Maybe (fromJust, isNothing) import Graphics.Gloss.Interface.IO.Game (Key(..)) import Graphics.Gloss.Interface.IO.Interact (SpecialKey(..)) -import Data.Maybe (isNothing, fromJust) ------------------------------ Exported ------------------------------ @@ -43,6 +32,8 @@ handleInputPlaying = composeInputHandlers [ handleKey (Char 'a') $ movePlayer West ] +---------------------------------------------------------------------- + -- Set the initial position of the player in a given level. spawnPlayer :: Level -> Player -> Player spawnPlayer l@Level{ layout = lay } p@Player{ position = prevPos } = p{ position = newPos } @@ -50,15 +41,30 @@ spawnPlayer l@Level{ layout = lay } p@Player{ position = prevPos } = p{ position newPos | isNothing try = prevPos | otherwise = fromJust try ----------------------------------------------------------------------- +checkPlaying :: Game -> Game +checkPlaying g@Game{ state = s@Playing{ restart = restart }} = newGame + where newGame | isPlayerDead g = loseGame + | isPlayerAtExit g = g{ state = goToNextLevel s } + | otherwise = g + loseGame = g{ state = restart } +checkPlaying g = g pauseGame :: Game -> Game pauseGame g@Game{ state = playing@Playing{} } = pausedGame - where pausedGame = g{ state = pausedState } - pausedState = Paused{ base = newBase, continue = playing } - newBase = StateBase { renderer = renderPaused, inputHandler = handleInputPaused } + where pausedGame = g{ state = Paused playing } pauseGame g = g +-- Go to next level if there is a next level, otherwise, initialize win state. +goToNextLevel :: State -> State +goToNextLevel s@Playing{ levels = levels, level = current, count = count, player = player } = nextState + where -- Either the next level or winState + nextState | (count + 1) < length levels = nextLevelState + | otherwise = Win + nextLevelState = s{ level = nextLevel, count = count + 1, player = movedPlayer } + nextLevel = levels !! (count + 1) + movedPlayer = spawnPlayer nextLevel player +goToNextLevel s = s + -- Move a player in a direction if possible. movePlayer :: Direction -> Game -> Game movePlayer dir g@Game{ state = s@Playing{ player = p@Player{ position = (x, y) }}} = newGame @@ -70,12 +76,6 @@ movePlayer dir g@Game{ state = s@Playing{ player = p@Player{ position = (x, y) } (xD, yD) = directionOffsets dir movePlayer _ g = g --- TODO -goToNextLevel :: Game -> Game -goToNextLevel = undefined - ----------------------------------------------------------------------- - -- Map all Physicals onto coordinates putCoords :: Level -> [(X, Y, Physical)] putCoords l@Level{ layout = lay } = concatMap (\(a, bs) -> map (\(b, c) -> (b, a, c)) bs) numberedList diff --git a/lib/RPGEngine/Input/Win.hs b/lib/RPGEngine/Input/Win.hs index 37434ee..3eeaf5d 100644 --- a/lib/RPGEngine/Input/Win.hs +++ b/lib/RPGEngine/Input/Win.hs @@ -1,13 +1,16 @@ -module RPGEngine.Input.Win +module RPGEngine.Input.Win ( handleInputWin ) where -import RPGEngine.Input.Core ( InputHandler ) - -import RPGEngine.Data ( Game ) +import RPGEngine.Input.Core (InputHandler, handleAnyKey) +import RPGEngine.Data (Game (..), State (Menu)) ------------------------------ Exported ------------------------------ --- TODO handleInputWin :: InputHandler Game -handleInputWin = undefined \ No newline at end of file +handleInputWin = handleAnyKey goToMenu + +---------------------------------------------------------------------- + +goToMenu :: Game -> Game +goToMenu g = g{ state = Menu } \ No newline at end of file diff --git a/lib/RPGEngine/Parse.hs b/lib/RPGEngine/Parse.hs index 9b1c976..c12e8da 100644 --- a/lib/RPGEngine/Parse.hs +++ b/lib/RPGEngine/Parse.hs @@ -13,4 +13,6 @@ import RPGEngine.Parse.TextToStructure (structure) parse :: FilePath -> Game parse filename = structureToGame struct where (Right struct) = unsafePerformIO io - io = parseFromFile structure filename \ No newline at end of file + io = parseFromFile structure filename + +tempParse = parseFromFile \ No newline at end of file diff --git a/lib/RPGEngine/Parse/StructureToGame.hs b/lib/RPGEngine/Parse/StructureToGame.hs index ddbcd3d..7e81274 100644 --- a/lib/RPGEngine/Parse/StructureToGame.hs +++ b/lib/RPGEngine/Parse/StructureToGame.hs @@ -11,24 +11,24 @@ import RPGEngine.Data Item(itemId, itemX, itemY, itemName, itemDescription, itemValue, itemActions, useTimes), Level(layout, items, entities), - Game (..), State (..), StateBase (..) ) + Game (..), State (..) ) import RPGEngine.Parse.TextToStructure ( Value(Infinite, Action, Layout, String, Direction, Integer), Key(Tag, ConditionList), Structure(..) ) import RPGEngine.Data.Default (defaultPlayer, defaultLevel, defaultItem, defaultEntity) -import RPGEngine.Render.Playing (renderPlaying) -import RPGEngine.Input.Playing (handleInputPlaying) ------------------------------ Exported ------------------------------ structureToGame :: Structure -> Game -structureToGame (Block [(Entry(Tag "player") playerBlock), (Entry(Tag "levels") levelsBlock)]) = game - where game = Game{ state = newState, levels = newLevels, player = newPlayer } - newState = Playing{ base = playingBase, level = currentLevel } - playingBase = StateBase{ renderer = renderPlaying, inputHandler = handleInputPlaying } - newLevels = structureToLevels levelsBlock - currentLevel = head newLevels +-- structureToGame [Entry(Tag "player") playerBlock, Entry(Tag "levels") levelsBlock] = game +structureToGame (Entry (Tag "player") playerBlock) = game + where game = Game{ state = newState } + newState = Playing{ levels = newLevels, level = currentLevel, player = newPlayer, restart = newState } + -- newLevels = structureToLevels levelsBlock + -- currentLevel = head newLevels + newLevels = [defaultLevel] + currentLevel = defaultLevel newPlayer = structureToPlayer playerBlock ------------------------------- Player ------------------------------- diff --git a/lib/RPGEngine/Parse/TextToStructure.hs b/lib/RPGEngine/Parse/TextToStructure.hs index 6f1b060..dc76003 100644 --- a/lib/RPGEngine/Parse/TextToStructure.hs +++ b/lib/RPGEngine/Parse/TextToStructure.hs @@ -27,9 +27,9 @@ import Text.Parsec.String ( Parser ) -- See documentation for more details, only a short description is -- provided here. data Structure = Block [Structure] - | Entry Key Structure -- Key + Value - | Regular Value -- Regular value, Integer or String or Infinite - deriving (Eq, Show) + | Entry Key Structure -- Key + Value + | Regular Value -- Regular value, Integer or String or Infinite + deriving (Eq, Show) ---------------------------------------------------------------------- diff --git a/lib/RPGEngine/Render.hs b/lib/RPGEngine/Render.hs index 0699d29..907f3e5 100644 --- a/lib/RPGEngine/Render.hs +++ b/lib/RPGEngine/Render.hs @@ -2,20 +2,22 @@ -- submodules. module RPGEngine.Render ( initWindow -, initGame , render ) where import RPGEngine.Render.Core ( Renderer(..) ) -import RPGEngine.Data ( State(..), Game(..), StateBase(..) ) -import Graphics.Gloss ( Display ) -import Graphics.Gloss.Data.Display ( Display(InWindow) ) -import Graphics.Gloss.Data.Picture (Picture) -import RPGEngine.Data.Default (defaultLevel, defaultPlayer) -import RPGEngine.Input.Playing (spawnPlayer) -import RPGEngine.Render.Menu (renderMenu) -import RPGEngine.Input.Menu (handleInputMenu) +import RPGEngine.Data (Game(..), State (..)) +import RPGEngine.Render.Menu( renderMenu ) +import RPGEngine.Render.LevelSelection ( renderLevelSelection ) +import RPGEngine.Render.Playing ( renderPlaying ) +import RPGEngine.Render.Paused ( renderPaused ) +import RPGEngine.Render.Win ( renderWin ) +import RPGEngine.Render.Lose ( renderLose ) + +import Graphics.Gloss (Display) +import Graphics.Gloss.Data.Picture (Picture, blank) +import Graphics.Gloss.Data.Display (Display(..)) ---------------------------------------------------------------------- @@ -23,17 +25,12 @@ import RPGEngine.Input.Menu (handleInputMenu) initWindow :: String -> (Int, Int) -> (Int, Int) -> Display initWindow = InWindow --- Initialize the game -initGame :: Game -initGame = Game { - state = Menu{ base = StateBase{ - renderer = renderMenu, - inputHandler = handleInputMenu - }} -} - -- Render all different states render :: Game -> Picture -render g@Game{ state = state } = renderFunc state - where stateBase = base state - renderFunc = renderer stateBase \ No newline at end of file +render Game{ state = s@Menu } = renderMenu s +render Game{ state = s@LevelSelection{} } = renderLevelSelection s +render Game{ state = s@Playing{} } = renderPlaying s +render Game{ state = s@Paused{} } = renderPaused s +render Game{ state = s@Win } = renderWin s +render Game{ state = s@Lose{} } = renderLose s +render _ = blank \ No newline at end of file diff --git a/lib/RPGEngine/Render/LevelSelection.hs b/lib/RPGEngine/Render/LevelSelection.hs index f00e157..f6e0912 100644 --- a/lib/RPGEngine/Render/LevelSelection.hs +++ b/lib/RPGEngine/Render/LevelSelection.hs @@ -2,16 +2,15 @@ module RPGEngine.Render.LevelSelection ( renderLevelSelection ) where -import RPGEngine.Config ( resolution, zoom ) -import RPGEngine.Data ( Game (..), State (..) ) -import RPGEngine.Data.Level ( getLevelList ) -import RPGEngine.Render.Core ( Renderer ) +import RPGEngine.Render.Core (Renderer) -import Graphics.Gloss - ( pictures, text, translate, blank, Picture, color ) -import Graphics.Gloss.Data.Picture (scale) -import RPGEngine.Input.Core (ListSelector (..)) +import RPGEngine.Config (resolution, zoom) +import RPGEngine.Data (State (..)) + +import Graphics.Gloss ( pictures, color, text, translate, blank ) import Graphics.Gloss.Data.Color (red) +import Graphics.Gloss.Data.Picture (scale) +import RPGEngine.Input.Core (ListSelector(..)) ------------------------------ Exported ------------------------------ diff --git a/lib/RPGEngine/Render/Lose.hs b/lib/RPGEngine/Render/Lose.hs index 3154bea..1e6d730 100644 --- a/lib/RPGEngine/Render/Lose.hs +++ b/lib/RPGEngine/Render/Lose.hs @@ -1,14 +1,15 @@ -module RPGEngine.Render.Lose +module RPGEngine.Render.Lose ( renderLose ) where -import RPGEngine.Render.Core ( Renderer ) +import RPGEngine.Render.Core (Renderer) -import RPGEngine.Data ( State ) -import Graphics.Gloss ( text ) +import RPGEngine.Data (State) ----------------------------------------------------------------------- +import Graphics.Gloss (text) + +------------------------------ Exported ------------------------------ -- TODO renderLose :: Renderer State -renderLose _ = text "Win" \ No newline at end of file +renderLose _ = text "You lose" \ No newline at end of file diff --git a/lib/RPGEngine/Render/Menu.hs b/lib/RPGEngine/Render/Menu.hs index 6905814..e5f66d8 100644 --- a/lib/RPGEngine/Render/Menu.hs +++ b/lib/RPGEngine/Render/Menu.hs @@ -2,12 +2,13 @@ module RPGEngine.Render.Menu ( renderMenu ) where -import RPGEngine.Render.Core ( Renderer ) +import RPGEngine.Render.Core (Renderer) + +import RPGEngine.Data (State) -import RPGEngine.Data ( State ) import Graphics.Gloss (text) ----------------------------------------------------------------------- +------------------------------ Exported ------------------------------ -- TODO renderMenu :: Renderer State diff --git a/lib/RPGEngine/Render/Paused.hs b/lib/RPGEngine/Render/Paused.hs index bc54169..6fa3d95 100644 --- a/lib/RPGEngine/Render/Paused.hs +++ b/lib/RPGEngine/Render/Paused.hs @@ -2,13 +2,12 @@ module RPGEngine.Render.Paused ( renderPaused ) where -import RPGEngine.Render.Core ( Renderer, overlay ) +import RPGEngine.Render.Core (Renderer, overlay) -import RPGEngine.Data ( State (..) ) -import Graphics.Gloss ( pictures, scale, text ) -import RPGEngine.Render.Playing ( renderPlaying ) -import Graphics.Gloss.Data.Picture (color) -import Graphics.Gloss.Data.Color (white) +import RPGEngine.Data (State(..)) +import RPGEngine.Render.Playing (renderPlaying) + +import Graphics.Gloss (pictures, white, color, Color(..), text, scale) ------------------------------ Exported ------------------------------ diff --git a/lib/RPGEngine/Render/Playing.hs b/lib/RPGEngine/Render/Playing.hs index dd9f739..6c5a47b 100644 --- a/lib/RPGEngine/Render/Playing.hs +++ b/lib/RPGEngine/Render/Playing.hs @@ -2,22 +2,13 @@ module RPGEngine.Render.Playing ( renderPlaying ) where -import RPGEngine.Render.Core ( Renderer, getRender, setRenderPos ) +import RPGEngine.Render.Core (Renderer, getRender, setRenderPos) -import RPGEngine.Data - ( Player(..), - Entity(..), - Item(..), - Physical(..), - Layout, - Level(..), - State(..), - Game(..) ) -import Graphics.Gloss ( Picture, pictures ) -import Graphics.Gloss.Data.Picture (translate) import RPGEngine.Config (resolution, zoom) -import Graphics.Gloss (text) -import Graphics.Gloss (blank) +import RPGEngine.Data (State(..), Player (..), Game (..), Level (..), Layout, Physical (..), Item (..), Entity (..)) + +import Graphics.Gloss ( pictures, Picture, translate ) +import Graphics.Gloss.Data.Picture (blank) ------------------------------ Exported ------------------------------ @@ -42,6 +33,7 @@ focusPlayer Game{ state = Playing{ player = Player{ position = (x, y) }}} = move where move = translate centerX centerY centerX = resolution * zoom * fromIntegral (negate x) centerY = resolution * zoom * fromIntegral (negate y) +focusPlayer _ = id ------------------------------- Level -------------------------------- diff --git a/lib/RPGEngine/Render/Win.hs b/lib/RPGEngine/Render/Win.hs index 62c93b5..189cef8 100644 --- a/lib/RPGEngine/Render/Win.hs +++ b/lib/RPGEngine/Render/Win.hs @@ -1,14 +1,15 @@ -module RPGEngine.Render.Win +module RPGEngine.Render.Win ( renderWin ) where -import RPGEngine.Render.Core ( Renderer ) +import RPGEngine.Render.Core (Renderer) + +import RPGEngine.Data (State) -import RPGEngine.Data ( State ) import Graphics.Gloss (text) ----------------------------------------------------------------------- +------------------------------ Exported ------------------------------ -- TODO renderWin :: Renderer State -renderWin _ = text "Win" \ No newline at end of file +renderWin _ = text "You win!\nPress any key to return to the menu." \ No newline at end of file diff --git a/rpg-engine.cabal b/rpg-engine.cabal index 9043757..76a2eee 100644 --- a/rpg-engine.cabal +++ b/rpg-engine.cabal @@ -23,12 +23,12 @@ library RPGEngine.Input RPGEngine.Input.Core - RPGEngine.Input.LevelSelection - RPGEngine.Input.Lose RPGEngine.Input.Menu - RPGEngine.Input.Paused + RPGEngine.Input.LevelSelection RPGEngine.Input.Playing + RPGEngine.Input.Paused RPGEngine.Input.Win + RPGEngine.Input.Lose RPGEngine.Parse RPGEngine.Parse.Core @@ -37,12 +37,12 @@ library RPGEngine.Render RPGEngine.Render.Core - RPGEngine.Render.LevelSelection - RPGEngine.Render.Lose RPGEngine.Render.Menu - RPGEngine.Render.Paused + RPGEngine.Render.LevelSelection RPGEngine.Render.Playing + RPGEngine.Render.Paused RPGEngine.Render.Win + RPGEngine.Render.Lose executable rpg-engine main-is: Main.hs