diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2018-04-12 21:12:28 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2018-04-12 21:13:35 +0200 |
commit | d307234efb1e687bfac06d48b01eb56d748e8510 (patch) | |
tree | 911312b01583dc65b7204ae9a5093e15949e6642 /src | |
parent | a5b20dcd57ddf7bdac8d9bce1aabf934badd837d (diff) |
Refactor trace calls and remove the debug crap from the previous commit.
Diffstat (limited to 'src')
-rw-r--r-- | src/common.hpp | 8 | ||||
-rw-r--r-- | src/game/game.cpp | 25 | ||||
-rw-r--r-- | src/game/interface.cpp | 6 | ||||
-rw-r--r-- | src/game/unit_soldier.cpp | 2 | ||||
-rw-r--r-- | src/game/unit_spider.cpp | 2 | ||||
-rw-r--r-- | src/world.cpp | 33 |
6 files changed, 40 insertions, 36 deletions
diff --git a/src/common.hpp b/src/common.hpp index ef96cc6..bf4482f 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -120,6 +120,9 @@ namespace world { bool hit; v2f_t end; float frac; + + entity_t *ent; + tile_t *tile; } trace_t; // for testing @@ -158,7 +161,10 @@ namespace world { std::list<entity_t*> get_render_entities(rectf_t rect); bool test_rect(const cmodel_t *cmodel, const entity_t *ignore); - trace_t trace(v2f_t start, v2f_t end, cflags_t cflags); + + trace_t ray_v_ents(v2f_t start, v2f_t end, cflags_t cflags, world::entity_t *ignore); + trace_t ray_v_tiles(v2f_t start, v2f_t end, cflags_t cflags); + trace_t ray_v_all(v2f_t start, v2f_t end, cflags_t cflags, world::entity_t *ignore); struct { size_t sectors = 0, tiles = 0; diff --git a/src/game/game.cpp b/src/game/game.cpp index 4b9a9a3..8deadd6 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -251,31 +251,6 @@ void state_t::command(v2f_t x, int number) if (!selected_units.size()) return; - // debugging - if (number == 666) { - for (size_t i = 0; i < 100; i++) { - v2f_t rad; - world::trace_t best; - - best.hit = false; - best.frac = 2.0f; - - rad = v2f_t::rad(i / 99.0f * 2.0f * M_PI); - - for (unit_t *unit : selected_units) { - world::trace_t trace; - - trace = world::ray_v_rect(x, x + rad * 10, unit->cmodel.bounds); - if (trace.frac < best.frac) - best = trace; - } - - debug_render->render_line(x, best.end, (best.hit ? sf::Color::Yellow : sf::Color::White)); - } - - return; - } - snap[0] = std::round(x[0] - 0.5f) + 0.5f; snap[1] = std::round(x[1] - 0.5f) + 0.5f; diff --git a/src/game/interface.cpp b/src/game/interface.cpp index 7615acf..b187d42 100644 --- a/src/game/interface.cpp +++ b/src/game/interface.cpp @@ -334,12 +334,6 @@ void state_t::render_to(render::state_t *render) std::stringstream ss; double fps; - // debug - { - auto wmouse = window->mapPixelToCoords(sf::Mouse::getPosition(*window)); - game->command(wmouse, 666); - } - if (select.selecting) { sf::Color color; diff --git a/src/game/unit_soldier.cpp b/src/game/unit_soldier.cpp index 4112d9a..e44e8a2 100644 --- a/src/game/unit_soldier.cpp +++ b/src/game/unit_soldier.cpp @@ -84,7 +84,7 @@ void unit_soldier_t::shoot(v2f_t aim) end = x + (aim - x).norm() * 40; - trace = world->trace(x, end, CF_SOLID); + trace = world->ray_v_all(x, end, CF_SOLID, this); muzzle_point = x + v2f_t(0, -1.0f); diff --git a/src/game/unit_spider.cpp b/src/game/unit_spider.cpp index 9d1ec30..728e42e 100644 --- a/src/game/unit_spider.cpp +++ b/src/game/unit_spider.cpp @@ -56,7 +56,7 @@ void unit_spider_t::target_and_attack(void) if ((x - target->x).len() >= 1.0f) return; - trace = world->trace(x, target->x, CF_SOLID); + trace = world->ray_v_tiles(x, target->x, CF_SOLID); if (trace.hit) return; diff --git a/src/world.cpp b/src/world.cpp index b03105e..05519f0 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -389,7 +389,18 @@ out_no_hit: return res; } -trace_t world_t::trace(v2f_t start, v2f_t end, cflags_t cflags) + +trace_t world_t::ray_v_ents(v2f_t start, v2f_t end, cflags_t cflags, + world::entity_t *ignore) +{ + // TODO + trace_t res; + res.hit = false; + return res; +} + + +trace_t world_t::ray_v_tiles(v2f_t start, v2f_t end, cflags_t cflags) { v2f_t x, dx; size_t quad; @@ -414,6 +425,7 @@ trace_t world_t::trace(v2f_t start, v2f_t end, cflags_t cflags) while (1) { v2f_t mod, P; tile_index_t index; + tile_t *tile; if (x[0] >= end[0] && x[1] >= end[1]) { res.hit = false; @@ -424,11 +436,13 @@ trace_t world_t::trace(v2f_t start, v2f_t end, cflags_t cflags) index = (tile_index_t(x.floor()) ^ transforms_index[quad]) + offsets_index[quad]; + tile = get_tile(index, SECTOR_FULL); - if (tiles[get_tile(index, SECTOR_FULL)->type] & cflags) { + if (tiles[tile->type] & cflags) { res.hit = true; res.end = x ^ transforms[quad]; res.frac = (x - start).len() / (end - start).len(); + res.tile = tile; return res; } @@ -452,6 +466,21 @@ trace_t world_t::trace(v2f_t start, v2f_t end, cflags_t cflags) } } +trace_t world_t::ray_v_all(v2f_t start, v2f_t end, cflags_t cflags, + world::entity_t *ignore) +{ + trace_t v_tiles, v_ents; + + v_tiles = ray_v_tiles(start, end, cflags); + v_ents = ray_v_ents(start, end, cflags, ignore); + + if (v_tiles.frac < v_ents.frac) + return v_tiles; + else + return v_ents; +} + + entity_t::entity_t(int type_) { type = type_; |