Created
September 15, 2017 14:13
-
-
Save dcastro/4e80bdf9d620ef69334ac2a54287fc2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TypeFamilies, FlexibleContexts #-} | |
type Position = (Int, Int) | |
newtype PlayerData = PlayerData { _pos :: Position } | |
data GameStateData = GameStateData PlayerData [Position] | |
-- player typeclass and instance | |
class Player p where | |
playerPosition :: p -> Position | |
playerMoveTo :: Position -> p -> p | |
instance Player PlayerData where | |
playerPosition = _pos | |
playerMoveTo pos player = player { _pos = pos } | |
-- gamestate typeclass and implementation | |
class Player (PlayerType g) => GameState g where | |
type PlayerType g :: * | |
getPlayer :: g -> PlayerType g | |
getMonsterPositions :: g -> [Position] | |
instance GameState GameStateData where | |
type PlayerType GameStateData = PlayerData | |
getPlayer (GameStateData p _) = p | |
getMonsterPositions (GameStateData _ mPoses) = mPoses | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment