diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2018-04-12 21:27:07 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2018-04-12 21:27:07 +0200 |
commit | f405c37daccac93323081216b2484da498e2fecf (patch) | |
tree | c61932fa3036f1185a1f8fc0dbffdd084c1da12a /src | |
parent | d307234efb1e687bfac06d48b01eb56d748e8510 (diff) |
Tracing vs entities, bullets can now hit.
Diffstat (limited to 'src')
-rw-r--r-- | src/common.hpp | 4 | ||||
-rw-r--r-- | src/game/unit_soldier.cpp | 13 | ||||
-rw-r--r-- | src/world.cpp | 35 |
3 files changed, 41 insertions, 11 deletions
diff --git a/src/common.hpp b/src/common.hpp index bf4482f..45782f7 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -162,9 +162,9 @@ namespace world { bool test_rect(const cmodel_t *cmodel, const entity_t *ignore); - trace_t ray_v_ents(v2f_t start, v2f_t end, cflags_t cflags, world::entity_t *ignore); + trace_t ray_v_ents(v2f_t start, v2f_t end, cflags_t cflags, const 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); + trace_t ray_v_all(v2f_t start, v2f_t end, cflags_t cflags, const entity_t *ignore); struct { size_t sectors = 0, tiles = 0; diff --git a/src/game/unit_soldier.cpp b/src/game/unit_soldier.cpp index e44e8a2..b3e93d1 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->ray_v_all(x, end, CF_SOLID, this); + trace = world->ray_v_all(x, end, CF_SOLID|CF_BODY|CF_BODY_SMALL, this); muzzle_point = x + v2f_t(0, -1.0f); @@ -95,8 +95,15 @@ void unit_soldier_t::shoot(v2f_t aim) flash->place(&game->world); last_attack = game->now; + assets::soldier.fire.play_3d(x); - //target->damage(3, this); FIXME + + if (trace.hit && trace.ent) { + if (trace.ent->type == ET_UNIT) { + unit_t *unit = dynamic_cast<unit_t*>(trace.ent); + unit->damage(3, this); + } + } } void unit_soldier_t::target_and_attack(void) @@ -127,7 +134,7 @@ skip_targetting: if (last_attack + game->prng.next_float(1.4f, 1.6f) > game->now) return; - shoot(spread_aim(aim, 0.2, &game->prng)); + shoot(spread_aim(aim, 0.01, &game->prng)); } void unit_soldier_t::on_think(void) diff --git a/src/world.cpp b/src/world.cpp index 05519f0..d09001c 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -391,12 +391,34 @@ out_no_hit: trace_t world_t::ray_v_ents(v2f_t start, v2f_t end, cflags_t cflags, - world::entity_t *ignore) + const entity_t *ignore) { - // TODO - trace_t res; - res.hit = false; - return res; + trace_t best; + rectf_t rect; + + best.hit = false; + best.frac = 2.0f; + + rect = rectf_t(start, end).norm(); + + for (entity_t *ent : get_entities(rect, cflags)) { + trace_t trace; + + if (ent == ignore) + continue; + + trace = ray_v_rect(start, end, ent->cmodel.bounds); + if (!trace.hit) + continue; + + if (trace.frac < best.frac) { + best = trace; + best.tile = nullptr; + best.ent = ent; + } + } + + return best; } @@ -442,6 +464,7 @@ trace_t world_t::ray_v_tiles(v2f_t start, v2f_t end, cflags_t cflags) res.hit = true; res.end = x ^ transforms[quad]; res.frac = (x - start).len() / (end - start).len(); + res.ent = nullptr; res.tile = tile; return res; } @@ -467,7 +490,7 @@ trace_t world_t::ray_v_tiles(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) + const entity_t *ignore) { trace_t v_tiles, v_ents; |