This commit is contained in:
Tibo De Peuter 2022-12-20 16:56:22 +01:00
parent 0720f3b719
commit d4fbcda73b
13 changed files with 412 additions and 169 deletions

View file

@ -1,13 +1,33 @@
module RPGEngine.Data.Types where
module RPGEngine.Data where
-------------------------------- Game --------------------------------
-- TODO Add more
data Game = Game {
-- Current state of the game
state :: State
state :: State,
playing :: Level,
levels :: [Level]
}
------------------------------- Level --------------------------------
data Level = Level {
layout :: Layout,
items :: [Item],
entities :: [Entity]
}
type Layout = [Strip]
type Strip = [Physical]
data Physical = Void
| Walkable
| Blocked
| Entrance
| Exit
deriving (Show, Eq)
------------------------------- Player -------------------------------
data Player = Player {
@ -108,8 +128,9 @@ data Action = Leave
------------------------------ Direction -----------------------------
data Direction = North
| East
| South
data Direction = North
| East
| South
| West
deriving (Show)
| Center -- Equal to 'stay where you are'
deriving (Show, Eq)

View file

@ -5,7 +5,7 @@ module RPGEngine.Data.Game
,initGame
) where
import RPGEngine.Data.Types
import RPGEngine.Data
import RPGEngine.Data.State
----------------------------------------------------------------------
@ -13,5 +13,13 @@ import RPGEngine.Data.State
-- Initialize the game
initGame :: Game
initGame = Game {
state = defaultState
state = defaultState,
playing = head levels,
levels = levels
}
where levels = [emptyLevel]
emptyLevel = Level {
layout = [],
items = [],
entities = []
}

View file

@ -9,7 +9,7 @@ module RPGEngine.Data.State
, nextState
) where
import RPGEngine.Data.Types
import RPGEngine.Data
----------------------------- Constants ------------------------------

View file

@ -4,7 +4,7 @@ module RPGEngine.Input
( handleAllInput
) where
import RPGEngine.Data.Types
import RPGEngine.Data
import RPGEngine.Data.State
import RPGEngine.Input.Core

View file

@ -1,8 +1,19 @@
module RPGEngine.Parse where
import RPGEngine.Data.Types
import RPGEngine.Data
import RPGEngine.Parse.StructureElement
import RPGEngine.Parse.Game
-- TODO parseFromFile gebruiken
import Text.Parsec.String
import System.IO.Unsafe
parseToGame :: Game
parseToGame = undefined
----------------------------- Constants ------------------------------
type FileName = String
----------------------------------------------------------------------
parseToGame :: FileName -> Game
parseToGame filename = structureToGame structure
where (Right structure) = unsafePerformIO io
io = parseFromFile structureElement filename

View file

@ -0,0 +1,22 @@
module RPGEngine.Parse.Game where
import RPGEngine.Data
import RPGEngine.Parse.StructureElement (StructureElement)
-------------------------------- Game --------------------------------
-- TODO
structureToGame :: StructureElement -> Game
structureToGame = undefined
------------------------------- Player -------------------------------
-- TODO
structureToPlayer :: StructureElement -> Player
structureToPlayer = undefined
------------------------------- Levels -------------------------------
-- TODO
structureToLevels :: StructureElement -> [Level]
structureToLevels = undefined

View file

@ -1,6 +1,6 @@
module RPGEngine.Parse.StructureElement where
import RPGEngine.Data.Types (Action(..), Condition(..))
import RPGEngine.Data (Action(..), Condition(..), Layout, Direction(..), Physical(..), Strip)
import RPGEngine.Parse.Core ( ignoreWS )
import Text.Parsec
@ -38,7 +38,7 @@ structureElement = try $ choice [block, entry, regular]
block :: Parser StructureElement
block = try $ do
open <- ignoreWS $ oneOf openingBrackets
middle <- ignoreWS entry `sepBy` ignoreWS (char ',')
middle <- ignoreWS $ choice [entry, block] `sepBy` char ','
let closingBracket = getMatchingClosingBracket open
ignoreWS $ char closingBracket
return $ Block middle
@ -75,7 +75,7 @@ tag = try $ Tag <$> many1 alphaNum
conditionList :: Parser Key
conditionList = try $ do
open <- ignoreWS $ oneOf openingBrackets
list <- try $ ignoreWS condition `sepBy` ignoreWS (char ',')
list <- ignoreWS condition `sepBy` char ','
let closingBracket = getMatchingClosingBracket open
ignoreWS $ char closingBracket
return $ ConditionList $ extract list
@ -102,13 +102,14 @@ data Value = String String
| Integer Int
| Infinite
| Action Action
| Layout -- TODO Add element
| Direction Direction
| Layout Layout
deriving (Show, Eq)
----------------------------------------------------------------------
value :: Parser Value
value = choice [string, integer, infinite, action]
value = choice [string, integer, infinite, action, direction]
string :: Parser Value
string = try $ String <$> between (char '\"') (char '\"') reading
@ -134,13 +135,52 @@ action = try $ do
| script == "useItem" = UseItem arg
| script == "decreaseHp" = DecreaseHp first second
| script == "increasePlayerHp" = IncreasePlayerHp arg
| otherwise = RPGEngine.Data.Types.Nothing
| otherwise = RPGEngine.Data.Nothing
(first, ',':second) = break (== ',') arg
return $ Action answer
-- TODO
direction :: Parser Value
direction = try $ do
value <- choice [
ignoreWS $ P.string "up",
ignoreWS $ P.string "down",
ignoreWS $ P.string "left",
ignoreWS $ P.string "right"
]
notFollowedBy alphaNum
return $ Direction $ make value
where make "up" = North
make "right" = East
make "down" = South
make "left" = West
make _ = Center
layout :: Parser Value
layout = undefined
layout = try $ do
ignoreWS $ char '|'
list <- ignoreWS strip `sepBy` ignoreWS (char '|')
return $ Layout list
strip :: Parser Strip
strip = try $ do
physical `sepBy` char ' '
physical :: Parser Physical
physical = try $ do
value <- choice [
char 'x',
char '.',
char '*',
char 's',
char 'e'
]
return $ make value
where make '.' = Walkable
make '*' = Blocked
make 's' = Entrance
make 'e' = Exit
make _ = Void
------------------------------ Brackets ------------------------------

View file

@ -7,7 +7,7 @@ module RPGEngine.Render
, render
) where
import RPGEngine.Data.Types
import RPGEngine.Data
import Graphics.Gloss
----------------------------- Constants ------------------------------

View file

@ -13,8 +13,8 @@ library
exposed-modules:
RPGEngine
RPGEngine.Data
RPGEngine.Data.Game
RPGEngine.Data.Types
RPGEngine.Data.State
RPGEngine.Input
@ -22,6 +22,7 @@ library
RPGEngine.Parse
RPGEngine.Parse.Core
RPGEngine.Parse.Game
RPGEngine.Parse.StructureElement
RPGEngine.Render
@ -39,6 +40,5 @@ test-suite rpg-engine-test
default-language: Haskell2010
build-depends: base >=4.7 && <5, hspec <= 2.10.6, hspec-discover, rpg-engine
other-modules:
InteractionSpec,
-- Parsing
ParsedToGameSpec, ParserSpec
ParseGameSpec, ParseStructureElementSpec

View file

@ -1,9 +0,0 @@
module InteractionSpec where
import Test.Hspec
spec :: Spec
spec = do
describe "Player with Inventory" $ do
it "TODO: Simple test" $ do
pending

View file

@ -1,7 +1,7 @@
module ParsedToGameSpec where
module ParseGameSpec where
import Test.Hspec
import RPGEngine.Internals.Parse.StructureElement
import RPGEngine.Parse.StructureElement
spec :: Spec
spec = do

View file

@ -0,0 +1,282 @@
module ParseStructureElementSpec where
import Test.Hspec
import RPGEngine.Data
import RPGEngine.Parse.Core
import RPGEngine.Parse.StructureElement
spec :: Spec
spec = do
describe "StructureElement" $ do
it "can parse blocks" $ do
let input = "{}"
correct = Right $ Block []
parseWith structureElement input `shouldBe` correct
let input = "{{}}"
correct = Right $ Block [Block []]
parseWith structureElement input `shouldBe` correct
let input = "{{}, {}}"
correct = Right $ Block [Block [], Block []]
parseWith structureElement input `shouldBe` correct
let input = "{ id: 1 }"
correct = Right (Block [
Entry (Tag "id") $ Regular $ Integer 1
], "")
parseWithRest structureElement input `shouldBe` correct
let input = "{ id: \"key\", x: 3, y: 1}"
correct = Right $ Block [
Entry (Tag "id") $ Regular $ String "key",
Entry (Tag "x") $ Regular $ Integer 3,
Entry (Tag "y") $ Regular $ Integer 1
]
parseWith structureElement input `shouldBe` correct
let input = "actions: { [not(inventoryFull())] retrieveItem(key), [] leave()}"
correct = Right (Entry (Tag "actions") $ Block [
Entry (ConditionList [Not InventoryFull]) $ Regular $ Action $ RetrieveItem "key",
Entry (ConditionList []) $ Regular $ Action Leave
], "")
parseWithRest structureElement input `shouldBe` correct
let input = "entities: [ { id: \"door\", x: 4, name:\"Secret door\", description: \"This secret door can only be opened with a key\", direction: left, y: 1}]"
correct = Right (Entry (Tag "entities") $ Block [ Block [
Entry (Tag "id") $ Regular $ String "door",
Entry (Tag "x") $ Regular $ Integer 4,
Entry (Tag "name") $ Regular $ String "Secret door",
Entry (Tag "description") $ Regular $ String "This secret door can only be opened with a key",
Entry (Tag "direction") $ Regular $ Direction West,
Entry (Tag "y") $ Regular $ Integer 1
]], "")
parseWithRest structureElement input `shouldBe` correct
let input = "entities: [ { id: \"door\", x: 4, y: 1, name:\"Secret door\", description: \"This secret door can only be opened with a key\", actions: { [inventoryContains(key)] useItem(key), [] leave() } } ]"
correct = Right (Entry (Tag "entities") $ Block [ Block [
Entry (Tag "id") $ Regular $ String "door",
Entry (Tag "x") $ Regular $ Integer 4,
Entry (Tag "y") $ Regular $ Integer 1,
Entry (Tag "name") $ Regular $ String "Secret door",
Entry (Tag "description") $ Regular $ String "This secret door can only be opened with a key",
Entry (Tag "actions") $ Block [
Entry (ConditionList [InventoryContains "key"]) $ Regular $ Action $ UseItem "key",
Entry (ConditionList []) $ Regular $ Action Leave
]
]], "")
parseWithRest structureElement input `shouldBe` correct
let input = "entities: [ { id: \"door\", x: 4, y: 1, name:\"Secret door\", description: \"This secret door can only be opened with a key\", direction: left , actions: { [inventoryContains(key)] useItem(key), [] leave() } } ]"
correct = Right (Entry (Tag "entities") $ Block [ Block [
Entry (Tag "id") $ Regular $ String "door",
Entry (Tag "x") $ Regular $ Integer 4,
Entry (Tag "y") $ Regular $ Integer 1,
Entry (Tag "name") $ Regular $ String "Secret door",
Entry (Tag "description") $ Regular $ String "This secret door can only be opened with a key",
Entry (Tag "direction") $ Regular $ Direction West,
Entry (Tag "actions") $ Block [
Entry (ConditionList [InventoryContains "key"]) $ Regular $ Action $ UseItem "key",
Entry (ConditionList []) $ Regular $ Action Leave
]
]], "")
parseWithRest structureElement input `shouldBe` correct
it "can parse entries" $ do
let input = "id: \"dagger\""
correct = Right $ Entry (Tag "id") $ Regular $ String "dagger"
parseWith entry input `shouldBe` correct
let input = "x: 0"
correct = Right $ Entry (Tag "x") $ Regular $ Integer 0
parseWith entry input `shouldBe` correct
let input = "useTimes: infinite"
correct = Right $ Entry (Tag "useTimes") $ Regular Infinite
parseWith entry input `shouldBe` correct
let input = "direction: up"
correct = Right $ Entry (Tag "direction") $ Regular $ Direction North
parseWith entry input `shouldBe` correct
let input = "actions: { [not(inventoryFull())] retrieveItem(key), [] leave()}"
correct = Right (Entry (Tag "actions") $ Block [
Entry (ConditionList [Not InventoryFull]) $ Regular $ Action $ RetrieveItem "key",
Entry (ConditionList []) $ Regular $ Action Leave
], "")
parseWithRest structureElement input `shouldBe` correct
it "can parse regulars" $ do
let input = "this is a string"
correct = Right $ Regular $ String input
parseWith regular ("\"" ++ input ++ "\"") `shouldBe` correct
let correct = Right $ Regular $ Integer 1
parseWith regular "1" `shouldBe` correct
let correct = Right $ Regular Infinite
parseWith regular "infinite" `shouldBe` correct
let wrong = Right $ Regular Infinite
parseWith regular "infinitee" `shouldNotBe` wrong
let input = "leave()"
correct = Right $ Regular $ Action Leave
parseWith regular input `shouldBe` correct
let input = "retrieveItem(firstId)"
correct = Right $ Regular $ Action $ RetrieveItem "firstId"
parseWith regular input `shouldBe` correct
let input = "useItem(secondId)"
correct = Right $ Regular $ Action $ UseItem "secondId"
parseWith regular input `shouldBe` correct
let input = "decreaseHp(entityId,objectId)"
correct = Right $ Regular $ Action $ DecreaseHp "entityId" "objectId"
parseWith regular input `shouldBe` correct
let input = "increasePlayerHp(objectId)"
correct = Right $ Regular $ Action $ IncreasePlayerHp "objectId"
parseWith regular input `shouldBe` correct
let input = "up"
correct = Right $ Regular $ Direction North
parseWith regular input `shouldBe` correct
let input = "right"
correct = Right $ Regular $ Direction East
parseWith regular input `shouldBe` correct
let input = "down"
correct = Right $ Regular $ Direction South
parseWith regular input `shouldBe` correct
let input = "left"
correct = Right $ Regular $ Direction West
parseWith regular input `shouldBe` correct
describe "Key" $ do
it "can parse tags" $ do
let input = "simpletag"
correct = Right $ Tag "simpletag"
parseWith tag input `shouldBe` correct
it "can parse conditionlists" $ do
let input = "[not(inventoryFull())]"
correct = Right (ConditionList [Not InventoryFull], "")
parseWithRest conditionList input `shouldBe` correct
let input = "[inventoryFull(), inventoryContains(itemId)]"
correct = Right (ConditionList [
InventoryFull,
InventoryContains "itemId"
], "")
parseWithRest conditionList input `shouldBe` correct
let input = "[]"
correct = Right $ ConditionList []
parseWith conditionList input `shouldBe` correct
it "can parse conditions" $ do
let input = "inventoryFull()"
correct = Right (Condition InventoryFull, "")
parseWithRest condition input `shouldBe` correct
let input = "inventoryContains(itemId)"
correct = Right (Condition $ InventoryContains "itemId", "")
parseWithRest condition input `shouldBe` correct
let input = "not(inventoryFull())"
correct = Right (Condition $ Not InventoryFull, "")
parseWithRest condition input `shouldBe` correct
let input = "not(inventoryContains(itemId))"
correct = Right (Condition $ Not $ InventoryContains "itemId", "")
parseWithRest condition input `shouldBe` correct
describe "Value" $ do
it "can parse strings" $ do
let input = "dit is een string"
correct = Right $ String input
parseWith string ("\"" ++ input ++ "\"") `shouldBe` correct
it "can parse integers" $ do
let correct = Right $ Integer 1
parseWith integer "1" `shouldBe` correct
it "can parse infinite" $ do
let correct = Right Infinite
parseWith infinite "infinite" `shouldBe` correct
let wrong = Right Infinite
parseWith infinite "infinitee" `shouldNotBe` wrong
it "can parse actions" $ do
let input = "leave()"
correct = Right $ Action Leave
parseWith action input `shouldBe` correct
let input = "retrieveItem(firstId)"
correct = Right $ Action $ RetrieveItem "firstId"
parseWith action input `shouldBe` correct
let input = "useItem(secondId)"
correct = Right $ Action $ UseItem "secondId"
parseWith action input `shouldBe` correct
let input = "decreaseHp(entityId,objectId)"
correct = Right $ Action $ DecreaseHp "entityId" "objectId"
parseWith action input `shouldBe` correct
let input = "increasePlayerHp(objectId)"
correct = Right $ Action $ IncreasePlayerHp "objectId"
parseWith action input `shouldBe` correct
it "can parse directions" $ do
let input = "up"
correct = Right $ Direction North
parseWith RPGEngine.Parse.StructureElement.direction input `shouldBe` correct
let input = "right"
correct = Right $ Direction East
parseWith RPGEngine.Parse.StructureElement.direction input `shouldBe` correct
let input = "down"
correct = Right $ Direction South
parseWith RPGEngine.Parse.StructureElement.direction input `shouldBe` correct
let input = "left"
correct = Right $ Direction West
parseWith RPGEngine.Parse.StructureElement.direction input `shouldBe` correct
it "can parse layouts" $ do
let input = "| * * * * * * * *\n| * s . . . . e *\n| * * * * * * * *"
correct = Right $ Layout [
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked],
[Blocked, Entrance, Walkable, Walkable, Walkable, Walkable, Exit, Blocked],
[Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked, Blocked]
]
parseWith RPGEngine.Parse.StructureElement.layout input `shouldBe` correct
describe "Brackets" $ do
it "matches closing <" $ do
let input = '<'
correct = '>'
getMatchingClosingBracket input `shouldBe` correct
it "matches closing (" $ do
let input = '('
correct = ')'
getMatchingClosingBracket input `shouldBe` correct
it "matches closing {" $ do
let input = '{'
correct = '}'
getMatchingClosingBracket input `shouldBe` correct
it "matches closing [" $ do
let input = '['
correct = ']'
getMatchingClosingBracket input `shouldBe` correct

View file

@ -1,132 +0,0 @@
module ParserSpec where
import Test.Hspec
import RPGEngine.Internals.Parse
import RPGEngine.Internals.Parse.StructureElement
import RPGEngine.Internals.Data.Internals
import Data.Either
spec :: Spec
spec = do
describe "Basics of entries" $ do
it "can parse integers" $ do
let correct = Right $ Regular $ Integer 1
parseWith regular "1" `shouldBe` correct
it "can parse string" $ do
let input = "dit is een string"
correct = Right $ Regular $ String input
parseWith regular ("\"" ++ input ++ "\"") `shouldBe` correct
it "can parse infinite" $ do
let correct = Right $ Regular Infinite
parseWith regular "infinite" `shouldBe` correct
let wrong = Right $ Regular Infinite
parseWith regular "infinitee" `shouldNotBe` wrong
it "can parse entries" $ do
let input = "id: \"dagger\""
correct = Right $ Entry (Tag "id") $ Regular $ String "dagger"
parseWith entry input `shouldBe` correct
let input = "x: 0"
correct = Right $ Entry (Tag "x") $ Regular $ Integer 0
parseWith entry input `shouldBe` correct
let input = "useTimes: infinite"
correct = Right $ Entry (Tag "useTimes") $ Regular Infinite
parseWith entry input `shouldBe` correct
describe "block: {...}" $ do
it "can parse a block with a single entry" $ do
let input = "{ id: 1}"
correct = Right (Block [
Entry (Tag "id") $ Regular $ Integer 1
], "")
parseWithRest structureElement input `shouldBe` correct
it "can parse a block with entries" $ do
let input = "{ id: \"key\", x: 3, y: 1}"
correct = Right $ Block [
Entry (Tag "id") $ Regular $ String "key",
Entry (Tag "x") $ Regular $ Integer 3,
Entry (Tag "y") $ Regular $ Integer 1
]
parseWith structureElement input `shouldBe` correct
describe "Basics" $ do
it "can parse leave()" $ do
let input = "leave()"
correct = Right $ Action Leave
parseWith action input `shouldBe` correct
it "can parse retrieveItem()" $ do
let input = "retrieveItem(firstId)"
correct = Right $ Action $ RetrieveItem "firstId"
parseWith action input `shouldBe` correct
it "can parse useItem()" $ do
let input = "useItem(secondId)"
correct = Right $ Action $ UseItem "secondId"
parseWith action input `shouldBe` correct
it "can parse decreaseHp()" $ do
let input = "decreaseHp(entityId,objectId)"
correct = Right $ Action $ DecreaseHp "entityId" "objectId"
parseWith action input `shouldBe` correct
it "can parse increasePlayerHp()" $ do
let input = "increasePlayerHp(objectId)"
correct = Right $ Action $ IncreasePlayerHp "objectId"
parseWith action input `shouldBe` correct
it "can parse inventoryFull()" $ do
let input = "inventoryFull()"
correct = Right (Condition InventoryFull, "")
parseWithRest condition input `shouldBe` correct
it "can parse inventoryContains()" $ do
let input = "inventoryContains(itemId)"
correct = Right (Condition $ InventoryContains "itemId", "")
parseWithRest condition input `shouldBe` correct
it "can parse not()" $ do
let input = "not(inventoryFull())"
correct = Right (Condition $ Not InventoryFull, "")
parseWithRest condition input `shouldBe` correct
let input = "not(inventoryContains(itemId))"
correct = Right (Condition $ Not $ InventoryContains "itemId", "")
parseWithRest condition input `shouldBe` correct
it "can parse conditionlists" $ do
let input = "[not(inventoryFull())]"
correct = Right (ConditionList [Not InventoryFull], "")
parseWithRest conditionList input `shouldBe` correct
let input = "[inventoryFull(), inventoryContains(itemId)]"
correct = Right (ConditionList [
InventoryFull,
InventoryContains "itemId"
], "")
parseWithRest conditionList input `shouldBe` correct
let input = "[]"
correct = Right $ ConditionList []
parseWith conditionList input `shouldBe` correct
it "can parse actions" $ do
let input = "actions: { [not(inventoryFull())] retrieveItem(key), [] leave()}"
correct = Right (Entry (Tag "actions") $ Block [
Entry (ConditionList [Not InventoryFull]) $ Regular $ Action $ RetrieveItem "key",
Entry (ConditionList []) $ Regular $ Action Leave
], "")
parseWithRest structureElement input `shouldBe` correct
describe "Layouts" $ do
it "can parse layouts" $ do
pending
describe "Lists and blocks" $ do
it "can parse entities" $ do
pending