From ff4929c650e6ed446b6faff9f6b0f078d0a3644c Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Mon, 9 Oct 2017 22:51:50 +0200 Subject: Initial work on entities. --- src/common.hpp | 12 +++++++++++- src/game.cpp | 14 +++++++++++--- src/main.cpp | 2 ++ src/render.cpp | 47 ++++++++++++++++++++++++++++++++++------------- src/world.cpp | 23 ++++++++++++++++++++++- 5 files changed, 80 insertions(+), 18 deletions(-) diff --git a/src/common.hpp b/src/common.hpp index 2dd7c45..6a30f9e 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -78,11 +78,15 @@ namespace world { world_t *parent_world; std::vector parents; + void link_to_sector(sector_t *sector); + public: - sf::Vector2f origin, size; + sf::FloatRect bounds; void link(world_t *world); void unlink(); + + virtual void render(sf::RenderWindow *window) = 0; }; } @@ -91,9 +95,15 @@ namespace game { world::world_t world; public: + void start(void); void tick(void); void render(sf::RenderWindow *window_); }; + + class human_t : public world::entity_t { + public: + void render(sf::RenderWindow *window); + }; } namespace interface { diff --git a/src/game.cpp b/src/game.cpp index 05b0139..6fff821 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2,9 +2,17 @@ namespace game { -class entity_t : world::entity_t { - virtual void render(sf::RenderWindow *window) = 0; -}; +void state_t::start(void) +{ + human_t *human; + + human = new human_t; + human->bounds.left = 0.2f; + human->bounds.top = 0.2f; + human->bounds.width = 1.0f; + human->bounds.height = 1.0f; + human->link(&world); +} void state_t::tick(void) { diff --git a/src/main.cpp b/src/main.cpp index d1afc2f..b63b610 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,8 @@ int main() window.setVerticalSyncEnabled(true); + game.start(); + while (1) { game.tick(); interface.tick(); diff --git a/src/render.cpp b/src/render.cpp index 05785ae..d63ca6f 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -1,4 +1,5 @@ #include "common.hpp" +#include static sf::RectangleShape wot_rect; @@ -12,35 +13,35 @@ static void draw_tile(sf::RenderWindow *window, float x, float y, window->draw(wot_rect); } -static void draw_sector(sf::RenderWindow *window, world::world_t *world, - world::sector_index_t index) +static void draw_sector(sf::RenderWindow *window, world::sector_t *sector) { - world::sector_t *sector; - - sector = world->get_sector(index); - for (ssize_t y = 0; y < SECTOR_SIZE; y++) - for (ssize_t x = 0; x < SECTOR_SIZE; x++) { + for (ssize_t x = 0; x < SECTOR_SIZE; x++) draw_tile(window, - index.x * SECTOR_SIZE + x, - index.y * SECTOR_SIZE + y, + sector->bounds.left + x, sector->bounds.top + y, sector->tiles + y * SECTOR_SIZE + x); - } wot_rect.setSize(sf::Vector2f(SECTOR_SIZE, SECTOR_SIZE)); - wot_rect.setPosition(sf::Vector2f(index.x * SECTOR_SIZE, index.y * SECTOR_SIZE)); + wot_rect.setPosition(sf::Vector2f(sector->bounds.left, sector->bounds.top)); wot_rect.setOutlineColor(sf::Color::Yellow); wot_rect.setOutlineThickness(0.06f); wot_rect.setFillColor(sf::Color::Transparent); window->draw(wot_rect); } +static void draw_sector_entities(sf::RenderWindow *window, world::sector_t *sector) +{ + for (world::entity_t *ent : sector->ents) + ent->render(window); +} + void game::state_t::render(sf::RenderWindow *window) { sf::Vector2u size = window->getSize(); sf::Vector2f A, B, C, D; sf::Rect bbox; sf::Rect index_box; + std::list sectors; A = window->mapPixelToCoords(sf::Vector2i(0, 0)); B = window->mapPixelToCoords(sf::Vector2i(size.x, 0)); @@ -58,8 +59,28 @@ void game::state_t::render(sf::RenderWindow *window) index_box.height = ceil(bbox.height / SECTOR_SIZE); for (ssize_t y = index_box.top; y < index_box.height; y++) - for (ssize_t x = index_box.left; x < index_box.width; x++) - draw_sector(window, &world, world::sector_index_t(x, y)); + for (ssize_t x = index_box.left; x < index_box.width; x++) { + world::sector_index_t index(x, y); + world::sector_t *sector; + + sector = world.get_sector(index); + sectors.push_back(sector); + } + + for (auto *sector : sectors) + draw_sector(window, sector); + + for (auto *sector : sectors) + draw_sector_entities(window, sector); +} + +void game::human_t::render(sf::RenderWindow *window) +{ + wot_rect.setPosition(bounds.left, bounds.top); + wot_rect.setSize(sf::Vector2f(bounds.width, bounds.height)); + wot_rect.setFillColor(sf::Color::Red); + wot_rect.setOutlineColor(sf::Color::Transparent); + window->draw(wot_rect); } void interface::state_t::render() diff --git a/src/world.cpp b/src/world.cpp index dc25f66..9d9b97d 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -83,9 +83,30 @@ tile_t *world_t::get_tile(ssize_t x, ssize_t y) return sector->tiles + ty * SECTOR_SIZE + tx; } +static sector_index_t get_index(float x, float y) +{ + return sector_index_t(floor(x), floor(y)); +} + +void entity_t::link_to_sector(sector_t *sector) +{ + parents.push_back(sector); + sector->ents.insert(this); +} + void entity_t::link(world_t *world) { - // TODO + sector_index_t base; + sector_t *sector; + ssize_t dx, dy; + + // An entity gets linked to at least one sector. + base = get_index(bounds.left, bounds.top); + sector = world->get_sector(base); + link_to_sector(sector); + + // There might be more, though. + //for (dy = 0; dy < } void entity_t::unlink(void) -- cgit