Zinggi/elm-2d-game/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Game.TwoD.Camera

This provides a basic camera.

You can also create your own camera type if you wish. To do so, have a look at the source of this file.

camera creation

type Camera

A camera represents how to render the virtual world. It's essentially a transformation from virtual game coordinates to pixel coordinates on the screen.

fixedArea : Float -> ( Float, Float ) -> Camera

A camera that always shows the same area. This is useful in a top down game. This means that you probably want to specify the area property like this:

fixedArea (16*10) (x, y)

This would show 16 by 10 units IF the game is displayed in a 16:10 viewport. But in a 4:3 viewport it would show sqrt(16104/3)=14.6 by sqrt(16103/4)=10.95 units.

fixedWidth : Float -> ( Float, Float ) -> Camera

A camera that always shows width units of your game horizontally. Well suited for a side-scroller.

fixedHeight : Float -> ( Float, Float ) -> Camera

A camera that always shows height units of your game vertically. Well suited for a vertical scroller.

custom : (( Float, Float ) -> ( Float, Float )) -> ( Float, Float ) -> Camera

The custom camera allows you to use a function that maps viewport size (in pixel) to game units. E.g. here's an implementation of the fixedWidth camera using custom:

fixedWidth width =
    custom (\(w, h) -> (width, width * h / w))

manipulate camera

getPosition : Camera -> ( Float, Float )
moveBy : ( Float, Float ) -> Camera -> Camera

Move a camera by the given vector relative to the camera.

moveTo : ( Float, Float ) -> Camera -> Camera

Move a camera to the given location. In absolute coordinates.

follow : Float -> Float -> ( Float, Float ) -> Camera -> Camera

Smoothly follow the given target. Use this in your tick function.

follow 1.5 dt target camera

view : Camera -> ( Float, Float ) -> Mat4

Gets the transformation that represents how to transform the camera back to the origin. The result of this is used in the vertex shader.

getViewSize : ( Float, Float ) -> Camera -> ( Float, Float )

Given the screen size, gets the width and height in game units