-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaplayermetadata.cpp
127 lines (106 loc) · 2.43 KB
/
maplayermetadata.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
#include "maplayermetadata.h"
#include "imetadataobject.h"
#include "collisionobject.h"
#include "triggerobject.h"
#include "spawnerobject.h"
MapLayerMetadata::MapLayerMetadata()
: arObjects()
, iObjects(0)
, ptiTileSize(0, 0)
{
arObjects.Truncate();
cScene.SetParent(this);
}
MapLayerMetadata::~MapLayerMetadata()
{
this->Reset();
}
void MapLayerMetadata::Reset()
{
for (u32 i = 0; i < iObjects; i++)
{
Delete(arObjects[i]);
}
arObjects.Truncate();
iObjects = 0;
cScene.Reset();
}
void MapLayerMetadata::Initialize(Point2u tileSize, u32 count, const LayerObjectHeader *data, b2World *world)
{
ptiTileSize = tileSize;
iObjects = count;
for (u32 i = 0; i < iObjects; i++)
{
arObjects.Add(this->CreateObject(&data[i], world));
}
}
IMetadataObject *MapLayerMetadata::CreateObject(const LayerObjectHeader *entry, b2World *world)
{
const char *type = Str(entry->iTypeId);
IMetadataObject *obj = NULL;
bool physics = false;
if (STRCMP(type, "trigger") == 0)
{
obj = New(TriggerObject());
}
else if (STRCMP(type, "spawner") == 0)
{
obj = New(SpawnerObject());
}
else
{
obj = New(CollisionObject(world));
physics = true;
}
obj->SetNameId(entry->iNameId);
obj->SetTypeId(entry->iTypeId);
obj->LoadProperties(entry->iPropertiesId);
obj->SetPosition(entry->fPosX * ptiTileSize.x, entry->fPosY * ptiTileSize.y);
obj->SetWidth(entry->fWidth * ptiTileSize.x);
obj->SetHeight(entry->fHeight * ptiTileSize.y);
if (physics)
{
((CollisionObject*)obj)->CreateStaticBody(obj->GetX(), obj->GetY() + obj->GetHeight(), obj->GetWidth(), obj->GetHeight());
}
cScene.Add(obj);
return obj;
}
Point2i MapLayerMetadata::ViewAt(Point2i pos)
{
cScene.SetPosition(-pos.x / static_cast<f32>(pScreen->GetWidth()), -pos.y / static_cast<f32>(pScreen->GetHeight()));
return pos;
}
BOOL MapLayerMetadata::CheckHit(const Rect4f &area, CollisionDataArrayPtr col) const
{
BOOL result = FALSE;
for (u32 i = 0; i < iObjects; i++)
{
IMetadataObject *obj = arObjects.At(i);
Rect4f overlap;
if (obj->CheckHit(area, overlap))
{
col->Add();
CollisionData *data = &(*col)[col->Size() - 1];
data->obj = obj;
data->overlap = overlap;
result = TRUE;
}
}
return result;
}
void MapLayerMetadata::Update(f32 dt)
{
cScene.Update(dt);
}
void MapLayerMetadata::Render()
{
for (u32 i = 0; i < iObjects; i++)
{
IMetadataObject *obj = arObjects[i];
obj->Render();
}
}
MapLayerMetadata *MapLayerMetadata::AsMetadata()
{
return this;
}