203 lines
9.2 KiB
C++
203 lines
9.2 KiB
C++
#include "level/grid/fire.h"
|
|
#include "entity/weapon/bomb.h"
|
|
#include "entity/config.h"
|
|
#include "entity/effect/effect.h"
|
|
#include "entity/npc/npc.h"
|
|
#include "entity/pickup/pickup.h"
|
|
#include "entity/player/player.h"
|
|
#include "entity/weapon/projectile.h"
|
|
#include "game/game.h"
|
|
#include "game/manager.h"
|
|
#include "entity/npc/fireplace.h"
|
|
#include "entity/player/stats.h"
|
|
#include "level/room.h"
|
|
#include "engine/seeds.h"
|
|
#ifdef ISAAC_REPENTOGON
|
|
#include "repentogon/callbacks.h"
|
|
#endif
|
|
|
|
// A grid fire's grid path while it stands, the odds a fresh one burns red, and its red tints.
|
|
constexpr int GRID_FIRE_PATH = 1400;
|
|
constexpr uint32_t GRID_FIRE_RED_ODDS = 40;
|
|
constexpr ColorMod GRID_FIRE_RED = {{0.65882355f, 0.023529412f, 0.023529412f, 1}, {0, 0, 0, 0}, {0, 0, 0}};
|
|
constexpr ColorMod GRID_EMBER_RED = {{1, 0.15f, 0.15f, 1}, {0, 0, 0, 0}, {0, 0, 0}};
|
|
|
|
// The fire's stages as it is put out, the last one out, and the animations of the burning ones.
|
|
constexpr int GRID_FIRE_OUT = 4;
|
|
constexpr const char* FIRE_STAGES[GRID_FIRE_OUT] = {"FireStage00", "FireStage01", "FireStage02", "FireStage03"};
|
|
|
|
// What a burning fire deals to what touches it, and how long a fresh room waits before a red one spits.
|
|
constexpr GridHurt GRID_FIRE_HURT = {.damageFlags = DAMAGE_FIRE,
|
|
.damage = 16,
|
|
.distance = 38,
|
|
.playerDistance = 38,
|
|
.playerDamage = 1,
|
|
.isIgnoreGridCollisionClass = true};
|
|
constexpr int GRID_FIRE_SHOOT_AFTER = 60;
|
|
|
|
// The embers: at most five at once, their lifetimes, and how they waver and slow.
|
|
constexpr size_t GRID_EMBERS_MAX = 5;
|
|
constexpr uint32_t GRID_EMBER_LIFETIME = 30;
|
|
constexpr float EMBER_WAVER_X = 0.4f;
|
|
constexpr float EMBER_WAVER_Y = 0.1f;
|
|
constexpr float EMBER_DRAG_X = 0.98f;
|
|
constexpr float EMBER_DRAG_Y = 0.97f;
|
|
|
|
// A fire put out: its poof a little above the cell, and the one in ten that drops a coin or a heart (one in four, or
|
|
// one in twenty with Daemon's Tail), and the odds a hit puts it out a stage more.
|
|
constexpr float PUT_OUT_POOF_HEIGHT = 10;
|
|
constexpr float PUT_OUT_DROP_CHANCE = 0.1f;
|
|
constexpr float PUT_OUT_HEART_CHANCE = 0.25f;
|
|
constexpr uint32_t FIRE_HIT_BONUS_ODDS = 3;
|
|
|
|
// 0067db00 Sets up the fire: a fresh one may burn red, its cell's path is claimed, the fireplace, flame and ember
|
|
// sprites load (tinted red for a red fire) with the poof and drops preloaded, and it takes one of three looks.
|
|
void GridEntity_Fire::subclass_init()
|
|
{
|
|
Room& room = *g_game->room;
|
|
EntityConfig& config = g_manager->entityConfig;
|
|
|
|
if (spawnCount < 2 && variant == FIRE_NORMAL && Random() % GRID_FIRE_RED_ODDS == 0) variant = FIRE_RED;
|
|
if ((uint32_t)gridIndex < ROOM_GRID_CELLS) room.gridPaths[gridIndex] = GRID_FIRE_PATH;
|
|
sprite.Load("gfx/grid/grid_fireplace.anm2", GRAPHICS_LOAD);
|
|
sprite.Play(FLICKERING[0]);
|
|
fireSprite.Load("gfx/grid/effect_005_fire.anm2", GRAPHICS_LOAD);
|
|
fireSprite.Play(FIRE_STAGES[0]);
|
|
for (auto [type, kind] : {std::pair{ENTITY_EFFECT, (int)EFFECT_POOF01}, {ENTITY_PICKUP, PICKUP_HEART}, {ENTITY_PICKUP, PICKUP_COIN}})
|
|
if (EntityConfig_Entity* entity = config.GetEntity(type, kind, 0)) config.PreloadResources(entity);
|
|
if (variant == FIRE_RED) fireSprite.color = GRID_FIRE_RED;
|
|
|
|
shotCooldown = 0;
|
|
flickerVariant = (int)(Random() % FIRE_LOOKS);
|
|
emberSprite.Load("gfx/grid/Effect_Ember.anm2", GRAPHICS_LOAD);
|
|
emberSprite.Play("Idle");
|
|
if (variant == FIRE_RED) emberSprite.color = GRID_EMBER_RED;
|
|
embers.clear();
|
|
}
|
|
|
|
// 0067e0f0 Shows the fire's stage and burns what touches it while lit, a red one now and then spitting a fire shot at
|
|
// the nearest player in sight. Embers drift up from it, wavering, until their time is up.
|
|
void GridEntity_Fire::Update()
|
|
{
|
|
#ifdef ISAAC_REPENTOGON
|
|
GridUpdateScope scope(*this, MC_PRE_GRID_ENTITY_FIRE_UPDATE, MC_POST_GRID_ENTITY_FIRE_UPDATE);
|
|
if (scope.isSkipped) return;
|
|
#endif
|
|
Game& game = *g_game;
|
|
Room& room = *game.room;
|
|
|
|
if (state < GRID_FIRE_OUT) fireSprite.Play(FIRE_STAGES[state]);
|
|
else if (state == GRID_FIRE_OUT)
|
|
{
|
|
fireSprite.Stop();
|
|
fireSprite.GetLayer(0)->isVisible = false;
|
|
}
|
|
if (state >= GRID_FIRE_OUT)
|
|
{
|
|
sprite.Play(NO_FIRE[flickerVariant]);
|
|
collisionClass = COLLISION_NONE;
|
|
}
|
|
else
|
|
{
|
|
sprite.Play(FLICKERING[flickerVariant]);
|
|
hurt_surroundings(GRID_FIRE_HURT);
|
|
collisionClass = COLLISION_OBJECT;
|
|
}
|
|
fireSprite.Update();
|
|
sprite.Update();
|
|
|
|
bool isAiming = variant == FIRE_RED && shotCooldown < 1 && state < GRID_FIRE_OUT && Random() % FIRE_AIM_ODDS == 0 &&
|
|
room.isInitialized && game.frameCount - room.enterFrame > GRID_FIRE_SHOOT_AFTER;
|
|
if (isAiming)
|
|
{
|
|
Vector position = room.GetGridPosition(gridIndex);
|
|
Vector target = game_nearest_player(position)->pos;
|
|
Vector direction = vector_normalized(vector_sub(target, position));
|
|
Vector mouth = vector_add(position, vector_scale(direction, FIRE_MOUTH_REACH));
|
|
if (vector_distance(target, position) < FIRE_SIGHT && room_check_line(mouth, target, LINE_MODE_PROJECTILE, 0) &&
|
|
Random() % FIRE_SHOOT_ODDS == 0)
|
|
{
|
|
Entity* shot = game.Spawn({.position = mouth,
|
|
.velocity = vector_scale(direction, FIRE_SHOT_SPEED),
|
|
.type = ENTITY_PROJECTILE,
|
|
.seed = Random()});
|
|
static_cast<Entity_Projectile*>(shot)->projectileFlags |= PROJECTILE_FIRE_FLAG;
|
|
shotCooldown = FIRE_SHOOT_COOLDOWN;
|
|
}
|
|
}
|
|
else if (shotCooldown > 0) shotCooldown--;
|
|
|
|
if (Random() % FIRE_EMBER_ODDS == 0 && embers.size() < GRID_EMBERS_MAX)
|
|
{
|
|
FireEmber ember;
|
|
ember.lifetime = (int)(Random() % GRID_EMBER_LIFETIME);
|
|
ember.velocity.y = random_unit() * -FIRE_EMBER_RISE_RANGE - FIRE_EMBER_RISE_MIN;
|
|
ember.velocity.x = random_unit() * FIRE_EMBER_DRIFT - FIRE_EMBER_DRIFT / 2;
|
|
ember.position.y = -FIRE_EMBER_HEIGHT - random_unit() * FIRE_EMBER_HEIGHT;
|
|
ember.position.x = random_unit() * FIRE_EMBER_SPREAD - FIRE_EMBER_SPREAD / 2;
|
|
embers.push_back(ember);
|
|
}
|
|
for (size_t i = 0; i < embers.size();)
|
|
{
|
|
FireEmber& ember = embers[i];
|
|
ember.position = vector_add(ember.position, ember.velocity);
|
|
float waverY = random_unit() * EMBER_WAVER_Y - EMBER_WAVER_Y / 2;
|
|
float waverX = random_unit() * EMBER_WAVER_X - EMBER_WAVER_X / 2;
|
|
ember.frame++;
|
|
ember.velocity.x = (waverX + ember.velocity.x) * EMBER_DRAG_X;
|
|
ember.velocity.y = (waverY + ember.velocity.y) * EMBER_DRAG_Y;
|
|
if (ember.frame < ember.lifetime) i++;
|
|
else embers.erase(embers.begin() + (long)i);
|
|
}
|
|
}
|
|
|
|
// 0067e880 Draws the fireplace and its flame, and while it burns each ember at its age's frame.
|
|
void GridEntity_Fire::Render(const Vector& offset)
|
|
{
|
|
Vector position = vector_add(get_render_position(), offset);
|
|
|
|
sprite.Render({.position = position});
|
|
fireSprite.Render({.position = position});
|
|
if (state >= GRID_FIRE_OUT) return;
|
|
|
|
for (FireEmber& ember : embers)
|
|
{
|
|
if (emberSprite.animState.animData)
|
|
{
|
|
float age = (float)ember.frame / (float)ember.lifetime;
|
|
emberSprite.animState.SetFrameFloat((float)(int)(age * (float)emberSprite.animState.GetAnimationLength()));
|
|
}
|
|
emberSprite.Render({.position = vector_add(position, ember.position)});
|
|
}
|
|
}
|
|
|
|
// 0067ea60 Puts the fire out by the damage's stages, one more a third of the time. Once out it hisses and poofs, frees
|
|
// its cell, counts for its player (Black Powder's blast) and one time in ten drops a coin, or a heart.
|
|
bool GridEntity_Fire::Hurt(int damage, const EntityRef& source)
|
|
{
|
|
Game& game = *g_game;
|
|
int stages = Random() % FIRE_HIT_BONUS_ODDS == 0 ? std::min(damage + 1, GRID_FIRE_OUT) : damage;
|
|
|
|
if (stages < 1 || state >= GRID_FIRE_OUT) return false;
|
|
state = std::min(state + stages, GRID_FIRE_OUT);
|
|
if (state != GRID_FIRE_OUT) return true;
|
|
|
|
grid_sound_play(SOUND_STEAM_HALFSEC, {.volume = PUT_OUT_VOLUME});
|
|
Vector poof = vector_add(game.room->GetGridPosition(gridIndex), vector_make(0, -PUT_OUT_POOF_HEIGHT));
|
|
game.Spawn({.position = poof, .type = ENTITY_EFFECT, .variant = EFFECT_POOF01, .seed = Random()});
|
|
if ((uint32_t)gridIndex < ROOM_GRID_CELLS) game.room->gridPaths[gridIndex] = GRID_PATH_FREED;
|
|
auto* player = source.entity ? static_cast<Entity_Player*>(source.entity->GetPlayerAncestor()) : nullptr;
|
|
if (player)
|
|
{
|
|
Player_stats_add(game.onlinePlayerStats, ONLINE_STAT_FIRES_PUT_OUT, player, 1);
|
|
if (player->HasCollectible(COLLECTIBLE_BLACK_POWDER))
|
|
explosion_effects({.position = Position(), .source = player, .damage = BLACK_POWDER_DAMAGE});
|
|
}
|
|
if (random_unit() >= PUT_OUT_DROP_CHANCE) return true;
|
|
|
|
bool isHeart = random_unit() < PUT_OUT_HEART_CHANCE &&
|
|
(!game.playerManager.AnyoneHasTrinket(TRINKET_DAEMONS_TAIL) || Random() % (uint32_t)DAEMONS_TAIL_HEART_ODDS == 0);
|
|
game.Spawn({.position = Position(), .type = ENTITY_PICKUP, .variant = isHeart ? PICKUP_HEART : PICKUP_COIN, .seed = Random()});
|
|
return true;
|
|
}
|