2013-07-29 07:13:03 -04:00
|
|
|
|
2020-03-23 09:45:09 -04:00
|
|
|
// JukeboxEntity.cpp
|
|
|
|
|
|
|
|
// Implements the cJukeboxEntity class representing a single jukebox in the world
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2020-03-23 09:45:09 -04:00
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
2013-07-29 07:13:03 -04:00
|
|
|
#include "JukeboxEntity.h"
|
|
|
|
#include "../World.h"
|
2015-11-23 18:39:19 -05:00
|
|
|
#include "../EffectID.h"
|
2014-09-26 13:13:19 -04:00
|
|
|
#include "json/value.h"
|
2018-08-28 20:51:25 -04:00
|
|
|
#include "../Entities/Player.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
cJukeboxEntity::cJukeboxEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World):
|
2020-04-13 12:38:06 -04:00
|
|
|
Super(a_BlockType, a_BlockMeta, a_Pos, a_World),
|
2013-11-14 09:37:09 -05:00
|
|
|
m_Record(0)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2017-06-15 09:32:33 -04:00
|
|
|
ASSERT(a_BlockType == E_BLOCK_JUKEBOX);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cJukeboxEntity::~cJukeboxEntity()
|
|
|
|
{
|
2020-03-23 09:45:09 -04:00
|
|
|
if (m_World && IsPlayingRecord())
|
|
|
|
{
|
|
|
|
// Stop playing music when destroyed by any means
|
|
|
|
m_World->BroadcastSoundParticleEffect(EffectID::SFX_RANDOM_PLAY_MUSIC_DISC, GetPos(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cJukeboxEntity::Destroy(void)
|
|
|
|
{
|
|
|
|
ASSERT(m_World != nullptr);
|
|
|
|
EjectRecord();
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-15 09:32:33 -04:00
|
|
|
void cJukeboxEntity::CopyFrom(const cBlockEntity & a_Src)
|
|
|
|
{
|
2020-04-13 12:38:06 -04:00
|
|
|
Super::CopyFrom(a_Src);
|
2018-05-02 03:50:36 -04:00
|
|
|
auto & src = static_cast<const cJukeboxEntity &>(a_Src);
|
2017-06-15 09:32:33 -04:00
|
|
|
m_Record = src.m_Record;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-01 17:12:44 -05:00
|
|
|
bool cJukeboxEntity::UsedBy(cPlayer * a_Player)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-02-21 16:26:04 -05:00
|
|
|
if (IsPlayingRecord())
|
|
|
|
{
|
|
|
|
EjectRecord();
|
2015-12-01 17:12:44 -05:00
|
|
|
return true;
|
2014-02-21 16:26:04 -05:00
|
|
|
}
|
|
|
|
else
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
const cItem & HeldItem = a_Player->GetEquippedItem();
|
2020-03-23 09:45:09 -04:00
|
|
|
if (PlayRecord(HeldItem.m_ItemType) && !a_Player->IsGameModeCreative())
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
a_Player->GetInventory().RemoveOneEquippedItem();
|
2015-12-01 17:12:44 -05:00
|
|
|
return true;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
}
|
2015-12-01 17:12:44 -05:00
|
|
|
return false;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-21 16:26:04 -05:00
|
|
|
bool cJukeboxEntity::PlayRecord(int a_Record)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-02-21 16:26:04 -05:00
|
|
|
if (!IsRecordItem(a_Record))
|
|
|
|
{
|
|
|
|
// This isn't a Record Item
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsPlayingRecord())
|
|
|
|
{
|
|
|
|
// A Record is already in the Jukebox.
|
|
|
|
EjectRecord();
|
|
|
|
}
|
|
|
|
m_Record = a_Record;
|
2018-07-26 19:12:41 -04:00
|
|
|
m_World->BroadcastSoundParticleEffect(EffectID::SFX_RANDOM_PLAY_MUSIC_DISC, GetPos(), m_Record);
|
2019-09-29 08:59:24 -04:00
|
|
|
m_World->SetBlockMeta(m_Pos, E_META_JUKEBOX_ON);
|
2014-02-21 16:26:04 -05:00
|
|
|
return true;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-21 16:26:04 -05:00
|
|
|
bool cJukeboxEntity::EjectRecord(void)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-02-21 16:26:04 -05:00
|
|
|
if (!IsPlayingRecord())
|
2013-11-14 10:05:55 -05:00
|
|
|
{
|
|
|
|
// There's no record here
|
2014-02-21 16:26:04 -05:00
|
|
|
return false;
|
2013-11-14 10:05:55 -05:00
|
|
|
}
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
cItems Drops;
|
2014-10-11 22:39:55 -04:00
|
|
|
Drops.push_back(cItem(static_cast<short>(m_Record), 1, 0));
|
2014-02-21 16:26:04 -05:00
|
|
|
m_Record = 0;
|
2020-03-23 09:45:09 -04:00
|
|
|
m_World->SpawnItemPickups(Drops, Vector3d(0.5, 0.5, 0.5) + m_Pos, 10);
|
2019-09-29 08:59:24 -04:00
|
|
|
m_World->SetBlockMeta(m_Pos, E_META_JUKEBOX_OFF);
|
2020-03-23 09:45:09 -04:00
|
|
|
m_World->BroadcastSoundParticleEffect(EffectID::SFX_RANDOM_PLAY_MUSIC_DISC, GetPos(), 0);
|
2014-02-21 16:26:04 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cJukeboxEntity::IsPlayingRecord(void)
|
|
|
|
{
|
|
|
|
return (m_Record != 0);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-14 10:05:55 -05:00
|
|
|
int cJukeboxEntity::GetRecord(void)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
return m_Record;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-14 10:05:55 -05:00
|
|
|
void cJukeboxEntity::SetRecord(int a_Record)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
m_Record = a_Record;
|
|
|
|
}
|