From b622b9393226b5da3c4a0c8c16356c3caa1ec0ec Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Tue, 13 Dec 2022 23:21:51 +0100 Subject: [PATCH 1/4] Ready for takeoff --- lib/RPGEngine.hs | 39 +++++++++++++++++++++++++++++++++++++++ lib/VoorbeeldModule.hs | 10 ---------- lib/data/Game.hs | 22 ++++++++++++++++++++++ lib/render/RenderGame.hs | 19 +++++++++++++++++++ rpg-engine.cabal | 15 ++++++++++----- src/Main.hs | 16 ++++++++++++++-- stack.yaml | 3 ++- test/RPG-Engine-Test.hs | 0 test/VoorbeeldTest.hs | 11 ----------- verslag.pdf | 0 10 files changed, 106 insertions(+), 29 deletions(-) create mode 100644 lib/RPGEngine.hs delete mode 100644 lib/VoorbeeldModule.hs create mode 100644 lib/data/Game.hs create mode 100644 lib/render/RenderGame.hs create mode 100644 test/RPG-Engine-Test.hs delete mode 100644 test/VoorbeeldTest.hs create mode 100644 verslag.pdf diff --git a/lib/RPGEngine.hs b/lib/RPGEngine.hs new file mode 100644 index 0000000..3c4812a --- /dev/null +++ b/lib/RPGEngine.hs @@ -0,0 +1,39 @@ +-- Allows to play a game using RPGEngine. +-- Includes all logic and rendering. + +module RPGEngine +( playRPGEngine +) where + +import Game +import RenderGame +import Graphics.Gloss ( + Color(..) + , black + , play + ) + +----------------------------- Constants ------------------------------ + +-- Dimensions for main window +winDimensions :: (Int, Int) +winDimensions = (1280, 720) + +-- Offsets for main window +winOffsets :: (Int, Int) +winOffsets = (0, 0) + +-- Game background color +bgColor :: Color +bgColor = black + +---------------------------------------------------------------------- + +-- 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 _ g = g -- TODO Implement inputHanlders 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/data/Game.hs b/lib/data/Game.hs new file mode 100644 index 0000000..350b386 --- /dev/null +++ b/lib/data/Game.hs @@ -0,0 +1,22 @@ +-- Representation of all the game's data + +module Game +( Game(..) +, initGame -- Initialize the game +) where + +----------------------------- Constants ------------------------------ + +data Game = Game { + -- TODO Add more + playerName :: String +} + +---------------------------------------------------------------------- + +-- Initialize the game +-- TODO Expand +initGame :: Game +initGame = Game { + playerName = "Tibo" +} diff --git a/lib/render/RenderGame.hs b/lib/render/RenderGame.hs new file mode 100644 index 0000000..aac0ed2 --- /dev/null +++ b/lib/render/RenderGame.hs @@ -0,0 +1,19 @@ +-- Allows to render the played game + +module RenderGame +( initWindow -- Initialize a window to play in +, render -- Rener the game +) where + +import Game +import Graphics.Gloss + +---------------------------------------------------------------------- + +-- Render the game +render :: Game -> Picture +render _ = Blank + +-- Initialize a window to play in +initWindow :: String -> (Int, Int) -> (Int, Int) -> Display +initWindow title dims offs = InWindow title dims offs diff --git a/rpg-engine.cabal b/rpg-engine.cabal index 7d6f4ad..b068317 100644 --- a/rpg-engine.cabal +++ b/rpg-engine.cabal @@ -1,13 +1,18 @@ 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, + Game, + RenderGame executable rpg-engine main-is: Main.hs @@ -17,7 +22,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..e69de29 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 -- 2.47.2 From f348a47281552082a607e45145eda69bb06154a5 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Wed, 14 Dec 2022 15:12:28 +0100 Subject: [PATCH 2/4] Added basic test --- test/RPG-Engine-Test.hs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/RPG-Engine-Test.hs b/test/RPG-Engine-Test.hs index e69de29..39131fd 100644 --- a/test/RPG-Engine-Test.hs +++ 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 -- 2.47.2 From 9e5f22458c509caf5ececc45950af09833675ac9 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Wed, 14 Dec 2022 15:13:43 +0100 Subject: [PATCH 3/4] Added state #6, #7, #8 --- lib/RPGEngine.hs | 6 ++--- lib/data/Game.hs | 15 ++++++++----- lib/data/State.hs | 32 +++++++++++++++++++++++++++ lib/render/Render.hs | 47 ++++++++++++++++++++++++++++++++++++++++ lib/render/RenderGame.hs | 19 ---------------- rpg-engine.cabal | 4 ++-- 6 files changed, 93 insertions(+), 30 deletions(-) create mode 100644 lib/data/State.hs create mode 100644 lib/render/Render.hs delete mode 100644 lib/render/RenderGame.hs diff --git a/lib/RPGEngine.hs b/lib/RPGEngine.hs index 3c4812a..44047fb 100644 --- a/lib/RPGEngine.hs +++ b/lib/RPGEngine.hs @@ -6,10 +6,10 @@ module RPGEngine ) where import Game -import RenderGame +import Render import Graphics.Gloss ( Color(..) - , black + , white , play ) @@ -25,7 +25,7 @@ winOffsets = (0, 0) -- Game background color bgColor :: Color -bgColor = black +bgColor = white ---------------------------------------------------------------------- diff --git a/lib/data/Game.hs b/lib/data/Game.hs index 350b386..3a07903 100644 --- a/lib/data/Game.hs +++ b/lib/data/Game.hs @@ -2,21 +2,24 @@ module Game ( Game(..) -, initGame -- Initialize the game + +-- Initialize the game +, initGame ) where +import State + ----------------------------- Constants ------------------------------ +-- TODO Add more data Game = Game { - -- TODO Add more - playerName :: String + -- Current state of the game + state :: State } ---------------------------------------------------------------------- --- Initialize the game --- TODO Expand initGame :: Game initGame = Game { - playerName = "Tibo" + 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/lib/render/RenderGame.hs b/lib/render/RenderGame.hs deleted file mode 100644 index aac0ed2..0000000 --- a/lib/render/RenderGame.hs +++ /dev/null @@ -1,19 +0,0 @@ --- Allows to render the played game - -module RenderGame -( initWindow -- Initialize a window to play in -, render -- Rener the game -) where - -import Game -import Graphics.Gloss - ----------------------------------------------------------------------- - --- Render the game -render :: Game -> Picture -render _ = Blank - --- Initialize a window to play in -initWindow :: String -> (Int, Int) -> (Int, Int) -> Display -initWindow title dims offs = InWindow title dims offs diff --git a/rpg-engine.cabal b/rpg-engine.cabal index b068317..754ec51 100644 --- a/rpg-engine.cabal +++ b/rpg-engine.cabal @@ -11,8 +11,8 @@ library gloss >= 1.11 && < 1.14, gloss-juicy >= 0.2.3 exposed-modules: RPGEngine, - Game, - RenderGame + Game, State, + Render executable rpg-engine main-is: Main.hs -- 2.47.2 From cdd0c3989cb1c6475b075c75ee17f1fbb9e43d4c Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Wed, 14 Dec 2022 22:20:44 +0100 Subject: [PATCH 4/4] #15 Basic input handling --- lib/RPGEngine.hs | 12 +++++------ lib/control/Input.hs | 23 ++++++++++++++++++++ lib/control/InputHandling.hs | 41 ++++++++++++++++++++++++++++++++++++ rpg-engine.cabal | 1 + 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 lib/control/Input.hs create mode 100644 lib/control/InputHandling.hs diff --git a/lib/RPGEngine.hs b/lib/RPGEngine.hs index 44047fb..d5ea2e7 100644 --- a/lib/RPGEngine.hs +++ b/lib/RPGEngine.hs @@ -7,6 +7,8 @@ module RPGEngine import Game import Render +import Input + import Graphics.Gloss ( Color(..) , white @@ -23,10 +25,6 @@ winDimensions = (1280, 720) winOffsets :: (Int, Int) winOffsets = (0, 0) --- Game background color -bgColor :: Color -bgColor = white - ---------------------------------------------------------------------- -- This is the gameloop. @@ -34,6 +32,6 @@ bgColor = white 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 _ g = g -- TODO Implement inputHanlders + where window = initWindow title winDimensions winOffsets + step _ g = g -- TODO Do something with step? + handleInputs = handleAllInput 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/rpg-engine.cabal b/rpg-engine.cabal index 754ec51..28aea68 100644 --- a/rpg-engine.cabal +++ b/rpg-engine.cabal @@ -11,6 +11,7 @@ library gloss >= 1.11 && < 1.14, gloss-juicy >= 0.2.3 exposed-modules: RPGEngine, + Input, InputHandling, Game, State, Render -- 2.47.2