Game Hierarchy Problem

Pages: 12
How would I do that?
Last edited on
put MapFunctions in a header file (the name "mapfunctions.h" is typical), then #include that file in whatever file uses that class.
I had already tried that. I changed the name of the header from AemRandW.h to MapFunctions.h (and did the same with the impementation file, to see if the name of the file affected it), after which I changed around a few lines of code in the Enemy implementation file which are dependent upon the level variable (from level to Mpo.getLevel()). but the program didn't compile and an error was returned ('expected class name before ''{"'), the error being in LivingObject.h).

Note: I have since modified the code, moving most of the MapFunctions class to the Player or GameObject class. This has created further errors, as the GameObjects Class can't access the variables in the Player class (and I do not wish to put a Player object in the GameObject class, as this will create a circular dependency (is this what resulted in the error that was stated above?)). Could anyone tell me of a better way to do it?

LivingObject.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef LIVINGOBJECT_H
#define LIVINGOBJECT_H

#include "GameObject.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class LivingObject: public GameObject
{
public:
    LivingObject();
    void DetermineFightFirst();
    int getAtk();
    int getDef();
    int getMag();
    int getHeal();
    int getMaxHeal();
    int getMon();
    int getSpeed();
    void setAtk(int a);
    void setDef(int a);
    void setMag(int a);
    void setHeal(int a);
    void setMaxheal(int a);
    void setMon(int a);
    void setSpeed(int a);
    int calcDamage(const LivingObject& attacker);
    void takePhysicalDamage(const LivingObject& attacker);
    ~LivingObject();
protected:
    int atk, def, mag, heal, maxheal, mon, speed, damage;
    bool EnemyFightActive;
};


What have I done wrong this time (probably something stupid)?

Other code that is of relevance is below:

GameObject.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H

#include "MapFunctions.h"

using namespace std;

class GameObject
{
public:
    GameObject();
    ~GameObject();
protected:
MapFunctions Mpo;
private:
};


MapFunctions.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef MAPFUNCTIONS_H_INCLUDED
#define MAPFUNCTIONS_H_INCLUDED

#include <iostream>
#include <cstdlib>
#include <new>
#include <string>
#include <sstream>
#include <fstream>
#include "Enemy.h"

using namespace std;

class MapFunctions
{
public:
    MapFunctions();
    void MapRead(); // Get a map from a file.
    int ReadMap(int xvalue, int yvalue); // Read one part of the map.
    void WriteMap(int xvalue, int yvalue, int writevalue); // Write a new value to one part of the map.
    void MapDelete();
    int getLevel();
    int getRows();
    int getCols();
    void setLevel();
    ~MapFunctions();
protected:
    int level;
private:
    int ** Map;
    int rows, cols, StartTextSize;
    string StartText;
};


GameObject.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "GameObject.h"

GameObject::GameObject()
{
    //ctor
}
void GameObject::PlayMap()
{
    Goblin GoblinObj;
    Orc OrcObj;
    Bat BatObj;
    Spirit SpiritObj;
    Skeleton SkelentonObj;
    DarkKnight DarkKnightObj;
    CarnivorousHorse CarnivorousHorseObj;
    STDMonster STDMonsterObj;
    InsectSwarm InsectSwarmObj;
    Dog DogObj;
    MrGappube MrGappubeObj;
    FootballMonster FootballMonsterObj;
    DemonStripOGram DemonStripOGramObj;
    Enemy *EnemyGoblin = &GoblinObj;
    Enemy *EnemyOrc = &OrcObj;
    Enemy *EnemyBat = &BatObj;
    Enemy *EnemySpirit = &SpiritObj;
    Enemy *EnemySkeleton = &DarkKnightObj;
    Enemy *EnemyCarnivorousHorse = &CarnivorousHorseObj;
    Enemy *EnemySTDMonster = &STDMonsterObj;
    Enemy *EnemyInsectSwarm = &InsectSwarmObj;
    Enemy *EnemyDog = &DogObj;
    Enemy *EnemyMrGappube = &MrGappubeObj;
    Enemy *EnemyFootballMonster = &FootballMonsterObj;
    Enemy *EnemyDemonStripOGram = &DemonStripOGramObj;
    EquateTextPrint();
};

//Works out the directions in which the player may travel and prints them to the screen.
void GameObject::DirectionCheck()
{

    bool DirectionSpecifier[4];
    int DirectionSpecifierCount = 0;

    if(locy<Mpo.getRows())
    {
                if(Mpo.ReadMap(locx,locy+1)!=0)
        {
            DirectionSpecifier[North]=1;
            DirectionSpecifierCount++;
        }
    }
    else
    {
        DirectionSpecifier[North]=0;
    }
    if(locx<Mpo.getCols())
    {
        if(Mpo.ReadMap(locx+1,locy)!=0)
        {
            DirectionSpecifier[East]=1;
            DirectionSpecifierCount++;
        }
    }
    else
    {
        DirectionSpecifier[East]=0;
    }
    if(locy>0)
    {
        if(Mpo.ReadMap(locx,locy-1)!=0)
        {
            DirectionSpecifier[South]=1;
            DirectionSpecifierCount++;
        }
    }
    else
    {
        DirectionSpecifier[South]=0;
    }
    if(locx>0)
    {
        if(Mpo.ReadMap(locx-1,locy)!=0)
        {
            DirectionSpecifier[West]=1;
            DirectionSpecifierCount++;
        }
    }
    else
    {
        DirectionSpecifier[West]=0;
    }
    for(int i=0; i<4; i++)
    {
        if(DirectionSpecifier[i]!=0 && DirectionSpecifierCount>2)
        {
            switch(i)
            {
            case 0:
                cout << "north, ";
                break;
            case 1:
                cout << "east, ";
                break;
            case 2:
                cout << "south, ";
                break;
            case 3:
                cout << "west, ";
                break;
            }
            DirectionSpecifierCount--;
        }
        else if(DirectionSpecifier[i]!=0 && DirectionSpecifierCount>1)
        {
            switch(i)
            {
            case 0:
                cout << "north and ";
                break;
            case 1:
                cout << "east and ";
                break;
            case 2:
                cout << "south and ";
                break;
            case 3:
                cout << "west and ";
                break;
            }
            DirectionSpecifierCount--;
        }
        else if(DirectionSpecifierCount=1 && DirectionSpecifier[i]!=0)
        {
            switch(i)
            {
            case 0:
                cout << "north.";
                break;
            case 1:
                cout << "east.";
                break;
            case 2:
                cout << "south.";
                break;
            case 3:
                cout << "west.";
                break;
            }
            DirectionSpecifierCount--;
        }
    }
}
//This function is not yet finished.
void GameObject::EquateTextPrint()
{
    switch(Mpo.ReadMap(locx,locy))
    {
    case 1:
        cout << "You can move: ";
        DirectionCheck();
        break;
    case 3:
        if(IgnoreSpecifier=0)
        {
            cout << "You approach the exit to the map. Do you wish to leave, or are you wishing to stay and find the riches of the land?" << endl;
        }
        if(IgnoreSpecifier=1)
        {
            cout << "You can still end this level if you want. If not, you can move: ";
            DirectionCheck();
            cout << endl;
        }
    case 4:
        cout << "You approach an ugly, smelly goblin. He grasps a dagger from the pocket of his weak armour and points it at you, ready to fight.";
    default:
        cout << "Something is wrong with this map.....";
        break;
    };
    cout << "This works";
};
GameObject::~GameObject()
{
    //dtor
}


More code is in the next post.
Last edited on
Player.h (header contains function definitions - no implementation file):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED

#define North 0
#define East 1
#define South 2
#define West 3

#include "LivingObject.h"

using namespace std;

class Player: public LivingObject
{
public:
    Player(){
    exp = 0;
    }
    //Increases the player's experience point count by a specified amount.
    void incExp(int a)
    {
        exp += a;
    }
    //Returns the amount of experience points that the player has
    int getExp(){
    return exp;}
//Finds the start of the map and puts the player there.
       void FindStart()
{
    for(int lyx = 0; lyx<Mpo.getRows(); lyx++)
    {
        for(int lxy = 0; lxy<Mpo.getCols(); lxy++)
        {
            if(Mpo.ReadMap(lyx,lxy) == 2)
            {
                locx = lyx;
                locy = lxy;
                Mpo.WriteMap(lyx,lxy,1);
            }
        }
    }
}
protected:
    int exp, locx, locy;
    bool IgnoreSpecifier;
};


If this is not enough information, then I can post the rest of the program's source code.

Thanks so much everyone for the help so far!!
Last edited on
This is too many different files for me to create all these and try and compile. Can you upload the source somewhere? Like on a filesharing site?

Also, what error(s) are you getting?



Anyway you still have some questionable designs here:

- GameObject seems to be doing things that only apply to the Player (if it doesn't apply to ALL game objects, it does not belong in GameObject).

- I don't know what you're trying to do with all those enemies in GameObject::PlayMap, but all that code effectively does absolutely nothing except for the EquateTextPrint call at the end.

- MapFunctions is the map, right? So why does GameObject own an instance of it? Does each GameObject have its own map?
Sorry about the file organisation, I assumed that you just looked for a bit and noticed what was wrong, as well as errors. I have posted them on a file sharing site (the links being at the bottom of the post).

As for the errors, I did not specify them for the reason stated above: I thought that you could look and tell (but now realise that that is not the case for projects of this size).

As for the design:
- GameObject does things that apply to player because I have the GameObject class running the PlayMap function (that plays the game), though some functions only apply to the player, so are part of the player class. Am I overdoing the classes (in the sense that I have nearly everything inside a class and the main function being extremely minimal, to the extent that the main function is not much more than a function to call the a few function and the game plays (or so I have unsuccessfully tried to achieve)) - should I scale down the use of classes a bit and instead implement the actual game in the main function instead.

- The code that does absolutely nothing was created for later, when I actually implement the enemies (but has no use now).

- When you said about the GameObject class having a reference to the map, I was pretty sure that (though not certain that) this was not what you meant, though decided to see anyway. Is what you are saying that 10 objects creates 10 different implementations of one class? Or was I right in believing that they point to one central class (or is it something completely different)?

Anyway, here are the links (Note: I don't know what IDE you use, but unless you use Code::Blocks on Linux, don't bother with the first 3 links, as they are used by the IDE and have nothing to do with the actual code to be compiled):
http://www.4shared.com/file/ufQjliNU/Text_Based_RPG.html
http://www.4shared.com/file/1k3wBFQI/Text_Based_RPG.html
http://www.4shared.com/file/NABkFuNZ/Text_Based_RPG.html
http://www.4shared.com/file/52VaM5uf/Player.html
http://www.4shared.com/file/eKK-YAuy/MapFunctions.html
http://www.4shared.com/file/ik2kiF3D/MapFunctions.html
http://www.4shared.com/file/n_rVfMan/Main.html
http://www.4shared.com/file/KJFIBvTc/LivingObject.html
http://www.4shared.com/file/eRjo9tiR/LivingObject.html
http://www.4shared.com/file/1jWSL9pf/GameObject.html
http://www.4shared.com/file/eoGLMjEf/GameObject.html
http://www.4shared.com/file/-gXIZDTi/Enemy.html
http://www.4shared.com/file/t08WU8q3/Enemy.html
http://www.4shared.com/file/1Yno9qi7/AesgRandW.html
http://www.4shared.com/file/TBTn3iO5/AesgRandW.html
http://www.4shared.com/file/WvTM2yE5/AemRandW.html
http://www.4shared.com/file/Ne3bspvG/AemRandW.html
http://www.4shared.com/file/SxJKv6nr/1_online.html

Thanks!
Zip it all up so it's 1 download, please. Downloading multiple files through a download site is very tedious.
Oops, sorry. I had completely forgot about that! Here it is:
http://www.4shared.com/zip/UHKWcsqm/Text_Based_RPG.html
Thanks. I'll check it out when I get home from work.
Any results yet?
Topic archived. No new replies allowed.
Pages: 12