-
Notifications
You must be signed in to change notification settings - Fork 6
/
4-bird.re
181 lines (161 loc) · 4.18 KB
/
4-bird.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
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.,
};
};
};
type t = {
bird: Bird.t,
time: float,
};
let initialState: t = {bird: Bird.initialState, time: 0.};
type action =
| Flap
| Step(float);
let reducer = (action, state: t) =>
switch (action) {
| Flap => {...state, bird: Bird.flap(state.bird)}
| Step(deltaTime) => {
bird: Bird.applyGravity(deltaTime, state.bird),
time: state.time +. deltaTime,
}
};
};
let%component world = () => {
let%hook (state, dispatch) =
Hooks.reducer(~initialState=State.initialState, State.reducer);
let%hook () =
Hooks.tick(~tickRate=Time.zero, t =>
dispatch(Step(Time.toFloatSeconds(t)))
);
<Center>
<View onMouseDown={_ => dispatch(Flap)}>
<ClipContainer
width=Constants.width
height=Constants.height
color=Colors.cornflowerBlue>
<sky />
<ground time={state.time} />
<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 />);