-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathObject.cpp
167 lines (142 loc) · 4.07 KB
/
Object.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "Object.h"
#include "ObjectManager.h"
#include "Logic.h"
#include "Sector.h"
#include <cassert>
#include <cfloat>
Object::Object()
{
m_ObjPhysicsData.m_uSector = INVALID_SECTOR;
m_uFlags = OBJFLAGS_ACTIVE;
m_uID = 0;
m_uRefCnt = 0;
m_uRenderKey = 0xffffffff;
m_pDataComp = nullptr;
m_pRenderComp = nullptr;
m_pCollisionComp = nullptr;
m_ObjPhysicsData.m_Loc.Set(0.0f, 0.0f, 0.0f);
m_ObjPhysicsData.m_Dir.Set(0.0f, 1.0f, 0.0f);
m_ObjPhysicsData.m_Up.Set(0.0f, 0.0f, 1.0f);
m_ObjPhysicsData.m_Scale.Set(1.0f, 1.0f, 1.0f);
m_ObjPhysicsData.m_worldX = 0;
m_ObjPhysicsData.m_worldY = 0;
m_worldMtx.Identity();
m_Name = "Default";
m_nLightCount = 0;
m_bAllocGameData = false;
m_bCollisionEnable = true;
}
Object::~Object()
{
Reset();
}
void Object::Reset()
{
m_uRefCnt = 0;
m_Name = "Default";
m_ObjPhysicsData.m_uSector = INVALID_SECTOR;
m_uFlags = OBJFLAGS_ACTIVE;
m_uRenderKey = 0xffffffff;
m_fBrightness = 1.0f;
m_ObjPhysicsData.m_Loc.Set(0.0f, 0.0f, 0.0f);
m_ObjPhysicsData.m_Dir.Set(0.0f, 1.0f, 0.0f);
m_ObjPhysicsData.m_Up.Set(0.0f, 0.0f, 1.0f);
m_ObjPhysicsData.m_Scale.Set(1.0f, 1.0f, 1.0f);
if ( m_pDataComp ) //this cannot be freed by the system, it must be freed by the caller.
{
if ( m_bAllocGameData )
{
xlFree(m_pDataComp);
}
m_pDataComp = nullptr;
}
if ( m_pRenderComp )
{
//xlDelete m_pRenderComp;
m_pRenderComp = nullptr;
}
m_Logics.clear();
m_worldMtx.Identity();
m_bCollisionEnable = true;
}
uint16_t Object::Release()
{
assert(m_uRefCnt);
if ( m_uRefCnt )
m_uRefCnt--;
if ( m_uRefCnt <= 0 )
{
ObjectManager::FreeObject(this);
return 0;
}
return m_uRefCnt;
}
void Object::AddLogic(Logic *pLogic)
{
m_Logics.push_back( pLogic );
}
//initialize the object
void Object::Init()
{
for (Logic *logic : m_Logics)
{
logic->InitObject(this);
}
}
void Object::SendMessage(uint32_t uMsgID, float fValue)
{
for (Logic *logic : m_Logics)
{
logic->SendMessage(this, uMsgID, fValue);
}
}
void Object::Update()
{
if ( m_uFlags&OBJFLAGS_ACTIVE )
{
for (Logic *logic : m_Logics)
{
logic->Update(this);
}
}
}
void Object::ComputeTransformedBounds()
{
if ( m_pRenderComp == nullptr )
return;
Vector3 vMin, vMax;
m_pRenderComp->GetBounds(vMin, vMax);
Vector3 avCorners[8], avWorldCorners[8];
avCorners[0].Set(vMin.x, vMin.y, vMin.z);
avCorners[1].Set(vMax.x, vMin.y, vMin.z);
avCorners[2].Set(vMax.x, vMax.y, vMin.z);
avCorners[3].Set(vMin.x, vMax.y, vMin.z);
avCorners[4].Set(vMin.x, vMin.y, vMax.z);
avCorners[5].Set(vMax.x, vMin.y, vMax.z);
avCorners[6].Set(vMax.x, vMax.y, vMax.z);
avCorners[7].Set(vMin.x, vMax.y, vMax.z);
m_worldCen.Set(0,0,0);
for (int i=0; i<8; i++)
{
avWorldCorners[i] = m_worldMtx.TransformVector(avCorners[i]);
m_worldCen = m_worldCen + avWorldCorners[i];
}
m_worldCen = m_worldCen * (1.0f/8.0f);
float fMaxDist = 0.0f, fDist;
m_worldBounds[0].Set(FLT_MAX, FLT_MAX, FLT_MAX);
m_worldBounds[1] = -m_worldBounds[0];
for (int i=0; i<8; i++)
{
Vector3 vOffs = avWorldCorners[i] - m_worldCen;
fDist = vOffs.Dot(vOffs);
if ( fDist > fMaxDist )
fMaxDist = fDist;
if ( avWorldCorners[i].x < m_worldBounds[0].x ) m_worldBounds[0].x = avWorldCorners[i].x;
if ( avWorldCorners[i].y < m_worldBounds[0].y ) m_worldBounds[0].y = avWorldCorners[i].y;
if ( avWorldCorners[i].z < m_worldBounds[0].z ) m_worldBounds[0].z = avWorldCorners[i].z;
if ( avWorldCorners[i].x > m_worldBounds[1].x ) m_worldBounds[1].x = avWorldCorners[i].x;
if ( avWorldCorners[i].y > m_worldBounds[1].y ) m_worldBounds[1].y = avWorldCorners[i].y;
if ( avWorldCorners[i].z > m_worldBounds[1].z ) m_worldBounds[1].z = avWorldCorners[i].z;
}
m_fRadius = sqrtf(fMaxDist)+0.01f;
}