How to imagine the world and dynamic objects in a console game in C ++?

I would be grateful for a little help. This is supposed to be a console game that uses elements from bagels, role-playing games, sandboxes, and survival games.

I tried to get a working architecture about three times. Every time I came across problems that I could not solve without hacking my way ...

I have already looked at numerous blogs and tutorials for this, but none of them matched my purpose.

1) http://trystans.blogspot.co.at/2011/08/roguelike-tutorial-01-java-eclipse.html
2) http://www.kathekonta.com/rlguide/index.html

I have already followed the Trystan block, which uses Java, and recreated its design in Java and AS3.
However, I'm not completely happy with the way he put classes together. Commands like World.dig () - it makes sense for me to call Player.dig () - or representing the map as an array of 3d (!) Made me believe that there should be a better way to do this.

The second link I posted is encoded in C ++, however there is only one central map. My goal is to have several cards that I can enter with characters, etc.

I will post the code of my last attempt below. It does not have any movement or interaction at all, since I want to get the design from the very beginning, without failing after several hours spent in the project (which I have already met too often).

main.cpp:

# include <iostream>

# include "World.h"
# include "Entity.h"

using namespace std;

int main(){

    int testFloor[] = {
        0, 0, 0, 0, 0, 
        0, 1, 1, 1, 0, 
        0, 1, 0, 0, 0, 
        0, 1, 1, 1, 0,
        0, 0, 0, 0, 0
    };

    World world(testFloor, 5, 5);

    //Creating an entity at the top left corner 
    Entity nine(9, 0);
    world.addEntity(nine);

    //Draw the entire world
    world.draw(0,0,5,5); 

    system("PAUSE");
    return 0;
}

World.h:

# ifndef WORLD_H
# define WORLD_H

# include <vector>

# include "Entity.h"

using namespace std;

class World {
private:
    int* _floor;
    int _width;
    int _height;

    vector<Entity> _entities;

public:
    World(int* floor, int width, int height) :
        _floor(floor), _width(width), _height(height){}

    /*Pushes an Entity to the _entities-vector*/
    void addEntity(Entity e){
        _entities.push_back(e);
    }

    /*Displays the floor at a position, or the glyph of an entity, if applicable*/
    //This "glyph" is just a number here, similar to the contents of the _floor array
    int glyph(int ID){
        int out = _floor[ID];
        for(Entity e : _entities){
            if(e.pos() == ID){
                out = e.glyph();
                break;
            }
        }
        return out;
    }

    //Prints the World as seen from "above" - entities will cover the floor
    void draw(int left, int top, int width, int height){
        system("cls");

        int ID;

        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                ID = (x+left)+(y+top)*width;
                cout << glyph(ID);
            }
            cout << endl;
        }
    }
};

# endif

Entity.h:

# ifndef ENTITY_H
# define ENTITY_H

class Entity{
private:
    int _glyph; //Will be a char later, integer for now, since the glyph function returns
                //an int for simplicity
    int _pos;

public:
    Entity(int glyph, int pos) : _glyph(glyph), _pos(pos) {}

    int glyph(){return _glyph;}
    int pos(){return _pos;}
};

# endif

. Entity , "9", , , ... .

1) Entity, (Worldposition), , .

2) , , , , , .

, , Entity. , World.update(), , , , , ... , .

, / .

, :

- , ?
- - , ( , )?

: , , . , , , , .

!

+4
1

1) , 2- 1- , - :

Point Entity::pos2D(World& world) const {
    int y = _pos / world.width();
    int x = _pos % world.width();
    return Point(x, y);
}

, Point x y. access() height() World .

, , 2d - 1- .

2) . .

Entity::update(World& world) {
   // do some stuff
}
Entity::move(World& world) {
   // do some stuff
}

. , . , , , , , , , . , .

+3

Source: https://habr.com/ru/post/1540502/


All Articles