Swap two components in the array
| 145 | |
| 146 | // Swap two components in the array |
| 147 | void BodyComponents::swapComponents(uint32 index1, uint32 index2) { |
| 148 | |
| 149 | // Copy component 1 data |
| 150 | Entity entity1(mBodiesEntities[index1]); |
| 151 | Body* body1 = mBodies[index1]; |
| 152 | Array<Entity> colliders1(mColliders[index1]); |
| 153 | bool isActive1 = mIsActive[index1]; |
| 154 | void* userData1 = mUserData[index1]; |
| 155 | bool hasSimulationCollider = mHasSimulationCollider[index1]; |
| 156 | |
| 157 | // Destroy component 1 |
| 158 | destroyComponent(index1); |
| 159 | |
| 160 | moveComponentToIndex(index2, index1); |
| 161 | |
| 162 | // Reconstruct component 1 at component 2 location |
| 163 | new (mBodiesEntities + index2) Entity(entity1); |
| 164 | new (mColliders + index2) Array<Entity>(colliders1); |
| 165 | mBodies[index2] = body1; |
| 166 | mIsActive[index2] = isActive1; |
| 167 | mUserData[index2] = userData1; |
| 168 | mHasSimulationCollider[index2] = hasSimulationCollider; |
| 169 | |
| 170 | // Update the entity to component index mapping |
| 171 | mMapEntityToComponentIndex.add(Pair<Entity, uint32>(entity1, index2)); |
| 172 | |
| 173 | assert(mMapEntityToComponentIndex[mBodiesEntities[index1]] == index1); |
| 174 | assert(mMapEntityToComponentIndex[mBodiesEntities[index2]] == index2); |
| 175 | assert(mNbComponents == static_cast<uint32>(mMapEntityToComponentIndex.size())); |
| 176 | } |
| 177 | |
| 178 | // Destroy a component at a given index |
| 179 | void BodyComponents::destroyComponent(uint32 index) { |