Another structure overhaul
This commit is contained in:
parent
3b0de65de1
commit
0720f3b719
14 changed files with 158 additions and 148 deletions
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
|
||||
}
|
29
lib/RPGEngine/Data/State.hs
Normal file
29
lib/RPGEngine/Data/State.hs
Normal file
|
@ -0,0 +1,29 @@
|
|||
-- 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 RPGEngine.Data.State
|
||||
( State(..)
|
||||
, defaultState
|
||||
|
||||
, nextState
|
||||
) where
|
||||
|
||||
import RPGEngine.Data.Types
|
||||
|
||||
----------------------------- Constants ------------------------------
|
||||
|
||||
-- Default state of the game, Menu
|
||||
defaultState :: State
|
||||
defaultState = Menu
|
||||
|
||||
----------------------------------------------------------------------
|
||||
|
||||
-- Get the next state based on the current state
|
||||
nextState :: State -> State
|
||||
nextState Menu = Playing
|
||||
nextState Playing = Pause
|
||||
nextState Pause = Playing
|
||||
nextState _ = Menu
|
||||
|
||||
----------------------------------------------------------------------
|
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)
|
Reference in a new issue