@Edit: I can't edit the title, I mean Hierarchy Entity System, sorry.
I've been working on a simple game with C++/SDL using hierarchy entities and I've been running into some problems to make the entities deal with the map in a intelligible way. I'm using the common way of states, currently having a Start Menu state and a Main Game state.
Short story: I want my entities to know about each other current states (though they should be ideally independent), what would be the most efficient way to do that, and what would be the most readable/organized way to do that?
I had a class called Map, which holds a bi-dimensional array, each position representing a "square" of the map. Also, I had my entities Enemy and my entity Player. My first problem came when I needed to make entities communicate to the map, like "I want to walk to square [x][y], can I do it?"
The first solution I thought I was declaring the map Static, this way everyone could access it in their classes. But I personally dislike using static objects, and I heard it's not a good idea to do it on games, so I tried thinking in something else.
Then, after thinking one more time, I thought of putting the Player and the Enemies INSIDE the map class. It sounded a little weird at first, but considering I had this only one map for the entire game, it seemed plausible. This way, to make entities communicate with the map, I had something like this:
(for each Enemy) Enemy->Update();
int MoveParam = IsMoveValid(Player->MovingDirection, Player->X, PlayerY);
(for each Enemy) MoveParam = IsMoveValid(Enemy->MovingDirection, Enemy->X, Enemy->Y);
Basically, it works like that: The player and enemies updates will set the directions they WANT to move.
Then IsMoveValid will get that direction they want to go, their currently X and Y position, will check if the X and Y position they want to move is ok, and will return a parameter for them (like "Ok, you can move", "You can't move this way", "You can go down but you have to move a pixel to the right first", something like that).
Based on this parameters, the Move function of each entity class will decide if they are moving or not. This implementation sounded really okay at first, but them suddenly I realized that I moved EVERYTHING to the map class, the Main Game state was basically updating and rendering the map object.
Then, considering the map class held only a bi-dimensional array of positions, I moved everything back to the state class and eliminated the map class. This is the version I currently have in here. It's working fine, but I thought on looking for more options to deal with this.
I saw some other feasible ways to implement it, like making your entities hold a pointer to the map and tho each others, or make the class that hold them implement a message system so they can communicate. I'm also reading a lot about Entity-component-system and it may (or may not?) offer some solutions for this question, but considering that there are so many games developed under Hierarchy Entities, I guess a lot of ways should have been thought already.
What's you guys thoughts on this? What is the way you usually implement your entities communication?
Edited by Hauck, 22 April 2014 - 07:54 AM.