Text Game Engine

Old style game, but still great

Page 1 of 1

4 Replies - 2137 Views - Last Post: 27 October 2008 - 10:15 AM

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Text Game Engine

Posted 26 October 2008 - 01:54 PM

This is something that me and Whizzy have been putting off for ages now~ Finally got around to writing some of it.

It's a C++ text game engine, and it's working as a navigable map right now. You can add rooms and move around.

They don't contain anything yet, but it's a start.

Nothing flashy yet, but it will be when it's done.

Here's the code, along with a simple example in main:
#include <iostream>
#include <cstdarg>
#include <cctype>
#include <map>

using namespace std;

class Exits {
    public:
        Exits();
        int N, E, S, W, NE, SE, NW, SW, UP, DOWN;
};

Exits::Exits() {
    this->N    = -1;
    this->E    = -1;
    this->S    = -1;
    this->W    = -1;
    this->NE   = -1;
    this->SE   = -1;
    this->NW   = -1;
    this->SW   = -1;
    this->UP   = -1;
    this->DOWN = -1;
}


class Room {
    private:
        bool dark, locked, combat;
        int value;
        string name;
        string description;
    public:
        Room(int _value, string _name, string _description);
        Exits exits;
        void printInfo();
        bool isDark();
        bool isLocked();
        bool isCombat();
        int  getValue();
        void setDark(bool _dark);
        void setLocked(bool _locked);
        void setCombat(bool _combat);
        void setName(string str);
        void setDescription(string str);
        void addExit(char* direction, int roomValue);
        void addExits(int count, ...);
};

Room::Room(int _value, string _name, string _description) {
    this->name = _name;
    this->description = _description;
    this->value = _value;
}

void Room::printInfo() {
    cout << this->name << endl
         << this->description << endl << endl;
}


bool Room::isDark() {return this->dark;}
bool Room::isLocked() {return this->locked;}
bool Room::isCombat() {return this->combat;}
int  Room::getValue() {return this->value;}
void Room::setDark(bool _dark) {this->dark = _dark;}
void Room::setLocked(bool _locked) {this->locked = _locked;}
void Room::setCombat(bool _combat) {this->combat = _combat;}
void Room::setName(string _name) {this->name = _name;}
void Room::setDescription(string _description) {this->description = _description;}
void Room::addExit(char* direction, int roomValue) {
    if      (direction == "N")    this->exits.N    = roomValue;
    else if (direction == "E")    this->exits.E    = roomValue;
    else if (direction == "S")    this->exits.S    = roomValue;
    else if (direction == "W")    this->exits.W    = roomValue;
    else if (direction == "NE")   this->exits.NE   = roomValue;
    else if (direction == "SE")   this->exits.SE   = roomValue;
    else if (direction == "NW")   this->exits.NW   = roomValue;
    else if (direction == "SW")   this->exits.SW   = roomValue;
    else if (direction == "UP")   this->exits.UP   = roomValue;
    else if (direction == "DOWN") this->exits.DOWN = roomValue;
}

void Room::addExits(int amount, ...) {
    va_list _exits;
    va_start(_exits, amount);
    for (int i = 0; i < amount; i+=2) {
        char* _direction = va_arg(_exits, char*);
        int _roomValue = va_arg(_exits, int);
        this->addExit(_direction, _roomValue);
    }
    va_end(_exits);
}

class Rooms {
    private:
        int currentRoomIndex;
        map<int, Room*> rooms;
    public:
        Room * currentRoom;
        void addRoom(Room *room);
        void changeRoom(int roomNumber);
        void changeRoom(string direction);
};

void Rooms::addRoom(Room *room) {
    this->rooms[room->getValue()] = room;
}

void Rooms::changeRoom(int roomNumber) {
    this->currentRoom = this->rooms[roomNumber];
    this->currentRoomIndex = roomNumber;
}

void Rooms::changeRoom(string direction) {
    for (unsigned int i = 0; i < direction.length(); i++)
        direction[i] = toupper(direction[i]);

    if (direction == "N" && this->currentRoom->exits.N != -1)
        this->currentRoomIndex = this->rooms[this->currentRoomIndex]->exits.N;
    else if (direction == "E" && this->currentRoom->exits.E != -1)
        this->currentRoomIndex = this->rooms[this->currentRoomIndex]->exits.E;
    else if (direction == "S" && this->currentRoom->exits.S != -1)
        this->currentRoomIndex = this->rooms[this->currentRoomIndex]->exits.S;
    else if (direction == "W" && this->currentRoom->exits.W != -1)
        this->currentRoomIndex = this->rooms[this->currentRoomIndex]->exits.W;
    else if (direction == "NE" && this->currentRoom->exits.NE != -1)
        this->currentRoomIndex = this->rooms[this->currentRoomIndex]->exits.NE;
    else if (direction == "SE" && this->currentRoom->exits.SE != -1)
        this->currentRoomIndex = this->rooms[this->currentRoomIndex]->exits.SE;
    else if (direction == "NW" && this->currentRoom->exits.NW != -1)
        this->currentRoomIndex = this->rooms[this->currentRoomIndex]->exits.NW;
    else if (direction == "SW" && this->currentRoom->exits.SW != -1)
        this->currentRoomIndex = this->rooms[this->currentRoomIndex]->exits.SW;
    else if (direction == "UP" && this->currentRoom->exits.UP != -1)
        this->currentRoomIndex = this->rooms[this->currentRoomIndex]->exits.UP;
    else if (direction == "DOWN" && this->currentRoom->exits.DOWN != -1)
        this->currentRoomIndex = this->rooms[this->currentRoomIndex]->exits.DOWN;

    this->currentRoom = this->rooms[this->currentRoomIndex];
}

int main() {
    Rooms myMap;
    Room *Docks = new Room(1, "Docks", "The docks. There are some boats!");
    Docks->addExit("S", 2);
    myMap.addRoom(Docks);
    Room *GuildHall = new Room(2, "Guild Hall", "This is the guild hall");
    // demo of how to add multiple exits~
    GuildHall->addExits(2, "N", 1, "S", 3);
    myMap.addRoom(GuildHall);
    Room *NorthPlaza = new Room(3, "North Plaza", "This is the north plaza");
    NorthPlaza->addExit("N", 2);
    NorthPlaza->addExit("E", 4);
    myMap.addRoom(NorthPlaza);
    Room *Temple = new Room(4, "Temple", "This is the temple");
    Temple->addExit("W", 3);
    myMap.addRoom(Temple);

    // THIS BIT IS IMPORTANT! You must first move into room #1, otherwise you'll get a runtime error
    myMap.changeRoom(1);
    while (true) { // main game loop
        myMap.currentRoom->printInfo();
        string response;
        cout << endl << "Which direction? ";
        cin >> response;
        myMap.changeRoom(response);
    }
}


The code can probably be dumbed down a bit, but that's just how I work~ big code first, shrink it later.

Feedback is greatly appreciated, as always.

:)

Is This A Good Question/Topic? 0
  • +

Replies To: Text Game Engine

#2 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Text Game Engine

Posted 26 October 2008 - 02:14 PM

Looks good.

I've one suggestion. Make isLocked part of the exits.

a-b---lockeddoor-c-d-e
| |
f--bigdungeon----g

The exit between b and c is locked, but the exit from d to c is not. Maybe the door between b and c is a fire door. B->c is locked, but c->b is not.

Edit: damn can't make my map look right. b-f, and g-d.

This post has been edited by brds: 26 October 2008 - 02:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 rudeboyco  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 16-July 08

Re: Text Game Engine

Posted 26 October 2008 - 02:15 PM

I argee it needs dumbing down but good overall.I hope you release and full verison.Been looking in to a text based engine

keep up the good work
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Text Game Engine

Posted 27 October 2008 - 05:53 AM

Thanks for the feedback~ :)

I haven't really focused too much on locks and stuff yet, like I said that will come later. I was mostly focused on making it possible to create a basic game loop without having to do much work.

Functionality will be added in soon, I've got way too many projects going at the moment, and I've set targets to get at least 2 finished this week.
Was This Post Helpful? 0
  • +
  • -

#5 Noldona  Icon User is offline

  • Addicted to DIC

Reputation: 5
  • View blog
  • Posts: 1,051
  • Joined: 02-July 02

Re: Text Game Engine

Posted 27 October 2008 - 10:15 AM

Check out AGT (Adventure Game Toolkit). Its freeware and open source now because it is so old. It works very well, only problem is that the code is written in Turbo Pascal. I have been planning on taking this code and rewriting it in C++ for a while with some OOP, I just never got around to it. It is worth a look to get some ideas for how yours should work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1