Jump to content
  • Advertisement
Sign in to follow this  
Tallkotten

Organizing of game classes!

This topic is 2262 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello!

I've been programming a game in C++/SDL for the last week and I've actually made a lot of progress. Anyways, I've got some problems that i need some help solving since this is my first ever attempt at making a game.

I've got a Game class which basically had access to everything and sort, draw and handles events.

Then i got a class called "Entity" which is a interface for everything that moves, the classes "player", "enemy" and "projectile" inheritances this interface.

I want both the player and enemy class to be able to create a projectile at any time. At the time only the class player can. In the projectile class i then need to check for collisions so that i know if the projectile hit something.

The problem is that if the player and enemy should be able to create a projectile i need to include that header file. Then if i want to check for collisions on the player and enemy i need to include those files. This in turn creates a loop where i in player.h includes projectile.h and i projectile.h i include player.h

I hope i make myself clear.

Greatly appreciates help!

I'm going to go ahead and post a file of how my program is built atm. You can open the program in Dia (http://dia-installer.de/)

Share this post


Link to post
Share on other sites
Advertisement
You can declare a class without including its headers, just ensure to refer it, don't use any constructors or methods of it:

File A.h:
[source lang="cpp"]
// declare B
class B;

class A {
B* myClassBRef;
}
[/source]

File B.h:
[source lang="cpp"]
// declare A
class A;

class B {
A* myClassARef;
}
[/source]

File A.cpp
[source lang="cpp"]
#include "A.h"
#include "B.h" // include B now to access constructor and methods

void A::myMethod() {
this->myClassBRef = new B();
}
[/source] Edited by Ashaman73

Share this post


Link to post
Share on other sites
I see. So i should just declare the class and then include it's code in the .cpp file instead of the header one.

Going to try that out asap! Thank you.

I've actually got another question if that's ok :)

The enemies needs to check for collisions on the environment and player.
The player needs to check for collisions on the environment, enemies.
The projectile needs to check for collisions on the environment, enemies and player.

In my entity class I've got a functions that looks like this:

virtual void movement(parameters) = 0;


Can i somehow override the parameters since every class needs receive something different in order to function correctly.

Thank you very much for the speedy response once again!

Share this post


Link to post
Share on other sites
Just pass in a pointer to an array/structure/class. Each type of class can handle it how it wants then. Edited by mark ds

Share this post


Link to post
Share on other sites
Yeah, at the moment one of the parameters goes something like this

(vector<Tile*> &mapTiles);


so basically what you are saying is that i should pass on every vector onto every class and then just chose to ignore some of them when necessary? Edited by Tallkotten

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!