Merge pull request 'dev' (#16) from dev into master

Reviewed-on: https://git.depeuter.tk/tdpeuter/RPG-Engine/pulls/16
This commit is contained in:
Tibo De Peuter 2022-12-15 09:24:28 +01:00
commit 30ae002434
13 changed files with 239 additions and 29 deletions

37
lib/RPGEngine.hs Normal file
View file

@ -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

View file

@ -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"

23
lib/control/Input.hs Normal file
View file

@ -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

View file

@ -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

25
lib/data/Game.hs Normal file
View file

@ -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
}

32
lib/data/State.hs Normal file
View file

@ -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

47
lib/render/Render.hs Normal file
View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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: {}

7
test/RPG-Engine-Test.hs Normal file
View file

@ -0,0 +1,7 @@
import Test.Hspec
main :: IO()
main = hspec $ do
describe "Dummy category" $ do
it "Dummy test" $ do
0 `shouldBe` 0

View file

@ -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"

0
verslag.pdf Normal file
View file