I have been working on a 2D platformer for some time now, and I've managed to lay out the basic framework pretty well. Right now I'm however
facing a problem. I'm trying to implement classes for things like animation, physics, input and AI. Due to their nature they need to be able to edit
the attributes of their "owner" (I don't know that proper word, but I mean the class that has instantiated it (it being physics for example)).
I know the editing can't be done directly, so I though about passing the pointer to the "owner" object to the update function in the target class, but I don't know where I can get this pointer from.
I also drew a rough illustration of the relevant class hierarchy to help explain my point (vertical arrows represent inheritance (or the "is a" -relationship) and horizontal arrows represent a "has a" -relationship (composition I believe it's called)).
My idea of the class hierarchy:
http://i.imgur.com/ytUZcnv.png?1
Also here's some relevant code (I've removed the irrelevant parts):
GameObject class (header):
class GameObject
{
public:
GameObject();
virtual ~GameObject();
virtual void Draw(sf::RenderWindow& window);
virtual void Update(sf::Time& time);
virtual void SetPosition(float x, float y);
virtual sf::Vector2f GetPosition() const;
virtual bool GetIsMoving() const {return isMoving;}
virtual void SetIsMoving(bool val) {isMoving = val;}
virtual bool IsAnimated() {return animated;}
virtual void SetAnimated(bool val) {animated = val;}
private:
sf::Sprite sprite;
sf::Texture texture;
bool isMoving;
bool animated;
protected:
Animation Anim;
Physics Phys;
};
Player class (header):
class Player : public GameObject
{
public:
Player();
~Player();
void Update(sf::Time& time);
void Draw(sf::RenderWindow& window);
float GetYVelocity() const;
void SetYVelocity(float yVelocity) {this->yVelocity = yVelocity;}
private:
float yVelocity;
int jumpHeight;
};
Physics class (header):
class Physics
{
public:
Physics();
~Physics();
void Update(sf::Time& time);
float GetGravity() {return gravity;}
void SetGravity(float gravity) {this->gravity = gravity;}
private:
float gravity;
protected:
};
Physics class (source):
void Physics::Update(sf::Time& time)
{
static bool isLoaded = false;
if (!isLoaded)
{
std::cout << "Physics loaded!" << std::endl;
isLoaded = true;
}
//Let's say I wanted to edit the player's yVelocity here, what would I do?
}
Physics::Update is called from Player::Update by Phys.Update(time).
I'd love to get this resolved as it has effectively halted all progress on the game, thank you.