Created
March 3, 2015 15:10
-
-
Save mverzilli/1162ed14819a056373ca to your computer and use it in GitHub Desktop.
Nautic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html (..) | |
import Html.Attributes (..) | |
import Html.Events (..) | |
import Signal | |
import Graphics.Collage (collage, filled, rect, toForm, move, rotate) | |
import Graphics.Element (Element, image) | |
import Window | |
import Color (..) | |
import Keyboard (arrows) | |
import Text (asText) | |
import Time (fps, Time) | |
type alias Model = { speed: Int, dir: Float, rpms: Int, pos: (Float, Float) } | |
defaultModel = { speed = 0, dir = 0.0, rpms = 0, pos = (0.0, 0.0) } | |
type alias Action = { x:Int, y:Int } | |
pivot : Action -> Model -> Model | |
pivot action model = { model | dir <- model.dir - 3 * (toFloat action.x) } | |
motor action model = { model | rpms <- model.rpms + action.y } | |
motion model = | |
let | |
mod = toFloat model.rpms | |
x = mod * (cos (degrees model.dir)) | |
y = mod * (sin (degrees model.dir)) | |
in | |
{ model | pos <- addV model.pos (x, y) } | |
addV (x,y) (x',y') = (x+x', y+y') | |
update : (Float, Action) -> Model -> Model | |
update (dt, action) = pivot action >> motor action >> motion | |
sea w h = rect (toFloat w) (toFloat h) |> filled blue | |
boat w h model = image (w*2 // 20) (h // 20) "boat_hatoup_top.png" |> toForm |> rotate (degrees (model.dir + 180)) |> move model.pos | |
inspect = asText >> toForm | |
view : (Int, Int) -> Model -> Element | |
view (w,h) model = collage w h | |
[ | |
sea w h | |
, boat w h model | |
, inspect model |> move(0, 200) | |
, inspect (cos (degrees model.dir)) |> move(0,300) | |
, inspect (sin (degrees model.dir)) |> move(0,290) | |
] | |
main : Signal Element | |
main = Signal.map2 view Window.dimensions model | |
input = let delta = Signal.map (\t -> t/20) (fps 25) | |
in Signal.sampleOn delta (Signal.map2 (,) delta arrows) | |
model : Signal Model | |
model = | |
Signal.foldp update defaultModel input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment