justgook/elm-game-logic/2.0.0

NOTE: the latest version is 3.0.0

Logic.System

System: main logic driver, that is used to step on each game-loop and update World

type alias System world =
world -> world
update : Spec comp world -> (Set comp -> Set comp) -> world -> world

Update whole Component.Set

step : (comp -> comp) -> Spec comp world -> world -> world

Single component mapping, Same asList.map - only for Component.Set inside World

gravitySystem =
    Logic.System.step (Vec2.add gravity) accelerationSpec
( ( comp1, comp1 -> System { c | a : Set comp1 } )
-> ( comp2, comp2 -> System { d | b : Set comp2 } )
-> System
{ a : Set comp1
, b : Set comp2
}
)
-> Spec comp1 world
-> Spec comp2 world
-> world
-> world

Step over all entities that have both components.

Example:

system =
    Logic.System.step2
        (\( v, _ ) ( p, setP ) -> setP (Vec2.add v p))
        velocitySpec
        positionSpec
( ( comp1, comp1 -> System { a | a : Set comp1 } )
-> ( comp2, comp2 -> System { a | b : Set comp2 } )
-> ( comp3, comp3 -> System { a | c : Set comp3 } )
-> System
{ a : Set comp1
, b : Set comp2
, c : Set comp3
}
)
-> Spec comp1 world
-> Spec comp2 world
-> Spec comp3 world
-> world
-> world

Same as step2 only with 3 components

( ( comp1, comp1 -> System { a | a : Set comp1 } )
-> ( comp2, comp2 -> System { a | b : Set comp2 } )
-> ( comp3, comp3 -> System { a | c : Set comp3 } )
-> ( comp4, comp4 -> System { a | d : Set comp4 } )
-> System
{ a : Set comp1
, b : Set comp2
, c : Set comp3
, d : Set comp4
}
)
-> Spec comp1 world
-> Spec comp2 world
-> Spec comp3 world
-> Spec comp4 world
-> world
-> world

Same as step2 only with 4 components

( ( comp1, comp1 -> System { a | a : Set comp1 } )
-> ( comp2, comp2 -> System { a | b : Set comp2 } )
-> ( comp3, comp3 -> System { a | c : Set comp3 } )
-> ( comp4, comp4 -> System { a | d : Set comp4 } )
-> ( comp5, comp5 -> System { a | e : Set comp5 } )
-> System
{ a : Set comp1
, b : Set comp2
, c : Set comp3
, d : Set comp4
, e : Set comp5
}
)
-> Spec comp1 world
-> Spec comp2 world
-> Spec comp3 world
-> Spec comp4 world
-> Spec comp5 world
-> world
-> world

Same as step2 only with 5 components

foldl : (comp1 -> acc -> acc) -> Set comp1 -> acc -> acc

Reduce an Component.Set from the left.

Example count how much enemies left in world:

enemySet =
    enemySpec.get world


count =
    foldl (\_ -> (+) 1) enemySet 0
foldl2 : (comp1 -> comp2 -> acc -> acc) -> Set comp1 -> Set comp2 -> acc -> acc

Step over all entities that have both components and reduce an Component.Sets from the left.

(comp1 -> comp2 -> comp3 -> acc -> acc)
-> Set comp1
-> Set comp2
-> Set comp3
-> acc
-> acc

Same as foldl2 only with 3 components

(comp1 -> comp2 -> comp3 -> comp4 -> acc -> acc)
-> Set comp1
-> Set comp2
-> Set comp3
-> Set comp4
-> acc
-> acc

Same as foldl2 only with 4 components

(comp1 -> comp2 -> comp3 -> comp4 -> acc -> acc)
-> Set comp1
-> Set comp2
-> Set comp3
-> Set comp4
-> acc
-> acc

Same as foldl2 only with 5 components

indexedFoldl : (EntityID -> comp1 -> acc -> acc) -> Set comp1 -> acc -> acc

Variant of foldl that passes the index of the current element to the step function.

indexedFoldl is to foldl as List.indexedMap is to List.map.

(EntityID -> comp1 -> comp2 -> acc -> acc)
-> Set comp1
-> Set comp2
-> acc
-> acc
(EntityID -> comp1 -> comp2 -> comp3 -> acc -> acc)
-> Set comp1
-> Set comp2
-> Set comp3
-> acc
-> acc

Same as indexedFoldl2 only with 3 components

(EntityID -> comp1 -> comp2 -> comp3 -> comp4 -> acc -> acc)
-> Set comp1
-> Set comp2
-> Set comp3
-> Set comp4
-> acc
-> acc

Same as indexedFoldl2 only with 4 components

(EntityID -> comp1 -> comp2 -> comp3 -> comp4 -> comp5 -> acc -> acc)
-> Set comp1
-> Set comp2
-> Set comp3
-> Set comp4
-> Set comp5
-> acc
-> acc

Same as indexedFoldl2 only with 5 components

Util

applyIf : Bool -> (a -> a) -> a -> a

Just nice helper function to pipe into systems

update msg world =
    world
        |> system1
        |> applyIf (msg === KeyUp "a") systemMoveLeft
        |> system2
applyMaybe : Maybe a -> (a -> c -> c) -> c -> c

Same as applyIf, but works with Maybe

update msg world =
    world
        |> system1
        |> applyMaybe (decode saveDecoder msg) loadGame
        |> system2