2014-12-05 19:33:59 +01:00
|
|
|
/* Copyright (C) 2014 Wildfire Games.
|
2010-09-23 14:13:13 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "simulation2/system/Component.h"
|
|
|
|
#include "ICmpVision.h"
|
|
|
|
|
2012-09-25 00:27:32 +02:00
|
|
|
#include "simulation2/MessageTypes.h"
|
|
|
|
#include "simulation2/components/ICmpOwnership.h"
|
|
|
|
#include "simulation2/components/ICmpPlayerManager.h"
|
2013-10-15 12:05:08 +02:00
|
|
|
#include "simulation2/components/ICmpValueModificationManager.h"
|
2012-09-25 00:27:32 +02:00
|
|
|
|
2011-01-07 21:31:48 +01:00
|
|
|
class CCmpVision : public ICmpVision
|
2010-09-23 14:13:13 +02:00
|
|
|
{
|
|
|
|
public:
|
2012-09-25 00:27:32 +02:00
|
|
|
static void ClassInit(CComponentManager& componentManager)
|
2010-09-23 14:13:13 +02:00
|
|
|
{
|
2014-12-21 02:44:00 +01:00
|
|
|
componentManager.SubscribeToMessageType(MT_ValueModification);
|
2010-09-23 14:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_COMPONENT_ALLOCATOR(Vision)
|
|
|
|
|
2010-10-23 21:59:40 +02:00
|
|
|
// Template state:
|
|
|
|
|
2012-09-25 00:27:32 +02:00
|
|
|
entity_pos_t m_Range, m_BaseRange;
|
2010-09-23 14:13:13 +02:00
|
|
|
|
|
|
|
static std::string GetSchema()
|
|
|
|
{
|
|
|
|
return
|
|
|
|
"<element name='Range'>"
|
|
|
|
"<data type='nonNegativeInteger'/>"
|
|
|
|
"</element>";
|
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Init(const CParamNode& paramNode)
|
2010-09-23 14:13:13 +02:00
|
|
|
{
|
2012-09-25 00:27:32 +02:00
|
|
|
m_BaseRange = m_Range = paramNode.GetChild("Range").ToFixed();
|
2010-09-23 14:13:13 +02:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Deinit()
|
2010-09-23 14:13:13 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Serialize(ISerializer& UNUSED(serialize))
|
|
|
|
{
|
2010-10-23 21:59:40 +02:00
|
|
|
// No dynamic state to serialize
|
2010-09-23 14:13:13 +02:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize))
|
2010-09-23 14:13:13 +02:00
|
|
|
{
|
2011-01-16 15:08:38 +01:00
|
|
|
Init(paramNode);
|
2010-09-23 14:13:13 +02:00
|
|
|
}
|
|
|
|
|
2012-09-25 00:27:32 +02:00
|
|
|
virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
|
|
|
|
{
|
|
|
|
switch (msg.GetType())
|
|
|
|
{
|
2013-10-15 12:05:08 +02:00
|
|
|
case MT_ValueModification:
|
2012-09-25 00:27:32 +02:00
|
|
|
{
|
2013-10-15 12:05:08 +02:00
|
|
|
const CMessageValueModification& msgData = static_cast<const CMessageValueModification&> (msg);
|
2012-09-25 00:27:32 +02:00
|
|
|
if (msgData.component == L"Vision")
|
|
|
|
{
|
2013-10-15 12:05:08 +02:00
|
|
|
CmpPtr<ICmpValueModificationManager> cmpValueModificationManager(GetSystemEntity());
|
|
|
|
entity_pos_t newRange = cmpValueModificationManager->ApplyModifications(L"Vision/Range", m_BaseRange, GetEntityId());
|
|
|
|
if (newRange != m_Range)
|
2012-09-25 00:27:32 +02:00
|
|
|
{
|
2013-10-15 12:05:08 +02:00
|
|
|
// Update our vision range and broadcast message
|
|
|
|
entity_pos_t oldRange = m_Range;
|
|
|
|
m_Range = newRange;
|
|
|
|
CMessageVisionRangeChanged msg(GetEntityId(), oldRange, newRange);
|
|
|
|
GetSimContext().GetComponentManager().BroadcastMessage(msg);
|
2012-09-25 00:27:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-23 14:13:13 +02:00
|
|
|
virtual entity_pos_t GetRange()
|
|
|
|
{
|
|
|
|
return m_Range;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_COMPONENT_TYPE(Vision)
|