Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpUnitMotionManager.cpp
- This file was added.
/* Copyright (C) 2018 Wildfire Games. | |||||
* 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 "simulation2/system/EntityMap.h" | |||||
#include "ICmpUnitMotionManager.h" | |||||
#include "ICmpUnitMotion.h" | |||||
#include "simulation2/MessageTypes.h" | |||||
/** | |||||
*/ | |||||
class CCmpUnitMotionManager : public ICmpUnitMotionManager | |||||
{ | |||||
public: | |||||
static void ClassInit(CComponentManager& componentManager) | |||||
{ | |||||
componentManager.SubscribeToMessageType(MT_Update_MotionUnit); | |||||
} | |||||
DEFAULT_COMPONENT_ALLOCATOR(UnitMotionManager) | |||||
static std::string GetSchema() | |||||
{ | |||||
return "<a:component type='system'/><empty/>"; | |||||
} | |||||
EntityMap<entity_id_t> m_Units; | |||||
virtual void Init(const CParamNode& UNUSED(paramNode)) | |||||
{ | |||||
} | |||||
virtual void Deinit() | |||||
{ | |||||
} | |||||
virtual void Serialize(ISerializer& UNUSED(serialize)) | |||||
{ | |||||
} | |||||
virtual void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize)) | |||||
{ | |||||
Init(paramNode); | |||||
} | |||||
virtual void HandleMessage(const CMessage& msg, bool UNUSED(global)) | |||||
{ | |||||
switch (msg.GetType()) | |||||
{ | |||||
case MT_Update_MotionUnit: | |||||
{ | |||||
fixed dt = static_cast<const CMessageUpdate_MotionUnit&> (msg).turnLength; | |||||
ValidateAllUnits(); | |||||
MoveAllUnits(dt); | |||||
HandleAllFailuresToMove(); | |||||
break; | |||||
} | |||||
} | |||||
} | |||||
virtual void RegisterUnit(entity_id_t ent) | |||||
{ | |||||
m_Units.insert(ent, ent); | |||||
} | |||||
virtual void UnregisterUnit(entity_id_t ent) | |||||
{ | |||||
m_Units.erase(ent); | |||||
} | |||||
void ValidateAllUnits(); | |||||
void MoveAllUnits(fixed dt); | |||||
void HandleAllFailuresToMove(); | |||||
}; | |||||
void CCmpUnitMotionManager::ValidateAllUnits() | |||||
{ | |||||
for (auto entity : m_Units) | |||||
{ | |||||
CmpPtr<ICmpUnitMotion> cmpUnitMotion(GetSimContext(), entity.first); | |||||
// no need to check for cmpUnitMotion's existence here. | |||||
cmpUnitMotion->ValidateCurrentPath(); | |||||
} | |||||
} | |||||
void CCmpUnitMotionManager::MoveAllUnits(fixed dt) | |||||
{ | |||||
/** | |||||
* First pass: try moving units along their path | |||||
* Second pass: handle consequences of that | |||||
* This architecture will let us easily add pushing to the first pass, | |||||
* and would help threading it in the future (as TryMoving never sends messages). | |||||
*/ | |||||
for (auto entity : m_Units) | |||||
{ | |||||
CmpPtr<ICmpUnitMotion> cmpUnitMotion(GetSimContext(), entity.first); | |||||
// no need to check for cmpUnitMotion's existence here. | |||||
cmpUnitMotion->TryMoving(dt); | |||||
cmpUnitMotion->PerformActualMovement(dt); | |||||
} | |||||
for (auto entity : m_Units) | |||||
{ | |||||
CmpPtr<ICmpUnitMotion> cmpUnitMotion(GetSimContext(), entity.first); | |||||
cmpUnitMotion->HandleMoveFailures(); | |||||
cmpUnitMotion->AnticipatePathingNeeds(); | |||||
} | |||||
} | |||||
void CCmpUnitMotionManager::HandleAllFailuresToMove() | |||||
{ | |||||
} | |||||
REGISTER_COMPONENT_TYPE(UnitMotionManager) |
Wildfire Games · Phabricator