|
|
|
|
Move Hero to new position Get tile ID of hero's new position If (primary type of tile is BARRIER) { move character just outside of tile } If(primary type of tile is RAMP) { move y position of character to x*slope } .... and so on |
So for example, if the data file says "return new Ramp()", how do I then make the vector element (which starts out as a pointer to the base class Tile) into a Ramp? |
|
|
I've heard that unique_ptr is a smart solution to this problem. For whatever reason my compiler didn't recognize it, even after I did some typedef like you have above in line 1. |
Ultimately, I wasn't that bothered by manually deleting my vector because I planned on doing so when changing rooms, anyways |
Basically, even though the behavior of the different tiles are all radically different, I thought that they all needed to be contained in the same vector |
|
|
vector<Tile>
and you don't have to deal with new/delete at all.
If you are talking about putting the Ramp into the vector... it's simple: you just assign it. A Ramp* is a Tile* because Ramp is derived from Tile: 1 2 3 vector<Tile*> foo; foo.push_back( new Ramp ); // <- perfectly legal |
Enter new room Determine total number of tiles in new room Declare a vector of pointers to base class, called room_tiles, with total number of elements equal to the number of tiles determined above ( vector<Tile*>(total number of tiles) room_tiles) Read tile data in from external data file e.g., DataFile >> Tile 17 >> is type Ramp >> with slope 1/2 >> and with y-intercept 3 (note that I realize that I can't just do exactly as written above; I'd store each data member to a temporary variable for processing) Set the vector item to have the above qualities: room_tiles[17] should be of derived class Ramp, room_tiles[17]->Slope = 1/2, etc. etc. |
Did you #include <memory>? |
To clarify... you're deleting the items in the vector and not the vector itself, right? ;P |
propose move to tile (x,y) get tile at position (x,y) if(room_tiles[ tile at (x,y) ]->primary_type == barrier) { move back to old position }. |
Now the last step is the one I'm having trouble with. Is it as simple as room_tiles[17] = new Ramp(Initialization variables)? |
|
|
The struct idea actually sounds pretty good. The one reservation I have is that some of the specialized tiles have shared behavior that I would like to contain in the base class. Specifically, the base class contains information about whether the tile blocks motion or not. |
|
|
The struct method just seems a bit inefficient, because only a very small subset of the total number of tiles in any given room will make use of the slope integer |
I think I can take it from here. Thanks for bothering to respond to all my questions and providing clear explanations! I really appreciate it. |