diff --git a/lib/RPGEngine.hs b/lib/RPGEngine.hs new file mode 100644 index 0000000..d5ea2e7 --- /dev/null +++ b/lib/RPGEngine.hs @@ -0,0 +1,37 @@ +-- Allows to play a game using RPGEngine. +-- Includes all logic and rendering. + +module RPGEngine +( playRPGEngine +) where + +import Game +import Render +import Input + +import Graphics.Gloss ( + Color(..) + , white + , play + ) + +----------------------------- Constants ------------------------------ + +-- Dimensions for main window +winDimensions :: (Int, Int) +winDimensions = (1280, 720) + +-- Offsets for main window +winOffsets :: (Int, Int) +winOffsets = (0, 0) + +---------------------------------------------------------------------- + +-- This is the gameloop. +-- It can receive input and update itself. It is rendered by a renderer. +playRPGEngine :: String -> Int -> IO() +playRPGEngine title fps = do + play window bgColor fps initGame render handleInputs step + where window = initWindow title winDimensions winOffsets + step _ g = g -- TODO Do something with step? + handleInputs = handleAllInput diff --git a/lib/VoorbeeldModule.hs b/lib/VoorbeeldModule.hs deleted file mode 100644 index 63dfdf3..0000000 --- a/lib/VoorbeeldModule.hs +++ /dev/null @@ -1,10 +0,0 @@ -module VoorbeeldModule - ( hoi -- oplijsting van de publieke functies - als je deze lijst en de haakjes weglaat, wordt alles publiek - , hallo - ) where - -hoi :: String -hoi = "Hoi" - -hallo :: String -hallo = "Hallo" diff --git a/lib/control/Input.hs b/lib/control/Input.hs new file mode 100644 index 0000000..e13b523 --- /dev/null +++ b/lib/control/Input.hs @@ -0,0 +1,23 @@ +module Input +( +-- Handle all input for RPG-Engine +handleAllInput +) where + +import Game +import State +import InputHandling + +import Graphics.Gloss.Interface.IO.Game + +---------------------------------------------------------------------- + +handleAllInput :: InputHandler Game +handleAllInput = composeInputHandlers [ + handleSpecialKey KeySpace setNextState + ] + +-- Go to the next stage of the Game +setNextState :: Game -> Game +setNextState game = game{ state = newState } + where newState = nextState $ state game diff --git a/lib/control/InputHandling.hs b/lib/control/InputHandling.hs new file mode 100644 index 0000000..86704e4 --- /dev/null +++ b/lib/control/InputHandling.hs @@ -0,0 +1,41 @@ +-- Allows to create a massive inputHandler that can handle anything +-- after you specify what you want it to do. + +module InputHandling +( InputHandler(..), +-- Compose multiple InputHandlers into one InputHandler that handles +-- all of them. +composeInputHandlers, + +handle, +handleSpecialKey +) where + +import Graphics.Gloss.Interface.IO.Game + +----------------------------- Constants ------------------------------ + +type InputHandler a = Event -> (a -> a) + +---------------------------------------------------------------------- + +composeInputHandlers :: [InputHandler a] -> InputHandler a +composeInputHandlers [] ev a = a +composeInputHandlers (ih:ihs) ev a = composeInputHandlers ihs ev (ih ev a) + +handle :: Event -> (a -> a) -> Event -> (a -> a) +handle (EventKey key _ _ _) = handleKey key +-- handle (EventMotion _) = undefined +-- handle (EventResize _) = undefined +handle _ = (\_ -> (\_ -> id)) + +handleKey :: Key -> (a -> a) -> Event -> (a -> a) +handleKey (SpecialKey key) = handleSpecialKey key +handleKey (Char _ ) = (\_ -> (\_ -> id)) +handleKey (MouseButton _ ) = (\_ -> (\_ -> id)) + +handleSpecialKey :: SpecialKey -> (a -> a) -> Event -> (a -> a) +handleSpecialKey sk1 f (EventKey (SpecialKey sk2) Down _ _) + | sk1 == sk2 = f + | otherwise = id +handleSpecialKey _ _ _ = id diff --git a/lib/data/Game.hs b/lib/data/Game.hs new file mode 100644 index 0000000..3a07903 --- /dev/null +++ b/lib/data/Game.hs @@ -0,0 +1,25 @@ +-- Representation of all the game's data + +module Game +( Game(..) + +-- Initialize the game +, initGame +) where + +import State + +----------------------------- Constants ------------------------------ + +-- TODO Add more +data Game = Game { + -- Current state of the game + state :: State +} + +---------------------------------------------------------------------- + +initGame :: Game +initGame = Game { + state = defaultState +} diff --git a/lib/data/State.hs b/lib/data/State.hs new file mode 100644 index 0000000..1ae7a29 --- /dev/null +++ b/lib/data/State.hs @@ -0,0 +1,32 @@ +-- Describes the current state of the game, +-- e.g. Main menu, game, pause, win or lose +-- Allows to easily go to a next state and change rendering accordingly + +module State +( State(..) +-- Default state of the game, Menu +, defaultState + +-- Get the next state based on the current state +, nextState +) where + +----------------------------- Constants ------------------------------ + +-- Current state of the game. +data State = Menu + | Playing + | Pause + | Win + | Lose + +---------------------------------------------------------------------- + +defaultState :: State +defaultState = Menu + +nextState :: State -> State +nextState Menu = Playing +nextState Playing = Pause +nextState Pause = Playing +nextState _ = Menu \ No newline at end of file diff --git a/lib/render/Render.hs b/lib/render/Render.hs new file mode 100644 index 0000000..a94fed5 --- /dev/null +++ b/lib/render/Render.hs @@ -0,0 +1,47 @@ +-- Allows to render the played game + +module Render +( +-- Initialize a window to play in +initWindow + +-- Render the game +, render +) where + +import Game(Game(..)) +import State(State(..)) +import Graphics.Gloss + +---------------------------------------------------------------------- + +initWindow :: String -> (Int, Int) -> (Int, Int) -> Display +initWindow title dims offs = InWindow title dims offs + +render :: Game -> Picture +render g@Game{ state = Menu } = renderMenu g +render g@Game{ state = Playing } = renderPlaying g +render g@Game{ state = Pause } = renderPause g +render g@Game{ state = Win } = renderWin g +render g@Game{ state = Lose } = renderLose g + + +-- TODO +renderMenu :: Game -> Picture +renderMenu _ = text "Menu" + +-- TODO +renderPlaying :: Game -> Picture +renderPlaying _ = text "Playing" + +-- TODO +renderPause :: Game -> Picture +renderPause _ = text "Pause" + +-- TODO +renderWin :: Game -> Picture +renderWin _ = text "Win" + +-- TODO +renderLose :: Game -> Picture +renderLose _ = text "Lose" \ No newline at end of file diff --git a/rpg-engine.cabal b/rpg-engine.cabal index 7d6f4ad..28aea68 100644 --- a/rpg-engine.cabal +++ b/rpg-engine.cabal @@ -1,13 +1,19 @@ name: rpg-engine version: 1.0.0 -author: Author name here +author: Tibo De Peuter cabal-version: 1.12 build-type: Simple library - hs-source-dirs: lib - build-depends: base >= 4.7 && <5 - exposed-modules: VoorbeeldModule + hs-source-dirs: lib, lib/control, lib/data, lib/render + build-depends: + base >= 4.7 && <5, + gloss >= 1.11 && < 1.14, gloss-juicy >= 0.2.3 + exposed-modules: + RPGEngine, + Input, InputHandling, + Game, State, + Render executable rpg-engine main-is: Main.hs @@ -17,7 +23,7 @@ executable rpg-engine test-suite rpg-engine-test type: exitcode-stdio-1.0 - main-is: VoorbeeldTest.hs + main-is: RPG-Engine-Test.hs hs-source-dirs: test default-language: Haskell2010 build-depends: base >=4.7 && <5, hspec <= 2.10.6, rpg-engine diff --git a/src/Main.hs b/src/Main.hs index 55d35ac..bb69131 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,4 +1,16 @@ -import VoorbeeldModule (hoi) +import RPGEngine + +----------------------------- Constants ------------------------------ + +-- Title of the game +title :: String +title = "RPG Engine" + +-- Framerate of the game +fps :: Int +fps = 60 + +---------------------------------------------------------------------- main :: IO () -main = putStrLn hoi +main = playRPGEngine title fps diff --git a/stack.yaml b/stack.yaml index 2c311ed..2539f5a 100644 --- a/stack.yaml +++ b/stack.yaml @@ -35,12 +35,13 @@ packages: # These entries can reference officially published versions as well as # forks / in-progress versions pinned to a git hash. For example: # -# extra-deps: +extra-deps: # - acme-missiles-0.3 # - git: https://github.com/commercialhaskell/stack.git # commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a # # extra-deps: [] +- gloss-juicy-0.2.3@sha256:0c3bca95237cbf91f8b3b1936a0661f1e0457acd80502276d54d6c5210f88b25,1618 # Override default flag values for local packages and extra-deps # flags: {} diff --git a/test/RPG-Engine-Test.hs b/test/RPG-Engine-Test.hs new file mode 100644 index 0000000..39131fd --- /dev/null +++ b/test/RPG-Engine-Test.hs @@ -0,0 +1,7 @@ +import Test.Hspec + +main :: IO() +main = hspec $ do + describe "Dummy category" $ do + it "Dummy test" $ do + 0 `shouldBe` 0 \ No newline at end of file diff --git a/test/VoorbeeldTest.hs b/test/VoorbeeldTest.hs deleted file mode 100644 index 2b94edb..0000000 --- a/test/VoorbeeldTest.hs +++ /dev/null @@ -1,11 +0,0 @@ -import Test.Hspec - -import VoorbeeldModule (hoi, hallo) - -main :: IO () -main = hspec $ do - it "Returns correct string for hoi" $ do - hoi `shouldBe` "Hoi" - - it "Returns correct string for hallo" $ do - hallo `shouldBe` "Hallo" diff --git a/verslag.pdf b/verslag.pdf new file mode 100644 index 0000000..e69de29