105 lines
3.8 KiB
C++
105 lines
3.8 KiB
C++
#include "grid_entity_lock.h"
|
|
#include "entity_pickup.h"
|
|
#include "game.h"
|
|
#include "room.h"
|
|
|
|
// The grid path a standing block claims, and how near a player must come to open it.
|
|
constexpr int GRID_PATH_LOCK = 4000;
|
|
constexpr float UNLOCK_DISTANCE_SQUARED = 961;
|
|
|
|
// A broken block's drop: one time in ten, a heart a quarter of the time (Daemon's Tail takes some back), else a penny.
|
|
constexpr float LOCK_DROP_CHANCE = 0.1f;
|
|
constexpr float LOCK_HEART_CHANCE = 0.25f;
|
|
|
|
// 00711780 Loads the block, a coin block under Pay To Play, standing solid or already broken.
|
|
void GridEntity_Lock::subclass_init()
|
|
{
|
|
sprite.Load("gfx/grid/grid_locks.anm2", GRAPHICS_LOAD);
|
|
isCoinLock = g_game->playerManager.FirstCollectibleOwner(COLLECTIBLE_PAY_TO_PLAY, LAZARUS_SHARE_CHECK);
|
|
if (state != 0)
|
|
{
|
|
sprite.SetFrame("Broken", 0);
|
|
collisionClass = COLLISION_NONE;
|
|
return;
|
|
}
|
|
|
|
sprite.SetFrame(isCoinLock ? "IdleCoin" : "Idle", 0);
|
|
collisionClass = COLLISION_WALL;
|
|
path_set(GRID_PATH_LOCK);
|
|
}
|
|
|
|
// 00711950 Frees the cell once the breaking animation breaks, and lets the players near a standing block open it.
|
|
void GridEntity_Lock::Update()
|
|
{
|
|
if (sprite.animState.animData && sprite.IsEventTriggered("Break"))
|
|
{
|
|
collisionClass = COLLISION_NONE;
|
|
path_set(0);
|
|
}
|
|
if (state == 0)
|
|
{
|
|
Vector position = Position();
|
|
for (size_t i = 0; i < g_game->playerManager.playerList.size(); i++)
|
|
{
|
|
Entity_Player* player = g_game->playerManager.playerList[i];
|
|
Vector offset = vector_make(player->pos.x - position.x, player->pos.y - position.y);
|
|
bool isNear = offset.y * offset.y + offset.x * offset.x < UNLOCK_DISTANCE_SQUARED;
|
|
if (player->variant == PLAYER_VARIANT_PLAYER && isNear) try_unlock(*player, false);
|
|
}
|
|
}
|
|
sprite.Update();
|
|
}
|
|
|
|
// 00711f30 Draws the sprite on its cell.
|
|
void GridEntity_Lock::Render(const Vector& offset)
|
|
{
|
|
sprite_render(offset);
|
|
}
|
|
|
|
// 00712090 Water clips around the block while it stands.
|
|
WaterClipInfo GridEntity_Lock::GetWaterClipInfo()
|
|
{
|
|
return {collisionClass != COLLISION_NONE ? WATER_CLIP_SOLID : WATER_CLIP_FLAT, 0};
|
|
}
|
|
|
|
// 00711af0 Breaks the standing block with a player's key (Paper Clip's strong form needs none) or, under Pay To Play,
|
|
// a coin. Forced, it costs nothing.
|
|
void GridEntity_Lock::try_unlock(Entity_Player& player, bool isForce)
|
|
{
|
|
if (state != 0) return;
|
|
|
|
if (!isForce && !isCoinLock && player.GetTrinketMultiplier(TRINKET_PAPER_CLIP) < 2 && !player.TryUseKey()) return;
|
|
if (!isForce && isCoinLock)
|
|
{
|
|
if (player.numCoins < 1) return;
|
|
player.AddCoins(-1);
|
|
}
|
|
grid_sound_play(isCoinLock ? SOUND_COIN_INSERT : SOUND_UNLOCK);
|
|
break_open(&player);
|
|
}
|
|
|
|
// 00711bb0 Breaks the block open (golden with a golden key), and one time in ten drops a heart or a penny. A block
|
|
// broken by no one counts the first player with Daemon's Tail.
|
|
void GridEntity_Lock::break_open(Entity_Player* player)
|
|
{
|
|
grid_sound_play(SOUND_METAL_BLOCKBREAK);
|
|
if (isCoinLock) sprite.Play("BreakingCoin");
|
|
else sprite.Play(player && player->hasGoldenKey ? "BreakingGolden" : "Breaking");
|
|
state = 1;
|
|
|
|
RNG dropRng = {spawnSeed, 1, 5, 16};
|
|
if (dropRng.RandomFloat() >= LOCK_DROP_CHANCE) return;
|
|
if (!player)
|
|
for (Entity_Player* candidate : g_game->playerManager.playerList)
|
|
if (candidate->variant == PLAYER_VARIANT_PLAYER && candidate->HasTrinket(TRINKET_DAEMONS_TAIL))
|
|
{
|
|
player = candidate;
|
|
break;
|
|
}
|
|
|
|
bool isHeart = dropRng.RandomFloat() < LOCK_HEART_CHANCE &&
|
|
(!player || player->GetTrinketRNG(TRINKET_DAEMONS_TAIL).Next() % DAEMONS_TAIL_ODDS == 0);
|
|
g_game->Spawn({.position = Position(), .type = ENTITY_PICKUP, .variant = isHeart ? PICKUP_HEART : PICKUP_COIN,
|
|
.seed = dropRng.Next()});
|
|
}
|