How to model a 2D world in Haskell

I am making a game. The game consists of an infinite plane. Units must be on a discrete square, so they can be arranged with a simpleLocation { x :: Int, y :: Int }

There can be many kinds of Units. Some of them may be creatures, and some may just be objects, for example, stone or wood (I think 2d minecraft is there). Many will be empty (just grass or something else).

How would you model this in Haskell? I thought about doing below, but what about Object vs Creature? can they have different fields? Normalize them all on Unit?

data Unit = Unit { x :: Int, y :: Int, type :: String, ... many shared properties... }

I also looked at the type of location.

data Location = Location { x :: Int, y :: Int, unit :: Unit } 
-- or this
data Location = Location { x :: Int, y :: Int }
data Unit = Unit { unitFields... , location :: Location }

Do you have any ideas? In OO, I probably would have Locationeither Unitinherited from another, and made certain types of Unit inherit from each other.

, , JSON .

+5
1

Location - Point.

Unit , ; Map Location Unit ( -) .

Unit go, , , :

data UnitInfo = UnitInfo { ... }

data BlockType = Grass | Wood | ...

data Unit
  = NPC UnitInfo AgentID
  | Player UnitInfo PlayerID
  | Block UnitInfo BlockType

.

, "" (.. , " ?", , , "", , ).

String "" a Unit Haskell; , , .

JSON , , Haskell String , . , ; JSON. , ADT, "" .., :

-- records containing functions to describe arbitrary behaviour; see FAQ entry
data BlockOps = BlockOps { ... }
data CreatureOps = CreatureOps { ... }
data Block = Block { ... }
data Creature = Creature { ... }
data Unit = BlockUnit Block | CreatureUnit Creature
newtype GameField = GameField (Map Point Unit)

-- these types are sent over the network, and mapped *back* to the "rich" but
-- non-transferable structures describing their behaviour; of course, this means
-- that BlockOps and CreatureOps must contain a BlockType/CreatureType to map
-- them back to this representation
data BlockType = Grass | Wood | ...
data CreatureType = ...
blockTypes :: Map BlockType BlockOps
creatureTypes :: Map CreatureType CreatureOps

, .

, ; . - , , . , , , ; , , , , .

+7

All Articles