-
Notifications
You must be signed in to change notification settings - Fork 13
/
NameManager.as
244 lines (211 loc) · 8.97 KB
/
NameManager.as
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*******************************************************************************
* PushButton Engine
* Copyright (C) 2009 PushButton Labs, LLC
* For more information see http://www.pushbuttonengine.com
*
* This file is licensed under the terms of the MIT license, which is included
* in the License.html file at the root directory of this SDK.
******************************************************************************/
package com.pblabs.engine.core
{
import com.pblabs.engine.debug.Logger;
import com.pblabs.engine.entity.IEntity;
import com.pblabs.engine.entity.IEntityComponent;
import flash.utils.Dictionary;
/**
* The name manager stores references to PBObjects that have been given
* names. These PBObjects can be looked up by name.
*/
public class NameManager
{
/**
* The list of registered PBObjects.
*/
public function get objectList():Dictionary
{
return _objects;
}
/**
* Registers an entity under a specific name. If the name is in use, lookups will
* return the last entity added under the name.
*
* @param entity The entity to add.
* @param name The name to register the entity under.
*/
public function add(object:IPBObject):void
{
var isNameValid:Boolean = (object.name != null && object.name != "");
var isAliasValid:Boolean = (object.alias != null && object.alias != "");
if (isNameValid && _objects[object.name])
Logger.warn(this, "add", "A PBObject with the name " + object.name + " already exists. Future lookups by this name will return the newest object. Did you mean to make an entity a template?");
if(TemplateManager.VERBOSE_LOGGING)
Logger.print(this, "Registering PBObject '" + object.name + "', alias '" + object.alias + "'");
if(isNameValid)
_objects[object.name] = object;
if(isAliasValid)
_objects[object.alias] = object;
}
/**
* Removes an object from the manager.
*
* @param entity The IPBObject to remove.
*/
public function remove(object:IPBObject):void
{
// Unregister its alias, if it has one and the alias points to us.
if (object.alias != null && object.alias != "" && _objects[object.alias] == object)
{
_objects[object.alias] = null;
delete _objects[object.alias];
}
// And the normal name, if it is us.
if(object.name != null && object.name != "" && _objects[object.name] == object )
{
_objects[object.name] = null;
delete _objects[object.name];
}
unregisterAliases(object);
}
/**
* Register an alias for an IPBObject
* @param object The object to register the alias for
* @param alias The alias to register
**/
public function registerAlias(object:IPBObject, alias:String):void
{
if(alias == null || alias == "")
{
Logger.warn(this, "registerAlias", "Attempt made to register invalid alias '"+alias+"'");
return;
}
if(_objects[alias])
{
Logger.warn(this, "registerAlias","A PBObject with the name '" + alias + "' already exists ");
return;
}
_objects[alias] = object;
//Store the alias in the registeredAliases map
if(!_registeredAliases[object])
_registeredAliases[object] = [];
_registeredAliases[object].push(alias);
}
/**
* Unregister an alias for an IPBObject
* @param object The object to register the alias for.
* @param alias The alias to unregister.
**/
public function unregisterAlias(object:IPBObject, alias:String):void
{
// Check if the alias exists, points to the IPBObject and is an alias.
if(_objects[alias] && _objects[alias] == object
&& _registeredAliases[object] && _registeredAliases[object].indexOf(alias) > -1)
{
_objects[alias] = null;
delete _objects[alias];
_registeredAliases[object].splice(_registeredAliases[object].indexOf(alias), 1);
if(_registeredAliases[object].length < 1)
delete _registeredAliases[object];
}
else
{
Logger.warn(this, "unregisterAlias", "Attempt made to unregister alias '"+alias+"' but the object registered is not the same or the alias is not a registered alias.");
return;
}
}
/**
* Unregister all aliases for an IPBObject
* @param object The IPBObject to unregister.
**/
public function unregisterAliases(object:IPBObject):void{
if(!_registeredAliases[object])
return;
for each(var alias:String in _registeredAliases[object])
{
_objects[alias] = null;
delete _objects[alias];
}
delete _registeredAliases[object];
}
/**
* Looks up a PBObject with the specified name.
*
* @param name The name of the object to look up.
*
* @return The object with the specified name, or null if it wasn't found.
*/
public function lookup(name:String):*
{
return _objects[name];
}
/**
* Turns a potentially used name and returns a related unused name. The
* given name will have a number appended, with the number continually
* incremented until an unused name is found.
*
* @param name The name to validate.
*
* @return The validated name. This is guaranteed to not be in use.
*/
public function validateName(name:String):String
{
if (!_objects[name])
return name;
var index:int = 1;
var testName:String = name + index;
while (_objects[testName])
testName = name + index++;
return testName;
}
/**
* Looks up a component on an entity that has been registered. The same
* conditions apply as with the lookupComponentByType method on IEntity.
*
* @param The name of the entity on which the component exists.
* @param componentType The type of the component to lookup.
*
* @see com.pblabs.engine.entity.IEntity#lookupComponentByType()
*/
public function lookupComponentByType(name:String, componentType:Class):IEntityComponent
{
var entity:IEntity = lookup(name);
if (!entity)
return null;
return entity.lookupComponentByType(componentType);
}
/**
* Looks up components on an entity that has been registered. The same
* conditions apply as with the lookupComponentsByType method on IEntity.
*
* @param The name of the entity on which the component exists.
* @param componentType The type of the components to lookup.
*
* @see com.pblabs.engine.entity.IEntity#lookupComponentByType()
*/
public function lookupComponentsByType(name:String, componentType:Class):Array
{
var entity:IEntity = lookup(name);
if (!entity)
return null;
return entity.lookupComponentsByType(componentType);
}
/**
* Looks up a component on an entity that has been registered. The same
* conditions apply as with the lookupComponentByName method on IEntity.
*
* @param The name of the entity on which the component exists.
* @param componentName The name of the component to lookup.
*
* @see com.pblabs.engine.entity.IEntity#lookupComponentByName()
*/
public function lookupComponentByName(name:String, componentName:String):IEntityComponent
{
var entity:IEntity = lookup(name);
if (!entity)
return null;
return entity.lookupComponentByName(componentName);
}
private var _objects:Dictionary = new Dictionary();
//Map from PBObject -> String[] that contains all registered aliases
private var _registeredAliases:Dictionary = new Dictionary();
}
}