Classes, inheritance, interface.. etc etc

I had a hard time naming this topic but i hope this is ok.

So my problem is that im making a 2D tower defence game and i was just gonna start making the tower classes and interface. And ive come to a problem.

I will only post the concept code not valid c++ code

So my interface class looks something like this.
1
2
3
4
5
6
7
class tower
{
 virtual fire();
 virtual stopFire();
 virtual render();
 virtual remove();
};


then i have a arrowTower class that inherits from tower and i think you understand how i mean so i dont have to post it.

but then i wanted to have a class called noTower which basicly means that there is no tower on the tile and it does not need to be rendered or updated.

But here is my problem.

So i put all those towers in a 2D vector that looks like this
std::vector< std::vector<tower*> > towerMap;

I think it is logical to use a remove function to set the arrowTower to noTower.
But im not sure how to implement that.

I know i can use the delete and new to do this more manually but i think its more logical to do it this way if its possible.

Is it possible to do something like:
this = noTower

Hope you can help me with this or maybe give me another way to do it.

Thanks in advance

EDIT: this maybe seems confusing. Just reply or PM and i can send my code and maybe you will understand what i mean better
Last edited on
A remove function would probably have to use delete and new internally. Something like this.
1
2
3
4
5
void remove(int x, int y)
{
	delete towerMap[y][x];
	towerMap[y][x] = new NoTower;
}


You can use dynamic_cast to check the actual type of the object but I don't know if it's necessary. In most cases you should not have to do that. No tower is not actually a tower so it makes more sense to not have a class for it. Instead you could store a null pointer to represent the lack of tower on that position. In that case the remove function will look something like
1
2
3
4
5
void remove(int x, int y)
{
	delete towerMap[y][x];
	towerMap[y][x] = nullptr;
}
Actually that was not what meant at all (it was me being unclear on what i mean). But when i think about it i think your solution is a better choice.

Never thought about that with the nullpointer. seems like a better idea. Thank you for the help.
You shouldn't delete it since then the tile wouldn't exist, but you want the tile to still exist, but it should be of the type "NoTower"
well
i think you dont need a notower class

add a bool named tower
then in update and every where else, first check if tower then rest of code
and for removing it you just need to set the bool to zero

mail me if you need more help
Tiles and towers are different things in my program. The drawing of the map is totally independent from drawing of towers. Therefore i can use null as tower values.
yes you can use, but it's not wise to remove some thing from vector get buggy if it's on the middle.

why dont you use what i suggested? a boolean is not so hard to add
@Sajjad Heydari
delete towerMap[y][x] does not delete anything from the vector. towerMap[y][x] is a pointer and what is deleted is the object that the pointer points to.
exactly, i will still stick with peters solution. It feels easy to implement. I am going to try it later today or tomorow and we will see how it goes. Otherwise i will leave a comment.

Actually i came up with a idea that combine both of our ideas peter.
i will make a remove function that takes a tower* as parameter and then just simply delete and NULL it. I dont like the idea of posting x,y like that.

Im pretty sure this will work good, tho im too tired for programming now and cant test it cause i know i will do something wrong that will take hours later to fix
No, when you delete a newed pointer, it becomes free, so the next time new is called, it will use that pointer, and overwrite it! Look at this code:
1
2
3
4
5
6
7
8
int main()
{
	int* p1=new int;
	cout<<"P1 is "<<p1<<endl;
	delete p1;
	int* p2=new int;
	cout<<"P2 is also "<<p2;
}
P1 is 0x350f58
P2 is also 0x350f58
i will make a remove function that takes a tower* as parameter and then just simply delete and NULL it.

In that case you need to pass the tower* by reference or otherwise the pointer in the towerMap will not be updated.
1
2
3
class tower{
   tower_prototype *stats; //not dynamically created
   int hp;
Maube easier to visualize. Especially if you are not changing behaviour
Well, my plan is to make very different kinds of towers and they are going to behave very different from eachother. So i do not think that is a good idea ne555. Tho i thought of using it from the start. But i think what im doing now is more logical
Does anyone have any ideas on how to make a tower upgrade system. My plan is to make a variable on each tower which keeps track on what tower i can be upgraded to. Is that a good idea? I have a enum list of all towers, so the variable should just be a object of that enum

Any other ideas?
what if you wanted a tower to have multiple path upgrades? I probably wouldn't over think it too much, a function that upgrades the tower would be how I'd do it, just have some different values it can use to determine what tower to upgrade to (e.g if a tower is at level 5, is of type fire, is on the lightning path, and is currently a Flame tower, upgrade it to a Spark tower and switch the type to lightning, or whatever.)
Last edited on
Zeph, That idea sounds simple and clear yes, thank you. By the way, i now done the tower class and it works great. I can add and remove towers with no problems and NULL means no tower.

So this is marked as solved now, thank you all
Topic archived. No new replies allowed.