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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started