Well, I am working on having units act upon given commands right now in my RTS game, and I have some questions on how would be the best way to go about some things.
For my game:
Left Mouse-Click == Select Unit
Right Mouse-Click == Have selected unit perform primary action at position of right mouse-click.
Take Age of Empires for example. The left and right mouse clicks in my game perform the same operations. Left selects units, right attacks/moves/gathers.
Also, at the current moment I keep all of my entities in the game stored in a std::vector. This way I can simply iterate through every entity each frame and do the proper updating to each entity. In my Entity::Update() function, the first thing I do is check for left clicks on the mouse. If a left click has occurred, I check to see if it has occurred in the area of that entity. If so, I select that unit. If not, I deselect the unit.
Then, if the unit is selected, I check for right clicks, which imply that the user wants the selected unit to act in some sort of way. Up to now the only action has been to move (as was seen in my recent Alpha demo on my other thread about 1 week ago). That is all well and good, but now I need to add additional actions such as attack and gather (mainly working on attack right now).
Therefore, this is what I have implemented:
Psuedo Code:
Now, that seems pretty sensible to me. What I am wondering about is how I am implementing my checking to see if an enemy exists at the target location.Code:if ( Entity is Selected )
{
if ( Right Click has occurred )
{
Get Position on screen in which click occurred;
Set destination of entity to that position;
Check to see if enemy unit is at that position;
if ( enemy exist at position )
Set Target to that enemy entity;
}
}
At the current moment, I call a function which iterates through the entire entity list and checks the position of each entity. If a certain entity is located at the right-clicked position, and that entity is an enemy of the selected enemy, then I set it as the target entity of the selected entity, and break out of the loop. In essence, it is an O(n) algorithm. This O(n) algorithm is run for every selected unit on the map. Therefore, if every single entity on the map is selected, it is order O(n^2). However, it is highly unplausable that every entity on the map will be selected. At most their might be 25 units selected at a time.
Do you think this is a good way to go about doing things? One obvious optimization is to split the entity list and have a seperate entity list for each player. That will happen later in development, but the project is not yet ready for that.
However, another thought has popped into my mind. I am not sure how efficient it would be, however, and so I thought I would throw it out in front of all of you guys and see what you think. What if, when I update each entity each frame, I look at its world position, and then I go to the physical map and make that specific position on the map point to that entity, basically tying that position on the map and that entity together. Then, when I right click, I simply take the map position, see if it is pointing to any entity, see if that entity is an enemy, and then set it as a target.
It seems efficient, but yet it also seems like it might be complicated at the same time. So what do you think about it?
Also, if you know of any other good methods, what are they?