@@ -73,18 +73,7 @@ void IdleState::enter(const Input& input, Entity& entity)
7373void can_fall (const Input& input, Entity& entity)
7474{
7575 // Can fall if there is no platform below
76- bool should_fall = true ;
77-
78- for (auto edge = entity.physics ->body ->GetContactList (); edge; edge = edge->next ) {
79- auto normal = edge->contact ->GetManifold ()->localNormal ;
80- if (edge->contact ->GetFixtureA () == entity.physics ->body ->GetFixtureList ()) {
81- normal = -normal;
82- }
83- if (normal.y > 0 .5f ) {
84- should_fall = false ;
85- break ;
86- }
87- }
76+ bool should_fall = !any (entity.physics ->obstacle & DirectionFlags::DOWN);
8877
8978 if (should_fall) {
9079 CHAR_STT (entity).set_state (State::JUMP_DOWN, input, entity);
@@ -145,8 +134,11 @@ void can_move_on_x(const Input& input, Entity& entity, f32 x_factor)
145134{
146135 f32 x_velocity = entity.physics ->body ->GetLinearVelocity ().x ;
147136
137+ // Applying a force to move in the opposite direction of current velocity is always allowed
148138 bool opposite_move = (input.joystick .move .x < 0 && x_velocity >= 0 ) ||
149139 (input.joystick .move .x >= 0 && x_velocity < 0 );
140+
141+ // Make sure current velocity is within limits, otherwise do not apply further force
150142 bool within_limit =
151143 fabs (entity.physics ->body ->GetLinearVelocity ().x ) < entity.physics ->max_x_speed ;
152144
@@ -182,6 +174,8 @@ void JumpUpState::handle(const Input& input, Entity& entity)
182174
183175void JumpUpState::enter (const Input& input, Entity& entity)
184176{
177+ entity.physics ->body ->GetFixtureList ()->SetFriction (0 .0f );
178+
185179 entity.transform .node ->addChildNode (&CHAR_GFX (entity).jump_up );
186180
187181 auto force = b2Vec2 (
@@ -197,6 +191,7 @@ void JumpUpState::update(const f32 dt, const Input& input, Entity& entity)
197191
198192void JumpUpState::exit (Entity& entity)
199193{
194+ entity.physics ->body ->GetFixtureList ()->SetFriction (3 .0f );
200195 entity.transform .node ->removeChildNode (&CHAR_GFX (entity).jump_up );
201196}
202197
@@ -207,29 +202,38 @@ JumpDownState::JumpDownState()
207202
208203void JumpDownState::enter (const Input& input, Entity& entity)
209204{
205+ entity.physics ->body ->GetFixtureList ()->SetFriction (0 .0f );
210206 entity.transform .node ->addChildNode (&CHAR_GFX (entity).jump_down );
211207}
212208
213209void JumpDownState::handle (const Input& input, Entity& entity)
214210{
215- for (auto edge = entity.physics ->body ->GetContactList (); edge; edge = edge->next ) {
216- auto & normal = edge->contact ->GetManifold ()->localNormal ;
217- if (normal.y != 0.0 ) {
218- LOGI_X (" Normal (%f, %f)" , normal.x , normal.y );
219- CHAR_STT (entity).set_state (State::MOVE, input, entity);
220- break ;
221- }
211+ if (any (entity.physics ->obstacle & DirectionFlags::DOWN)
212+ ) {
213+ CHAR_STT (entity).set_state (State::MOVE, input, entity);
222214 }
223215}
224216
225217void JumpDownState::update (const f32 , const Input& input, Entity& entity)
226218{
227219 // Can move a bit
228220 can_move_on_x (input, entity, entity.physics ->jump_x_factor );
221+
222+ // Make sure it goes down
223+ if (entity.physics ->body ->GetLinearVelocity ().y > -1.0 ) {
224+ entity.physics ->body ->ApplyForceToCenter ({0.0 , -100 .0f }, true );
225+ }
229226}
230227
231228void JumpDownState::exit (Entity& entity)
232229{
230+ auto fixture = entity.physics ->body ->GetFixtureList ();
231+ fixture->SetFriction (3 .0f );
232+
233+ for (auto edge = entity.physics ->body ->GetContactList (); edge; edge = edge->next ) {
234+ edge->contact ->ResetFriction ();
235+ }
236+
233237 entity.transform .node ->removeChildNode (&CHAR_GFX (entity).jump_down );
234238}
235239
0 commit comments