examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

commit 9f2c69b4af7d95cc5592864416d1bb4c143a2054
parent 317ff4eee92b6038d53305429b2cce648ad633e1
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Tue,  5 Apr 2022 23:49:45 +0100

gl-asteroids: Delete bullets which exit the arena

Diffstat:
Msrc/gl-asteroids.cpp | 58++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 44 insertions(+), 14 deletions(-)

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -48,7 +48,7 @@ coord const rockv4[] = {{0, 0}, {1, 0}, {.6,.4}, {0, .5}, {-.3,.6}, {-1, 0}, {-. color const rockc[] = {{.5,.5,.5}, {.2,.2,.2}}; -coord const bulletv[] = {{0, 4}, {1, 1}, {-1, 1}, {1, -4}, {-1, -4}}; +coord const bulletv[] = {{0, 1}, {.25, .25}, {-.25, .25}, {.25, -1}, {-.25, -1}}; color const bulletc[] = {{.0,.8,.0}, {.0,.4,.0}, {.0,.4,.0}, {.0,.0,.0}}; coord starsv[64]; @@ -125,17 +125,31 @@ struct physics void move(float dt) { pos += vel * dt; - if (pos.x > 1 && vel.x > 0) - pos.x -= 2; - else if (pos.x < -1 && vel.x < 0) - pos.x += 2; - - if (pos.y > 1 && vel.y > 0) - pos.y -= 2; - else if (pos.y < -1 && vel.y < 0) - pos.y += 2; rot += ang_mom * dt; } + static void wrap_pos(float& pos, float vel) + { + if (pos > 1 && vel > 0) + pos -= 2; + else if (pos < -1 && vel < 0) + pos += 2; + } + void wrap_pos(void) + { + wrap_pos(pos.x, vel.x); + wrap_pos(pos.y, vel.y); + } + static bool left_axis(float pos, float vel, float scale) + { + if (vel < 0) + pos = -pos; + + return pos > 1 + scale; + } + bool left_arena() const + { + return left_axis(pos.x, vel.x, scale) or left_axis(pos.y, vel.y, scale); + } }; bool is_colliding(physics const& a, physics const& b) @@ -205,6 +219,7 @@ struct particle p.e.p.move(dt); } std::erase_if(particles, [](auto& p) { return p.ttl <= 0; }); + std::erase_if(particles, [](auto const& p) { return p.e.p.left_arena(); }); } static void draw_all() { @@ -271,6 +286,7 @@ visual const& random_rock_visual(void) int main(int argc, char* argv[]) { unsigned score = 0; + unsigned ammo = 10; std::random_device rd; std::default_random_engine gen(rd()); @@ -288,7 +304,7 @@ int main(int argc, char* argv[]) float last_t = glfwGetTime(); - float rock_time = 1, bullet_time = 0; + float rock_time = 1, bullet_time = 0, ammo_refill_time = .3; for (auto& c : starsv) c.x = dis_p(gen), c.y = dis_p(gen); @@ -326,15 +342,22 @@ int main(int argc, char* argv[]) rocks.emplace_back(random_rock_visual(), physics{p, v, 0, dis_a(gen), scale}); } - if (is_pressed(GLFW_KEY_SPACE) && bullet_time < t) + if (ammo < 20 && ammo_refill_time < t) + { + ++ammo; + ammo_refill_time += .4; + } + + if (is_pressed(GLFW_KEY_SPACE) && bullet_time < t && ammo) { - bullet_time = t + .3f; + --ammo; + bullet_time = t + .15f; bullets.emplace_back(bullet, s); auto& b = bullets.back().p; b.ang_mom = 0; - b.scale /= 5; + b.scale /= 1.25; b.vel += {(float)sin(b.rot), (float)cos(b.rot)}; } @@ -359,10 +382,15 @@ int main(int argc, char* argv[]) // resolve motion for (auto& r : rocks) + { r.p.move(dt); + r.p.wrap_pos(); + } for (auto& b : bullets) b.p.move(dt); + s.move(dt); + s.wrap_pos(); particle::move_all(dt); // handle collisions @@ -418,6 +446,8 @@ int main(int argc, char* argv[]) break; } + std::erase_if(bullets, [](auto const& e) { return e.p.left_arena(); }); + draw_scene(t); }