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 VectorD gravity which are used for the basic physics we are implementing. We also have a const int jumpStrength private member set to 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 doPhysicsconst 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 then you need to move it to the left, far enough to get out of the object. If the velocity is less than 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 then the object is moving up The objects vertical velocity needs to be set to 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 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 pposition, vvelocity, ttime interval, and pchange of position:
p tv
p pp
Changes in velocity are the result of the object accelerating over some interval of time aacceleration and vchange of velocity:
v ta
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 collisionconst std::uniqueptr
#include
#include
#include "Object.h
class AnimatedObject : public Object
public:
AnimatedObject delete;
AnimatedObjectstd::string animationFile, VectorD position, Type name, int speed, GUI& gui;
enum class Direction left right ;
enum class State still, walk, jump, attack, crouch;
int getSpriteIndex const final;
bool facingLeft const final;
protected:
bool doPhysicsconst std::vector& objects;
void updateSprite;
const int walkSpeed;
const int jumpStrength walkSpeed ;
VectorD velocity;
Direction direction Direction::right ;
State state State::still ;
int currentSprite;
private:
static const float deltaT;f
static const VectorD gravity;
int currentAnimation;
std::unorderedmap sprites;
void loadSpritesstd::string animationFile;
void applyGravity;
bool collisionconst std::uniqueptr
#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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
