examples

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

commit c76c91fa614d2aedef9301f7207bc41e3632821c
parent 45f10eb2c52687daf0d6dee9f27c8080c0112bb3
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Sat,  9 Apr 2022 23:12:31 +0100

gl-asteroids: Add more than one type of powerup

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

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -9,6 +9,7 @@ #include <thread> #include <chrono> #include <optional> +#include <functional> struct coord { @@ -48,7 +49,8 @@ coord const rockv3[] = {{0, 0}, {1, 0}, {.7,.4}, {0, 1}, {-.8,.6}, {-1, 0}, {-.6 coord const rockv4[] = {{0, 0}, {1, 0}, {.6,.4}, {0, .5}, {-.3,.6}, {-1, 0}, {-.6,-.5}, {0, -1}, {1, 0}}; color const rockc[] = {{.5,.5,.5}, {.2,.2,.2}}; -color const powerupc[] = {{.2,.4,.8}, {.1,.2,.4}}; +color const powerupcb[] = {{.8,.4,.2}, {.4,.2,.1}}; +color const powerupcf[] = {{.2,.4,.8}, {.1,.2,.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}}; @@ -62,7 +64,8 @@ visual rock1{GL_TRIANGLE_FAN, rockv1, rockc}; visual rock2{GL_TRIANGLE_FAN, rockv2, rockc}; visual rock3{GL_TRIANGLE_FAN, rockv3, rockc}; visual rock4{GL_TRIANGLE_FAN, rockv4, rockc}; -visual powerup{GL_TRIANGLE_FAN, rockv1, powerupc}; +visual powerup_firerate{GL_TRIANGLE_FAN, rockv1, powerupcf}; +visual powerup_bulletspeed{GL_TRIANGLE_FAN, rockv1, powerupcb}; std::array<visual const*, 4> rock_types = {&rock1, &rock2, &rock3, &rock4}; float wrap(float n, float min, float max) @@ -250,6 +253,8 @@ bool is_pressed(int key) std::vector<entity> rocks; std::vector<entity> bullets; std::optional<entity> powerups; +std::function<void(void)> powerup_cb; + physics s{{}, {}, 0.f, 0.f, .06f}; bool up = false; @@ -298,7 +303,7 @@ int main(int argc, char* argv[]) { signed score = 0; unsigned ammo = 10; - float firerate = 2; + float firerate = 2, bullet_speed = 1; std::random_device rd; std::default_random_engine gen(rd()); @@ -370,7 +375,22 @@ int main(int argc, char* argv[]) std::swap(v.x, v.y); } - powerups.emplace(powerup, physics{p, v, 0, 0, scale}); + visual const* vis; + + static int type; + switch (type++ % 2) + { + case 0: + vis = &powerup_bulletspeed; + powerup_cb = [&firerate](){ firerate += .5f; }; + break; + case 1: + vis = &powerup_firerate; + powerup_cb = [&bullet_speed](){ bullet_speed += .2f; }; + break; + } + + powerups.emplace(*vis, physics{p, v, 0, 0, scale}); } if (ammo < 20 && ammo_refill_time < t) @@ -389,7 +409,9 @@ int main(int argc, char* argv[]) b.ang_mom = 0; b.scale /= 1.25; - b.vel += {(float)sin(b.rot), (float)cos(b.rot)}; + auto vel = coord{(float)sin(b.rot), (float)cos(b.rot)} * bullet_speed; + b.vel += vel; + s.vel -= vel * .05f; } last_t = t; @@ -499,9 +521,9 @@ int main(int argc, char* argv[]) } if (is_colliding(s, powerups->p)) { + powerup_cb(); powerups.reset(); score += 10; - ++firerate; } }