From f66cf28c8b4d80122896c87dba8af74ea1872eba Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Tue, 17 Oct 2017 16:39:13 +0200 Subject: New vectors. Start refactoring to use it. --- src/world.cpp | 111 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 61 insertions(+), 50 deletions(-) (limited to 'src/world.cpp') diff --git a/src/world.cpp b/src/world.cpp index d340cc1..06369f6 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -2,31 +2,10 @@ namespace world { -sector_index_t::sector_index_t () +sector_index_t sector_index_at(v2f_t x) { -} - -sector_index_t::sector_index_t (int64_t x_, int64_t y_) -{ - x = x_; - y = y_; -} - -sector_index_t::sector_index_t (float x_, float y_) -{ - x = (int64_t)floor(x_ / SECTOR_SIZE); - y = (int64_t)floor(y_ / SECTOR_SIZE); -} - -bool sector_index_t::operator<(sector_index_t B) const -{ - if (x < B.x) - return true; - if (x > B.x) - return false; - if (y < B.y) - return true; - return false; + return sector_index_t(floor(x[0] / SECTOR_SIZE), + floor(x[1] / SECTOR_SIZE)); } world_t::world_t(void) @@ -35,17 +14,17 @@ world_t::world_t(void) perlin.generate(&prng, 32); } -void world_t::generate_tile(ssize_t x, ssize_t y, tile_t *tile) +void world_t::generate_tile(tile_t *tile, tile_index_t x) { float waterlevel, height; - waterlevel = perlin.get(x, y, 1000.0f) * 0.3f + - perlin.get(x, y, 500.0f) * 0.1f; + waterlevel = perlin.get(x, 1000.0f) * 0.3f + + perlin.get(x, 500.0f) * 0.1f; - height = perlin.get(x, y, 60.0f) * 0.6f + - perlin.get(x, y, 30.0f) * 0.25f + - perlin.get(x, y, 14.0f) * 0.1f + - perlin.get(x, y, 6.0f) * 0.05f; + height = perlin.get(x, 60.0f) * 0.6f + + perlin.get(x, 30.0f) * 0.25f + + perlin.get(x, 14.0f) * 0.1f + + perlin.get(x, 6.0f) * 0.05f; if (height < waterlevel - 0.2f) tile->type = -1; @@ -54,7 +33,7 @@ void world_t::generate_tile(ssize_t x, ssize_t y, tile_t *tile) else if (height < waterlevel + 0.05f) tile->type = 1; else { - if (perlin.get(x, y, 5.0f) > 0.0f) + if (perlin.get(x, 5.0f) > 0.0f) tile->type = 3; else tile->type = 2; @@ -64,18 +43,18 @@ void world_t::generate_tile(ssize_t x, ssize_t y, tile_t *tile) void world_t::generate(sector_t *sector, sector_index_t index) { sector->index = index; - sector->bounds.left = index.x * SECTOR_SIZE; - sector->bounds.top = index.y * SECTOR_SIZE; + sector->bounds.left = index[0] * SECTOR_SIZE; + sector->bounds.top = index[1] * SECTOR_SIZE; sector->bounds.width = SECTOR_SIZE; sector->bounds.height = SECTOR_SIZE; - std::cout << "generating (" << index.x << ", " << index.y << ")\n"; + std::cout << "generating " << index << "\n"; for (ssize_t ly = 0; ly < SECTOR_SIZE; ly++) for (ssize_t lx = 0; lx < SECTOR_SIZE; lx++) - generate_tile(index.x * SECTOR_SIZE + lx, - index.y * SECTOR_SIZE + ly, - sector->tiles + ly * SECTOR_SIZE + lx); + generate_tile(sector->tiles + ly * SECTOR_SIZE + lx, + tile_index_t(index[0] * SECTOR_SIZE + lx, + index[1] * SECTOR_SIZE + ly)); sector->empty = false; } @@ -98,8 +77,8 @@ tile_t *world_t::get_tile(ssize_t x, ssize_t y) sector_t *sector; ssize_t tx, ty; - index.x = divide_rmi(x, (ssize_t)SECTOR_SIZE, &tx); - index.y = divide_rmi(y, (ssize_t)SECTOR_SIZE, &ty); + index[0] = divide_rmi(x, (ssize_t)SECTOR_SIZE, &tx); + index[1] = divide_rmi(y, (ssize_t)SECTOR_SIZE, &ty); sector = get_sector(index); return sector->tiles + ty * SECTOR_SIZE + tx; @@ -107,12 +86,14 @@ tile_t *world_t::get_tile(ssize_t x, ssize_t y) std::list world_t::get_sectors(sf::FloatRect rect) { - sector_index_t base(rect.left, rect.top), - upper(rect.left + rect.width, rect.top + rect.height); + sector_index_t base, upper; std::list list; - for (int64_t y = base.y; y <= upper.y; y++) - for (int64_t x = base.x; x <= upper.x; x++) { + base = sector_index_at(v2f_t(rect.left, rect.top)); + upper = sector_index_at(v2f_t(rect.left + rect.width, rect.top + rect.height)); + + for (int64_t y = base[1]; y <= upper[1]; y++) + for (int64_t x = base[0]; x <= upper[0]; x++) { sector_index_t index(x, y); list.push_back(get_sector(index)); } @@ -120,6 +101,29 @@ std::list world_t::get_sectors(sf::FloatRect rect) return list; } +std::list world_t::get_render_entities(sf::FloatRect rect) +{ + static size_t cookie = 0; + std::list list; + + cookie++; + + for (sector_t *sector : get_sectors(rect)) + for (entity_t *ent : sector->ents) { + if (ent->cookie == cookie) + continue; + + if (!rect.intersects(ent->render_bounds)) + continue; + + ent->cookie = cookie; + + list.push_back(ent); + } + + return list; +} + std::list world_t::get_entities(sf::FloatRect rect) { static size_t cookie = 0; @@ -145,8 +149,8 @@ std::list world_t::get_entities(sf::FloatRect rect) void world_t::debug_point(sf::Vector2f point) { - sector_index_t index(point.x, point.y); - printf("sector (%zd, %zd)\n", index.x, index.y); + sector_index_t index = sector_index_at(point); + printf("sector (%zd, %zd)\n", index[0], index[1]); } void entity_t::link_to_sector(sector_t *sector) @@ -157,17 +161,24 @@ void entity_t::link_to_sector(sector_t *sector) void entity_t::link(world_t *world) { + //sf::FloatRect total_bounds; float fx, fy; sector_index_t base; float xlip, ylip; size_t xsecs, ysecs; + // TODO + //total.bounds.left = std::min(bounds.left, render_bounds.left); + //total.bounds.top = std::min(bounds.top, render_bounds.top); + //total.bounds.width = std::max(bounds.width, render_bounds.width); + //total.bounds.height = std::max(bounds.height, render_bounds.height); + fx = floor(bounds.left); fy = floor(bounds.top); - base = sector_index_t(fx, fy); - xlip = bounds.left + bounds.width - (base.x + 1) * SECTOR_SIZE; - ylip = bounds.top + bounds.height - (base.y + 1) * SECTOR_SIZE; + base = sector_index_at(v2f_t(fx, fy)); + xlip = bounds.left + bounds.width - (base[0] + 1) * SECTOR_SIZE; + ylip = bounds.top + bounds.height - (base[1] + 1) * SECTOR_SIZE; if (xlip > 0.0f) xsecs = ceil(xlip / SECTOR_SIZE) + 1; @@ -181,7 +192,7 @@ void entity_t::link(world_t *world) for (int64_t y = 0; y < (int64_t)ysecs; y++) for (int64_t x = 0; x < (int64_t)xsecs; x++) { - sector_index_t index(base.x + x, base.y + y); + sector_index_t index = sector_index_at(v2f_t(base[0] + x, base[1] + y)); sector_t *sector; sector = world->get_sector(index); -- cgit