Pacing: - Spin cooldown 0.55 -> 0.95 s (-3%/level instead of -8%); damage ramp +28%/level - Weaken fire-rate offers (Rapid Cycling +8%, Board Favor +4%, Maintenance +5%) - Stretch XP curve to 10+6*level and offer cadence to 3 s (5 s idle) - Calm early director income and spawn interval; docs updated Crystal/buff decoupling: - Kills drop XP shards only; remove per-kill random buff world object - PickupSystem owns shards alone; BuffSystem catches world objects with its own 2.8 m radius, independent of shard pickup magnet/upgrades - Explicit guaranteed world-object spawns for rituals and side rooms; effect-less bait consumed without a buff - ShardCollected only emitted for actual shard/XP rewards - Update design docs, RPG_LITE and README
269 lines
11 KiB
C++
269 lines
11 KiB
C++
#include "game/weapons.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <limits>
|
|
|
|
#include "core/spatial_hash.hpp"
|
|
#include "game/floor.hpp"
|
|
|
|
namespace {
|
|
|
|
constexpr float kMaxEnemyRadius = 1.3f; // for broad-phase query radii
|
|
constexpr float kMuzzleForward = 0.92f; // agent-local +Z, past the chest
|
|
constexpr float kMuzzleSide = -0.32f; // the rifle sits on the agent's left
|
|
|
|
const WeaponDef kDefs[kWeaponTypeCount] = {
|
|
{WeaponType::Spin, "SPIN",
|
|
"Service Weapon: measured resonant rounds at the nearest target",
|
|
14.0f, 0.95f, 42.0f, 1, 0.0f, 0.0f, 0.0f, 1.6f, 0.35f,
|
|
Directive::PiercingRounds, "SPIN - CORRIDOR"},
|
|
{WeaponType::Shatter, "SHATTER",
|
|
"shotgun cone that shreds whatever is closest",
|
|
6.0f, 1.6f, 34.0f, 5, 50.0f, 0.0f, 0.0f, 0.40f, 0.30f,
|
|
Directive::BlackRockPlating, "SHATTER - QUARRY"},
|
|
{WeaponType::Charge, "CHARGE",
|
|
"slow homing nuke aimed at the densest cluster",
|
|
42.0f, 4.0f, 20.0f, 1, 0.0f, 2.2f, 2.2f, 4.0f, 0.80f,
|
|
Directive::BoardFavor, "CHARGE - DIRECTIVE"},
|
|
{WeaponType::Launch, "LAUNCH",
|
|
"telekinetic debris pelts random targets",
|
|
18.0f, 1.1f, 44.0f, 1, 0.0f, 5.0f, 0.0f, 1.2f, 0.30f,
|
|
Directive::LaunchTraining, "LAUNCH - MASS DRIVER"},
|
|
{WeaponType::Projector, "PROJECTOR",
|
|
"briefing reel: slow homing projections that burst on impact",
|
|
26.0f, 3.0f, 26.0f, 1, 0.0f, 1.8f, 3.0f, 3.2f, 0.65f,
|
|
Directive::ResonanceAttunement, "PROJECTOR - BRIEFING REEL"},
|
|
};
|
|
|
|
// Level scaling: level 4 ~= +84% damage, -9% cooldown. The gentle cooldown
|
|
// ramp keeps OoP levels from turning into a projectile hose (see the pacing
|
|
// pass in docs/GAME_DESIGN_PASS.md).
|
|
float level_damage(int level) { return 1.0f + 0.28f * static_cast<float>(level - 1); }
|
|
float level_cooldown(int level) { return 1.0f - 0.03f * static_cast<float>(level - 1); }
|
|
|
|
const Enemy* nearest_enemy(const EnemySystem& enemies, Vec2 from, float max_range) {
|
|
const Enemy* target = nullptr;
|
|
float best = max_range * max_range;
|
|
for (const Enemy& e : enemies.enemies) {
|
|
const float d = length_sq(e.pos - from);
|
|
if (d < best) {
|
|
best = d;
|
|
target = &e;
|
|
}
|
|
}
|
|
return target;
|
|
}
|
|
|
|
// Charge targets the densest cluster: the enemy with the most neighbours.
|
|
const Enemy* densest_cluster(const EnemySystem& enemies, const SpatialHash& hash) {
|
|
const Enemy* best = nullptr;
|
|
int best_count = -1;
|
|
const std::size_t n = enemies.enemies.size();
|
|
const std::size_t stride = n > 200 ? n / 100 : 1; // sample on huge hordes
|
|
for (std::size_t i = 0; i < n; i += stride) {
|
|
const Enemy& e = enemies.enemies[i];
|
|
int count = 0;
|
|
hash.query(e.pos, 2.5f, [&](int id) {
|
|
if (distance(enemies.enemies[static_cast<std::size_t>(id)].pos, e.pos) < 2.5f) ++count;
|
|
});
|
|
if (count > best_count) {
|
|
best_count = count;
|
|
best = &e;
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
void fire_spread(Projectile proto, Vec2 dir, int pellets, float spread_deg,
|
|
float speed, DensePool<Projectile>& out) {
|
|
const float base = std::atan2(dir.y, dir.x);
|
|
const float spread = spread_deg * 3.14159265f / 180.0f;
|
|
for (int i = 0; i < pellets; ++i) {
|
|
const float t = pellets > 1 ? static_cast<float>(i) / static_cast<float>(pellets - 1) - 0.5f : 0.0f;
|
|
const float angle = base + t * spread;
|
|
Projectile p = proto;
|
|
p.vel = Vec2{std::cos(angle), std::sin(angle)} * speed;
|
|
out.add(p);
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Vec2 muzzle_pos(const Player& player) {
|
|
const Vec2 f = length_sq(player.facing) > 1e-6f
|
|
? normalized(player.facing) : Vec2{0.0f, 1.0f};
|
|
const Vec2 right{f.y, -f.x};
|
|
return player.pos + f * kMuzzleForward + right * kMuzzleSide;
|
|
}
|
|
|
|
Vec2 aim_at(Player& player, Vec2 target) {
|
|
const Vec2 dir = normalized(target - player.pos);
|
|
if (length_sq(dir) > 1e-6f) {
|
|
player.facing = dir;
|
|
player.aim_hold = 0.6f;
|
|
}
|
|
return dir;
|
|
}
|
|
|
|
const WeaponDef& weapon_def(WeaponType type) {
|
|
return kDefs[static_cast<int>(type)];
|
|
}
|
|
|
|
void WeaponSystem::reset() {
|
|
weapons.clear();
|
|
projectiles.clear();
|
|
weapons.push_back({WeaponType::Spin, 1, false, 0.0f}); // the Service Weapon
|
|
}
|
|
|
|
void WeaponSystem::update(float dt, Player& player, EnemySystem& enemies,
|
|
EventQueue& events, Rng& rng, const Floor& floor) {
|
|
// Broad phase over the horde, rebuilt once per tick.
|
|
SpatialHash hash(2.0f);
|
|
hash.clear();
|
|
for (std::size_t i = 0; i < enemies.enemies.size(); ++i) {
|
|
hash.insert(static_cast<int>(i), enemies.enemies[i].pos);
|
|
}
|
|
hash.build();
|
|
|
|
// --- Auto-fire each owned OoP ---
|
|
for (WeaponInstance& w : weapons) {
|
|
w.cooldown_left -= dt;
|
|
if (w.cooldown_left > 0.0f || !player.alive()) continue;
|
|
|
|
const WeaponDef& def = weapon_def(w.type);
|
|
const int tier = (w.evolved || w.level >= 4) ? 2
|
|
: (w.level >= 2 ? 1 : 0);
|
|
const float speed = def.projectile_speed * (1.0f + 0.16f * tier);
|
|
const float dmg = def.damage * level_damage(w.level)
|
|
* (1.0f + 0.10f * tier)
|
|
* (w.evolved ? 1.35f : 1.0f) * player.stats.damage_mult;
|
|
const float cd = def.cooldown * level_cooldown(w.level)
|
|
/ std::max(0.25f, player.stats.fire_rate_mult);
|
|
|
|
Projectile proto;
|
|
proto.damage = dmg;
|
|
proto.radius = def.projectile_radius;
|
|
proto.life = def.projectile_life;
|
|
proto.homing = def.homing;
|
|
proto.source = w.type;
|
|
proto.tier = tier;
|
|
proto.evolved = w.evolved;
|
|
proto.pierce = player.stats.pierce;
|
|
|
|
bool fired = false;
|
|
switch (w.type) {
|
|
case WeaponType::Spin: {
|
|
const Enemy* target = nearest_enemy(enemies, player.pos, 1e6f);
|
|
if (target == nullptr) break;
|
|
aim_at(player, target->pos);
|
|
proto.pos = muzzle_pos(player);
|
|
proto.vel = normalized(target->pos - proto.pos) * speed;
|
|
if (w.evolved) proto.pierce += 1; // Altered Form: Corridor
|
|
projectiles.add(proto);
|
|
fired = true;
|
|
break;
|
|
}
|
|
case WeaponType::Shatter: {
|
|
const Enemy* target = nearest_enemy(enemies, player.pos, 16.0f);
|
|
if (target == nullptr) break;
|
|
aim_at(player, target->pos);
|
|
proto.pos = muzzle_pos(player);
|
|
const int pellets = w.evolved ? 8 : def.pellets; // Altered Form: Quarry
|
|
const float spread = w.evolved ? 70.0f : def.spread_deg;
|
|
fire_spread(proto, target->pos - proto.pos, pellets, spread,
|
|
speed, projectiles);
|
|
fired = true;
|
|
break;
|
|
}
|
|
case WeaponType::Charge: {
|
|
const Enemy* target = densest_cluster(enemies, hash);
|
|
if (target == nullptr) target = nearest_enemy(enemies, player.pos, 1e6f);
|
|
if (target == nullptr) break;
|
|
aim_at(player, target->pos);
|
|
proto.pos = muzzle_pos(player);
|
|
proto.vel = normalized(target->pos - proto.pos) * speed;
|
|
proto.aoe_radius = def.aoe_radius * (w.evolved ? 1.6f : 1.0f); // Directive
|
|
projectiles.add(proto);
|
|
fired = true;
|
|
break;
|
|
}
|
|
case WeaponType::Launch: {
|
|
const int shots = w.evolved ? 3 : 1; // Altered Form: Mass Driver
|
|
for (int s = 0; s < shots; ++s) {
|
|
if (enemies.enemies.empty()) break;
|
|
const int id = rng.range_int(0, static_cast<int>(enemies.enemies.size()) - 1);
|
|
const Enemy& target = enemies.enemies[static_cast<std::size_t>(id)];
|
|
aim_at(player, target.pos);
|
|
proto.pos = muzzle_pos(player)
|
|
+ Vec2{rng.range(-0.35f, 0.35f), rng.range(-0.35f, 0.35f)};
|
|
proto.vel = normalized(target.pos - proto.pos) * speed;
|
|
projectiles.add(proto);
|
|
}
|
|
fired = shots > 0;
|
|
break;
|
|
}
|
|
}
|
|
if (fired) {
|
|
w.cooldown_left = cd;
|
|
events.push(EventType::WeaponFired, muzzle_pos(player),
|
|
static_cast<float>(tier));
|
|
}
|
|
}
|
|
|
|
// --- Projectile flight + collision ---
|
|
for (std::size_t i = 0; i < projectiles.size();) {
|
|
Projectile& p = projectiles[i];
|
|
|
|
if (p.homing > 0.0f) {
|
|
const Enemy* target = nearest_enemy(enemies, p.pos, 14.0f);
|
|
if (target != nullptr) {
|
|
const float speed = length(p.vel);
|
|
const Vec2 desired = normalized(target->pos - p.pos) * speed;
|
|
const float turn = std::min(1.0f, p.homing * dt);
|
|
p.vel = normalized(p.vel + (desired - p.vel) * turn) * speed;
|
|
}
|
|
}
|
|
|
|
p.pos += p.vel * dt;
|
|
p.life -= dt;
|
|
if (p.life <= 0.0f || floor.blocked(p.pos, p.radius)) {
|
|
projectiles.remove_at(i);
|
|
continue;
|
|
}
|
|
|
|
bool consumed = false;
|
|
hash.query(p.pos, p.radius + kMaxEnemyRadius, [&](int id) {
|
|
if (consumed) return;
|
|
Enemy& e = enemies.enemies[static_cast<std::size_t>(id)];
|
|
if (distance(p.pos, e.pos) >= p.radius + e.radius) return;
|
|
|
|
if (p.aoe_radius > 0.0f) {
|
|
// Detonation: damage everything in the blast.
|
|
hash.query(p.pos, p.aoe_radius + kMaxEnemyRadius, [&](int id2) {
|
|
Enemy& e2 = enemies.enemies[static_cast<std::size_t>(id2)];
|
|
if (distance(p.pos, e2.pos) < p.aoe_radius + e2.radius) {
|
|
e2.hp -= p.damage * e2.mods.damage_taken_mult;
|
|
e2.hit_flash = 0.15f;
|
|
events.push(EventType::EnemyHit, e2.pos, p.damage);
|
|
}
|
|
});
|
|
const unsigned flags =
|
|
(p.source == WeaponType::Charge && p.evolved)
|
|
? kDetonateConcrete : 0u;
|
|
events.push(EventType::AreaDetonation, p.pos, p.aoe_radius,
|
|
flags);
|
|
consumed = true;
|
|
} else {
|
|
e.hp -= p.damage * e.mods.damage_taken_mult;
|
|
e.hit_flash = 0.15f;
|
|
events.push(EventType::EnemyHit, e.pos, p.damage);
|
|
if (p.pierce > 0) p.pierce -= 1;
|
|
else consumed = true;
|
|
}
|
|
});
|
|
|
|
if (consumed) projectiles.remove_at(i);
|
|
else ++i;
|
|
}
|
|
}
|