Question: Add this functionality to my code: Enemies fight back ( spearKnight ) : they chase the player ( Simon ) and can damage or kill

Add this functionality to my code: Enemies fight back(spearKnight): they chase the player(Simon) and can damage or kill him
Simon.h
#ifndef Samus_H
#define Samus_H
#include
#include
#include "AnimatedObject.h"
class Simon: public AnimatedObject
{
public:
Simon()= delete;
Simon(std::string animationFile, Vector2D position, int speed, GUI& gui);
void update(const std::vector>& objects) override;
Vector2D getPosition() const override;
private:
//Helper functions
void moveLeft();
void moveRight();
void moveDown();
void moveJump();
void moveAttack();
void noAction();
void walk();
int calculateTranslation(const std::vector>& objects);
void sideScrolling(const std::vector>& objects);
void attackXAdjustment();
const int scrollColumn{6};
int xAdjustment{0}; //used to adjust the x position of the samus sprite when attacking
bool jumpReleased{ true };
bool canJump{ true };
};
#endif //!Samus_H
AnimatedObject.h
#ifndef ANIMATEDOBJECT_H
#define ANIMATEDOBJECT_H
#include
#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 "Object.h"
class Block:public Object
{
public:
enum class Type
{
none,
ground_block,
water,
below_ground_1,
wall_1,
below_ground_2,
wall_2,
stump,
dirt,
evergreen,
small_tree,
big_tree,
mountain_1,
mountain_2,
sky,
blank
};
Block()= delete;
Block(Vector2D position, Type name, GUI& gui);
void update(const std::vector>& objects) override {}
int getSpriteIndex() const override;
bool collideable() const override;
private:
Type name{ Type::ground_block};
};
#endif //!BLOCK_H
SpearKnight.h
#ifndef SPEARKNIGHT_H
#define SPEARKNIGHT_H
#include "AnimatedObject.h"
#include "Vector2D.h"
class SpearKnight : public AnimatedObject, public Position {
public:
SpearKnight()= delete;
SpearKnight(std::string animationFile, Vector2D position, int speed, GUI& gui);
void update(const std::vector>& objects) override;
private:
Vector2D playerPosition;
};
#endif // SPEARKNIGHT_H
Block.h
#ifndef BLOCK_H
#define BLOCK_H
#include
#include "Object.h"
class Block:public Object
{
public:
enum class Type
{
none,
ground_block,
water,
below_ground_1,
wall_1,
below_ground_2,
wall_2,
stump,
dirt,
evergreen,
small_tree,
big_tree,
mountain_1,
mountain_2,
sky,
blank
};
Block()= delete;
Block(Vector2D position, Type name, GUI& gui);
void update(const std::vector>& objects) override {}
int getSpriteIndex() const override;
bool collideable() const override;
private:
Type name{ Type::ground_block};
};
#endif //!BLOCK_H
SpearKnight.cpp
#include "SpearKnight.h"
#include "GUI.h"
SpearKnight::SpearKnight(std::string animationFile, Vector2D position, int speed, GUI& gui)
: AnimatedObject(animationFile, position, Object::Type::enemy, speed, gui)
{
state = State::walk;
direction = Direction::left;
velocity.x = walkSpeed;
updateSprite();
}
void SpearKnight::update(const std::vector>& objects)
{
if (doPhysics(objects))
{
if(direction == Direction::left)
direction=Direction::right;
else
direction = Direction::left;
velocity.x =-velocity.x;
}
updateSprite();
}
Add this functionality to my code: Enemies fight

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!