Jump to content
  • Advertisement
Sign in to follow this  
ChrisKelso

My character has no legs!

This topic is 2499 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

I am creating a simple rpg to test my knowledge. The thing is, I can't seem to figure out how to move my character from room to room. Each room may have up to 4 doors. Each door has a boolean tag and int id tag to identify it. that was the easy part. I can't seem to figure out how to jump my character from one room - through a door - into the next room.
Here is my code9as sloppy as it is):

#include <iostream>
#include <conio.h>
#include <vector>
#include "MenuCreator.h"
using namespace std;
class room
{
public:
room() :
doorw(false),
doore(false),
doorn(false),
doors(false)
{
}
~room() {}
int getlocation()
{
return location;
}
void setlocation(int loc)
{
location = loc;
}
void setdoorw(bool tf)
{
doorw = tf;
}
bool getdoorw()
{
return doorw;
}
void setdoore(bool tf)
{
doore = tf;
}
bool getdoore()
{
return doore;
}
void setdoorn(bool tf)
{
doorn = tf;
}
bool getdoorn()
{
return doorn;
}
void setdoors(bool tf)
{
doors = tf;
}
bool getdoors()
{
return doors;
}
void setidw(int iw)
{
idw = iw;
}
int getidw()
{
return idw;
}
void setide(int ie)
{
ide = ie;
}
int getide()
{
return ide;
}
void setidn(int in)
{
idn = in;
}
int getidn()
{
return idn;
}
void setids(int is)
{
ids = is;
}
int getids()
{
return ids;
}
void describeroom()
{
cout << "You are in a room...\n\n";
cout << "Doors:\n" << "=======\n\n";
if(doorw)
cout << "There is a door to the west.\n";
if(doore)
cout << "There is a door to the East.\n";
if(doorn)
cout << "There is a door to the north.\n";
if(doors)
cout << "There is a door to the south.\n";
}
private:
int location;//1000 = regular, 2000+ = other stuff
bool doorw;
int idw;
bool doore;
int ide;
bool doorn;
int idn;
bool doors;
int ids;
};
class character : public room
{
public:
void setpower(int p)
{
power = p;
}
int getpower()
{
return power;
}
void setdefense(int d)
{
defense = d;
}
int getdefense()
{
return defense;
}
void setname(string n)
{
name = n;
}
string getname()
{
return name;
}
void move(character &PC_, room &room_, char direction);
private:
string name;
int power;
int defense;
};
void character::move(character &PC_, room &room_, char direction)
{
//move him/her
switch(direction)
{
case 'w':
{
if(room_.getdoorw())
PC_.setlocation(room_.getidw());
break;
}
case 'e':
{
if(room_.getdoore())
PC_.setlocation(room_.getide());
break;
}
case 'n':
{
if(room_.getdoorn())
PC_.setlocation(room_.getidn());
break;
}
case 's':
{
if(room_.getdoors())
PC_.setlocation(room_.getids());
break;
}
default:
cout << "There is no door that way.\n\n";
break;
}
}
void createcharacter(character &PC_);
void playgame(character &PC_, room &room_)
{
//testbed
MenuCreator playmenu;
playmenu.addtitle(" Play the game ");
playmenu.add("Move (n)orth, (s)outh, (w)est, (e)ast");
playmenu.add("(L)ook");
playmenu.add('X');
bool exit = false;
while(!exit)
{
playmenu.display();
char cmd = getch();
switch(cmd)//command: direction or look
{
case 'L':
case 'l':
PC_.describeroom();
break;
case 'W':
case 'w':
PC_.move(PC_, room_, 'w');
break;
case 'E':
case 'e':
PC_.move(PC_, room_, 'e');
break;
case 'N':
case 'n':
PC_.move(PC_, room_, 'n');
break;
case 'S':
case 's':
PC_.move(PC_, room_, 's');
break;
case 'x':
exit = true;
break;
default:
cout << "Perhaps a simpleton could do better...\n\n";
}
}
}
void cheat(character &PC_);
void showstats(character &PC_);
void game(bool &exit, character &PC_)
{
int const maxrooms = 2;
vector<room> rm;
for
(int i = 0 ; i < maxrooms ; i++)
{
room _room;
_room.setlocation(1000 + i);
rm.push_back(_room);
}
rm[0].setdoore(true);
rm[0].setide(1002);
rm[1].setdoorw(true);
rm[1].setidw(1001);
bool gameexit = false;
while(!gameexit)
{
/*room startingroom;
startingroom.setlocation(1001);
startingroom.setdoore(true);
startingroom.setide(1002);
room room1;
room1.setlocation(1002);
room1.setdoorw(true);
room1.setidw(1001);*/
MenuCreator gamemenu;
gamemenu.addtitle("Game Menu");
gamemenu.add("(C)reate character");
gamemenu.add("(P)lay game");
gamemenu.add("C(h)eat");
gamemenu.add("(Q)uit to windows");
gamemenu.add('E');
gamemenu.display();
char choice = getch();
switch(choice)
{
case 'c':
case 'C':
createcharacter(PC_);
break;
case 'p':
case 'P':
playgame(PC_, rm[0]);
break;
case 'h':
case 'H':
cheat(PC_);
break;
case 'q':
case 'Q':
gameexit = true;
exit = true;
break;
case 'e':
case 'E':
gameexit = true;
break;
default:
cout << "try another option..." << "\n\n";
break;
}
}
}
int main()
{
character PC;
bool exit = false;
while(!exit)
{
MenuCreator mainmenu;
mainmenu.addtitle(" Main Menu ");
mainmenu.add("(P)lay");
mainmenu.add('E');
mainmenu.display();
char choice = getch();
switch(choice)
{
case 'p':
case 'P':
game(exit, PC);
break;
case 'e':
case 'E':
exit = true;
break;
case '~':
case '`':
cout << "You've entered the secret command area..." << "\n\n";
break;
default:
cout << "...Invalid key..." << endl << endl;
break;
}
}
return 0;
}
void cheat(character &PC_)
{
MenuCreator cheatmenu;
cheatmenu.addtitle(" Cheat Menu ");
cheatmenu.add("Change (n)ame");
cheatmenu.add("Change (p)ower");
cheatmenu.add("Change (d)efense");
cheatmenu.add(" ");
cheatmenu.add("(S)how stats");
cheatmenu.add('E');
bool exitcheat = false;
while(!exitcheat)
{
cheatmenu.display();
char choice = getch();
switch(choice)
{
case 'n':
case 'N':
{
cout << "Choose name: ";
string strn;
cin >> strn;
PC_.setname(strn);
cout << "\n\n";
break;
}
case 'p':
case 'P':
{
cout << "Choose power: ";
int intp;
cin >> intp;
PC_.setpower(intp);
cout << "\n\n";
break;
}
case 'd':
case 'D':
{
cout << "Choose defense: ";
int intd;
cin >> intd;
PC_.setdefense(intd);
cout << "\n\n";
break;
}
case 's':
case 'S':
showstats(PC_);
break;
case 'e':
case 'E':
exitcheat = true;
break;
default:
cout << "Choose your option more wisely young one..." << "\n\n";
break;
}
}
}
void showstats(character &PC_)
{
cout << " Character Stats " << endl;
cout << "=================" << endl;
cout << "Name: " << PC_.getname() << "\n";
cout << "Power: " << PC_.getpower() << "\n";
cout << "Defense: " << PC_.getdefense() << "\n\n";
}
void createcharacter(character &PC_)
{
//testbed
cout << "Create/Re-Create character? (Y/N)";
char answer = getch();
cout << "\n\n";
if(answer == 'y' || answer == 'Y')
{
cout << "Choose name: ";
string name;
cin >> name;
cout << "\n";
PC_.setname(name);
PC_.setpower(10);
PC_.setdefense(10);
cout << "Character created..." << "\n\n";
}
}

I know some of you are thinking how easy this is...but I have never attempted to use "warp doors" to move my character between rooms.

Share this post


Link to post
Share on other sites
Advertisement
hmm thats hard to read..

Why is your character a room ?
Your main problem seems to be that you change the players location variable (Which it inherits from room, oddly enough) but never actually use that variable to determine which room he is in, When you call Look you are trying to describe the room that the character IS, (not the room he is in), Basically your inheritance is a mess.

Why not something like this:


struct Exit {
std::string description;
char key;
Room *target;
}


class Room {
private:
vector <Exit> exits;
std:string description;
public:
Room *getExitDestination(char key) {
for (int i=0;i<exits.size();i++) {
if (exits.key==key) return exits.target;
}
return null;
}
void printDescription() {
std::cout << description;
if (exits.size()>0) {
std::cout << "Exits: " << std::endl;
for (int i=0;i<exits.size();i++) {
std::cout << exits.description << std::endl;
}
}
}
}

class Character {
Room *currentLocation;

void moveTo(char key) {
Room *temp = currentLocation->getExitDestination(key);
if (temp==null) {
std::cout << "There is no exit in that direction"<<std::endl;
} else {
currentLocation=temp;
temp->printDescription();
}
}
}


This lets you make as many exits as you like from a room and describe them however you please, moving between rooms is simply a matter of changing the characters currentLocation pointer. (the Room class can also be extended to contain a list of items that can be interacted with or picked up aswell and you might want a short description (That is printed as soon as you enter) and a longer one that is printed when you (L)ook)

Share this post


Link to post
Share on other sites
Thank you for your response. I look at this and see what some of it is. I understand what you say it is. But as you know, programming is in the details. Could you please add comments to this? It would really help me understand. I kept my program simple so I could learn my limits and build up from what I had. It took me about half an hour to write it. i know there are probably better ways to do everything I coded. But I like the idea of dynamic doors. Could you please explain the how of it as well as the idea of it. Again, thank you for responding.

P.S. I am still a beginner.(As if no one could tell.)

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!