@@ -35,7 +35,7 @@ const float POSITION_FIX_AY = 50; // y-wise acceleration applied to player when
35
35
36
36
Climbable::Climbable (const ReaderMapping& reader) :
37
37
climbed_by(),
38
- activate_try_timer (),
38
+ trying_to_climb (),
39
39
message(),
40
40
new_size(0 .0f , 0 .0f )
41
41
{
@@ -52,7 +52,7 @@ Climbable::Climbable(const ReaderMapping& reader) :
52
52
53
53
Climbable::Climbable (const Rectf& area) :
54
54
climbed_by(),
55
- activate_try_timer (),
55
+ trying_to_climb (),
56
56
message(),
57
57
new_size(0 .0f , 0 .0f )
58
58
{
@@ -65,6 +65,7 @@ Climbable::~Climbable()
65
65
player->stop_climbing (*this );
66
66
67
67
climbed_by.clear ();
68
+ trying_to_climb.clear ();
68
69
}
69
70
70
71
ObjectSettings
@@ -91,11 +92,40 @@ Climbable::after_editor_set() {
91
92
}
92
93
93
94
void
94
- Climbable::update (float /* dt_sec*/ )
95
+ Climbable::update (float dt_sec)
95
96
{
96
- for (auto * player : climbed_by)
97
- if (!may_climb (*player))
98
- player->stop_climbing (*this );
97
+ TriggerBase::update (dt_sec);
98
+ auto it = climbed_by.begin ();
99
+ while (it != climbed_by.end ())
100
+ {
101
+ if (!may_climb (**it))
102
+ {
103
+ (*it)->stop_climbing (*this );
104
+ it = climbed_by.erase (it);
105
+ continue ;
106
+ }
107
+ it++;
108
+ }
109
+ auto it2 = trying_to_climb.begin ();
110
+ while (it2 != trying_to_climb.end ())
111
+ {
112
+ if (it2->m_activate_try_timer ->started ())
113
+ {
114
+ // the "-20" to y velocity prevents Tux from walking in place on the ground for horizonal adjustments
115
+ if (it2->m_player ->get_bbox ().get_left () < m_col.m_bbox .get_left () - GRACE_DX) it2->m_player ->add_velocity (Vector (POSITION_FIX_AX,-20 ));
116
+ if (it2->m_player ->get_bbox ().get_right () > m_col.m_bbox .get_right () + GRACE_DX) it2->m_player ->add_velocity (Vector (-POSITION_FIX_AX,-20 ));
117
+ if (it2->m_player ->get_bbox ().get_top () < m_col.m_bbox .get_top () - GRACE_DY) it2->m_player ->add_velocity (Vector (0 ,POSITION_FIX_AY));
118
+ if (it2->m_player ->get_bbox ().get_bottom () > m_col.m_bbox .get_bottom () + GRACE_DY) it2->m_player ->add_velocity (Vector (0 ,-POSITION_FIX_AY));
119
+ }
120
+ if (may_climb (*(it2->m_player )))
121
+ {
122
+ climbed_by.push_back (it2->m_player );
123
+ it2->m_player ->start_climbing (*this );
124
+ it2 = trying_to_climb.erase (it2);
125
+ continue ;
126
+ }
127
+ it2++;
128
+ }
99
129
}
100
130
101
131
void
@@ -118,20 +148,19 @@ Climbable::draw(DrawingContext& context)
118
148
void
119
149
Climbable::event (Player& player, EventType type)
120
150
{
121
- if (( type == EVENT_ACTIVATE) || (activate_try_timer. started ()) ) {
151
+ if (type == EVENT_ACTIVATE) {
122
152
if (player.get_grabbed_object () == nullptr ){
123
- if (may_climb (player)) {
124
- climbed_by.push_back (&player);
125
- player.start_climbing (*this );
126
- activate_try_timer.stop ();
127
- } else {
128
- if (type == EVENT_ACTIVATE) activate_try_timer.start (ACTIVATE_TRY_FOR);
129
- // the "-13" to y velocity prevents Tux from walking in place on the ground for horizonal adjustments
130
- if (player.get_bbox ().get_left () < m_col.m_bbox .get_left () - GRACE_DX) player.add_velocity (Vector (POSITION_FIX_AX,-13 ));
131
- if (player.get_bbox ().get_right () > m_col.m_bbox .get_right () + GRACE_DX) player.add_velocity (Vector (-POSITION_FIX_AX,-13 ));
132
- if (player.get_bbox ().get_top () < m_col.m_bbox .get_top () - GRACE_DY) player.add_velocity (Vector (0 ,POSITION_FIX_AY));
133
- if (player.get_bbox ().get_bottom () > m_col.m_bbox .get_bottom () + GRACE_DY) player.add_velocity (Vector (0 ,-POSITION_FIX_AY));
153
+ auto it = std::find_if (trying_to_climb.begin (), trying_to_climb.end (),
154
+ [&player](ClimbPlayer& element)
155
+ {
156
+ return element.m_player == &player;
157
+ });
158
+ if (it == trying_to_climb.end ()) {
159
+ trying_to_climb.push_back (ClimbPlayer{&player, std::make_unique<Timer>()});
160
+ it = trying_to_climb.begin () + (trying_to_climb.size () - 1 );
134
161
}
162
+ if (!may_climb (player))
163
+ it->m_activate_try_timer ->start (ACTIVATE_TRY_FOR);
135
164
}
136
165
}
137
166
@@ -145,6 +174,14 @@ Climbable::event(Player& player, EventType type)
145
174
else
146
175
it++;
147
176
}
177
+ auto it2 = trying_to_climb.begin ();
178
+ while (it2 != trying_to_climb.end ())
179
+ {
180
+ if (it2->m_player == &player)
181
+ it2 = trying_to_climb.erase (it2);
182
+ else
183
+ it2++;
184
+ }
148
185
}
149
186
}
150
187
0 commit comments