Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Technology And Management Computers And Information Processing Systems For Business

Authors: Robert C. Goldstein

1st Edition

0471887374, 978-0471887379

More Books

Students also viewed these Databases questions

Question

=+21.16. For the density Cexp( -1x| "), - o Answered: 1 week ago

Answered: 1 week ago