Skip to content

Commit 9040d3c

Browse files
committed
WorldMapObject: Do not update tile position every frame, ensure tile position stays valid in Editor
Makes a few changes to `WorldMapObject`, revolving around making it not have to `update()` its tile position based on sector position every frame: * Overrides `set_pos()`, `move_to()` and `move()` from `MovingObject` to instead move the object to a floored tile position. This also ensures that positions set via scripting will continue to be floored as well. * Updates tile position based on sector position in `after_editor_set()` for undo/redo support and allowing for X and Y tile position to be set via the object option (applicable for the properties sidebar, not merged into master yet).
1 parent cf13392 commit 9040d3c

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/worldmap/worldmap_object.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,11 @@ void
5757
WorldMapObject::initialize()
5858
{
5959
// Set sector position from provided tile position
60-
set_pos(Vector(32.0f * m_col.m_bbox.get_left() +
60+
m_col.m_bbox.set_pos(Vector(32.0f * m_col.m_bbox.get_left() +
6161
(m_col.m_bbox.get_width() < 32.f ? (32.f - m_col.m_bbox.get_width()) / 2 : 0),
62-
32.0f * m_col.m_bbox.get_top() +
63-
(m_col.m_bbox.get_width() < 32.f ? (32.f - m_col.m_bbox.get_height()) / 2 : 0)));
64-
62+
32.0f * m_col.m_bbox.get_top() +
63+
(m_col.m_bbox.get_height() < 32.f ? (32.f - m_col.m_bbox.get_height()) / 2 : 0)));
6564
update_pos();
66-
update_hitbox();
6765
}
6866

6967
ObjectSettings
@@ -107,9 +105,15 @@ WorldMapObject::draw_normal(DrawingContext& context)
107105
}
108106

109107
void
110-
WorldMapObject::update(float)
108+
WorldMapObject::after_editor_set()
111109
{
112-
update_pos();
110+
MovingSprite::after_editor_set();
111+
112+
// Set sector position from provided tile position
113+
m_col.m_bbox.set_pos(Vector(32.0f * m_tile_x +
114+
(m_col.m_bbox.get_width() < 32.f ? (32.f - m_col.m_bbox.get_width()) / 2 : 0),
115+
32.0f * m_tile_y +
116+
(m_col.m_bbox.get_height() < 32.f ? (32.f - m_col.m_bbox.get_height()) / 2 : 0)));
113117
}
114118

115119
void
@@ -120,14 +124,33 @@ WorldMapObject::update_pos()
120124
}
121125

122126
void
123-
WorldMapObject::move_to(const Vector& pos)
127+
WorldMapObject::update_pos(const Vector& pos)
124128
{
125129
// Set sector position to the provided position, rounding it to be divisible by 32
126-
set_pos(Vector(32.0f * static_cast<float>(pos.x / 32) +
130+
m_col.m_bbox.set_pos(Vector(32.0f * static_cast<int>(pos.x / 32) +
127131
(m_col.m_bbox.get_width() < 32.f ? (32.f - m_col.m_bbox.get_width()) / 2 : 0),
128-
32.0f * static_cast<float>(pos.y / 32) +
129-
(m_col.m_bbox.get_width() < 32.f ? (32.f - m_col.m_bbox.get_height()) / 2 : 0)));
132+
32.0f * static_cast<int>(pos.y / 32) +
133+
(m_col.m_bbox.get_height() < 32.f ? (32.f - m_col.m_bbox.get_height()) / 2 : 0)));
130134
update_pos();
131135
}
132136

137+
void
138+
WorldMapObject::set_pos(const Vector& pos)
139+
{
140+
update_pos(pos);
141+
}
142+
143+
void
144+
WorldMapObject::move_to(const Vector& pos)
145+
{
146+
update_pos(pos);
147+
}
148+
149+
void
150+
WorldMapObject::move(const Vector& dist)
151+
{
152+
m_col.m_bbox.move(dist);
153+
update_pos(m_col.m_bbox.p1());
154+
}
155+
133156
} // namespace worldmap

src/worldmap/worldmap_object.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,23 @@ class WorldMapObject : public MovingSprite
4545
/** Draws the object, when on a worldmap. */
4646
virtual void draw_worldmap(DrawingContext& context);
4747

48-
void update(float) override;
49-
5048
virtual HitResponse collision(MovingObject& other, const CollisionHit& hit) override { return FORCE_MOVE; }
5149
virtual ObjectSettings get_settings() override;
52-
virtual void move_to(const Vector& pos) override;
50+
void after_editor_set() override;
51+
52+
void set_pos(const Vector& pos) override;
53+
void move_to(const Vector& pos) override;
54+
void move(const Vector& dist) override;
5355

5456
inline Vector get_tile_pos() const { return { m_tile_x, m_tile_y }; }
5557

5658
private:
5759
void initialize();
5860

5961
void draw_normal(DrawingContext& context);
62+
6063
void update_pos();
64+
void update_pos(const Vector& pos);
6165

6266
private:
6367
int m_tile_x;

0 commit comments

Comments
 (0)