Question: This class has a switch statement in the Update methods which implements a state machine. Refactor this code using the State pattern. void PlatformController::Update(float dt,

This class has a switch statement in the Update methods which implements a state machine. Refactor this code using the State pattern.

void PlatformController::Update(float dt, Collision& collision, Input& input) { MoveAndCollide(dt, collision); switch (curState) { case GROUND: sprite.setTexture(groundTexture); // check for input if (input.Left() || input.Right()) { // move to the side vel = sf::Vector2f(speed * (input.Right() - input.Left()), 0.0f);

} if (input.Jump()) { vel.y = -jumpSpeed; vel.x *= 0.5f; // reduce horizontal speed curState = AIR; } if (!groundColl) curState = AIR; break; case AIR: sprite.setTexture(airTexture); // air control if (!(wallCollLeft || wallCollRight) && (input.Left() || input.Right())) { vel += sf::Vector2f((input.Right() - input.Left()), 0.0f) * speed * dt; // clamp the sideways velocity to the max speed if (vel.x > speed) vel.x = speed; if (vel.x < -speed) vel.x = -speed; } // check for landing if (groundColl) curState = GROUND; break; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!