justgook / elm-game-logic / Logic.Component

Component: the raw data for one aspect of the object, and how it interacts with the world. "Labels the Entity as possessing this particular aspect".

Example:

type alias Velocity =
    { x : Float, y : Float }

spec : Component.Spec Velocity { world | v : Component.Set Velocity }
spec =
    Component.Spec .v (\comps world -> { world | v = comps })

empty : Component.Set Velocity
empty =
    Component.empty


type alias Set comp =
Array (Maybe comp)

Component storage, the main building block of the world


type alias Spec comp world =
{ get : world -> Set comp
, set : Set comp -> world -> world 
}

Component specification, how to get Component.Set from the world and set back into the world (mainly used by Systems)

empty : Set comp

Create an empty Component.Set - mostly used to init component sets in the world.

Manipulations

set : EntityID -> a -> Set a -> Set a

Set the component at a particular index. Returns an updated Component.Set. If the index is out of range, the Component.Set is unaltered.

test =
    -- Nothing
    empty |> set 5 10 |> get 5

spawn : EntityID -> a -> Set a -> Set a

Safe way to create a component, same as set, only if an index is out of range Component.Set will be stretched.

test =
    -- Just 5
    empty |> spawn 5 10 |> get 5

remove : EntityID -> Set a -> Set a

Remove component from Component.Set by EntityID, or return unchanged if component not in Set.

get : EntityID -> Set comp -> Maybe comp

Get component for EntityId.

get2 : EntityID -> Set comp -> Set comp2 -> Maybe ( comp, comp2 )

Get components Tuple for EntityId.

update : EntityID -> (comp -> comp) -> Set comp -> Set comp

Update Component by EntityID.

map : (comp -> comp) -> Set comp -> Set comp

Apply a function on every component in a Component.Set.

map sqrt (fromList [ ( 0, 1 ), ( 1, 4 ), ( 2, 9 ) ])
    |> (==) fromList [ ( 0, 1 ), ( 1, 2 ), ( 2, 3 ) ]

filterMap : (comp -> Maybe comp) -> Set comp -> Set comp

Filter out certain components.

List

fromList : List ( EntityID, a ) -> Set a

Create a Component.Set from a List.

Note: Useful for data serialization.

toList : Set a -> List ( EntityID, a )

Convert a Component.Set into an association list of id-component pairs, sorted by id.

Note: Useful for data deserialization.

Dict

fromDict : Dict EntityID a -> Set a

Create a Component.Set from a dictionary.

Note: Useful for data serialization.

toDict : Set a -> Dict EntityID a

Create a dictionary from a Component.Set.

Note: Useful for data deserialization.