Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Entity: Track ownership for the player torch
  • Loading branch information
dscharrer committed Dec 31, 2022
1 parent b94c18c commit d057550
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 53 deletions.
14 changes: 10 additions & 4 deletions src/game/Entity.cpp
Expand Up @@ -260,6 +260,10 @@ void Entity::setOwner(Entity * owner) {
m_owner->_npcdata->weapon = nullptr;
m_owner->_npcdata->weapontype = 0;
}
if(player.torch == this) {
arx_assert(m_owner == entities.player());
ARX_PLAYER_KillTorch();
}
}

m_owner = owner;
Expand All @@ -278,6 +282,12 @@ void Entity::updateOwner() {

if(m_owner) {

if(player.torch == this) {
arx_assert(m_owner == entities.player());
show = SHOW_FLAG_ON_PLAYER;
return;
}

if((m_owner->ioflags & IO_NPC) && m_owner->_npcdata->weapon == this) {
if(show != SHOW_FLAG_HIDDEN && show != SHOW_FLAG_MEGAHIDE) {
show = (m_owner == entities.player()) ? SHOW_FLAG_ON_PLAYER : SHOW_FLAG_LINKED;
Expand Down Expand Up @@ -330,10 +340,6 @@ void Entity::cleanReferences() {
ioSteal = nullptr;
}

if(player.torch == this) {
player.torch = nullptr;
}

TREATZONE_RemoveIO(this);
gameFlags &= ~GFLAG_ISINTREATZONE;

Expand Down
74 changes: 37 additions & 37 deletions src/game/Player.cpp
Expand Up @@ -246,49 +246,52 @@ void ARX_PLAYER_RectifyPosition() {

void ARX_PLAYER_KillTorch() {

ARX_SOUND_PlaySFX(g_snd.TORCH_END);
ARX_SOUND_Stop(player.torch_loop);
player.torch_loop = audio::SourcedSample();

giveToPlayer(player.torch);
if(Entity * torch = player.torch) {
ARX_SOUND_PlaySFX(g_snd.TORCH_END);
ARX_SOUND_Stop(player.torch_loop);
player.torch_loop = audio::SourcedSample();
lightHandleGet(torchLightHandle)->m_exists = false;
player.torch = nullptr;
torch->updateOwner();
}

player.torch = nullptr;
lightHandleGet(torchLightHandle)->m_exists = false;
}

void ARX_PLAYER_ClickedOnTorch(Entity * io) {

if(!io)
return;

if(player.torch == io) {
ARX_PLAYER_KillTorch();
if(!io) {
return;
}

if(player.torch)
ARX_PLAYER_KillTorch();


if(io->durability > 0 && (!player.torch || io != player.torch)) {
// Remove the torch from the player inventory (and other ownerships) when equipping it
// Do this early to make space for the unequipped old torch
io->setOwner(nullptr);
}

if(Entity * oldTorch = player.torch) {
InventoryPos pos = locateInInventories(oldTorch);
oldTorch->setOwner(nullptr);
giveToPlayer(oldTorch, pos);
if(io == oldTorch) {
return;
}
}

if(io->durability > 0) {

if(io->ignition > 0) {
lightHandleDestroy(io->ignit_light);

ARX_SOUND_Stop(io->ignit_sound);
io->ignit_sound = audio::SourcedSample();

io->ignition = 0;
}

ARX_SOUND_PlaySFX(g_snd.TORCH_START);
player.torch_loop = ARX_SOUND_PlaySFX_loop(g_snd.TORCH_LOOP, nullptr, 1.f);

io->setOwner(nullptr);
io->show = SHOW_FLAG_ON_PLAYER;
player.torch = io;

if(g_draggedEntity == io) {
setDraggedEntity(nullptr);
}
io->setOwner(entities.player());

}

Expand All @@ -302,13 +305,10 @@ static void ARX_PLAYER_ManageTorch() {
player.torch->durability -= g_framedelay * 0.0001f;

if(player.torch->durability <= 0) {
ARX_SOUND_PlaySFX(g_snd.TORCH_END);
ARX_SOUND_Stop(player.torch_loop);
player.torch_loop = audio::SourcedSample();

player.torch->destroy();
player.torch = nullptr;
lightHandleGet(torchLightHandle)->m_exists = false;
arx_assert(player.torch_loop == audio::SourcedSample());
arx_assert(lightHandleGet(torchLightHandle)->m_exists == false);
arx_assert(!player.torch);
}

}
Expand Down Expand Up @@ -1588,7 +1588,10 @@ void ARX_PLAYER_InitPlayer() {
player.lifePool.current = player.m_lifeMaxWithoutMods = player.lifePool.max = 100.f;
player.manaPool.current = player.m_manaMaxWithoutMods = player.manaPool.max = 100.f;
player.falling = false;
player.torch = nullptr;
if(Entity * torch = player.torch) {
player.torch = nullptr;
torch->updateOwner();
}
player.gold = 0;
if(entities.player()) {
entities.player()->inventory->setBags(1);
Expand Down Expand Up @@ -2355,7 +2358,7 @@ void ARX_PLAYER_Start_New_Quest() {
SetEditMode();

g_characterCreation.resetCheat();
player.torch = nullptr;
arx_assert(!player.torch);
svar.clear();

ARX_CHANGELEVEL_StartNew();
Expand Down Expand Up @@ -2509,10 +2512,7 @@ void ARX_GAME_Reset() {
// Paths
ARX_PATH_ClearAllControled();
ARX_PATH_ClearAllUsePath();

// Player Torch
player.torch = nullptr;


// Player Quests
ARX_PLAYER_Quest_Init();

Expand Down
13 changes: 3 additions & 10 deletions src/gui/Hud.cpp
Expand Up @@ -487,17 +487,10 @@ void CurrentTorchIconGui::updateInput() {
cursorSetInteraction();

if(!g_draggedEntity && !PLAYER_MOUSELOOK_ON && DRAGGING) {
Entity * io = player.torch;

ARX_SOUND_PlaySFX(g_snd.TORCH_END);
ARX_SOUND_Stop(player.torch_loop);
player.torch_loop = audio::SourcedSample();

player.torch = nullptr;
lightHandleGet(torchLightHandle)->m_exists = false;
io->ignition = 1;
setDraggedEntity(io);
player.torch->ignition = 1;
setDraggedEntity(player.torch);
g_draggedIconOffset = m_rect.topLeft() - Vec2f(DANAEMouse);
arx_assert(!player.torch);
} else {
if(eeMouseDoubleClick1() && !COMBINE) {
COMBINE = player.torch;
Expand Down
6 changes: 4 additions & 2 deletions src/gui/Interface.cpp
Expand Up @@ -944,8 +944,10 @@ void ArxGame::managePlayerControls() {
}

if(GInput->actionNowPressed(CONTROLS_CUST_TORCH)) {
if(player.torch) {
ARX_PLAYER_KillTorch();
if(Entity * torch = player.torch) {
InventoryPos pos = locateInInventories(torch);
torch->setOwner(nullptr);
giveToPlayer(torch, pos);
} else {
useInventoryItemWithLowestDurability("torch");
}
Expand Down
3 changes: 3 additions & 0 deletions src/scene/ChangeLevel.cpp
Expand Up @@ -1617,6 +1617,9 @@ static bool ARX_CHANGELEVEL_Pop_Player(std::string_view target, float angle) {

// Restoring Player equipment...
player.torch = ConvertToValidIO(asp->curtorch);
if(player.torch) {
player.torch->setOwner(entities.player());
}
progressBarAdvance();
LoadLevelScreen();

Expand Down

0 comments on commit d057550

Please sign in to comment.