Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpUnitMotionManager.cpp
Show First 20 Lines • Show All 96 Lines • ▼ Show 20 Lines | virtual bool ComputingMotion() const | ||||
return m_ComputingMotion; | return m_ComputingMotion; | ||||
} | } | ||||
void OnTurnStart(); | void OnTurnStart(); | ||||
void MoveUnits(fixed dt); | void MoveUnits(fixed dt); | ||||
void MoveFormations(fixed dt); | void MoveFormations(fixed dt); | ||||
void Move(EntityMap<MotionState>& ents, fixed dt); | void Move(EntityMap<MotionState>& ents, fixed dt); | ||||
void Push(EntityMap<MotionState>::value_type& a, EntityMap<MotionState>::value_type& b); | |||||
}; | }; | ||||
void CCmpUnitMotionManager::Register(entity_id_t ent, bool formationController) | void CCmpUnitMotionManager::Register(entity_id_t ent, bool formationController) | ||||
{ | { | ||||
MotionState state = { | MotionState state = { | ||||
CmpPtr<ICmpPosition>(GetSimContext(), ent), | CmpPtr<ICmpPosition>(GetSimContext(), ent), | ||||
CmpPtr<ICmpUnitMotion>(GetSimContext(), ent), | CmpPtr<ICmpUnitMotion>(GetSimContext(), ent), | ||||
CFixedVector2D(), | CFixedVector2D(), | ||||
CFixedVector2D(), | CFixedVector2D(), | ||||
CFixedVector2D(), | |||||
fixed::Zero(), | fixed::Zero(), | ||||
fixed::Zero(), | fixed::Zero(), | ||||
false, | false, | ||||
false | false | ||||
}; | }; | ||||
if (!formationController) | if (!formationController) | ||||
m_Units.insert(ent, state); | m_Units.insert(ent, state); | ||||
else | else | ||||
Show All 38 Lines | void CCmpUnitMotionManager::Move(EntityMap<MotionState>& ents, fixed dt) | ||||
{ | { | ||||
data.second.initialPos = data.second.cmpPosition->GetPosition2D(); | data.second.initialPos = data.second.cmpPosition->GetPosition2D(); | ||||
data.second.initialAngle = data.second.cmpPosition->GetRotation().Y; | data.second.initialAngle = data.second.cmpPosition->GetRotation().Y; | ||||
data.second.pos = data.second.initialPos; | data.second.pos = data.second.initialPos; | ||||
data.second.angle = data.second.initialAngle; | data.second.angle = data.second.initialAngle; | ||||
} | } | ||||
for (EntityMap<MotionState>::value_type& data : ents) | for (EntityMap<MotionState>::value_type& data : ents) | ||||
if (data.second.cmpPosition->IsInWorld()) | |||||
data.second.cmpUnitMotion->Move(data.second, dt); | data.second.cmpUnitMotion->Move(data.second, dt); | ||||
for (EntityMap<MotionState>::value_type& data1 : m_Units) | |||||
{ | |||||
if (!data1.second.cmpPosition->IsInWorld()) | |||||
continue; | |||||
for (EntityMap<MotionState>::value_type& data2 : m_Units) | |||||
if (data1.first < data2.first && data2.second.cmpPosition->IsInWorld()) | |||||
Push(data1, data2); | |||||
} | |||||
for (EntityMap<MotionState>::value_type& data : ents) | for (EntityMap<MotionState>::value_type& data : ents) | ||||
{ | |||||
if (data.second.push.CompareLength(fixed::FromInt(2)) >= 0) | |||||
{ | |||||
data.second.wasObstructed = true; | |||||
data.second.wentStraight = false; | |||||
data.second.pos = data.second.initialPos + (data.second.pos - data.second.initialPos) / 2 + data.second.push / 2; | |||||
} | |||||
else | |||||
{ | |||||
data.second.pos += data.second.push; | |||||
} | |||||
data.second.push = CFixedVector2D(); | |||||
data.second.cmpUnitMotion->PostMove(data.second, dt); | data.second.cmpUnitMotion->PostMove(data.second, dt); | ||||
} | } | ||||
} | |||||
void CCmpUnitMotionManager::Push(EntityMap<MotionState>::value_type& a, EntityMap<MotionState>::value_type& b) | |||||
{ | |||||
CFixedVector2D offset = a.second.pos - b.second.pos; | |||||
if (offset.CompareLength(fixed::FromInt(2)) > 0) | |||||
return; | |||||
CFixedVector2D initial_offset = a.second.initialPos - b.second.initialPos; | |||||
if (initial_offset.Dot(offset) < fixed::Zero()) | |||||
{ | |||||
CFixedVector2D delta = offset.Perpendicular(); | |||||
a.second.push += delta; | |||||
b.second.push -= delta; | |||||
return; | |||||
} | |||||
offset = offset.Multiply(std::max(fixed::Zero(), fixed::FromInt(2) - offset.Length())) / 2; | |||||
a.second.push += offset; | |||||
b.second.push -= offset; | |||||
} | |||||
REGISTER_COMPONENT_TYPE(UnitMotionManager) | REGISTER_COMPONENT_TYPE(UnitMotionManager) |
Wildfire Games · Phabricator