Question: Update the AnimatedObject.h and AnimatedObject.cpp code based on the specification and base code provided below: Specification: This is where the majority of the changes have

Update the AnimatedObject.h and AnimatedObject.cpp code based on the specification and base code provided below:
Specification:
This is where the majority of the changes have happened. There are two new static const variables (float deltaT and Vector2D gravity) which are used for the basic physics we are implementing. We also have a const int jumpStrength private member set to 3 times the walkSpeed which is used to adjust the objects y velocity for the jump command.
The main changes are via three new protected methods.
bool doPhysics(const std::vector>& objects);
It is very challenging to decipher from which side an object is colliding with another. Therefore we must apply horizontal and vertical velocity separately.
Well start with horizontal velocity. Add this to the objects position. Then loop over every object and check for collisions using the collision method. If there is a collision, keep track of this because youll need to return true because, depending on the child object, something different will happen.
If the objects horizontal velocity is greater than 0, then you need to move it to the left, far enough to get out of the object. If the velocity is less than 0, then move the object to the right.
If the two objects colliding are a player and an enemy, you need to call the GUIs loseGame() method. The instructor created a boolean free function called collidedWithEnemy to check for this.
call the applyGravity() method.
Now that the object has been moved vertically, we need to loop over all the objects again looking for collision. IF there is collision and the velocity is less than 0, then the object is moving up. The objects vertical velocity needs to be set to 0 and the object needs to be moved down to get it out of collision.
If the object has a negative vertical velocity, then it is falling. Its velocity still needs to be set to 0, but it needs to be moved up to get out of colliding. If the object is in the jump state, it needs its state changed to the still state. We will also need to check for collisions between the player and enemy and call the GUIs loseGame() method if it happens.
void applyGravity();
You will modify the players y position by applying gravity to the player every turn.
Changes in position are the result of the object having velocity over some interval of time, described by the following equations (p=position, v=velocity, t=time interval, and p=change of position):
p =t*v
p= p+p
Changes in velocity are the result of the object accelerating over some interval of time (a=acceleration and v=change of velocity):
v =t*a
v = v +v
For this assignment, the game physics is very simple: acceleration, a, is a constant (gravity in AnimatedObject.h) and the interval of time, t, is also fixed (deltaT in AnimatedObject.h).
This method simply puts these calculations into code.
bool collision(const std::unique_ptr
#include
#include
#include "Object.h"
class AnimatedObject : public Object
{
public:
AnimatedObject()= delete;
AnimatedObject(std::string animationFile, Vector2D position, Type name, int speed, GUI& gui);
enum class Direction { left =-1, right =1};
enum class State { still, walk, jump, attack, crouch};
int getSpriteIndex() const final;
bool facingLeft() const final;
protected:
bool doPhysics(const std::vector>& objects);
void updateSprite();
const int walkSpeed{12};
const int jumpStrength{ walkSpeed *6};
Vector2D velocity{0,0};
Direction direction{ Direction::right };
State state{ State::still };
int currentSprite{0};
private:
static const float deltaT;//0.5f
static const Vector2D gravity;//{0,10}
int currentAnimation{0};
std::unordered_map> sprites;
void loadSprites(std::string animationFile);
void applyGravity();
bool collision(const std::unique_ptr
#include
#include "AnimatedObject.h"
#include "GUI.h"

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 Programming Questions!