Skip to content

Commit 809ebb8

Browse files
authored
Fix various issues with sectors without any solid tilemap (#2228)
1 parent 81809dd commit 809ebb8

File tree

5 files changed

+47
-30
lines changed

5 files changed

+47
-30
lines changed

src/editor/layers_widget.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ EditorLayersWidget::refresh()
312312
m_layer_icons.clear();
313313

314314
bool tsel = false;
315+
TileMap* first_tm = nullptr;
315316
for (auto& i : m_editor.get_sector()->get_objects())
316317
{
317318
auto* go = i.get();
@@ -323,6 +324,8 @@ EditorLayersWidget::refresh()
323324

324325
auto tm = dynamic_cast<TileMap*>(go);
325326
if (tm) {
327+
if (first_tm == nullptr)
328+
first_tm = tm;
326329
if ( !tm->is_solid() || tsel ) {
327330
tm->m_editor_active = false;
328331
} else {
@@ -333,6 +336,11 @@ EditorLayersWidget::refresh()
333336
}
334337
}
335338
}
339+
if (!tsel && first_tm != nullptr)
340+
{
341+
first_tm->m_editor_active = true;
342+
m_selected_tilemap = first_tm;
343+
}
336344

337345
sort_layers();
338346
refresh_sector_text();

src/supertux/game_object_manager.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ GameObjectManager::GameObjectManager() :
2828
m_gameobjects(),
2929
m_gameobjects_new(),
3030
m_solid_tilemaps(),
31+
m_all_tilemaps(),
3132
m_objects_by_name(),
3233
m_objects_by_uid(),
3334
m_objects_by_type_index(),
@@ -198,18 +199,7 @@ GameObjectManager::flush_game_objects()
198199
}
199200
}
200201
}
201-
update_solids();
202-
}
203-
204-
void
205-
GameObjectManager::update_solids()
206-
{
207-
m_solid_tilemaps.clear();
208-
for (auto tilemap : get_objects_by_type_index(typeid(TileMap)))
209-
{
210-
TileMap* tm = static_cast<TileMap*>(tilemap);
211-
if (tm->is_solid()) m_solid_tilemaps.push_back(tm);
212-
}
202+
update_tilemaps();
213203
}
214204

215205
void
@@ -222,6 +212,20 @@ GameObjectManager::update_solid(TileMap* tm) {
222212
m_solid_tilemaps.erase(it);
223213
}
224214
}
215+
216+
void
217+
GameObjectManager::update_tilemaps()
218+
{
219+
m_solid_tilemaps.clear();
220+
m_all_tilemaps.clear();
221+
for (auto tilemap : get_objects_by_type_index(typeid(TileMap)))
222+
{
223+
TileMap* tm = static_cast<TileMap*>(tilemap);
224+
if (tm->is_solid()) m_solid_tilemaps.push_back(tm);
225+
m_all_tilemaps.push_back(tm);
226+
}
227+
}
228+
225229
void
226230
GameObjectManager::this_before_object_add(GameObject& object)
227231
{
@@ -270,8 +274,8 @@ float
270274
GameObjectManager::get_width() const
271275
{
272276
float width = 0;
273-
for (auto& solids: get_solid_tilemaps()) {
274-
width = std::max(width, solids->get_bbox().get_right());
277+
for (auto& tilemap: get_all_tilemaps()) {
278+
width = std::max(width, tilemap->get_bbox().get_right());
275279
}
276280

277281
return width;
@@ -281,8 +285,8 @@ float
281285
GameObjectManager::get_height() const
282286
{
283287
float height = 0;
284-
for (const auto& solids: get_solid_tilemaps()) {
285-
height = std::max(height, solids->get_bbox().get_bottom());
288+
for (const auto& tilemap: get_all_tilemaps()) {
289+
height = std::max(height, tilemap->get_bbox().get_bottom());
286290
}
287291

288292
return height;
@@ -292,9 +296,9 @@ float
292296
GameObjectManager::get_tiles_width() const
293297
{
294298
float width = 0;
295-
for (const auto& solids : get_solid_tilemaps()) {
296-
if (static_cast<float>(solids->get_width()) > width)
297-
width = static_cast<float>(solids->get_width());
299+
for (const auto& tilemap : get_all_tilemaps()) {
300+
if (static_cast<float>(tilemap->get_width()) > width)
301+
width = static_cast<float>(tilemap->get_width());
298302
}
299303
return width;
300304
}
@@ -303,9 +307,9 @@ float
303307
GameObjectManager::get_tiles_height() const
304308
{
305309
float height = 0;
306-
for (const auto& solids : get_solid_tilemaps()) {
307-
if (static_cast<float>(solids->get_height()) > height)
308-
height = static_cast<float>(solids->get_height());
310+
for (const auto& tilemap : get_all_tilemaps()) {
311+
if (static_cast<float>(tilemap->get_height()) > height)
312+
height = static_cast<float>(tilemap->get_height());
309313
}
310314
return height;
311315
}

src/supertux/game_object_manager.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,13 @@ class GameObjectManager
178178
}
179179

180180
const std::vector<TileMap*>& get_solid_tilemaps() const { return m_solid_tilemaps; }
181+
const std::vector<TileMap*>& get_all_tilemaps() const { return m_all_tilemaps; }
181182

182-
void update_solids();
183183
void update_solid(TileMap* solid);
184184

185185
protected:
186+
void update_tilemaps();
187+
186188
void process_resolve_requests();
187189

188190
/** Same as process_resolve_requests(), but those it can't find will be kept in the buffer */
@@ -214,6 +216,9 @@ class GameObjectManager
214216
/** Fast access to solid tilemaps */
215217
std::vector<TileMap*> m_solid_tilemaps;
216218

219+
/** Fast access to all tilemaps */
220+
std::vector<TileMap*> m_all_tilemaps;
221+
217222
std::unordered_map<std::string, GameObject*> m_objects_by_name;
218223
std::unordered_map<UID, GameObject*> m_objects_by_uid;
219224
std::unordered_map<std::type_index, std::vector<GameObject*> > m_objects_by_type_index;

src/supertux/sector.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,8 @@ Sector::can_see_player(const Vector& eye) const
538538
bool
539539
Sector::inside(const Rectf& rect) const
540540
{
541-
for (const auto& solids : get_solid_tilemaps()) {
542-
Rectf bbox = solids->get_bbox();
541+
for (const auto& tilemap : get_all_tilemaps()) {
542+
Rectf bbox = tilemap->get_bbox();
543543

544544
// the top of the sector extends to infinity
545545
if (bbox.get_left() <= rect.get_left() &&
@@ -554,14 +554,14 @@ Sector::inside(const Rectf& rect) const
554554
Size
555555
Sector::get_editor_size() const
556556
{
557-
// Find the solid tilemap with the greatest surface
557+
// Find the tilemap with the greatest surface
558558
size_t max_surface = 0;
559559
Size size;
560-
for (const auto& solids: get_solid_tilemaps()) {
561-
size_t surface = solids->get_width() * solids->get_height();
560+
for (const auto& tilemap : get_all_tilemaps()) {
561+
size_t surface = tilemap->get_width() * tilemap->get_height();
562562
if (surface > max_surface) {
563563
max_surface = surface;
564-
size = solids->get_size();
564+
size = tilemap->get_size();
565565
}
566566
}
567567

src/worldmap/worldmap_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ WorldMapParser::load_worldmap(const std::string& filename)
146146
m_worldmap.flush_game_objects();
147147

148148
if (m_worldmap.get_solid_tilemaps().empty())
149-
throw std::runtime_error("No solid tilemap specified");
149+
log_warning << "No solid tilemap specified" << std::endl;
150150

151151
m_worldmap.move_to_spawnpoint("main");
152152

0 commit comments

Comments
 (0)