Orasund/elm-game-essentials/2.0.0

NOTE: the latest version is 3.0.0

Direction

= Up
| Down
| Left
| Right

A abstract concept of a direction on a grid.

Rotates a Direction for 180 Degrees.

Up
|> mirror
--> Down


Left
|> mirror
--> Right

Rotates a Direction clockwise

Up
    |> rotateLeftwise
    --> Left

Rotates a Direction counter-clockwise

Up
    |> rotateRightwise
    --> Right
{ x : Int
, y : 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 -> Float

Convert a direction into an angle.

Right
|> toAngle
--> 0


Up
|> toAngle
--> pi / 2


Left
|> toAngle
--> pi


Down
|> toAngle
--> 3 * pi / 2
-> { x : Int
, y : Int
}

Convert a Direction into a coord.

let
    list = asList
        |> (\(head,tail) -> head :: tail)
in
    list
    |> List.map toCoord
    |> List.map fromCoord
    --> list


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