-
Notifications
You must be signed in to change notification settings - Fork 6
/
5-obstacles.re
252 lines (222 loc) · 6.23 KB
/
5-obstacles.re
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
open Revery;
open Revery.Math;
open Revery.UI;
open Revery.UI.Components;
module Assets = {
module Sky = {
let height = 128;
let width = 256;
let image = `File("sky.png");
};
module Pipe = {
let width = 90;
let height = 480;
let imageUp = `File("PipeUp.png");
let imageDown = `File("PipeDown.png");
};
module Bird = {
let height = 32;
let width = 32;
let image01 = `File("bird-01.png");
let image02 = `File("bird-02.png");
let image03 = `File("bird-03.png");
let image04 = `File("bird-04.png");
};
module Land = {
let image = `File("land.png");
let width = 256;
let height = 32;
};
};
module Constants = {
let gravity = 2000.0;
let floorHeight = 32;
let flapForce = (-250.0);
/* Speed (float) - horizontal speed of the flappy bird */
let speedF = 100.;
/* width (int) - the width of our 'game surface' */
let width = 800;
let height = 600;
let birdX = 50;
let pipeGap = 200;
};
let bird = (~y, ()) => {
<Positioned top=y left=Constants.birdX>
<Image
src=Assets.Bird.image01
width=Assets.Bird.width
height=Assets.Bird.height
/>
</Positioned>;
};
let ground = (~time, ()) => {
let parallax =
(-1.0)
*. mod_float(time *. Constants.speedF, float_of_int(Assets.Land.width))
|> int_of_float;
<Positioned bottom=0 left=0>
<Stack width=Constants.width height=Constants.floorHeight>
<Positioned bottom=0 left=parallax>
<Image
src=Assets.Land.image
width=Constants.width
height=Assets.Land.height
resizeMode=ImageResizeMode.Repeat
/>
</Positioned>
<Positioned bottom=0 left={parallax + Assets.Land.width}>
<Image
src=Assets.Land.image
width=Constants.width
height=Assets.Land.height
resizeMode=ImageResizeMode.Repeat
/>
</Positioned>
</Stack>
</Positioned>;
};
let sky = () => {
<Positioned bottom=0 left=0>
<Image
src=Assets.Sky.image
width=Constants.width
height=Assets.Sky.height
resizeMode=ImageResizeMode.Repeat
/>
</Positioned>;
};
let fontRoboto = Font.Family.system("Roboto-Regular.ttf");
let textStyle = Style.[color(Colors.white)];
module State = {
module Bird = {
type t = {
position: float,
velocity: float,
acceleration: float,
};
let minimumPosition =
Constants.height
- Assets.Land.height
- Assets.Bird.height
|> float_of_int;
let initialState: t = {position: 300., velocity: 100., acceleration: 0.};
let applyGravity = (time: float, bird: t) => {
let velocity = bird.velocity +. bird.acceleration *. time;
let acceleration = bird.acceleration +. Constants.gravity *. time;
let position =
min(minimumPosition, bird.position +. bird.velocity *. time);
{position, velocity, acceleration};
};
let flap = (bird: t) => {
{
position: bird.position,
velocity: Constants.flapForce,
acceleration: 0.,
};
};
};
module Pipe = {
type t = {
top: Rectangle.t,
bottom: Rectangle.t,
};
let create = (~x, ~height as h, ()) => {
let width = Assets.Pipe.width |> float_of_int;
let height = Assets.Pipe.height |> float_of_int;
let almostRect = Rectangle.create(~x, ~width, ~height);
let topY =
float_of_int(Constants.height - Constants.pipeGap) -. h -. height;
let bottomY = float_of_int(Constants.height) -. h;
let top = almostRect(~y=topY);
let bottom = almostRect(~y=bottomY);
{top, bottom};
};
let getX = (pipe: t) => {
Rectangle.getX(pipe.top);
};
let step = (t: float, pipe: t) => {
let translate = Rectangle.translate(~x=Constants.speedF *. t *. (-1.));
{top: translate(pipe.top), bottom: translate(pipe.bottom)};
};
};
type t = {
pipes: list(Pipe.t),
bird: Bird.t,
time: float,
};
let initialState: t = {pipes: [], bird: Bird.initialState, time: 0.};
type action =
| CreatePipe(float)
| Flap
| Step(float);
let reducer = (action, state: t) =>
switch (action) {
| CreatePipe(height) =>
let pipe = Pipe.create(~x=float_of_int(Constants.width), ~height, ());
let pipes = [pipe, ...state.pipes];
{...state, pipes};
| Flap => {...state, bird: Bird.flap(state.bird)}
| Step(deltaTime) => {
pipes: List.map(Pipe.step(deltaTime), state.pipes),
bird: Bird.applyGravity(deltaTime, state.bird),
time: state.time +. deltaTime,
}
};
};
let pipe = (~pipe: State.Pipe.t, ()) => {
let x = State.Pipe.getX(pipe) |> int_of_float;
let topY = Rectangle.getY(pipe.top) |> int_of_float;
let bottomY = Rectangle.getY(pipe.bottom) |> int_of_float;
<Positioned top=0 left=x>
<Stack width=Assets.Pipe.width height=Constants.height>
<Positioned top=topY left=0>
<Image
src=Assets.Pipe.imageDown
width=Assets.Pipe.width
height=Assets.Pipe.height
/>
</Positioned>
<Positioned top=bottomY left=0>
<Image
src=Assets.Pipe.imageUp
width=Assets.Pipe.width
height=Assets.Pipe.height
/>
</Positioned>
</Stack>
</Positioned>;
};
let%component world = () => {
let%hook (state, dispatch) =
Hooks.reducer(~initialState=State.initialState, State.reducer);
let pipes =
state.pipes |> List.map(p => <pipe pipe=p />) |> React.listToElement;
let%hook () =
Hooks.tick(~tickRate=Time.zero, t =>
dispatch(Step(Time.toFloatSeconds(t)))
);
let%hook () =
Hooks.tick(~tickRate=Time.seconds(4), _ =>
dispatch(CreatePipe(Random.float(float_of_int(Assets.Pipe.height))))
);
<Center>
<View onMouseDown={_ => dispatch(Flap)}>
<ClipContainer
width=Constants.width
height=Constants.height
color=Colors.cornflowerBlue>
<sky />
<ground time={state.time} />
<View> ...pipes </View>
<bird y={int_of_float(state.bird.position)} />
<Text
style=textStyle
fontFamily=fontRoboto
fontSize=24.
text={"Time: " ++ string_of_float(state.time)}
/>
</ClipContainer>
</View>
</Center>;
};
Playground.render(<world />);