Files
isaac-port/src/npc_sub_horf.cpp
T

177 lines
7.8 KiB
C++

#include "npc_caves.h"
#include "backdrop.h"
#include "game.h"
#include "level.h"
#include "room.h"
#include "sfx_manager.h"
#include <cstdio>
// The Sub Horf spins in place as it drifts, spitting along its facing. Its tainted variant chases its target instead,
// shooting bigger, slower arcs. A hit knocks it away at 8 or more.
constexpr int SUB_HORF_TAINTED = 1;
constexpr float SUB_HORF_KNOCKBACK = 8;
constexpr float SUB_HORF_STILL_SQUARED = 1e-6f;
constexpr int SUB_HORF_KNOCKED_FRAMES = 15;
constexpr int SUB_HORF_SHAKE_FRAMES = 12;
constexpr uint32_t SUB_HORF_GRUNT_ODDS = 3;
constexpr int SUB_HORF_GRUNT_DELAY = 10;
constexpr float SUB_HORF_TAINTED_PITCH = 0.85f;
constexpr float SUB_HORF_FACING_OFFSET = 90;
constexpr float SUB_HORF_HALF_TURN = 180;
constexpr float SUB_HORF_FULL_TURN = 360;
// The tainted one pushes at 0.2 toward its target, turning its facing 0.4 of the way.
constexpr float SUB_HORF_CHASE_PUSH = 0.2f;
constexpr float SUB_HORF_CHASE_TURN = 0.4f;
constexpr float SUB_HORF_CHASE_FRICTION = 0.9f;
// The plain one spins by twice its speed, shooting at 10 more often the faster it goes, and bleeds while fast.
constexpr float SUB_HORF_STEER_PUSH = 0.2f;
constexpr float SUB_HORF_STEER_DAMP = 0.97f;
constexpr float SUB_HORF_SPIN = 2;
constexpr float SUB_HORF_MOVING = 1;
constexpr float SUB_HORF_STOPPED = 0.01f;
constexpr float SUB_HORF_SHOT_REACH = 20;
constexpr float SUB_HORF_TAINTED_SHOT_REACH = 5;
constexpr float SUB_HORF_SHOT_SPEED = 10;
constexpr float SUB_HORF_SHOT_SPEED_SHARE = 0.7f;
constexpr float SUB_HORF_TAINTED_FALL_ACCEL = -0.1f;
constexpr float SUB_HORF_TAINTED_SHOT_SCALE = 1.4f;
constexpr float SUB_HORF_FRICTION = 0.97f;
constexpr float SUB_HORF_SPLAT_SPEED = 2;
constexpr int SUB_HORF_SPLAT_FRAMES = 7;
constexpr float SUB_HORF_SPLAT_MIN = 0.1f;
constexpr float SUB_HORF_SPLAT_RANGE = 0.3f;
// An angle in degrees brought within -180 to 180.
static float sub_horf_angle_wrap(float angle)
{
if (std::abs(angle) > SUB_HORF_FULL_TURN) angle = std::fmod(angle, SUB_HORF_FULL_TURN);
if (angle > SUB_HORF_HALF_TURN) return angle - SUB_HORF_FULL_TURN;
if (angle < -SUB_HORF_HALF_TURN) return angle + SUB_HORF_FULL_TURN;
return angle;
}
// A hit knocks it straight away from the last attacker's position (a random way when on it), at 8 or more.
static void sub_horf_knockback(Entity_NPC& npc)
{
if (!npc.damageEvents.empty())
npc.velocity = vector_normalized(vector_sub(npc.pos, npc.damageEvents.back().source.position));
if (vector_length_squared(npc.velocity) < SUB_HORF_STILL_SQUARED)
{
float angle = random_unit() * RANDOM_HALF_TURN * 2;
npc.velocity = vector_make(std::cos(angle), std::sin(angle));
}
float speed = vector_length(npc.velocity);
if (speed < SUB_HORF_KNOCKBACK && speed > 0) npc.velocity = vector_scale(npc.velocity, SUB_HORF_KNOCKBACK / speed);
npc.stateFrame = SUB_HORF_KNOCKED_FRAMES;
}
// The tainted one pushes toward its target (where its player steers), turning to face its push.
static void sub_horf_chase(Entity_NPC& npc, const Vector& facing)
{
Vector toward = npc.controllerID != -1 ? npc.GetControllerInput(0) : vector_sub(npc.CalcTargetPosition(0), npc.pos);
float distance = vector_length(toward);
if (distance > 0)
{
Vector push = vector_scale(toward, SUB_HORF_CHASE_PUSH / distance);
if (npc.friction != 0)
npc.velocity = vector_add(npc.velocity, vector_scale(push, npc.speedMultiplier / npc.friction));
Vector turned = vector_add(facing, vector_scale(push, SUB_HORF_CHASE_TURN));
npc.v1.x = vector_angle(turned) - SUB_HORF_FACING_OFFSET;
}
if (vector_length(npc.velocity) > SUB_HORF_KNOCKBACK) npc.stateFrame = SUB_HORF_KNOCKED_FRAMES;
npc.friction *= SUB_HORF_CHASE_FRICTION;
}
// The plain one (or the tainted one while knocked about) spins with its speed and spits along its facing, more often
// the faster it goes: tears in flooded stages, puke in the Dross.
static void sub_horf_spin(Entity_NPC& npc, const Vector& facing, int shotVariant)
{
if (npc.controllerID != -1)
{
Vector input = npc.GetControllerInput(0);
if (!vector_is_zero(input))
{
float cap = std::max(vector_length(npc.velocity) / SUB_HORF_STEER_DAMP, 1.0f);
npc.velocity = vector_add(npc.velocity, vector_scale(input, SUB_HORF_STEER_PUSH * npc.speedMultiplier));
float speed = vector_length(npc.velocity);
if (speed > cap && speed > 0) npc.velocity = vector_scale(npc.velocity, cap / speed);
}
}
float speed = vector_length(npc.velocity);
float spin = speed * SUB_HORF_SPIN;
if (npc.velocity.x < 0) spin = -spin;
npc.v1.x = sub_horf_angle_wrap(spin + npc.v1.x);
if (speed >= SUB_HORF_MOVING) npc.stateFrame = SUB_HORF_KNOCKED_FRAMES;
else if (npc.stateFrame > 0) npc.stateFrame -= npc_ticks_get(npc);
if (speed <= SUB_HORF_STOPPED) npc.i1 = 0;
else
{
float reach = npc.variant == SUB_HORF_TAINTED ? SUB_HORF_TAINTED_SHOT_REACH : SUB_HORF_SHOT_REACH;
int interval = std::max((int)(reach / speed), 1);
if (npc.i1 > interval * 2)
{
ProjectileParams params;
params.variant = shotVariant;
params.velocityMulti = SUB_HORF_SHOT_SPEED_SHARE;
if (npc.variant == SUB_HORF_TAINTED)
{
params.fallingAccelModifier = SUB_HORF_TAINTED_FALL_ACCEL;
params.scale = SUB_HORF_TAINTED_SHOT_SCALE;
}
npc.FireProjectiles(npc.pos, vector_scale(vector_normalized(facing), SUB_HORF_SHOT_SPEED),
PROJECTILES_SINGLE, params);
npc.PlaySound(SOUND_TEARS_FIRE, 1, 2, false, 1);
npc.i1 = 0;
}
npc.i1++;
}
npc.friction *= SUB_HORF_FRICTION;
}
// 0055fb60 The Sub Horf's AI: knocked away by hits, it spins as it drifts, its shake frame showing its facing, and
// spits along it. The tainted one chases its target. Outside flooded stages it bleeds while moving fast.
void npc_sub_horf_update(Entity_NPC& npc)
{
Vector facing = vector_from_angle(npc.v1.x + SUB_HORF_FACING_OFFSET);
uint32_t stage = Level::GetStageID();
int backdrop = g_game->room->backdropType;
bool isFlooded = backdrop == BACKDROP_DOWNPOUR || backdrop == BACKDROP_FLOODED_CAVES ||
stage == STAGE_ID_DOWNPOUR || stage == STAGE_ID_DROSS;
int shotVariant = !isFlooded ? PROJECTILE_NORMAL : stage == STAGE_ID_DROSS ? PROJECTILE_PUKE : PROJECTILE_TEAR;
if (npc.pendingDamage > 0) sub_horf_knockback(npc);
int state = npc.state;
if (state == NPC_STATE_INIT)
{
npc.entityGridCollisionClass = GRIDCOLL_NOPITS;
npc.state = NPC_STATE_SPECIAL;
npc.v1.x = 0;
return;
}
if (state == NPC_STATE_SPECIAL)
{
npc.sprite.Play("AppearEnd");
if (npc_anim_is_done(npc) || npc.pendingDamage > 0) npc.state = NPC_STATE_IDLE;
return;
}
if (state != NPC_STATE_IDLE) return;
float turn = sub_horf_angle_wrap(npc.v1.x - SUB_HORF_HALF_TURN);
int frame = std::max((int)std::floor((turn + SUB_HORF_HALF_TURN) * SUB_HORF_SHAKE_FRAMES / SUB_HORF_FULL_TURN), 0);
char shake[8];
std::snprintf(shake, sizeof(shake), "Shake%02d", frame % SUB_HORF_SHAKE_FRAMES);
npc.sprite.Play(shake);
if (npc.pendingDamage > 0 && Random() % SUB_HORF_GRUNT_ODDS == 0)
npc.PlaySound(SOUND_SHAKEY_KID_ROAR, 1, SUB_HORF_GRUNT_DELAY, false,
npc.variant == SUB_HORF_TAINTED ? SUB_HORF_TAINTED_PITCH : 1);
if (npc.variant == SUB_HORF_TAINTED && npc.stateFrame < 1) sub_horf_chase(npc, facing);
else sub_horf_spin(npc, facing, shotVariant);
bool isBleeding = vector_length(npc.velocity) > SUB_HORF_SPLAT_SPEED && npc.IsFrame(SUB_HORF_SPLAT_FRAMES, 0);
if (!isFlooded && isBleeding) npc.MakeSplat(random_unit() * SUB_HORF_SPLAT_RANGE + SUB_HORF_SPLAT_MIN);
}