314 lines
13 KiB
C++
314 lines
13 KiB
C++
#include "npc_caves.h"
|
|
#include "sfx_manager.h"
|
|
#include "entity_effect.h"
|
|
#include "game.h"
|
|
#include "room.h"
|
|
#include "room_grid.h"
|
|
|
|
// The Globin's variants (the Black Globin is its own type), and the subtype a Globin that rises from a poof has.
|
|
enum GlobinVariant : int
|
|
{
|
|
GLOBIN_PLAIN,
|
|
GLOBIN_GAZING,
|
|
GLOBIN_DANK,
|
|
GLOBIN_CURSED
|
|
};
|
|
constexpr int GLOBIN_RISING = 10;
|
|
|
|
constexpr ColorMod DANK_GLOBIN_SPLAT_COLOR = {{0.02f, 0.05f, 0.05f, 1}, {0, 0, 0, 0}, {0, 0, 0}};
|
|
constexpr ColorMod CURSED_SPLAT_COLOR = {{1, 1, 1, 1}, {0.84f, 0.4f, 0.68f, 1}, {0, 0, 0}};
|
|
|
|
// A walking Globin paths to its target (the cursed one's pieces at their own pace), bleeds now and then and groans
|
|
// when it sees its target. A scared one runs.
|
|
constexpr int GLOBIN_SPLAT_ODDS = 10;
|
|
constexpr float GLOBIN_SPLAT_SIZE = 0.2f;
|
|
constexpr float GLOBIN_SPLAT_RANGE = 0.5f;
|
|
constexpr float GLOBIN_WALK_THRESHOLD = 0.1f;
|
|
constexpr float GLOBIN_SPEED = 0.75f;
|
|
constexpr float GAZING_SPEED = 1.1f;
|
|
constexpr float BLACK_GLOBIN_SPEED = 0.6f;
|
|
constexpr float CURSED_SPEED = 0.65f;
|
|
constexpr float CURSED_SPEED_RANGE = 0.25f;
|
|
constexpr RNG CURSED_SPEED_RNG = {0, 11, 7, 12};
|
|
constexpr float GLOBIN_EVADE_FRICTION = 0.8f;
|
|
constexpr float GLOBIN_FRICTION = 0.97f;
|
|
constexpr int GLOBIN_GROAN_ODDS = 3;
|
|
constexpr int GLOBIN_GROAN_DELAY = 40;
|
|
constexpr int CONTROLLER_ACTION_BOMB = 10;
|
|
|
|
// A regenerating Globin stays put (the gazing one backs off a target within 200), then walks again.
|
|
constexpr float GAZING_PULL = 0.5f;
|
|
constexpr float GAZING_RANGE = 200;
|
|
constexpr float GAZING_MIN_RANGE = 0.01f;
|
|
constexpr float GAZING_INPUT_DEADZONE = 0.1f;
|
|
constexpr float GAZING_INPUT_SCALE = 10;
|
|
constexpr float GAZING_FRICTION = 0.83f;
|
|
constexpr float REGEN_FRICTION = 0.7f;
|
|
|
|
// The Black Globin throws a spider toward its target each time it loses 5 health.
|
|
constexpr float BLACK_SPIDER_HEALTH = 5;
|
|
constexpr float BLACK_SPIDER_REACH = 0.75f;
|
|
constexpr float BLACK_SPIDER_RISE = -30;
|
|
constexpr float BLACK_PIECE_STEP = 40;
|
|
constexpr int SOUND_SPIDER_THROW = 181;
|
|
constexpr float SOUND_SPIDER_VOLUME = 0.25f;
|
|
|
|
// The dank one leaves a trail of black creep as it dies, and a spider as it falls (with few enemies around).
|
|
constexpr int DANK_GLOBIN_CREEP_TIMEOUT = 210;
|
|
constexpr float DANK_GLOBIN_CREEP_SCALE = 2;
|
|
constexpr int DANK_SPIDER_ENEMY_LIMIT = 7;
|
|
|
|
// A fallen Globin regenerates with 10 health. The cursed one comes back smaller and sometimes splits in two, more often
|
|
// the more it split and in boss rooms. Its pieces flicker untouchable for two frames.
|
|
constexpr float GLOBIN_REGEN_HEALTH = 10;
|
|
constexpr RNG CURSED_SPLIT_RNG = {0, 11, 7, 16};
|
|
constexpr int CURSED_SPLIT_CHANCE = 15;
|
|
constexpr int CURSED_BOSS_SPLIT_CHANCE = 25;
|
|
constexpr int CURSED_SPLIT_CAP = 99;
|
|
constexpr int CURSED_SPLIT_ROLL = 100;
|
|
constexpr float CURSED_STILL_SPEED = 0.01f;
|
|
constexpr float CURSED_SPLIT_SPEED = 4;
|
|
constexpr float CURSED_SPLIT_TURN = 1.5707964f;
|
|
constexpr int CURSED_MAX_PIECES = 4;
|
|
constexpr int CURSED_FRAMES_PER_PIECE = 10;
|
|
constexpr int CURSED_RISEN_FRAME = 40;
|
|
constexpr float CURSED_HEALTH_SHARE = 0.6f;
|
|
constexpr int CURSED_INVINCIBLE_FRAMES = 2;
|
|
|
|
// The first frame: the blood of its kind (the cursed one counts its untouchable frames before each update), then it
|
|
// walks. One that rises from a poof shows the poof and regenerates first.
|
|
static void globin_init(Entity_NPC& npc)
|
|
{
|
|
if (npc.variant == GLOBIN_CURSED)
|
|
{
|
|
npc.splatColor = g_game->GetStageID() == STAGE_ID_GEHENNA ? COLOR_DEFAULT : CURSED_SPLAT_COLOR;
|
|
npc.aiCallFlags |= AI_CALL_PRE_UPDATE;
|
|
}
|
|
else if (npc.variant == GLOBIN_DANK) npc.splatColor = DANK_GLOBIN_SPLAT_COLOR;
|
|
|
|
npc.v1.x = npc.health;
|
|
npc.state = NPC_STATE_MOVE;
|
|
if (npc.subtype != GLOBIN_RISING) return;
|
|
|
|
if (npc.HasEntityFlags(FLAG_APPEAR))
|
|
{
|
|
npc.ClearEntityFlags(FLAG_APPEAR);
|
|
Entity* poof = g_game->Spawn(
|
|
{.position = npc.pos, .spawner = &npc, .type = ENTITY_EFFECT, .variant = EFFECT_POOF01, .seed = Random()});
|
|
poof->posOffset = npc.posOffset;
|
|
poof->renderDepthOffset = npc.posOffset.y + 1;
|
|
if (npc.isChampion)
|
|
poof->SetColor(champion_sprite_color_get(npc.championColorIdx), -1, COLOR_PRIORITY_DEFAULT, false, true);
|
|
poof->Update();
|
|
}
|
|
npc.sprite.Play("ReGen");
|
|
npc.state = NPC_STATE_IDLE;
|
|
}
|
|
|
|
// The Black Globin throws a spider most of the way to its target each time it loses 5 health.
|
|
static void black_globin_spider_throw(Entity_NPC& npc)
|
|
{
|
|
if (npc.v1.x - npc.health <= BLACK_SPIDER_HEALTH) return;
|
|
|
|
npc.v1.x = npc.health;
|
|
Vector target = npc.GetPlayerTarget()->pos;
|
|
Vector reach = vector_add(npc.pos, vector_scale(vector_sub(target, npc.pos), BLACK_SPIDER_REACH));
|
|
Vector spot = room_free_pickup_position_find({.position = reach});
|
|
Entity_NPC::ThrowSpider(npc.pos, &npc, spot, false, BLACK_SPIDER_RISE);
|
|
npc.PlaySound(SOUND_SPIDER_THROW, SOUND_SPIDER_VOLUME, 2, false, 1);
|
|
}
|
|
|
|
// The dank Globin's black creep where it dies, lasting a while.
|
|
static void dank_globin_creep(Entity_NPC& npc)
|
|
{
|
|
auto* creep = static_cast<Entity_Effect*>(g_game->Spawn(
|
|
{.position = npc.pos, .spawner = &npc, .type = ENTITY_EFFECT, .variant = EFFECT_CREEP_BLACK, .seed = Random()}));
|
|
creep->parent = &npc;
|
|
creep->SetTimeout(DANK_GLOBIN_CREEP_TIMEOUT);
|
|
creep->sprite.scale = vector_make(DANK_GLOBIN_CREEP_SCALE, DANK_GLOBIN_CREEP_SCALE);
|
|
creep->Update();
|
|
}
|
|
|
|
// The walking pace: the Black Globin's slow one, the gazing one's quick one, the cursed pieces' own.
|
|
static float globin_speed_get(Entity_NPC& npc)
|
|
{
|
|
if (npc.type == ENTITY_BLACK_GLOBIN) return BLACK_GLOBIN_SPEED;
|
|
if (npc.variant == GLOBIN_GAZING) return GAZING_SPEED;
|
|
if (npc.variant != GLOBIN_CURSED || npc.subtype == 0) return GLOBIN_SPEED;
|
|
|
|
RNG rng = CURSED_SPEED_RNG;
|
|
rng._seed = npc.initSeed;
|
|
return rng.RandomFloat() * CURSED_SPEED_RANGE + CURSED_SPEED;
|
|
}
|
|
|
|
// Walking: it bleeds now and then and paths to its target (or runs when scared), groaning now and then while it sees
|
|
// its target.
|
|
static void globin_walk(Entity_NPC& npc)
|
|
{
|
|
if (Random() % GLOBIN_SPLAT_ODDS == 0)
|
|
{
|
|
Entity_Effect* splat = npc.MakeSplat(random_unit() * GLOBIN_SPLAT_RANGE + GLOBIN_SPLAT_SIZE);
|
|
splat->SetColor(npc.splatColor, -1, COLOR_PRIORITY_DEFAULT, false, true);
|
|
}
|
|
npc.AnimWalkFrame("WalkHori", "WalkVert", GLOBIN_WALK_THRESHOLD);
|
|
|
|
bool isControlled = npc.controllerID != -1;
|
|
if (npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK) && !isControlled)
|
|
{
|
|
npc.pathfinder.EvadeTarget(npc.GetPlayerTarget()->pos, STATUS_OBEYED);
|
|
npc.friction *= GLOBIN_EVADE_FRICTION;
|
|
}
|
|
else
|
|
{
|
|
float speed = globin_speed_get(npc);
|
|
npc.pathfinder.FindGridPath(npc.CalcTargetPosition(0), speed, 0, DIRECT_PATH_FAR);
|
|
}
|
|
|
|
bool isGroaning = isControlled ? npc.IsControllerActionTriggered(CONTROLLER_ACTION_BOMB)
|
|
: npc.IsHostileTo(npc.GetPlayerTarget()) && npc.pathfinder.hasDirectPath &&
|
|
Random() % GLOBIN_GROAN_ODDS == 0;
|
|
if (isGroaning) npc.PlaySound(SOUND_ZOMBIE_WALKER_KID, 1, GLOBIN_GROAN_DELAY, false, 1);
|
|
npc.friction *= GLOBIN_FRICTION;
|
|
npc.sprite.isFlipX = npc.velocity.x < 0;
|
|
}
|
|
|
|
// Regenerating: it stays put (the gazing one backs off its target), then walks again once its animation ends.
|
|
static void globin_regen_update(Entity_NPC& npc)
|
|
{
|
|
Vector toward = vector_sub(npc.GetPlayerTarget()->pos, npc.pos);
|
|
if (npc.variant == GLOBIN_GAZING)
|
|
{
|
|
if (npc.controllerID != -1)
|
|
toward = vector_scale(npc.GetControllerInput(GAZING_INPUT_DEADZONE), GAZING_INPUT_SCALE);
|
|
float distance = vector_length(toward);
|
|
if (distance < GAZING_RANGE && distance > GAZING_MIN_RANGE)
|
|
npc.AddVelocity_divided(vector_scale(vector_scale(toward, -1), GAZING_PULL / distance), false);
|
|
npc.friction *= GAZING_FRICTION;
|
|
}
|
|
else npc.friction *= REGEN_FRICTION;
|
|
|
|
if (npc.sprite.animState.animData)
|
|
{
|
|
if (npc.sprite.animState.IsEventTriggered("Regen")) npc.PlaySound(SOUND_DEATH_REVERSE, 1, 2, false, 1);
|
|
if (!npc_anim_is_done(npc)) return;
|
|
}
|
|
npc.state = NPC_STATE_MOVE;
|
|
}
|
|
|
|
// The direction a cursed Globin splits along: across its motion, or anywhere while still.
|
|
static Vector cursed_split_direction(Entity_NPC& npc)
|
|
{
|
|
if (vector_length_squared(npc.velocity) < CURSED_STILL_SPEED) return random_direction();
|
|
|
|
float cosine = std::cos(CURSED_SPLIT_TURN);
|
|
float sine = std::sin(CURSED_SPLIT_TURN);
|
|
Vector v = npc.velocity;
|
|
return vector_normalized(vector_make(v.x * cosine - v.y * sine, v.y * cosine + v.x * sine));
|
|
}
|
|
|
|
// The cursed Globin's second piece, flung the other way with the same health, look and frame of regeneration.
|
|
static void cursed_piece_spawn(Entity_NPC& npc, RNG& rng, Vector offset, int frame)
|
|
{
|
|
npc.subtype++;
|
|
npc.isInvincible = true;
|
|
npc.i1 = CURSED_INVINCIBLE_FRAMES;
|
|
npc.initSeed = rng.Next();
|
|
uint32_t seed = rng.Next();
|
|
Entity* piece = g_game->Spawn({.position = vector_add(offset, npc.pos),
|
|
.velocity = offset,
|
|
.spawner = &npc,
|
|
.type = npc.type,
|
|
.variant = npc.variant,
|
|
.seed = seed});
|
|
auto& other = static_cast<Entity_NPC&>(*piece);
|
|
other.health = npc.maxHitPoints;
|
|
other.maxHitPoints = npc.maxHitPoints;
|
|
other.sprite.Play("ReGen");
|
|
if (frame > 0 && other.sprite.animState.animData) other.sprite.animState.SetFrameFloat((float)frame);
|
|
other.ClearEntityFlags(FLAG_APPEAR);
|
|
other.subtype = npc.subtype;
|
|
other.state = NPC_STATE_IDLE;
|
|
other.splatColor = npc.splatColor;
|
|
other.AddEntityFlags(FLAG_NO_REWARD);
|
|
other.aiCallFlags |= AI_CALL_PRE_UPDATE;
|
|
other.isInvincible = true;
|
|
other.i1 = 1;
|
|
|
|
npc.velocity = vector_scale(offset, -1);
|
|
npc.pos = vector_sub(npc.pos, offset);
|
|
}
|
|
|
|
// A fallen cursed Globin rises again at 60% of its health, unless a killswitch ends it. It splits in two by chance
|
|
// (always rising, even mid regeneration), else only one walking comes back.
|
|
static void cursed_globin_rise(Entity_NPC& npc)
|
|
{
|
|
RNG rng = CURSED_SPLIT_RNG;
|
|
rng._seed = npc.initSeed;
|
|
bool isSplitting = false;
|
|
Vector direction{};
|
|
if (!npc.HasEntityFlags(FLAG_KILLSWITCH))
|
|
{
|
|
Room& room = *g_game->room;
|
|
int chance = (room.roomType == ROOM_BOSS ? CURSED_BOSS_SPLIT_CHANCE : CURSED_SPLIT_CHANCE) * npc.subtype;
|
|
isSplitting = rng.RandomInt(CURSED_SPLIT_ROLL) > (uint32_t)std::min(chance, CURSED_SPLIT_CAP);
|
|
if (isSplitting) direction = cursed_split_direction(npc);
|
|
}
|
|
if (!isSplitting && npc.state != NPC_STATE_MOVE) return;
|
|
|
|
npc.isDead = false;
|
|
int frame = (CURSED_MAX_PIECES - npc.subtype) * CURSED_FRAMES_PER_PIECE;
|
|
if (npc.state != NPC_STATE_MOVE) frame = CURSED_RISEN_FRAME;
|
|
npc_anim_restart(npc.sprite, "ReGen");
|
|
if (frame > 0 && npc.sprite.animState.animData) npc.sprite.animState.SetFrameFloat((float)frame);
|
|
|
|
npc.state = NPC_STATE_IDLE;
|
|
npc.maxHitPoints = std::max(1.0f, npc.maxHitPoints * CURSED_HEALTH_SHARE);
|
|
npc.health = npc.maxHitPoints;
|
|
if (isSplitting) cursed_piece_spawn(npc, rng, vector_scale(direction, CURSED_SPLIT_SPEED), frame);
|
|
}
|
|
|
|
// A fallen Globin: the Black Globin leaves its body and head, the cursed one rises (maybe split), and the others get
|
|
// up with 10 health while walking (the dank one dropping a spider), else stay down.
|
|
static void globin_death(Entity_NPC& npc)
|
|
{
|
|
if (npc.type == ENTITY_BLACK_GLOBIN)
|
|
{
|
|
g_game->Spawn({.position = npc.pos, .spawner = &npc, .type = ENTITY_BLACK_GLOBIN_BODY, .seed = Random()});
|
|
uint32_t seed = Random();
|
|
Vector spot = room_free_pickup_position_find({.position = npc.pos, .step = BLACK_PIECE_STEP});
|
|
g_game->Spawn({.position = spot, .spawner = &npc, .type = ENTITY_BLACK_GLOBIN_HEAD, .seed = seed});
|
|
return;
|
|
}
|
|
if (npc.variant == GLOBIN_CURSED) return cursed_globin_rise(npc);
|
|
if (npc.state != NPC_STATE_MOVE) return;
|
|
|
|
npc.isDead = false;
|
|
npc.health = GLOBIN_REGEN_HEALTH;
|
|
npc.sprite.Play("ReGen");
|
|
npc.state = NPC_STATE_IDLE;
|
|
if (npc.variant != GLOBIN_DANK || npc.GetAliveEnemyCount() >= DANK_SPIDER_ENEMY_LIMIT) return;
|
|
|
|
Entity* spider = g_game->Spawn({.position = npc.pos, .spawner = &npc, .type = ENTITY_SPIDER, .seed = Random()});
|
|
spider->ClearEntityFlags(FLAG_APPEAR);
|
|
}
|
|
|
|
// 0050e4d0 The Globins' AI: a Globin walks to its target and, beaten, falls and regenerates until it is finished off
|
|
// as a pile. The gazing one backs away while regenerating, the dank one leaves creep and spiders, the cursed one
|
|
// splits as it rises, and the Black Globin throws spiders and falls apart into its body and head.
|
|
void npc_globin_update(Entity_NPC& npc)
|
|
{
|
|
if (npc.aiCallPhase == AI_PHASE_PRE_UPDATE)
|
|
{
|
|
if (npc.i1 > 0 && --npc.i1 <= 0) npc.isInvincible = false;
|
|
return;
|
|
}
|
|
if (npc.state == NPC_STATE_INIT) return globin_init(npc);
|
|
|
|
if (npc.type == ENTITY_BLACK_GLOBIN) black_globin_spider_throw(npc);
|
|
if (npc.isDead && npc.type == ENTITY_GLOBIN && npc.variant == GLOBIN_DANK) dank_globin_creep(npc);
|
|
|
|
if (npc.state == NPC_STATE_MOVE) globin_walk(npc);
|
|
else globin_regen_update(npc);
|
|
if (npc.isDead) globin_death(npc);
|
|
}
|