examples

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

commit d4d0d98cea7a7f26e02da1cfa40935fd7fd3415b
parent ff494da64cb87734b2a98880e423546b093ab569
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Mon,  9 May 2022 23:47:05 +0100

gl-asteroids: Add lives

Diffstat:
Msrc/gl-asteroids.cpp | 36++++++++++++++++++++++++++++++++++--
1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -90,7 +90,7 @@ struct visual std::span<color const> cols; }; -coord const shipv[] = {{0, 2}, {1, -1}, {-1, -1}, {1,-1}, {.5,0}, {.5,0}}; +coord const shipv[] = {{0, 2}, {1, -1}, {-1, -1}}; color const shipc[] = {{1,1,1}, {.5,.5,.5}}; coord const flamev[3] = {{0, -2.5}, {.6, -.25}, {-.6, -.25}}; @@ -335,6 +335,8 @@ std::optional<powerup> powerups; physics ship_phys{{}, {}, 0.f, 0.f, .06f}; bool up = false; +int lives = 3; + void draw_scene(float t) { glClearColor(0,0,0,1); @@ -362,6 +364,13 @@ void draw_scene(float t) } draw_wrapped(ship, ship_phys); + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + + for (int i = 0; i < lives-1; ++i) + draw(ship, {{.9f, -.9f + i * .15f}, {}, 0.f, 0.f, .04f}); + + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glfwSwapBuffers(w); glfwPollEvents(); } @@ -625,9 +634,32 @@ int main(int argc, char* argv[]) } } - if(any_of(rocks.begin(), rocks.end(), [](auto& r) { return is_colliding(ship_phys, r.phys); })) + for (auto r = rocks.begin(); r != rocks.end(); /**/) { + if (!is_colliding(ship_phys, r->phys)) + { + ++r; + continue; + } + play_sample(bang); + lives--; + + for (unsigned j = 0; j < 7; ++j) + { + static std::uniform_real_distribution<float> scale(0.005,0.025); + entity debris = *r; + debris.vis = &random_rock_visual(); + debris.phys.vel.x += .3 * dis_p(gen); + debris.phys.vel.y += .3 * dis_p(gen); + debris.phys.scale = scale(gen); + particle::make_particle(debris, 1); + } + r = rocks.erase(r); + } + + if (!lives) + { using namespace std::chrono_literals; auto timeout = std::chrono::steady_clock::now() + 1s; score += t;