-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw12.elm
42 lines (36 loc) · 1.09 KB
/
hw12.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import MonsterGame exposing (..)
import Vec2 exposing (Vec2, (.-.), (./))
import Collage exposing (collage)
import Element exposing (toHtml)
import Random exposing (generate)
import Time exposing (Time)
import Html exposing (Html)
import Html.App exposing (program)
import Mouse
import AnimationFrame
type Action = Init Game | Tick Time | Haunt Mouse.Position
canvasW : Int
canvasW = 600
canvasH : Int
canvasH = 600
view : Game -> Html Action
view = drawGame >> collage canvasW canvasH >> toHtml
mouseToCanvas : Mouse.Position -> Vec2
mouseToCanvas {x, y} = (toFloat (x - canvasW//2), toFloat (canvasH//2 - y))
update : Action -> Game -> (Game, Cmd Action)
update action game =
( case action of
Init g -> g
Tick dt -> run (Time.second / 60) game
Haunt mouse -> controlMonster (mouseToCanvas mouse) game
, if game.reset > 0 then Cmd.none else generate Init genGame )
main : Program Never
main = program
{ init = ( game0, generate Init genGame )
, view = view
, update = update
, subscriptions = always <| Sub.batch
[ AnimationFrame.diffs Tick
, Mouse.clicks Haunt
]
}