-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroupManager.cpp
184 lines (148 loc) · 4.58 KB
/
GroupManager.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//------------------------------------------------------------------------------
// File: GroupManager.cpp
// Desc: Looks after object groups
//
// Created: 27 September 2005 17:42:05
//
// (c)2005 Neil Wakefield, Lionhead Studios Ltd
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Included files:
//------------------------------------------------------------------------------
#include "GroupManager.h"
#include "ObjectGroupStraight.h"
#include "ObjectGroupSine.h"
#include "ObjectGroupCircle.h"
#include "ObjectManager.h"
#include <new>
#include <cassert>
//------------------------------------------------------------------------------
// Statics:
//------------------------------------------------------------------------------
GGroupManager::GroupList GGroupManager::Groups;
GGroupManager::GroupList GGroupManager::DeletedGroups;
bool GGroupManager::Initialised = false;
//------------------------------------------------------------------------------
// Definitions:
//------------------------------------------------------------------------------
bool GGroupManager::Initialise()
{
assert( ! Initialised );
Initialised = true;
return true;
}
//------------------------------------------------------------------------------------------------------------
bool GGroupManager::Shutdown()
{
assert( Initialised );
//Delete all groups
for( GroupList::iterator iter = Groups.begin(); iter != Groups.end(); ++iter )
{
ObjectGroup *group = *iter;
delete group;
}
Groups.clear();
for( GroupList::iterator iter = DeletedGroups.begin(); iter != DeletedGroups.end(); ++iter )
{
ObjectGroup *group = *iter;
delete group;
}
DeletedGroups.clear();
Initialised = false;
return true;
}
//------------------------------------------------------------------------------------------------------------
bool GGroupManager::Process()
{
assert( Initialised );
bool retval = true;
//Empty the deadlist
for( GroupList::iterator iter = DeletedGroups.begin(); iter != DeletedGroups.end(); ++iter )
{
ObjectGroup *group = *iter;
delete group;
}
DeletedGroups.clear();
//Process the groups
GroupList::iterator iter2 = Groups.begin();
while( iter2 != Groups.end() )
{
ObjectGroup *group = *iter2;
if( ! group->Process() )
{
retval = false;
}
if( group->GetNumObjects() == 0 )
{
//Group is empty - delete it!
DeletedGroups.push_back( group );
iter2 = Groups.erase( iter2 );
group->Deleted = true;
continue;
}
++iter2;
}
return retval;
}
//------------------------------------------------------------------------------------------------------------
ObjectGroup* GGroupManager::CreateGroup( GROUP_TYPE group_type, D3DXVECTOR2 pos, D3DXVECTOR2 direction, unsigned int object_count, OBJECT_TYPE object_type, unsigned int object_number, unsigned int player_number )
{
assert( Initialised );
//Create the group
ObjectGroup *object_group = NULL;
switch( group_type )
{
case GROUP_TYPE_STRAIGHT:
object_group = new(std::nothrow) ObjectGroupStraight;
break;
case GROUP_TYPE_SINE:
object_group = new(std::nothrow) ObjectGroupSine;
break;
case GROUP_TYPE_CIRCLE:
object_group = new(std::nothrow) ObjectGroupCircle;
break;
default:
OutputDebugStringA( "PT_ERROR: Unknown group type\n" );
return NULL;
};
if( ! object_group )
{
OutputDebugStringA( "PT_ERROR: Out of memory\n" );
return NULL;
}
//Set the parameters
object_group->Position = pos;
object_group->Direction = direction;
//Work out the orientation
float orientation = 0.0f;
if( direction.y < 0.0f )
{
orientation = D3DX_PI;
}
//Create the objects
for( unsigned int i = 0; i < object_count; ++i )
{
Object *object = GObjectManager::CreateObject( object_type, object_number, player_number, pos, orientation );
if( object )
{
object_group->AddObject( object );
}
}
//Initialise the objects
object_group->InitialiseObjectMovement();
//Store and return
Groups.push_back( object_group );
return object_group;
}
//------------------------------------------------------------------------------------------------------------
bool GGroupManager::DeleteAllGroups()
{
assert( Initialised );
for( GroupList::iterator iter = Groups.begin(); iter != Groups.end(); ++iter )
{
ObjectGroup *group = *iter;
DeletedGroups.push_back( group );
}
Groups.clear();
return true;
}