I've been over this for a long time, I can't seem to understand what is wrong, is the use of the this keyword right? I want to add the instance of the class that is being created to the vector 'allEntities' in the constructor, or am I not using the iterators right? I'm very new to c++ and programming in general so all help is appreciated.
GameObject.h
class GameObject {
public:
GameObject(const char* texturesheet, SDL_Renderer* ren, int x, int y, int _maxHp, int _currentHp, int _strength);
~GameObject();
static std::vector<GameObject> allEntities;
void update();
void Render();
void movement(const char* coord, bool positive);
int getMaxHp();
void addMaxHp(int change);
int getCurrentHp();
void setCurrentHp(int change);
int getStrength();
void changeStrength(int change);
void attack(GameObject &other);
int getX() { return x; };
int getY() { return y; };
void setDirectionFacing(char direction) { directionFacing = direction; };
char getDirectionFacing() { return directionFacing; };
private:
int x;
int y;
int strength;
int maxHp;
int currentHp;
char directionFacing;
SDL_Texture* objectTexture;
SDL_Rect srcRect, destRect;
SDL_Renderer* renderer;
};
GameObject.cpp
GameObject::GameObject(const char* texturesheet, SDL_Renderer* ren, int xx, int yy, int _maxHp, int _currentHp, int _strength)
{
allEntities.push_back(*this);
renderer = ren;
objectTexture = textureCreator::loadTexture(texturesheet, ren);
GameObject::maxHp = _maxHp;
GameObject::currentHp = _currentHp;
changeStrength(_strength);
x = xx;
y = yy;
}
GameObject::~GameObject()
{
}
void GameObject::update()
{
srcRect.h = 48;
srcRect.w = 48;
srcRect.x = 0;
srcRect.y = 0;
destRect.x = x;
destRect.y = y;
destRect.w = srcRect.w * 1.5;
destRect.h = srcRect.h * 1.5;
}
void GameObject::movement(const char* coord, bool positive)
{
if (coord == "x" && positive == true)
{
y -= 4;
setDirectionFacing('w');
std::cout << 'w' << std::endl;
};
if (coord == "x" && positive == false)
{
y += 4;
setDirectionFacing('s');
std::cout << 's' << std::endl;
};
if (coord == "y" && positive == true)
{
x += 4;
setDirectionFacing('d');
std::cout << 'd' << std::endl;
};
if (coord == "y" && positive == false)
{
x -= 4;
setDirectionFacing('a');
std::cout << 'a' << std::endl;
};
}
void GameObject::Render()
{
SDL_RenderCopy(renderer, objectTexture, &srcRect, &destRect);
}
int GameObject::getCurrentHp() { return currentHp; };
int GameObject::getMaxHp() { return maxHp; };
void GameObject::setCurrentHp(int change) { currentHp += change; };
void GameObject::addMaxHp(int change) { maxHp += change; };
int GameObject::getStrength() { return strength; }
void GameObject::changeStrength(int change) { strength += change; }
void GameObject::attack(GameObject &other)
{
}
game.cpp
GameObject* player;
player = new GameObject("player.png", renderer, 10 , 10, 10, 10, 2);
for (std::vector<GameObject>::iterator it = GameObject::allEntities.begin(); it != GameObject::allEntities.end(); it++) {
int hp = it->getCurrentHp();
std::cout << hp << std::endl;
}
I expect to print out the hp value which is 10, but instead it prints 0 instead.
Also I need this vector so when the player attacks, it checks every entity and sees if it is in the area of the attack in a for loop, is there any better way to do this?