Another structure overhaul
This commit is contained in:
parent
3b0de65de1
commit
0720f3b719
14 changed files with 158 additions and 148 deletions
|
@ -5,7 +5,7 @@ module RPGEngine
|
||||||
( playRPGEngine
|
( playRPGEngine
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import RPGEngine.Internals.Data.Game
|
import RPGEngine.Data.Game
|
||||||
import RPGEngine.Render
|
import RPGEngine.Render
|
||||||
import RPGEngine.Input
|
import RPGEngine.Input
|
||||||
|
|
||||||
|
|
17
lib/RPGEngine/Data/Game.hs
Normal file
17
lib/RPGEngine/Data/Game.hs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
-- Representation of all the game's data
|
||||||
|
|
||||||
|
module RPGEngine.Data.Game
|
||||||
|
( Game(..)
|
||||||
|
,initGame
|
||||||
|
) where
|
||||||
|
|
||||||
|
import RPGEngine.Data.Types
|
||||||
|
import RPGEngine.Data.State
|
||||||
|
|
||||||
|
----------------------------------------------------------------------
|
||||||
|
|
||||||
|
-- Initialize the game
|
||||||
|
initGame :: Game
|
||||||
|
initGame = Game {
|
||||||
|
state = defaultState
|
||||||
|
}
|
|
@ -2,21 +2,16 @@
|
||||||
-- e.g. Main menu, game, pause, win or lose
|
-- e.g. Main menu, game, pause, win or lose
|
||||||
-- Allows to easily go to a next state and change rendering accordingly
|
-- Allows to easily go to a next state and change rendering accordingly
|
||||||
|
|
||||||
module RPGEngine.Internals.Data.State
|
module RPGEngine.Data.State
|
||||||
( State(..)
|
( State(..)
|
||||||
, defaultState
|
, defaultState
|
||||||
|
|
||||||
, nextState
|
, nextState
|
||||||
) where
|
) where
|
||||||
|
|
||||||
----------------------------- Constants ------------------------------
|
import RPGEngine.Data.Types
|
||||||
|
|
||||||
-- Current state of the game.
|
----------------------------- Constants ------------------------------
|
||||||
data State = Menu
|
|
||||||
| Playing
|
|
||||||
| Pause
|
|
||||||
| Win
|
|
||||||
| Lose
|
|
||||||
|
|
||||||
-- Default state of the game, Menu
|
-- Default state of the game, Menu
|
||||||
defaultState :: State
|
defaultState :: State
|
115
lib/RPGEngine/Data/Types.hs
Normal file
115
lib/RPGEngine/Data/Types.hs
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
module RPGEngine.Data.Types where
|
||||||
|
|
||||||
|
-------------------------------- Game --------------------------------
|
||||||
|
|
||||||
|
-- TODO Add more
|
||||||
|
data Game = Game {
|
||||||
|
-- Current state of the game
|
||||||
|
state :: State
|
||||||
|
}
|
||||||
|
|
||||||
|
------------------------------- Player -------------------------------
|
||||||
|
|
||||||
|
data Player = Player {
|
||||||
|
playerHp :: Maybe Int,
|
||||||
|
inventory :: [Item]
|
||||||
|
}
|
||||||
|
|
||||||
|
instance Living Player where
|
||||||
|
hp = playerHp
|
||||||
|
|
||||||
|
------------------------------- State --------------------------------
|
||||||
|
|
||||||
|
-- Current state of the game.
|
||||||
|
data State = Menu
|
||||||
|
| Playing
|
||||||
|
| Pause
|
||||||
|
| Win
|
||||||
|
| Lose
|
||||||
|
|
||||||
|
------------------------------- Object -------------------------------
|
||||||
|
|
||||||
|
class Object a where
|
||||||
|
id :: a -> String
|
||||||
|
x :: a -> Int
|
||||||
|
y :: a -> Int
|
||||||
|
name :: a -> String
|
||||||
|
description :: a -> String
|
||||||
|
actions :: a -> [([Condition], Action)]
|
||||||
|
value :: a -> Maybe Int
|
||||||
|
|
||||||
|
class Living a where
|
||||||
|
hp :: a -> Maybe Int
|
||||||
|
|
||||||
|
data Item = Item {
|
||||||
|
itemId :: ItemId,
|
||||||
|
itemX :: Int,
|
||||||
|
itemY :: Int,
|
||||||
|
itemName :: String,
|
||||||
|
itemDescription :: String,
|
||||||
|
itemActions :: [([Condition], Action)],
|
||||||
|
itemValue :: Maybe Int,
|
||||||
|
useTimes :: Maybe Int
|
||||||
|
}
|
||||||
|
|
||||||
|
instance Object Item where
|
||||||
|
id = itemId
|
||||||
|
x = itemX
|
||||||
|
y = itemY
|
||||||
|
name = itemName
|
||||||
|
description = itemDescription
|
||||||
|
actions = itemActions
|
||||||
|
value = itemValue
|
||||||
|
|
||||||
|
data Entity = Entity {
|
||||||
|
entityId :: EntityId,
|
||||||
|
entityX :: Int,
|
||||||
|
entityY :: Int,
|
||||||
|
entityName :: String,
|
||||||
|
entityDescription :: String,
|
||||||
|
entityActions :: [([Condition], Action)],
|
||||||
|
entityValue :: Maybe Int,
|
||||||
|
entityHp :: Maybe Int,
|
||||||
|
direction :: Maybe Direction
|
||||||
|
}
|
||||||
|
|
||||||
|
instance Object Entity where
|
||||||
|
id = entityId
|
||||||
|
x = entityX
|
||||||
|
y = entityY
|
||||||
|
name = entityName
|
||||||
|
description = entityDescription
|
||||||
|
actions = entityActions
|
||||||
|
value = entityValue
|
||||||
|
|
||||||
|
instance Living Entity where
|
||||||
|
hp = entityHp
|
||||||
|
|
||||||
|
type EntityId = String
|
||||||
|
type ItemId = String
|
||||||
|
|
||||||
|
------------------------------ Condition -----------------------------
|
||||||
|
|
||||||
|
data Condition = InventoryFull
|
||||||
|
| InventoryContains ItemId
|
||||||
|
| Not Condition
|
||||||
|
| AlwaysFalse
|
||||||
|
deriving (Show, Eq)
|
||||||
|
|
||||||
|
------------------------------- Action -------------------------------
|
||||||
|
|
||||||
|
data Action = Leave
|
||||||
|
| RetrieveItem ItemId
|
||||||
|
| UseItem ItemId
|
||||||
|
| DecreaseHp EntityId ItemId
|
||||||
|
| IncreasePlayerHp ItemId
|
||||||
|
| Nothing
|
||||||
|
deriving (Show, Eq)
|
||||||
|
|
||||||
|
------------------------------ Direction -----------------------------
|
||||||
|
|
||||||
|
data Direction = North
|
||||||
|
| East
|
||||||
|
| South
|
||||||
|
| West
|
||||||
|
deriving (Show)
|
|
@ -4,9 +4,9 @@ module RPGEngine.Input
|
||||||
( handleAllInput
|
( handleAllInput
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import RPGEngine.Internals.Data.Game
|
import RPGEngine.Data.Types
|
||||||
import RPGEngine.Internals.Data.State
|
import RPGEngine.Data.State
|
||||||
import RPGEngine.Internals.Input
|
import RPGEngine.Input.Core
|
||||||
|
|
||||||
import Graphics.Gloss.Interface.IO.Game
|
import Graphics.Gloss.Interface.IO.Game
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
-- Allows to create a massive inputHandler that can handle anything
|
-- Allows to create a massive inputHandler that can handle anything
|
||||||
-- after you specify what you want it to do.
|
-- after you specify what you want it to do.
|
||||||
|
|
||||||
module RPGEngine.Internals.Input
|
module RPGEngine.Input.Core
|
||||||
( InputHandler(..)
|
( InputHandler(..)
|
||||||
, composeInputHandlers
|
, composeInputHandlers
|
||||||
, handle
|
, handle
|
|
@ -1,25 +0,0 @@
|
||||||
-- Representation of all the game's data
|
|
||||||
|
|
||||||
module RPGEngine.Internals.Data.Game
|
|
||||||
( Game(..),
|
|
||||||
|
|
||||||
initGame
|
|
||||||
) where
|
|
||||||
|
|
||||||
import RPGEngine.Internals.Data.State
|
|
||||||
|
|
||||||
----------------------------- Constants ------------------------------
|
|
||||||
|
|
||||||
-- TODO Add more
|
|
||||||
data Game = Game {
|
|
||||||
-- Current state of the game
|
|
||||||
state :: State
|
|
||||||
}
|
|
||||||
|
|
||||||
----------------------------------------------------------------------
|
|
||||||
|
|
||||||
-- Initialize the game
|
|
||||||
initGame :: Game
|
|
||||||
initGame = Game {
|
|
||||||
state = defaultState
|
|
||||||
}
|
|
|
@ -1,77 +0,0 @@
|
||||||
-- Represents an item in the game.
|
|
||||||
|
|
||||||
module RPGEngine.Internals.Data.Internals
|
|
||||||
( Action(..)
|
|
||||||
, Condition(..)
|
|
||||||
, Object(..)
|
|
||||||
, EntityId
|
|
||||||
, ItemId
|
|
||||||
) where
|
|
||||||
|
|
||||||
----------------------------- Constants ------------------------------
|
|
||||||
|
|
||||||
type EntityId = String
|
|
||||||
type ItemId = String
|
|
||||||
|
|
||||||
data Object =
|
|
||||||
Item { -- All fields are required
|
|
||||||
-- Easy way to identify items
|
|
||||||
id :: ItemId,
|
|
||||||
-- Horizontal coördinate in the level
|
|
||||||
x :: Int,
|
|
||||||
-- Vertical coördinate in the level
|
|
||||||
y :: Int,
|
|
||||||
name :: String,
|
|
||||||
-- Short description of the object
|
|
||||||
description :: String,
|
|
||||||
-- Counts how often the object can be used by the player. Either
|
|
||||||
-- infinite or a natural number
|
|
||||||
useTimes :: Maybe Int,
|
|
||||||
-- List of conditional actions when the player is standing on this object
|
|
||||||
actions :: [([Condition], Action)],
|
|
||||||
-- Interpretation depends on action with this object.
|
|
||||||
value :: Maybe Int
|
|
||||||
}
|
|
||||||
| Entity {
|
|
||||||
-- Required fields
|
|
||||||
-- Easy way to identify items
|
|
||||||
id :: EntityId,
|
|
||||||
-- Horizontal coördinate in the level
|
|
||||||
x :: Int,
|
|
||||||
-- Vertical coördinate in the level
|
|
||||||
y :: Int,
|
|
||||||
name :: String,
|
|
||||||
-- Short description of the object
|
|
||||||
description :: String,
|
|
||||||
-- List of conditional actions when the player is standing on this object
|
|
||||||
actions :: [([Condition], Action)],
|
|
||||||
-- Optional fields
|
|
||||||
-- The direction of the item. e.g. a door has a direction.
|
|
||||||
direction :: Maybe Direction,
|
|
||||||
-- Some entities have health points.
|
|
||||||
hp :: Maybe Int,
|
|
||||||
-- Interpretation depends on action with this object.
|
|
||||||
value :: Maybe Int
|
|
||||||
}
|
|
||||||
|
|
||||||
data Direction = North
|
|
||||||
| East
|
|
||||||
| South
|
|
||||||
| West
|
|
||||||
deriving (Show)
|
|
||||||
|
|
||||||
data Action = Leave
|
|
||||||
| RetrieveItem ItemId
|
|
||||||
| UseItem ItemId
|
|
||||||
| DecreaseHp EntityId ItemId
|
|
||||||
| IncreasePlayerHp ItemId
|
|
||||||
| Nothing
|
|
||||||
deriving (Show, Eq)
|
|
||||||
|
|
||||||
data Condition = InventoryFull
|
|
||||||
| InventoryContains ItemId
|
|
||||||
| Not Condition
|
|
||||||
| AlwaysFalse
|
|
||||||
deriving (Show, Eq)
|
|
||||||
|
|
||||||
----------------------------------------------------------------------
|
|
|
@ -1,15 +0,0 @@
|
||||||
-- Represents a player in the game. This player can move around, pick
|
|
||||||
-- up items and interact with the world.
|
|
||||||
|
|
||||||
module RPGEngine.Internals.Data.Player
|
|
||||||
( Player(..)
|
|
||||||
) where
|
|
||||||
|
|
||||||
import RPGEngine.Internals.Data.Internals
|
|
||||||
|
|
||||||
----------------------------- Constants ------------------------------
|
|
||||||
|
|
||||||
data Player = Player {
|
|
||||||
hp :: Int,
|
|
||||||
inventory :: [Object]
|
|
||||||
}
|
|
|
@ -1,6 +1,6 @@
|
||||||
module RPGEngine.Parse where
|
module RPGEngine.Parse where
|
||||||
|
|
||||||
import RPGEngine.Internals.Data.Game
|
import RPGEngine.Data.Types
|
||||||
|
|
||||||
-- TODO parseFromFile gebruiken
|
-- TODO parseFromFile gebruiken
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
module RPGEngine.Internals.Parse where
|
module RPGEngine.Parse.Core where
|
||||||
|
|
||||||
import Text.Parsec
|
import Text.Parsec
|
||||||
import Text.Parsec.String
|
import Text.Parsec.String
|
|
@ -1,7 +1,7 @@
|
||||||
module RPGEngine.Internals.Parse.StructureElement where
|
module RPGEngine.Parse.StructureElement where
|
||||||
|
|
||||||
import RPGEngine.Internals.Data.Internals (Action(..), Condition(..))
|
import RPGEngine.Data.Types (Action(..), Condition(..))
|
||||||
import RPGEngine.Internals.Parse ( ignoreWS )
|
import RPGEngine.Parse.Core ( ignoreWS )
|
||||||
|
|
||||||
import Text.Parsec
|
import Text.Parsec
|
||||||
( char,
|
( char,
|
||||||
|
@ -134,7 +134,7 @@ action = try $ do
|
||||||
| script == "useItem" = UseItem arg
|
| script == "useItem" = UseItem arg
|
||||||
| script == "decreaseHp" = DecreaseHp first second
|
| script == "decreaseHp" = DecreaseHp first second
|
||||||
| script == "increasePlayerHp" = IncreasePlayerHp arg
|
| script == "increasePlayerHp" = IncreasePlayerHp arg
|
||||||
| otherwise = RPGEngine.Internals.Data.Internals.Nothing
|
| otherwise = RPGEngine.Data.Types.Nothing
|
||||||
(first, ',':second) = break (== ',') arg
|
(first, ',':second) = break (== ',') arg
|
||||||
return $ Action answer
|
return $ Action answer
|
||||||
|
|
|
@ -7,8 +7,7 @@ module RPGEngine.Render
|
||||||
, render
|
, render
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import RPGEngine.Internals.Data.Game(Game(..))
|
import RPGEngine.Data.Types
|
||||||
import RPGEngine.Internals.Data.State(State(..))
|
|
||||||
import Graphics.Gloss
|
import Graphics.Gloss
|
||||||
|
|
||||||
----------------------------- Constants ------------------------------
|
----------------------------- Constants ------------------------------
|
||||||
|
|
|
@ -13,17 +13,18 @@ library
|
||||||
exposed-modules:
|
exposed-modules:
|
||||||
RPGEngine
|
RPGEngine
|
||||||
|
|
||||||
RPGEngine.Input
|
RPGEngine.Data.Game
|
||||||
RPGEngine.Parse
|
RPGEngine.Data.Types
|
||||||
RPGEngine.Render
|
RPGEngine.Data.State
|
||||||
|
|
||||||
RPGEngine.Internals.Data.Game
|
RPGEngine.Input
|
||||||
RPGEngine.Internals.Data.Internals
|
RPGEngine.Input.Core
|
||||||
RPGEngine.Internals.Data.Player
|
|
||||||
RPGEngine.Internals.Data.State
|
RPGEngine.Parse
|
||||||
RPGEngine.Internals.Input
|
RPGEngine.Parse.Core
|
||||||
RPGEngine.Internals.Parse
|
RPGEngine.Parse.StructureElement
|
||||||
RPGEngine.Internals.Parse.StructureElement
|
|
||||||
|
RPGEngine.Render
|
||||||
|
|
||||||
executable rpg-engine
|
executable rpg-engine
|
||||||
main-is: Main.hs
|
main-is: Main.hs
|
||||||
|
|
Reference in a new issue