1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
4 #include "VehicleHelperObject.h"
7 #include "VehicleData.h"
8 #include "VehiclePrototype.h"
9 #include "VehiclePart.h"
11 REGISTER_CLASS_DESC(CVehicleHelperClassDesc
);
13 //////////////////////////////////////////////////////////////////////////
14 // CBase implementation.
15 //////////////////////////////////////////////////////////////////////////
16 IMPLEMENT_DYNCREATE(CVehicleHelper
, CBaseObject
)
20 #define VEHICLE_HELPER_COLOR (RGB(255, 255, 120))
21 #define VEHICLE_ASSET_HELPER_COLOR (RGB(120, 255, 120))
23 //////////////////////////////////////////////////////////////////////////
24 CVehicleHelper::CVehicleHelper()
27 m_fromGeometry
= false;
30 ChangeColor(VEHICLE_HELPER_COLOR
);
32 InitOnTransformCallback(this);
35 CVehicleHelper::~CVehicleHelper()
39 //////////////////////////////////////////////////////////////////////////
40 void CVehicleHelper::SetVariable(IVariable
* pVar
)
44 DisableUpdateObjectOnVarChange("position");
45 DisableUpdateObjectOnVarChange("direction");
47 if (IVariable
* pVar
= GetChildVar(m_pVar
, "position"))
49 pVar
->SetLimits(-10, 10);
52 EnableUpdateObjectOnVarChange("position");
53 EnableUpdateObjectOnVarChange("direction");
56 //////////////////////////////////////////////////////////////////////////
57 void CVehicleHelper::UpdateVarFromObject()
66 if (!m_pVar
|| !m_pVehicle
)
69 IEntity
* pEnt
= m_pVehicle
->GetCEntity()->GetIEntity();
72 CryWarning(VALIDATOR_MODULE_EDITOR
, VALIDATOR_WARNING
, "[CVehicleHelper::UpdateVariable] pEnt is null, returning");
76 if (IVariable
* pVar
= GetChildVar(m_pVar
, "name"))
79 if (IVariable
* pVar
= GetChildVar(m_pVar
, "position"))
81 Vec3 pos
= pEnt
->GetWorldTM().GetInvertedFast().TransformPoint(GetWorldPos());
85 IVariable
* pVar
= GetChildVar(m_pVar
, "direction");
88 pVar
= new CVariable
<Vec3
>;
89 pVar
->SetName("direction");
90 m_pVar
->AddVariable(pVar
);
93 IVariable
* pPartVar
= GetChildVar(m_pVar
, "part");
96 pPartVar
= new CVariable
<string
>;
97 pPartVar
->SetName("part");
98 m_pVar
->AddVariable(pPartVar
);
101 Matrix33 relTM
= Matrix33(pEnt
->GetWorldTM().GetInvertedFast()) * Matrix33(GetWorldTM());
102 Vec3 dir
= relTM
.TransformVector(FORWARD_DIRECTION
);
107 //////////////////////////////////////////////////////////////////////////
108 void CVehicleHelper::UpdateObjectFromVar()
110 if (!m_pVar
|| !m_pVehicle
)
113 if (IVariable
* pVar
= GetChildVar(m_pVar
, "position"))
117 Matrix34 tm
= GetWorldTM();
118 tm
.SetTranslation(m_pVehicle
->GetCEntity()->GetIEntity()->GetWorldTM().TransformPoint(local
));
123 //////////////////////////////////////////////////////////////////////////
124 void CVehicleHelper::Done()
126 VeedLog("[CVehicleHelper:Done] <%s>", GetName());
130 //////////////////////////////////////////////////////////////////////////
131 bool CVehicleHelper::Init(CBaseObject
* prev
, const string
& file
)
133 SetColor(RGB(255, 255, 0));
134 bool res
= CBaseObject::Init(prev
, file
);
139 //////////////////////////////////////////////////////////////////////////
140 void CVehicleHelper::PostClone(CBaseObject
* pFromObject
, CObjectCloneContext
& ctx
)
142 CBaseObject
* pFromParent
= pFromObject
->GetParent();
145 CBaseObject
* pChildParent
= ctx
.FindClone(pFromParent
);
147 pChildParent
->AttachChild(this, false);
150 // // helper was cloned and attached to same parent
151 // if (pFromParent->IsKindOf(RUNTIME_CLASS(CVehiclePart)))
152 // ((CVehiclePart*)pFromParent)->AddHelper( this, 0 );
154 // pFromParent->AttachChild(this, false);
159 //////////////////////////////////////////////////////////////////////////
160 bool CVehicleHelper::HitTest(HitContext
& hc
)
162 Vec3 origin
= GetWorldPos();
163 float radius
= RADIUS
;
165 Vec3 w
= origin
- hc
.raySrc
;
166 w
= hc
.rayDir
.Cross(w
);
167 float d
= w
.GetLength();
169 if (d
< radius
+ hc
.distanceTolerance
)
172 if (Intersect::Ray_SphereFirst(Ray(hc
.raySrc
, hc
.rayDir
), Sphere(origin
, radius
), i0
))
174 hc
.dist
= hc
.raySrc
.GetDistance(i0
);
177 hc
.dist
= hc
.raySrc
.GetDistance(origin
);
183 //////////////////////////////////////////////////////////////////////////
184 void CVehicleHelper::Display(DisplayContext
& dc
)
186 COLORREF color
= GetColor();
187 float radius
= RADIUS
;
189 //dc.SetColor( color, 0.5f );
190 //dc.DrawBall( GetPos(), radius );
194 dc
.SetSelectedColor(0.6f
);
199 dc
.PushMatrix(GetWorldTM());
201 if (!IsHighlighted())
203 dc
.SetColor(color
, 0.8f
);
205 dc
.DrawWireBox(box
.min
, box
.max
);
211 Vec3
dirEndPos(0, 4 * box
.max
.y
, 0);
212 dc
.DrawArrow(Vec3(0, box
.max
.y
, 0), dirEndPos
, 0.15f
);
218 if (dc
.flags
& DISPLAY_HIDENAMES
)
220 Vec3
p(GetWorldPos());
221 DrawLabel(dc
, p
, RGB(255, 255, 255));
227 //////////////////////////////////////////////////////////////////////////
228 void CVehicleHelper::GetBoundBox(AABB
& box
)
230 // Transform local bounding box into world space.
232 box
.SetTransformedAABB(GetWorldTM(), box
);
235 //////////////////////////////////////////////////////////////////////////
236 void CVehicleHelper::GetLocalBounds(AABB
& box
)
238 // return local bounds
240 box
.min
= -Vec3(r
, r
, r
);
241 box
.max
= Vec3(r
, r
, r
);
244 //////////////////////////////////////////////////////////////////////////
245 void CVehicleHelper::GetBoundSphere(Vec3
& pos
, float& radius
)
251 //////////////////////////////////////////////////////////////////////////
252 void CVehicleHelper::UpdateScale(float scale
)
254 if (IVariable
* pPos
= GetChildVar(m_pVar
, "position"))
258 pPos
->Set(pos
*= scale
);
259 UpdateObjectFromVar();
263 //////////////////////////////////////////////////////////////////////////
264 void CVehicleHelper::SetIsFromGeometry(bool b
)
269 ChangeColor(VEHICLE_ASSET_HELPER_COLOR
);
274 ChangeColor(VEHICLE_HELPER_COLOR
);
279 //////////////////////////////////////////////////////////////////////////
280 void CVehicleHelper::OnTransform()
282 UpdateVarFromObject();