Files
isaac-port/src/weapon_spirit_sword.cpp
T

225 lines
9.8 KiB
C++

#include "weapon_spirit_sword.h"
#include "boss_ai.h"
#include "entity_factory.h"
#include "entity_familiar.h"
#include "entity_knife.h"
#include "entity_npc.h"
#include "entity_player.h"
#include "entity_tear.h"
#include "game.h"
#include "manager.h"
#include "npc_ai.h"
#include "npc_basement.h"
#include "sfx_manager.h"
#include <algorithm>
#include <cmath>
constexpr float SWORD_LIFT = -10;
constexpr float SWORD_CHARGE_DELAYS = 4;
constexpr float SWORD_SWING_DELAY = 1;
constexpr float SWORD_VERSUS_DELAY = 30;
constexpr float SWORD_BEAM_DELAYS = 2;
constexpr float SWORD_BEAM_DELAY_MAX = 60;
constexpr float SWORD_PITCH_MIN = 0.5f;
constexpr float SWORD_SWING_PITCH = 1.1f;
constexpr float SWORD_SWING_PITCH_SPREAD = 0.2f;
constexpr int SWORD_SWING_SOUND = 252;
constexpr float SWORD_SHAKE_DAMAGE = 20;
constexpr float SWORD_SHAKE_SCALE = 1.5f;
constexpr float SWORD_SHAKE_PER_SCALE = 5;
constexpr float SWORD_RANDOM_TURN = 360;
constexpr int SWORD_SPIN_EFFECTS = 6;
constexpr float SWORD_RANGE_SHARE = 0.6f;
constexpr float SWORD_REACH = 300;
constexpr float SWORD_ORBIT_REACH = 100000;
constexpr int SWORD_RESIZE_FRAMES = 2;
constexpr int HELD_SWORD_BEAM_WAIT = 1;
// 009f92d0 009f9020's maker for the Spirit Sword, or none for the other types.
Weapon* weapon_spirit_sword_make(WeaponType type)
{
return type == WEAPON_SPIRIT_SWORD ? new Weapon_SpiritSword() : nullptr;
}
// 009f6c50 Spawns a sword at the owner (a familiar's sword too) hanging from a parent, made like a fresh tear of the
// owning player (the tear side picked at random), turned by its offset.
Entity_Knife* Weapon::FireSword(const KnifeShot& shot)
{
Entity_Player* player = owner_player();
Entity_Knife* sword = static_cast<Entity_Knife*>(game_spawn({.position = owner ? owner->pos : Vector{},
.spawner = owner,
.type = ENTITY_KNIFE,
.variant = shot.variant,
.seed = Random()},
POOL_OVERWRITE));
sword->parent = shot.parent;
sword->posOffset = vector_make(0, SWORD_LIFT);
if (player)
{
int displacement = (int)(Random() & 1) * 2 - 1;
TearParams params = player->GetTearHitParams(WEAPON_SPIRIT_SWORD, 1, displacement, owner);
sword->SetColor(params.tearColor, -1, TEAR_COLOR_PRIORITY, false, true);
sword->tearFlags = params.tearFlags;
sword->SetCollisionDamage(params.tearDamage);
}
sword->rotationOffset = angle_wrap(shot.rotationOffset);
if (shot.isCantOverwrite) sword->AddEntityFlags(FLAG_DONT_OVERWRITE);
return sword;
}
// 00a0a6f0 Lets the next press swing. The base's reset does not run.
void Weapon_SpiritSword::Reset() { isSwung = false; }
// The sword is removed and forgotten (Weapon_Laser::ClearReferences 00a063b0, which swords share).
void Weapon_SpiritSword::ClearReferences()
{
for (EntityPtr* held : {&technology2Laser, &sword})
{
if (!*held) continue;
(*held)->Remove();
*held = nullptr;
}
}
// 00a0a700 A spin takes four fire delays of charge.
float Weapon_SpiritSword::GetMaxCharge() { return maxFireDelay * SWORD_CHARGE_DELAYS; }
// A swing: the fire delay restarts (longer in versus), the tear side flips, Anti-Gravity lets its held beams go, the
// old sword gives way to a new one (a beam ready at full health, at most every two fire delays), an extra sword fans
// out for each extra shot, all swing at the size of the owner, the swing sounds (lower for big swords) and a heavy big
// sword shakes the screen.
void Weapon_SpiritSword::swing(const Vector& aim)
{
Entity_Player* player = owner_player();
fireDelay = g_game->deathmatchManager.state >= DEATHMATCH_INTRO ? SWORD_VERSUS_DELAY : SWORD_SWING_DELAY;
charge = 0;
isSwung = true;
bool isFull = player && player->soulHearts + player->redHearts >= player->maxHearts;
if (owner && owner->is_player()) owner->ToPlayer()->tearDisplacement = (int8_t)-tear_displacement();
if (player && player->HasCollectible(COLLECTIBLE_ANTI_GRAVITY))
for (Entity* entity : g_game->room->entityList.updateEL)
if (entity && entity->type == ENTITY_TEAR && entity->variant == TEAR_SWORD_BEAM &&
entity->spawnerEntity == owner && static_cast<Entity_Tear*>(entity)->waitFrames > HELD_SWORD_BEAM_WAIT)
static_cast<Entity_Tear*>(entity)->waitFrames = HELD_SWORD_BEAM_WAIT;
bool isBeam = isFull && beamCooldown <= 0;
if (isBeam) beamCooldown = std::min(maxFireDelay * SWORD_BEAM_DELAYS, SWORD_BEAM_DELAY_MAX);
if (sword) sword->Remove();
int variant = player && player->HasCollectible(COLLECTIBLE_TECHNOLOGY) ? KNIFE_TECH_SWORD : KNIFE_SPIRIT_SWORD;
float angle = vector_angle(aim);
std::vector<Entity_Knife*> swords;
auto fire = [&](Entity* parent, float offset, bool isCantOverwrite) {
Entity_Knife* fired = FireSword({.parent = parent, .rotationOffset = offset, .variant = variant,
.isCantOverwrite = isCantOverwrite});
fired->rotation = angle;
fired->swingEffects = 1;
fired->swingTick = 0;
swords.push_back(fired);
return fired;
};
sword = fire(owner, 0, true);
if (isBeam) sword->AddEntityFlags(FLAG_SWORD_BEAM);
Weapon_MultiShotParams params = GetMultiShotParams();
if (params.isShootingBackwards) fire(sword, 180, false);
if (params.isShootingSideways)
{
fire(sword, 90, false);
fire(sword, -90, false);
}
for (int i = 0; i < params.numRandomDirTears; i++) fire(sword, random_unit() * SWORD_RANDOM_TURN, false);
float swordScale = held_scale();
float size = swordScale;
if (g_game->frameCount - sword->spawnFrame > SWORD_RESIZE_FRAMES)
size = (swordScale - sword->sprite.scale.x) * 0.5f + sword->sprite.scale.x;
for (Entity_Knife* fired : swords)
if (fired->subtype == KNIFE_SUBTYPE_HELD && (fired->parent == sword || fired == sword))
fired->sprite.scale = vector_make(size, size);
float heaviest = 0;
for (Entity_Knife* fired : swords)
{
fired->SetAttackAnimation();
fired->Update();
heaviest = std::max(fired->collisionDamage, heaviest);
}
float pitch = std::max(1 / (swordScale * 0.5f + 0.5f), SWORD_PITCH_MIN);
int sound = SWORD_SWING_SOUND;
seed_fart_sound_override(&sound);
g_manager->sfxManager.Play(sound, {.frameDelay = 1, .pitch = (random_unit() * SWORD_SWING_PITCH_SPREAD + SWORD_SWING_PITCH) * pitch});
if (heaviest > SWORD_SHAKE_DAMAGE && swordScale > SWORD_SHAKE_SCALE)
game_shake_screen((int)(swordScale * SWORD_SHAKE_PER_SCALE));
TriggerTearEffects(aim, 1);
}
// Holding on after a swing charges the sword, which shows once full. A release once full spins it (one spin per shot
// of the volley), and Mom's Knife throws it as far as the range allows.
void Weapon_SpiritSword::charge_update(const Vector& aim, bool isShooting)
{
Entity_Knife& held = *static_cast<Entity_Knife*>(sword.get());
float full = maxFireDelay * SWORD_CHARGE_DELAYS;
if (isShooting || charge < full)
{
held.isAttacking = isShooting;
if (!held.isAttacking) charge = 0;
else
{
if (!held.isSwinging) charge = std::min(charge + (owner ? owner->speedMultiplier : 1), full);
held.isSwingCharged = full <= charge;
}
return;
}
held.SpinAttack(GetMultiShotParams().numTears, 0);
Entity_Player* player = owner_player();
if (player && player->HasCollectible(COLLECTIBLE_MOMS_KNIFE))
{
float range = player->tearRange * SWORD_RANGE_SHARE;
held.Shoot(1, std::min(range, held.tearFlags[TEAR_ORBIT] ? SWORD_ORBIT_REACH : SWORD_REACH));
}
charge = 0;
TriggerTearEffects(aim, SWORD_SPIN_EFFECTS);
}
// 00a099e0 Swings, charges and spins the sword toward the aim (the last aim, else the head's way), snapped to the four
// directions unless free, turned with the eye under R U A Wizard. A press swings once the fire delay passed and the
// sword is idle. The head turns with the sword (with its hit point while spinning). An idle sword goes away.
void Weapon_SpiritSword::Fire(const Vector& shootDirection, bool isShooting, bool isInterpolated)
{
Weapon::Fire(shootDirection, isShooting, isInterpolated);
Entity_Player* holder = owner && owner->is_player() ? owner->ToPlayer() : nullptr;
if (!isInterpolated && beamCooldown > 0) beamCooldown -= owner ? owner->speedMultiplier : 1;
if (!isShooting) isSwung = false;
Vector aim = vector_is_zero(shootDirection) ? direction : shootDirection;
if (vector_is_zero(aim) && holder) aim = vector_from_direction(holder->headDirection);
if (IsAxisAligned()) aim = vector_cardinal(aim);
if (holder && is_shot_rotated()) aim = tear_vector_rotate(aim, (float)tear_displacement() * SHOT_ROTATION_DEGREES);
if (isInterpolated) return;
bool isIdle = !sword || !static_cast<Entity_Knife*>(sword.get())->isAttacking;
if (isShooting && fireDelay <= 0 && isIdle)
{
if (sword) sword->Remove();
if (!isSwung) swing(aim);
}
if (!sword)
{
charge = 0;
return;
}
charge_update(aim, isShooting);
Entity_Knife& held = *static_cast<Entity_Knife*>(sword.get());
Vector facing = held.sprite.GetAnimation().starts_with("Spin") ? held.GetNullOffset("Hit")
: vector_from_angle(held.rotation);
Direction head = facing.y >= 0 ? DIRECTION_DOWN : DIRECTION_UP;
if (std::fabs(facing.y) < std::fabs(facing.x)) head = facing.x < 0 ? DIRECTION_LEFT : DIRECTION_RIGHT;
set_head_direction(head);
SetHeadLockTime(1);
if (!held.isFlying && !held.isSwinging && !held.isAttacking) held.Remove();
}