Remove a component at a given index
| 106 | |
| 107 | // Remove a component at a given index |
| 108 | void Components::removeComponent(Entity entity) { |
| 109 | |
| 110 | assert(mMapEntityToComponentIndex.containsKey(entity)); |
| 111 | |
| 112 | uint32 index = mMapEntityToComponentIndex[entity]; |
| 113 | |
| 114 | assert(index < mNbComponents); |
| 115 | |
| 116 | // We want to keep the arrays tightly packed. Therefore, when a component is removed, |
| 117 | // we replace it with the last element of the array. But we need to make sure that enabled |
| 118 | // and disabled components stay grouped together. |
| 119 | |
| 120 | // Destroy the component |
| 121 | destroyComponent(index); |
| 122 | |
| 123 | // If the component to remove is disabled |
| 124 | if (index >= mDisabledStartIndex) { |
| 125 | |
| 126 | // If the component is not the last one |
| 127 | if (index != mNbComponents - 1) { |
| 128 | |
| 129 | // We replace it by the last disabled component |
| 130 | moveComponentToIndex(mNbComponents - 1, index); |
| 131 | } |
| 132 | } |
| 133 | else { // If the component to remove is enabled |
| 134 | |
| 135 | // If it not the last enabled component |
| 136 | if (index != mDisabledStartIndex - 1) { |
| 137 | |
| 138 | // We replace it by the last enabled component |
| 139 | moveComponentToIndex(mDisabledStartIndex - 1, index); |
| 140 | } |
| 141 | |
| 142 | // If there are disabled components at the end |
| 143 | if (mDisabledStartIndex != mNbComponents) { |
| 144 | |
| 145 | // We replace the last enabled component by the last disabled component |
| 146 | moveComponentToIndex(mNbComponents - 1, mDisabledStartIndex - 1); |
| 147 | } |
| 148 | |
| 149 | mDisabledStartIndex--; |
| 150 | } |
| 151 | |
| 152 | mNbComponents--; |
| 153 | |
| 154 | assert(mDisabledStartIndex <= mNbComponents); |
| 155 | assert(mNbComponents == static_cast<uint32>(mMapEntityToComponentIndex.size())); |
| 156 | } |
| 157 | |
| 158 | // Notify if a given entity is disabled (sleeping) or not |
| 159 | void Components::setIsEntityDisabled(Entity entity, bool isDisabled) { |
no test coverage detected