| 89 | } |
| 90 | |
| 91 | void TileObject::setPosition(Vec3<float> newPosition) |
| 92 | { |
| 93 | auto thisPtr = shared_from_this(); |
| 94 | if (!thisPtr) |
| 95 | { |
| 96 | LogError("This == null"); |
| 97 | } |
| 98 | if (newPosition.x < 0 || newPosition.y < 0 || newPosition.z < 0 || |
| 99 | newPosition.x > map.size.x + 1 || newPosition.y > map.size.y + 1 || |
| 100 | newPosition.z > map.size.z + 1) |
| 101 | { |
| 102 | LogWarning("Trying to place object at %s in map of size %s", newPosition, map.size); |
| 103 | newPosition.x = clamp(newPosition.x, 0.0f, (float)map.size.x + 1); |
| 104 | newPosition.y = clamp(newPosition.y, 0.0f, (float)map.size.y + 1); |
| 105 | newPosition.z = clamp(newPosition.z, 0.0f, (float)map.size.z + 1); |
| 106 | LogWarning("Clamped object to %s", newPosition); |
| 107 | } |
| 108 | this->removeFromMap(); |
| 109 | |
| 110 | this->owningTile = map.getTile(newPosition); |
| 111 | if (!this->owningTile) |
| 112 | { |
| 113 | LogError("Failed to get tile for position %s", newPosition); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | auto inserted = this->owningTile->ownedObjects.insert(thisPtr); |
| 118 | if (!inserted.second) |
| 119 | { |
| 120 | LogError("Object already in owned object list?"); |
| 121 | } |
| 122 | |
| 123 | Vec3<int> minBounds = {floorf(newPosition.x + getCenterOffset().x - this->bounds_div_2.x), |
| 124 | floorf(newPosition.y + getCenterOffset().y - this->bounds_div_2.y), |
| 125 | floorf(newPosition.z + getCenterOffset().z - this->bounds_div_2.z)}; |
| 126 | Vec3<int> maxBounds = {ceilf(newPosition.x + getCenterOffset().x + this->bounds_div_2.x), |
| 127 | ceilf(newPosition.y + getCenterOffset().y + this->bounds_div_2.y), |
| 128 | ceilf(newPosition.z + getCenterOffset().z + this->bounds_div_2.z)}; |
| 129 | |
| 130 | for (int x = minBounds.x; x < maxBounds.x; x++) |
| 131 | { |
| 132 | for (int y = minBounds.y; y < maxBounds.y; y++) |
| 133 | { |
| 134 | for (int z = minBounds.z; z < maxBounds.z; z++) |
| 135 | { |
| 136 | if (x < 0 || y < 0 || z < 0 || x >= map.size.x || y >= map.size.y || |
| 137 | z >= map.size.z) |
| 138 | { |
| 139 | // TODO: Decide if having bounds outside the map are really valid? |
| 140 | continue; |
| 141 | } |
| 142 | Tile *intersectingTile = map.getTile(x, y, z); |
| 143 | if (!intersectingTile) |
| 144 | { |
| 145 | LogError("Failed to get intersecting tile at {%d,%d,%d}", x, y, z); |
| 146 | continue; |
| 147 | } |
| 148 | this->intersectingTiles.push_back(intersectingTile); |
no test coverage detected