Orasund / elm-game-essentials / Direction


type Direction
    = Up
    | Down
    | Left
    | Right

An abstract concept of a direction on a grid.

asList : ( Direction, List Direction )

Create a list of all directions

mirror : Direction -> Direction

Rotates a Direction for 180 Degrees.

Up
|> mirror
--> Down

Left
|> mirror
--> Right

rotateLeftwise : Direction -> Direction

Rotates a Direction clockwise

Up
    |> rotateLeftwise
    --> Left

rotateRightwise : Direction -> Direction

Rotates a Direction counter-clockwise

Up
    |> rotateRightwise
    --> Right

fromCoord : { x : Basics.Int, y : Basics.Int } -> Maybe Direction

Convert coordinates into a direction by comparing the sign

{ x = 0, y = 1 }
    |> fromCoord
    --> Just Down

{ x = 0, y = -1 }
    |> fromCoord
    --> Just Up

{ x = 1, y = 0 }
    |> fromCoord
    --> Just Right

{ x = -1, y = 0 }
    |> fromCoord
    --> Just Left

{ x = 1, y = 1 }
    |> fromCoord
    --> Nothing

toAngle : Direction -> Basics.Float

Convert a direction into an angle.

toCoord : Direction -> { x : Basics.Int, y : Basics.Int }

Convert a Direction into a coord.

list : List Direction
list =
    asList
    |> (\(head,tail) -> head :: tail)

list
    |> List.map toCoord
    |> List.filterMap fromCoord
    --> list

Right
    |> toCoord
    --> {x = 1, y = 0}