00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "physicsObjectCollection.h"
00016
00017 #include "indent.h"
00018
00019
00020
00021
00022
00023
00024 PhysicsObjectCollection::
00025 PhysicsObjectCollection() {
00026 }
00027
00028
00029
00030
00031
00032
00033 PhysicsObjectCollection::
00034 PhysicsObjectCollection(const PhysicsObjectCollection ©) :
00035 _physics_objects(copy._physics_objects)
00036 {
00037 }
00038
00039
00040
00041
00042
00043
00044 void PhysicsObjectCollection::
00045 operator = (const PhysicsObjectCollection ©) {
00046 _physics_objects = copy._physics_objects;
00047 }
00048
00049
00050
00051
00052
00053
00054 void PhysicsObjectCollection::
00055 add_physics_object(PT(PhysicsObject) physics_object) {
00056
00057
00058
00059
00060
00061 if (_physics_objects.get_ref_count() > 1) {
00062 PhysicsObjects old_physics_objects = _physics_objects;
00063 _physics_objects = PhysicsObjects::empty_array(0);
00064 _physics_objects.v() = old_physics_objects.v();
00065 }
00066
00067 _physics_objects.push_back(physics_object);
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077 bool PhysicsObjectCollection::
00078 remove_physics_object(PT(PhysicsObject) physics_object) {
00079 int object_index = -1;
00080 for (int i = 0; object_index == -1 && i < (int)_physics_objects.size(); i++) {
00081 if (_physics_objects[i] == physics_object) {
00082 object_index = i;
00083 }
00084 }
00085
00086 if (object_index == -1) {
00087
00088 return false;
00089 }
00090
00091
00092
00093
00094
00095
00096 if (_physics_objects.get_ref_count() > 1) {
00097 PhysicsObjects old_physics_objects = _physics_objects;
00098 _physics_objects = PhysicsObjects::empty_array(0);
00099 _physics_objects.v() = old_physics_objects.v();
00100 }
00101
00102 _physics_objects.erase(_physics_objects.begin() + object_index);
00103 return true;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 void PhysicsObjectCollection::
00116 add_physics_objects_from(const PhysicsObjectCollection &other) {
00117 int other_num_physics_objects = other.get_num_physics_objects();
00118 for (int i = 0; i < other_num_physics_objects; i++) {
00119 add_physics_object(other.get_physics_object(i));
00120 }
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130 void PhysicsObjectCollection::
00131 remove_physics_objects_from(const PhysicsObjectCollection &other) {
00132 PhysicsObjects new_physics_objects;
00133 int num_physics_objects = get_num_physics_objects();
00134 for (int i = 0; i < num_physics_objects; i++) {
00135 PT(PhysicsObject) physics_object = get_physics_object(i);
00136 if (!other.has_physics_object(physics_object)) {
00137 new_physics_objects.push_back(physics_object);
00138 }
00139 }
00140 _physics_objects = new_physics_objects;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 void PhysicsObjectCollection::
00152 remove_duplicate_physics_objects() {
00153 PhysicsObjects new_physics_objects;
00154
00155 int num_physics_objects = get_num_physics_objects();
00156 for (int i = 0; i < num_physics_objects; i++) {
00157 PT(PhysicsObject) physics_object = get_physics_object(i);
00158 bool duplicated = false;
00159
00160 for (int j = 0; j < i && !duplicated; j++) {
00161 duplicated = (physics_object == get_physics_object(j));
00162 }
00163
00164 if (!duplicated) {
00165 new_physics_objects.push_back(physics_object);
00166 }
00167 }
00168
00169 _physics_objects = new_physics_objects;
00170 }
00171
00172
00173
00174
00175
00176
00177
00178 bool PhysicsObjectCollection::
00179 has_physics_object(PT(PhysicsObject) physics_object) const {
00180 for (int i = 0; i < get_num_physics_objects(); i++) {
00181 if (physics_object == get_physics_object(i)) {
00182 return true;
00183 }
00184 }
00185 return false;
00186 }
00187
00188
00189
00190
00191
00192
00193 void PhysicsObjectCollection::
00194 clear() {
00195 _physics_objects.clear();
00196 }
00197
00198
00199
00200
00201
00202
00203
00204 bool PhysicsObjectCollection::
00205 is_empty() const {
00206 return _physics_objects.empty();
00207 }
00208
00209
00210
00211
00212
00213
00214 int PhysicsObjectCollection::
00215 get_num_physics_objects() const {
00216 return _physics_objects.size();
00217 }
00218
00219
00220
00221
00222
00223
00224 PT(PhysicsObject) PhysicsObjectCollection::
00225 get_physics_object(int index) const {
00226 nassertr(index >= 0 && index < (int)_physics_objects.size(), PT(PhysicsObject)());
00227
00228 return _physics_objects[index];
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238 PT(PhysicsObject) PhysicsObjectCollection::
00239 operator [] (int index) const {
00240 nassertr(index >= 0 && index < (int)_physics_objects.size(), PT(PhysicsObject)());
00241
00242 return _physics_objects[index];
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252 int PhysicsObjectCollection::
00253 size() const {
00254 return _physics_objects.size();
00255 }
00256
00257
00258
00259
00260
00261
00262
00263 void PhysicsObjectCollection::
00264 output(ostream &out) const {
00265 if (get_num_physics_objects() == 1) {
00266 out << "1 PhysicsObject";
00267 } else {
00268 out << get_num_physics_objects() << " PhysicsObjects";
00269 }
00270 }
00271
00272
00273
00274
00275
00276
00277
00278 void PhysicsObjectCollection::
00279 write(ostream &out, int indent_level) const {
00280 for (int i = 0; i < get_num_physics_objects(); i++) {
00281 indent(out, indent_level) << get_physics_object(i) << "\n";
00282 }
00283 }
00284