/
gameplay.cpp
346 lines (291 loc) · 10.9 KB
/
gameplay.cpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "gameplay.h"
#include "config.h"
#include "entt_helper.h"
#include "entt_types.h"
#include "image.h"
#include "lane_position.h"
#include "rng.h"
#include "screencoord.h"
#include "strategy.h"
#include "trail.h"
#include "ufo.h"
#include <entt/entt.hpp>
#if _MSC_VER < 1928 // Visual Studio 2019 version 16.8
constexpr double pi = 3.14159265359;
#else
#include <numbers>
constexpr double pi = std::numbers::pi;
#endif // _MSC_VER
namespace {
using namespace moo;
template<class T>
[[nodiscard]] auto get_closest_cow_if(
const ScreenCoord& pos,
entt::registry& registry,
const T& pred
) -> std::optional<entt::entity>
{
const auto cows = registry.view<IsCow, LanePosition>();
if (cows.empty())
return std::nullopt;
entt::entity closest_entity;
double closest_dist = 10.0;
cows.each([&](entt::entity entity, const LanePosition& cow_pos) {
const double dist = std::abs(cow_pos.m_x_pos - pos.x);
if (dist < closest_dist && pred(cow_pos)) {
closest_entity = entity;
closest_dist = dist;
}
});
if (!registry.valid(closest_entity))
return std::nullopt;
return closest_entity;
}
/// <summary>First look for closest cow on the right. Then look everywhere.</summary>
[[nodiscard]] auto get_closest_cow(
const ScreenCoord& pos,
entt::registry& registry
) -> std::optional<entt::entity>
{
const auto is_right = [&](const LanePosition& cow_pos) {
return cow_pos.m_x_pos > pos.x;
};
const auto always_true = [](const LanePosition&) {return true; };
const auto closest_right_cow = get_closest_cow_if(pos, registry, is_right);
if (closest_right_cow.has_value())
return closest_right_cow.value();
return get_closest_cow_if(pos, registry, always_true);
}
auto get_new_ufo_pos(
const ScreenCoord& ufo_pos,
const double ufo_speed,
const PixelCoord& target,
const Seconds& dt
) -> ScreenCoord
{
const ScreenCoord pos_diff = get_screen_coord(target) - ufo_pos;
const ScreenCoord pos_change = dt.m_value * ufo_speed * get_normalized(pos_diff);
return ufo_pos + pos_change;
}
[[nodiscard]] auto get_cow_width(entt::registry& registry) -> double {
const auto cow_anim_ett = registry.view<CowAnimation>().front();
const CowAnimation& cow_animation = registry.get<CowAnimation>(cow_anim_ett);
const double fractional_cow_bitmap_width = 1.0 * cow_animation.m_width / static_columns;
return fractional_cow_bitmap_width;
}
[[nodiscard]] auto get_ufo_speed(const int level) -> double {
return get_config().ufo_base_speed + level * get_config().ufo_speed_increment;
}
auto add_trail_puffs(
const Bullet& bullet,
Trail& trail,
const Seconds& dt
) -> void
{
const double path_progress = get_length(bullet.m_pos - bullet.m_initial_pos);
const ScreenCoord pos_change = dt.m_value * (bullet.m_bullet_speed * bullet.m_trajectory + bullet.m_gravity_speed);
const ScreenCoord new_pos = bullet.m_pos + pos_change;
trail.add_puff(new_pos, bullet.m_pos, path_progress);
}
auto add_puff(
entt::registry& registry,
const ScreenCoord& pos
) -> void
{
auto entity = registry.create();
constexpr double puff_variation = 0.02;
const std::uniform_real_distribution<double> pos_var_dist(-puff_variation, puff_variation);
const std::uniform_int_distribution<> green_dist(0, 255);
const ScreenCoord puff_pos = pos + ScreenCoord{ pos_var_dist(get_rng()), pos_var_dist(get_rng()) };
const RGB color{ 255, static_cast<unsigned char>(green_dist(get_rng())), 0 };
registry.emplace<IsPuff>(entity);
registry.emplace<ScreenCoord>(entity, puff_pos);
registry.emplace<RGB>(entity, color);
}
} // namespace {}
auto moo::set_ufo_abducting(
Ufo& ufo,
entt::registry& registry
) -> void
{
const auto closest_cow = get_closest_cow(ufo.m_pos, registry);
if (!closest_cow.has_value()) {
ufo.m_strategy = moo::Shoot{};
ufo.m_beaming = false;
return;
}
ufo.m_strategy = moo::Abduct{ closest_cow.value() };
}
auto moo::set_ufo_shooting(
Ufo& ufo,
entt::registry& registry
) -> void
{
if (std::holds_alternative<moo::Abduct>(ufo.m_strategy)) {
const moo::Abduct& strategy = std::get<moo::Abduct>(ufo.m_strategy);
if (registry.valid(strategy.m_target_cow)) {
moo::BeingBeamed& being_beamed = registry.get<moo::BeingBeamed>(strategy.m_target_cow);
being_beamed = false;
}
ufo.m_beaming = false;
}
ufo.m_strategy = moo::Shoot{};
}
auto moo::ufo_progress(
const Seconds& dt,
const ScreenCoord& player_pos,
Ufo& ufo,
entt::registry& registry,
const int level
) -> void
{
ufo.m_animation_frame.progress(dt);
if (ufo.is_invul()) {
ufo.m_hit_timer -= dt;
if (ufo.m_hit_timer < 0.0)
ufo.m_hit_timer = 0.0;
}
ufo.m_shooting_cooldown.iterate(dt);
if (std::holds_alternative<Shoot>(ufo.m_strategy)) {
ufo.fire(player_pos, registry);
}
else if (std::holds_alternative<Abduct>(ufo.m_strategy)) {
if (ufo.m_beaming)
return;
const Abduct strategy = std::get<Abduct>(ufo.m_strategy);
if (!registry.valid(strategy.m_target_cow)) { // moved off screen
ufo.m_strategy = Shoot{};
return;
}
const ScreenCoord raw_target_pos = registry.get<LanePosition>(strategy.m_target_cow).get_screen_pos() + ScreenCoord{ 0.0, -0.3 };
const PixelCoord target_pixel_coord = get_beam_aligned_pixel_coord(raw_target_pos);
const bool position_reached = to_pixel_coord(ufo.m_pos) == target_pixel_coord;
if (position_reached) {
BeingBeamed& being_beamed = registry.get<BeingBeamed>(strategy.m_target_cow);
being_beamed = true;
ufo.m_beaming = true;
}
ufo.m_pos = get_new_ufo_pos(ufo.m_pos, get_ufo_speed(level), target_pixel_coord, dt);
}
}
auto moo::get_new_cow_position(entt::registry& registry) -> std::optional<LanePosition> {
constexpr double free_area_threshold = 0.8;
auto cows = registry.view<IsCow, LanePosition>();
for (auto cow : cows) {
const bool cow_in_right_area = cows.get<LanePosition>(cow).m_x_pos > free_area_threshold;
if (cow_in_right_area)
return std::nullopt;
}
LanePosition new_cow_position = get_new_lane_position(get_ground_row_height(), get_cow_width(registry));
return new_cow_position;
}
auto moo::spawn_new_cows(
entt::registry& registry,
const bool inactive
) -> void
{
if (inactive)
return;
std::uniform_real_distribution<double> grazing_progress_dist(0.0, 1.0);
if (const auto cow_pos = get_new_cow_position(registry); cow_pos.has_value()) {
auto cow_entity = registry.create();
registry.emplace<IsCow>(cow_entity);
registry.emplace<Alpha>(cow_entity, 1.0);
registry.emplace<BeingBeamed>(cow_entity, false);
registry.emplace<LanePosition>(cow_entity, cow_pos.value());
registry.emplace<AnimationFrame>(cow_entity, 2, 1.0, grazing_progress_dist(get_rng()));
registry.emplace<CowVariant>(cow_entity, get_random_view_entity<CowAnimation>(registry));
}
}
auto moo::do_trail_logic(
entt::registry& registry,
const Seconds dt
) -> void
{
registry.view<Trail>().each([&](auto trail_entity, Trail& trail) {
trail.thin_trail(dt);
const bool is_bullet_still_alive = registry.valid(trail.m_bullet_ref);
if (trail.m_smoke_puffs.empty() && !is_bullet_still_alive)
registry.destroy(trail_entity);
});
registry.view<Bullet>().each([&](Bullet& bullet) {
Trail& trail = registry.get<Trail>(bullet.m_trail);
trail.update_puff_colors(bullet.m_pos);
add_trail_puffs(bullet, trail, dt);
});
}
auto moo::do_explosion_logic(entt::registry& registry, const Seconds dt) -> void{
const auto puff_spawners = registry.view<PuffSpawner, ScreenCoord, Direction, GravitySpeed>();
puff_spawners.each([&](entt::entity entity, ScreenCoord& pos, Direction& direction, GravitySpeed& gravity_speed) {
gravity_speed += dt.m_value * moo::get_config().gravity_strength;
constexpr double speed = 1;
pos += dt.m_value * (speed * static_cast<ScreenCoord>(direction) + gravity_speed * ScreenCoord{0.0, 1.0});
add_puff(registry, pos);
if (!pos.is_on_screen()) {
registry.destroy(entity);
}
});
const std::uniform_real_distribution<double> one_dist(0.0, 1.0);
const double elim_threshold = 5.0 * dt;
registry.view<IsPuff>().each([&](entt::entity ett) {
if (one_dist(get_rng()) < elim_threshold)
registry.destroy(ett);
});
}
auto moo::discard_ground_bullet(
entt::registry& registry,
entt::entity bullet_entity,
const Bullet& bullet
) -> void
{
const bool bullet_under_horizon = bullet.m_pos.y > (get_sky_row_height() + 0.5 * get_ground_row_height()) / static_rows;
const bool remove_bullet = !bullet.m_pos.is_on_screen() || bullet_under_horizon;
if (remove_bullet)
registry.destroy(bullet_entity);
}
auto moo::set_new_ufo_strategies(
entt::registry& registry,
Ufo& ufo
) -> void
{
const auto cows = registry.view<IsCow>();
if (cows.empty()) {
set_ufo_shooting(ufo, registry);
return;
}
if (std::holds_alternative<Shoot>(ufo.m_strategy))
set_ufo_abducting(ufo, registry);
else
set_ufo_shooting(ufo, registry);
}
auto moo::does_bullet_hit_ufo(
const Bullet& bullet,
const Ufo& ufo,
const ScreenCoord& ufo_dimensions
) -> bool
{
return !ufo.is_invul() && does_bullet_hit(bullet, ufo.m_pos, ufo_dimensions, BulletStyle::Rocket);
}
auto moo::create_explosion_streak(
entt::registry& registry,
const ScreenCoord& start_pos,
const Direction& direction
) -> void
{
auto trail_entity = registry.create();
registry.emplace<PuffSpawner>(trail_entity);
registry.emplace<ScreenCoord>(trail_entity, start_pos);
registry.emplace<Direction>(trail_entity, direction);
registry.emplace<GravitySpeed>(trail_entity, GravitySpeed{});
}
auto moo::create_explosion(entt::registry& registry, const ScreenCoord& start_pos) -> void{
constexpr int explosion_streak_count = 8;
const double average_angle = 2.0 * pi / explosion_streak_count;
const double angle_var = 0.5 * average_angle;
const std::uniform_real_distribution<double> angle_var_dist(-angle_var, angle_var);
for (int i = 0; i < explosion_streak_count; ++i) {
const double angle = i * average_angle + angle_var_dist(get_rng());
Direction dir{std::cos(angle), std::sin(angle)};
create_explosion_streak(registry, start_pos, dir);
}
}