-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.elm
169 lines (140 loc) · 4.44 KB
/
View.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
module View exposing (view)
import Collage exposing (Form)
import Color
import Element exposing (Element)
import Html exposing (Html, div, node)
import Html.Attributes exposing (href, rel, style)
import Model exposing (GameState(..), Model, Tree)
import Shared exposing (..)
import Text
import Update exposing (Msg)
yOffset : Float
yOffset =
gameHeight / 2
xOffset : Float
xOffset =
gameWidth / 2
position : ( Float, Float ) -> Element -> Form
position ( x, y ) element =
Element.container gameWidth gameHeight Element.bottomLeft element
|> Collage.toForm
|> Collage.move ( x, y )
view : Model -> Html Msg
view model =
div
[ style
[ ( "margin", "auto" )
]
]
[ [ Element.image gameWidth gameHeight "images/bg.png"
|> Collage.toForm
, movingGround model.groundPosition
, power model.power
]
++ trees model.trees
++ [ text model
, player model
]
|> Collage.collage gameWidth gameHeight
|> Element.toHtml
]
text : Model -> Form
text { score, highscore, gameState } =
let
txt size x y string color =
Text.fromString string
|> Text.typeface [ "Press Start 2P", "cursive" ]
|> Text.height size
|> Text.color color
|> Collage.text
|> Collage.move ( x, y )
text size height string =
Collage.group
[ txt size 0 height string Color.black
, txt size -3 (height + 3) string Color.white
]
in
case gameState of
Playing ->
text 50 (gameHeight / 4) (toString score)
_ ->
Collage.group
[ text 25
((gameHeight / 3) + 20)
"score"
, text 25
((gameHeight / 3) - 20)
(toString score)
, text 25
((gameHeight / 3) - 80)
"best"
, text 25
((gameHeight / 3) - 120)
(toString highscore)
, text
25
((gameHeight / 3) - 250)
"Press space"
]
movingGround : Float -> Form
movingGround groundPosition =
Element.tiledImage (gameWidth + 50) 12 "images/moving_ground.png"
|> Collage.toForm
|> Collage.move ( groundPosition, -254 )
player : Model -> Form
player { height, rotation, gameState, xPos } =
let
img =
case gameState of
Playing ->
"frog1"
Waiting ->
"frog2"
in
Element.image playerWidth playerHeight ("images/" ++ img ++ ".png")
|> Collage.toForm
|> Collage.rotate (degrees rotation)
|> List.singleton
|> Collage.collage playerWidth playerHeight
|> position ( xPos, height )
power : Float -> Collage.Form
power power =
let
powerScale =
(gameWidth - 32) / maxPower
powerWidth =
power * powerScale
in
Element.tiledImage (floor powerWidth) 20 "images/power.png"
|> position ( 16, 16 )
trees : List Tree -> List Collage.Form
trees =
List.map
(\{ holeHeight, xPos } ->
let
treeEndPart =
Element.image treeWidth 4
treeEndElement dir height =
Element.flow dir
[ Element.tiledImage treeWidth height "images/tree_trunk.png"
, treeEndPart "images/tree_end1.png"
, treeEndPart "images/tree_end2.png"
]
treeEndHeight =
8
topTreeHeight =
gameHeight - holeHeight
bottomTreeHeight =
gameHeight - topTreeHeight - holeSize - groundHeight
topTree =
treeEndElement Element.down (topTreeHeight - treeEndHeight)
bottomTree =
treeEndElement Element.up (bottomTreeHeight - treeEndHeight)
in
Element.flow Element.down
[ topTree
, Element.spacer treeWidth holeSize
, bottomTree
]
|> position ( xPos, groundHeight )
)