From e4ca910e7731e2a12815a4a8de2582157f519898 Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Sat, 14 Oct 2017 15:54:01 +0200 Subject: Correct entity drawing order. --- src/game.cpp | 47 +++++++++++++++++++++++++++-------------------- src/render.cpp | 11 ++++++++++- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index bb04ee7..58b56c6 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -26,6 +26,7 @@ class human_t : public unit_t { public: float angle = 0.0f; bool walking = false; + float stalin = 0.0f; void render_to(render::state_t *render) { @@ -36,37 +37,43 @@ public: } }; -static human_t *human; +static std::list humans; void state_t::start(void) { - human = new human_t; - human->bounds.left = 0.33f; - human->bounds.top = 0.0f; - human->bounds.width = 0.66f; - human->bounds.height = 1.0f; - human->link(&world); } void state_t::tick(void) { - static float stalin = 2.0f; + if (rand() % 5 == 0) { + humans.emplace(humans.end()); - stalin -= ((float)rand() / RAND_MAX) * 0.1f; + human_t &human = *(--humans.end()); - human->walking = false; - - if (stalin < 0) { - human->walking = true; - human->angle += (rand() & 2 ? 0.2f : -0.2f); - human->unlink(); - human->bounds.left += cos(human->angle) * 0.04; - human->bounds.top += sin(human->angle) * 0.04; - human->link(&world); + human.bounds.left = 0.33f; + human.bounds.top = 0; + human.bounds.width = 0.66f; + human.bounds.height = 1.0f; + human.link(&world); } - if (stalin < -4.0f) - stalin = 2.0f; + for (human_t &human : humans) { + human.stalin -= ((float)rand() / RAND_MAX) * 0.1f; + + human.walking = false; + + if (human.stalin < 0) { + human.walking = true; + human.angle += (rand() & 2 ? 0.2f : -0.2f); + human.unlink(); + human.bounds.left += cos(human.angle) * 0.04; + human.bounds.top += sin(human.angle) * 0.04; + human.link(&world); + } + + if (human.stalin < -4.0f) + human.stalin = 2.0f; + } } } //namespace game diff --git a/src/render.cpp b/src/render.cpp index f56cb37..ce9b837 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -79,6 +79,7 @@ void state_t::render(game::state_t *game) sf::Vector2u size = window->getSize(); sf::Vector2f A, B, C, D; sf::Rect bbox; + std::list ents; A = window->mapPixelToCoords(sf::Vector2i(0, 0)); B = window->mapPixelToCoords(sf::Vector2i(size.x, 0)); @@ -93,7 +94,15 @@ void state_t::render(game::state_t *game) for (world::sector_t *sector : game->world.get_sectors(bbox)) draw_sector(window, sector); - for (world::entity_t *ent : game->world.get_entities(bbox)) + ents = game->world.get_entities(bbox); + ents.sort( + [](const world::entity_t *x, const world::entity_t *y) -> bool + { + return x->bounds.top + x->bounds.height + < y->bounds.top + y->bounds.height; + }); + + for (world::entity_t *ent : ents) ent->render_to(this); } -- cgit