Game objects accessing map tiles.

I have 2 classes. 1st class is World class which contains array of map tiles (Uint8 WorldMap[500][500]) and some functions(Draw, getTile(x,y)...). Second class is Entity which represents every object in the game world.

The problem

How do i access that "WorldMap" array from Entity's object? I can't seem to be able to make a pointer to "WorldMap" array or the "World" object.

Please point me in the right direction. Thank you! :)
What does your World class look like? Is Uint8 WorldMap[500][500] a private attribute or public?

For design purposes, it might be best to create a third class that controls both the WorldMap and the Object, so that when the object needs to know something about the WorldMap, the control class can compare.

In theory, an object could have a WorldMap object, but that would be a horrendous design because each object would then have it's own instance of the World. This is why I suggest a control class.

There are lots of ways to design, and you don't necessarily need to follow this one. I am sure there are plenty of better ones. Good luck!
From your brief description, it is not clear how your entities interact with your WorldMap.

Normally, your WorldMap class would have functions that effect changes upon the map. For example, move the contents of tile at x,y to tile at a,b.

It's also not clear what you're representing in each Uint8 that makes up your map.

You might be looking for something like this:
1
2
3
4
// Return a reference to the tile at the specified coordinates
Uint8 & World::get_tile (int x, int y)
{ return WorldMap[x][y];
}


I would also suggest you make Tile a class. Right now it's simply a Uint8, but I suspect your tiles are going to get more complex. By making Tile a class now, it makes extending it later much simpler.

A Control class may be premature until you have a better definition of how your entities interact with your world.






Last edited on
Thank you both for your suggestions!

I'm sorry i didn't give enough information.
Here is the World Class
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
class World
{
private:
	SDL_Renderer * renderer;
	SDL_Texture * WorldSprites;
        Uint8 WorldMap[500][500];//holds values 0-255
	SDL_Rect tile;
	SDL_Rect tileSpr;
public:
	World(void);
	~World(void);

	void setRenderer(SDL_Renderer * renderer){World::renderer = renderer;};
	void setRenderer(SDL_Renderer * renderer){World::renderer = renderer;};
	void setSprites(SDL_Texture * WorldSprites){World::WorldSprites = WorldSprites;};

	void genWorld();

	void Draw(int tilesX, int tilesY, int cameraX, int cameraY);

	void addTile(int x, int y, Uint8 tile);

	void setSpriteRect(int x, int y);

	Uint8 getTile(int x, int y){return WorldMap[x][y];}
};


And here is Entity
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
class Entity
{
protected:
	//Sprites
	SDL_Rect SpriteRect;//Sprite rect which goes on "Creaturs.png"
	SDL_Rect WorldRect;//Rect that we see in the actual game world
	SDL_Texture * CreatureSprites;

	SDL_Renderer * renderer;
	int x, y;
public:
	Entity(void);
	~Entity(void);

	virtual void Update();
	virtual void Draw(int cameraX, int cameraY);

	void Init(SDL_Renderer* renderer, SDL_Texture * CreatureSprites);

	void setX(int x);
	void setY(int y);

	void setPos(int x, int y);

	int getX(){return x;}
	int getY(){return y;}
};


I'll play around and try some different things.
Also some time before i was making a roguelike - c++ console application
and i made the map there as "static" and that made it really easy to access whole map array and it's functions. All i had to do was include the header file. Maybe i should try doing that again?

EDIT:
I'm rewriting whole engine to make code less messy...
And to fix my problem i just set the map array as "static"
Last edited on
Topic archived. No new replies allowed.