Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411658 Posts in 69395 Topics- by 58451 Members - Latest Member: Monkey Nuts

May 15, 2024, 07:25:50 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperDesignBest way of implementing this in C++?
Pages: [1]
Print
Author Topic: Best way of implementing this in C++?  (Read 2139 times)
stef1a
Guest
« on: January 31, 2011, 01:18:03 PM »

Okay, I'm in a bit of a design slump... I'm making a 2D terminal game, and I currently have Player and Map classes. I want to add an Object/Item class so I can start putting objects on the screen and have the player interact with them. Right now, the map class instances are 2d arrays of ints (each int represents a tile), and they're stored in a world map array. I think doing a similar thing for the objects would be effective, but instead of having the 2d arrays of ints where the 2d array is the instance, the ints would be the different instances. Is this an effective way of making dynamic class instances? How else could I do it so that I could create the object instances (i.e. based on a system similar to the map system - each int in a 2d array (representing the map) will represent a different type of Object/Item instance)? The thing is, having a 2d array for the Object "map" might not work, because multiple items could have the same x-y position on a given map... I need some input. I really have no idea how to turn this into code. Please advise!  Beg
Logged
Fallsburg
Level 10
*****


Fear the CircleCat


View Profile
« Reply #1 on: January 31, 2011, 01:27:02 PM »

Well, to be honest, I have no idea what you are talking about.

Beyond that, this should probably go in the technical forum.
Logged
stef1a
Guest
« Reply #2 on: January 31, 2011, 01:35:53 PM »

Whoops... Can a mod please move this to the Technical Forum?

Basically, I have a 2d map system where each map is a 2d array of int stored in a worldmap array of pointers to maps. So, a map might look like

Map map00 = {
{0, 0, 0, 0},
{0, 1, 2, 0},
{0, 1, 1, 0},
{0, 1, 1, 0}
};

where 1 is a floor tile, 0 is a wall, and 2 is another type of floor tile.
The world map might look like

Map* worldmap = {
{{&map00, &map01},
{&map10, &map11}
};

Now then, I want to start putting objects into the game... I initially thought that I could have a world map of ITEM maps, where each ITEM map would have a 2d arry of ints, and depending on what ints they are, different objects are created dynamically. An object map for the first map, map00, might be:

Object obj00 = {
{0, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 2, 0}
};

where 1 could be, say, a Scroll object and 2 could be a Potion object. However, the downside of this is that it would be difficult to add two objects in the same position of the map. Also, making a world map for object maps adds another layer of confusion to it. I'm looking for better ways to accomplish this.
Logged
J-Snake
Level 10
*****


A fool with a tool is still a fool.


View Profile WWW
« Reply #3 on: January 31, 2011, 02:37:12 PM »

The solution to adding different objects on one place is easy in case you have only few types of objects. I am currently making a game that simply uses multiple Grids. One Grid specifies the stone-type. Another grid stores crystals since crystals can be placed over a stone.  

Now you might still see a restriction in it. The int-grid can allow multiple objects on one place but it assumes the same type of them. Or the int-grid for stones allows to place different types of stones but only one of them on the same place. These are the two ways how to nicely use an int-grid and it is sufficient for my current project.

But you might have the following problem: What if we have 100 types of fruits like apples,cherries, bananas and so on? And all of them can be placed on top of another.
Making 100 int-grids sounds like a quantity overhead, just too much to deal with.
So how about making a grid(2d-array) that stores a fruit-list in each cell. If you make an abstract fruit-class and then derive apples, bananas and whatever you need from that you will have many different types of fruits but all of them are fruits. So now your fruit-grid can do both, save multiple fruits and different types of fruits on the same place. I hope you are familiar with polymorphism. This is an example for how powerfull this concept is.

Let me know if it helped.
« Last Edit: January 31, 2011, 02:49:26 PM by J-Snake » Logged

Independent game developer with an elaborate focus on interesting gameplay, rewarding depth of play and technical quality.<br /><br />Trap Them: http://store.steampowered.com/app/375930
eclectocrat
Level 5
*****


Most of your personality is unconscious.


View Profile
« Reply #4 on: January 31, 2011, 06:45:10 PM »

So you want a grid of ints? How do you translate the ints back into items? Another array? Is this for the map save file, or the in memory map? WTF I'd just go with instance pointers, but hey, it's only wrong if it doesn't work.

This along the lines of what J-Snake was mentioning, basically a grid of lists.

class Object { ... };
class Container : public Object {
// ...
private:
   list<Object*> _contents; // Or whatever you want here.
};

Object * item_grid[3][3] = {
{new Container, new Container, new Container},
... };

Now you can define the Container mechanics however you like, as your kinda bizzare sounding int map, an stl vector, a straight up array, a ball of fuzzy cats, whatever.

Basically, you have a concept of each grid square being able to contain multiple items. Then you can refine the concept into an implementation. So maybe when drawing the map you'll draw each item in the container, one atop the other, or maybe instead of drawing all the items, you'll just draw a little bag icon, or whatever. Just start with the conceptualization of how you're going to manage the items, then find an implementation to express that concept.

PS > in my game, I opted for extreme simplicity at the cost of memory (a pretty cheap resource these days).

struct Cell
{
    Sprite * tile;
    Entity * layers[MAX_LAYERS];
    // miscellaneous stuff here
};
typedef vector<vector<Cell> > Map;
Logged

I make Mysterious Castle, a Tactics-Roguelike
shadowdim
Level 1
*



View Profile
« Reply #5 on: February 01, 2011, 01:17:27 PM »

Looks like someone is coding some sort of engine without knowing much, here Smiley It's okay though. But you should probably use an existing 2D-engine before going this deep into code.

Also, an std::map of pointers to instances would be good, I guess.
Logged
Fallsburg
Level 10
*****


Fear the CircleCat


View Profile
« Reply #6 on: February 01, 2011, 01:53:32 PM »

Looks like someone is coding some sort of engine without knowing much, here Smiley It's okay though. But you should probably use an existing 2D-engine before going this deep into code.

Also, an std::map of pointers to instances would be good, I guess.

Not to belittle the OP, but I would think that a std::map of pointers would just lead to lots of leaked memory.
Logged
Defsan
Level 0
**



View Profile
« Reply #7 on: February 02, 2011, 04:21:12 AM »

I don't think I quite understand what you mean, but have you tried using heritage? Make an Object superclass, and inside it have all your objects (maybe divided into subsequent superclasses as well?). Then you'd create a matrix of Object pointers and call them from there.
Logged
baconman
Level 10
*****


Design Guru


View Profile WWW
« Reply #8 on: February 06, 2011, 09:58:19 AM »

Well, first of all, how much do you really need for an object to coincide with another? A lot of Roguelikes I see deals with this by randomly selecting an empty, adjacent space (if possible) and allowing the second "placed" object to go there, instead. Or, you can create a compound object (a bag or box, for example) that is capable of containing multiple objects within it. For that matter, you can just make it a "stack" or a "pile."
Logged

X3N
Level 6
*


View Profile
« Reply #9 on: February 21, 2011, 10:01:57 AM »

Well, first of all, how much do you really need for an object to coincide with another? A lot of Roguelikes I see deals with this by randomly selecting an empty, adjacent space (if possible) and allowing the second "placed" object to go there, instead. Or, you can create a compound object (a bag or box, for example) that is capable of containing multiple objects within it. For that matter, you can just make it a "stack" or a "pile."

If each tile object has a pile array, and you just show the icon for the topmost one (or sort by threat level eg. show a trap before you show armor), does that accomplish what you want?

(not efficient: )Or you could track an array of items, figure out their co-ordinates and draw that way.
Logged

destiny is truth pre-op
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic