-
Notifications
You must be signed in to change notification settings - Fork 1
/
Koala.elm
144 lines (101 loc) · 2.68 KB
/
Koala.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
module Koala exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Keyboard.Extra
import List exposing (..)
import Time exposing (Time, second)
import Vector exposing (Vector)
import Game exposing (Game)
-- import Random
config :
{ fps : Float
}
config =
{ fps = 60
}
main : Program Never Model Msg
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ keyboardModel : Keyboard.Extra.Model
, game : Game
, arrows : Vector
}
type Msg
= KeyboardMsg Keyboard.Extra.Msg
| Tick Time
| GameMsg Game.Msg
initialKeyboard : Keyboard.Extra.Model
initialKeyboard =
Tuple.first Keyboard.Extra.init
initialModel : Model
initialModel =
Model initialKeyboard Game.initial Vector.initial
init : ( Model, Cmd Msg )
init =
( initialModel, Cmd.none )
-- UPDATE
handleKeyboard : Model -> Keyboard.Extra.Msg -> ( Model, Cmd Msg )
handleKeyboard model keyMsg =
let
( keyboardModel, keyboardCmd ) =
Keyboard.Extra.update keyMsg model.keyboardModel
in
( { model
| keyboardModel = keyboardModel
, arrows = Keyboard.Extra.arrows keyboardModel
}
, Cmd.map KeyboardMsg keyboardCmd
)
generateRandomEnemies : Time -> Model -> Cmd Msg
generateRandomEnemies time model =
if floor (Time.inMilliseconds time) % 100 == 0 then
model.game
|> Game.generateRandomEnemies
|> Cmd.map GameMsg
else
Cmd.none
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
KeyboardMsg keyMsg ->
handleKeyboard model keyMsg
Tick newTime ->
( { model | game = Game.step model.game model.arrows }
, model |> generateRandomEnemies newTime
)
GameMsg msg ->
( { model | game = Game.update msg model.game }, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Sub.map KeyboardMsg Keyboard.Extra.subscriptions
, Time.every (second / config.fps) Tick
]
title : Model -> Html Msg
title model =
h1 [ style [ ( "position", "absolute" ) ] ]
[ ("score : "
++ (((model.game.enemies |> length) - 1)
|> toString
)
)
|> text
, " hi-score : " |> text
, model.game.hiScore |> toString |> text
]
-- VIEW
view : Model -> Html Msg
view model =
div []
[ model |> title
, Game.view model.game
|> Html.map GameMsg
]