Changeset View
Standalone View
source/simulation2/components/CCmpVisualActor.cpp
Show All 33 Lines | |||||
#include "ICmpVisibility.h" | #include "ICmpVisibility.h" | ||||
#include "ICmpSound.h" | #include "ICmpSound.h" | ||||
#include "simulation2/serialization/SerializeTemplates.h" | #include "simulation2/serialization/SerializeTemplates.h" | ||||
#include "graphics/Decal.h" | #include "graphics/Decal.h" | ||||
#include "graphics/Frustum.h" | #include "graphics/Frustum.h" | ||||
#include "graphics/Model.h" | #include "graphics/Model.h" | ||||
#include "graphics/ModelDef.h" | |||||
#include "graphics/ObjectBase.h" | #include "graphics/ObjectBase.h" | ||||
#include "graphics/ObjectEntry.h" | #include "graphics/ObjectEntry.h" | ||||
#include "graphics/Unit.h" | #include "graphics/Unit.h" | ||||
#include "graphics/UnitAnimation.h" | #include "graphics/UnitAnimation.h" | ||||
#include "graphics/UnitManager.h" | #include "graphics/UnitManager.h" | ||||
#include "maths/BoundingSphere.h" | #include "maths/BoundingSphere.h" | ||||
#include "maths/Matrix3D.h" | #include "maths/Matrix3D.h" | ||||
#include "maths/Vector3D.h" | #include "maths/Vector3D.h" | ||||
Show All 31 Lines | private: | ||||
bool m_AnimOnce; | bool m_AnimOnce; | ||||
fixed m_AnimSpeed; | fixed m_AnimSpeed; | ||||
std::wstring m_SoundGroup; | std::wstring m_SoundGroup; | ||||
fixed m_AnimDesync; | fixed m_AnimDesync; | ||||
fixed m_AnimSyncRepeatTime; // 0.0 if not synced | fixed m_AnimSyncRepeatTime; // 0.0 if not synced | ||||
fixed m_AnimSyncOffsetTime; | fixed m_AnimSyncOffsetTime; | ||||
std::map<CStr, CStr> m_VariantSelections; | std::map<CStr, CStr> m_VariantSelections; | ||||
/** | |||||
elexisUnsubmitted Done Inline Actionselexis: ```
/**
* Can be used for ...
*/
``` | |||||
Done Inline Actions/** * Can be used to retrieve special locations given by the artists * to place actors with precision on this actor. */ Stan: ```lang=cpp
/**
* Can be used to retrieve special locations given by the artists
* to place… | |||||
* Can be used to retrieve special locations given by the artists | |||||
* to place actors with precision on this actor. | |||||
*/ | |||||
std::vector<JsProp> m_PropPointsPositions; | |||||
u32 m_Seed; // seed used for random variations | u32 m_Seed; // seed used for random variations | ||||
bool m_ConstructionPreview; | bool m_ConstructionPreview; | ||||
bool m_VisibleInAtlasOnly; | bool m_VisibleInAtlasOnly; | ||||
bool m_IsActorOnly; // an in-world entity should not have this or it might not be rendered. | bool m_IsActorOnly; // an in-world entity should not have this or it might not be rendered. | ||||
▲ Show 20 Lines • Show All 113 Lines • ▼ Show 20 Lines | virtual void Init(const CParamNode& paramNode) | ||||
m_IsActorOnly = paramNode.GetChild("ActorOnly").IsOk(); | m_IsActorOnly = paramNode.GetChild("ActorOnly").IsOk(); | ||||
m_SilhouetteDisplay = paramNode.GetChild("SilhouetteDisplay").ToBool(); | m_SilhouetteDisplay = paramNode.GetChild("SilhouetteDisplay").ToBool(); | ||||
m_SilhouetteOccluder = paramNode.GetChild("SilhouetteOccluder").ToBool(); | m_SilhouetteOccluder = paramNode.GetChild("SilhouetteOccluder").ToBool(); | ||||
m_DisableShadows = paramNode.GetChild("DisableShadows").ToBool(); | m_DisableShadows = paramNode.GetChild("DisableShadows").ToBool(); | ||||
// Initialize the model's selection shape descriptor. This currently relies on the component initialization order; the | // Initialize the model's selection shape descriptor. This currently relies on the component initialization order; the | ||||
// Footprint component must be initialized before this component (VisualActor) to support the ability to use the footprint | // Footprint component must be initialized before this component (VisualActor) to support the ability to use the footprint | ||||
// shape for the selection box (instead of the default recursive bounding box). See TypeList.h for the order in | // shape for the selection box (instead of the default recursive bounding box). See TypeList.h for the order in | ||||
Done Inline ActionsWhy does the simulation store printable names intead of the names? elexis: Why does the simulation store printable names intead of the names? | |||||
Done Inline ActionsThat's the only function I found that returns a std::string. Stan: That's the only function I found that returns a std::string. | |||||
// which components are initialized; if for whatever reason you need to get rid of this dependency, you can always just | // which components are initialized; if for whatever reason you need to get rid of this dependency, you can always just | ||||
// initialize the selection shape descriptor on-demand. | // initialize the selection shape descriptor on-demand. | ||||
InitSelectionShapeDescriptor(paramNode); | InitSelectionShapeDescriptor(paramNode); | ||||
} | } | ||||
virtual void Deinit() | virtual void Deinit() | ||||
{ | { | ||||
if (m_Unit) | if (m_Unit) | ||||
Done Inline ActionsIf you know the size of m_PropPointsPositions you can reserve the space in advance. Also you can avoid the copy by either using emplace_back and passing the three arguments to the constructor, or default constructing with emplace_back() and then operating on the back() reference. elexis: If you know the size of `m_PropPointsPositions` you can `reserve` the space in advance.
Also… | |||||
Done Inline ActionsSo something like m_PropPointsPositions.resize(props.size())
Do you have an example of the latter ? Stan: So something like
```lang=cpp ?
m_PropPointsPositions.resize(props.size())
```
> Also you… | |||||
{ | { | ||||
GetSimContext().GetUnitManager().DeleteUnit(m_Unit); | GetSimContext().GetUnitManager().DeleteUnit(m_Unit); | ||||
m_Unit = NULL; | m_Unit = NULL; | ||||
} | } | ||||
} | } | ||||
template<typename S> | template<typename S> | ||||
void SerializeCommon(S& serialize) | void SerializeCommon(S& serialize) | ||||
▲ Show 20 Lines • Show All 208 Lines • ▼ Show 20 Lines | virtual void SetVariant(const CStr& key, const CStr& selection) | ||||
} | } | ||||
} | } | ||||
virtual std::string GetAnimationName() const | virtual std::string GetAnimationName() const | ||||
{ | { | ||||
return m_AnimName; | return m_AnimName; | ||||
} | } | ||||
virtual std::vector<JsProp> GetPropPoints() const; | |||||
virtual void SelectAnimation(const std::string& name, bool once = false, fixed speed = fixed::FromInt(1)) | virtual void SelectAnimation(const std::string& name, bool once = false, fixed speed = fixed::FromInt(1)) | ||||
{ | { | ||||
m_AnimName = name; | m_AnimName = name; | ||||
m_AnimOnce = once; | m_AnimOnce = once; | ||||
m_AnimSpeed = speed; | m_AnimSpeed = speed; | ||||
m_SoundGroup = L""; | m_SoundGroup = L""; | ||||
m_AnimDesync = fixed::FromInt(1)/20; // TODO: make this an argument | m_AnimDesync = fixed::FromInt(1)/20; // TODO: make this an argument | ||||
m_AnimSyncRepeatTime = fixed::Zero(); | m_AnimSyncRepeatTime = fixed::Zero(); | ||||
m_AnimSyncOffsetTime = fixed::Zero(); | m_AnimSyncOffsetTime = fixed::Zero(); | ||||
SetVariant("animation", m_AnimName); | SetVariant("animation", m_AnimName); | ||||
CmpPtr<ICmpSound> cmpSound(GetEntityHandle()); | CmpPtr<ICmpSound> cmpSound(GetEntityHandle()); | ||||
if (cmpSound) | if (cmpSound) | ||||
m_SoundGroup = cmpSound->GetSoundGroup(wstring_from_utf8(m_AnimName)); | m_SoundGroup = cmpSound->GetSoundGroup(wstring_from_utf8(m_AnimName)); | ||||
if (!m_Unit || !m_Unit->GetAnimation() || !m_Unit->GetID()) | if (!m_Unit || !m_Unit->GetAnimation() || !m_Unit->GetID()) | ||||
return; | return; | ||||
m_Unit->GetAnimation()->SetAnimationState(m_AnimName, m_AnimOnce, m_AnimSpeed.ToFloat(), m_AnimDesync.ToFloat(), m_SoundGroup.c_str()); | m_Unit->GetAnimation()->SetAnimationState(m_AnimName, m_AnimOnce, m_AnimSpeed.ToFloat(), m_AnimDesync.ToFloat(), m_SoundGroup.c_str()); | ||||
} | } | ||||
virtual void SelectMovementAnimation(const std::string& name, fixed speed) | virtual void SelectMovementAnimation(const std::string& name, fixed speed) | ||||
{ | { | ||||
ENSURE(name == "idle" || name == "walk" || name == "run"); | ENSURE(name == "idle" || name == "walk" || name == "run"); | ||||
if (m_AnimName != "idle" && m_AnimName != "walk" && m_AnimName != "run") | if (m_AnimName != "idle" && m_AnimName != "walk" && m_AnimName != "run") | ||||
return; | return; | ||||
SelectAnimation(name, false, speed); | SelectAnimation(name, false, speed); | ||||
▲ Show 20 Lines • Show All 167 Lines • ▼ Show 20 Lines | if (cmpModelRenderer) | ||||
m_ModelTag = cmpModelRenderer->AddUnit(GetEntityHandle(), m_Unit, boundSphere, flags); | m_ModelTag = cmpModelRenderer->AddUnit(GetEntityHandle(), m_Unit, boundSphere, flags); | ||||
} | } | ||||
} | } | ||||
// the model is now responsible for cleaning up the descriptor | // the model is now responsible for cleaning up the descriptor | ||||
if (m_ShapeDescriptor != nullptr) | if (m_ShapeDescriptor != nullptr) | ||||
m_Unit->GetModel().SetCustomSelectionShape(m_ShapeDescriptor); | m_Unit->GetModel().SetCustomSelectionShape(m_ShapeDescriptor); | ||||
if (m_Unit == nullptr || m_Unit->GetModel().ToCModel() == nullptr) | |||||
return; | |||||
std::vector<SPropPoint>& props = m_Unit->GetModel().ToCModel()->GetModelDef()->m_PropPoints; | |||||
m_PropPointsPositions.reserve(props.size()); | |||||
for (const SPropPoint& prop : props) | |||||
{ | |||||
JsProp jsProp; | |||||
jsProp.m_Name = prop.m_Name.EscapeToPrintableASCII(); | |||||
jsProp.m_Position = CFixedVector3D( | |||||
fixed::FromFloat(prop.m_Position.X), | |||||
fixed::FromFloat(prop.m_Position.Y), | |||||
fixed::FromFloat(prop.m_Position.Z) | |||||
); | |||||
m_PropPointsPositions.emplace_back(jsProp); | |||||
} | |||||
} | } | ||||
void CCmpVisualActor::InitSelectionShapeDescriptor(const CParamNode& paramNode) | void CCmpVisualActor::InitSelectionShapeDescriptor(const CParamNode& paramNode) | ||||
{ | { | ||||
// by default, we don't need a custom selection shape and we can just keep the default behaviour | // by default, we don't need a custom selection shape and we can just keep the default behaviour | ||||
Done Inline ActionsNewline Stan: Newline | |||||
m_ShapeDescriptor = nullptr; | m_ShapeDescriptor = nullptr; | ||||
const CParamNode& shapeNode = paramNode.GetChild("SelectionShape"); | const CParamNode& shapeNode = paramNode.GetChild("SelectionShape"); | ||||
if (shapeNode.IsOk()) | if (shapeNode.IsOk()) | ||||
{ | { | ||||
Done Inline Actionswhitespace elexis: whitespace | |||||
if (shapeNode.GetChild("Bounds").IsOk()) | if (shapeNode.GetChild("Bounds").IsOk()) | ||||
{ | { | ||||
// default; no need to take action | // default; no need to take action | ||||
} | } | ||||
else if (shapeNode.GetChild("Footprint").IsOk()) | else if (shapeNode.GetChild("Footprint").IsOk()) | ||||
{ | { | ||||
CmpPtr<ICmpFootprint> cmpFootprint(GetEntityHandle()); | CmpPtr<ICmpFootprint> cmpFootprint(GetEntityHandle()); | ||||
if (cmpFootprint) | if (cmpFootprint) | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | void CCmpVisualActor::ReloadUnitAnimation() | ||||
m_Unit->GetAnimation()->SetAnimationState(m_AnimName, m_AnimOnce, m_AnimSpeed.ToFloat(), m_AnimDesync.ToFloat(), m_SoundGroup.c_str()); | m_Unit->GetAnimation()->SetAnimationState(m_AnimName, m_AnimOnce, m_AnimSpeed.ToFloat(), m_AnimDesync.ToFloat(), m_SoundGroup.c_str()); | ||||
// We'll lose the exact synchronisation but we should at least make sure it's going at the correct rate | // We'll lose the exact synchronisation but we should at least make sure it's going at the correct rate | ||||
if (!m_AnimSyncRepeatTime.IsZero()) | if (!m_AnimSyncRepeatTime.IsZero()) | ||||
m_Unit->GetAnimation()->SetAnimationSyncRepeat(m_AnimSyncRepeatTime.ToFloat()); | m_Unit->GetAnimation()->SetAnimationSyncRepeat(m_AnimSyncRepeatTime.ToFloat()); | ||||
if (!m_AnimSyncOffsetTime.IsZero()) | if (!m_AnimSyncOffsetTime.IsZero()) | ||||
m_Unit->GetAnimation()->SetAnimationSyncOffset(m_AnimSyncOffsetTime.ToFloat()); | m_Unit->GetAnimation()->SetAnimationSyncOffset(m_AnimSyncOffsetTime.ToFloat()); | ||||
} | } | ||||
std::vector<JsProp> CCmpVisualActor::GetPropPoints() const | |||||
Done Inline ActionsHope that it performs copy elision or take it into your own hands to ensure that? elexis: Hope that it performs copy elision or take it into your own hands to ensure that? | |||||
Done Inline ActionsLikely not since I don't know what https://en.cppreference.com/w/cpp/language/copy_elision is :) Stan: Likely not since I don't know what https://en.cppreference.com/w/cpp/language/copy_elision is :) | |||||
{ | |||||
return m_PropPointsPositions; | |||||
} |