First of all hello, this is my first time here. I'm young coder (15) and this is my second attempt to make a game (actually whole reusable game engine AND framework, not sure how to call it, maybe GDK?), while making it much more organized this time.
IMPORTANT NOTE: I'm using XNA framework, so the game engine is not purely low level DirectX management! I'm trying to expand XNA with predefined classes that will be easy to insert into any game and manage later on!
I got stuck at finding a way to put an Entity into WorldManager's list, later retrieve the entity for direct changes, and then put it back to the list (so it's used with Update method).
My ideas (and questions) were:
Best possible solution would be to somehow make the entity linked with the list, so as I update the lone entity object it updates in the list too. Unfortunately I don't know how to do it, and this could be the best answer.
What I also thought about was to edit the entity I created first, and then use find to look it up in the list and update. The problem is that I'm unsure of how the find method searches. What if any change can make method think it's a whole new different object? (How does find actually find?)
What I did was to add IDs to Entities at the moment they're added to the list, so I can put them back in their place after editing. The problem is that I've got remove method too, and I'm unsure would it change the indexes, because if it would then I'd have to make re-update IDs every time I remove an entity. I find that quite inefficient. So would List<>.Remove change the indexes? (I guess it would)
These are actually 3 questions in one, and I'd appreciate answer on any of these, of course answers to topmost questions will be more appreciated!
A piece of code to get idea how does it all work:
public class WorldManager
{
List<Entity> Entities = new List<Entity>();
List<Entity> ToAdd_Entities = new List<Entity>();
List<Entity> ToRemove_Entities = new List<Entity>();
...
public void Update()
{
for(int count = 0; count < ToAdd_Entities.Count; count++)
{
Entities.Add(ToAdd_Entities[count]);
int index = Entities.IndexOf(ToAdd_Entities[count]);
Entities[index].ID = index;
ToAdd_Entities.Remove(ToAdd_Entities[count]);
}
for (int count = 0; count < ToRemove_Entities.Count; count++)
{
Entities.Remove(ToRemove_Entities[count]);
ToAdd_Entities.Remove(ToAdd_Entities[count]);
//index update is required here too, I just didn't implement it yet.
}
...
}
}
How would I access it:
List<Entity> bots = WorldManager.GetEntitiesByName("bot");
bots[1].Location = new Vector2(x,y);
WorldManager.UpdateEntities(bots);
I don't use that piece of code, that's just a plain example of my idea.
Please forgive me for my confusing English (if you find it confusing), I'm a Serbian.
Thanks in advance.