I am creating a console application game.
The game contains Point
object (x
and y
properties) and GameObject
which other classes inherit from.
I create a dictionary:
Dictionary<Point, List<GameObject>> myDict
Each point in the dictionary stores list with object / objects. I don't wanna empty points. An example for initializing dictionary:
1. <(0,0), List<obj1>>
2. <(35,10), List<obj2, obj3>>
3. <(8,9), List<obj4, obj5>>
4. <(40,3), List<obj6>>
If obj1
moves to (0,1):
<(0,1), List<obj1>>
<(35,10), List<obj2, obj3>>
<(8,9), List<obj4, obj5>>
<(40,3), List<obj6>>
As you can see, the pair with the key
(0,0) has been removed and (0,1) has been added.
if obj2
moves to (8,9):
<(0,1), List<obj1>>
<(35,10), List<obj3>>
<(8,9), List<obj4, obj5, obj2>>
<(40,3), List<obj6>>
In this case, obj2
has just been removed from the list and the key wasn't removed because it stores obj3
and it's not empty. obj3
was added to (8,9) list.
These manipulations will be done every 250 milliseconds, inside the game loop, automatically. I tried to use foreach loop
, something like that:
foreach (KeyValuePair<Point, List<GameObject>> kvp in myDict)
{
foreach (GameObject obj in kvp.Value) {
obj.move();
}
}
But it doesn't work because as I understood I cannot add / remove keys when I iterate.
Do you have any suggestions?
Some important thing:
- I have to use dictionary for that.
GameObject
don't havePoint
property. you can know where theGameObject
located only if you check in the dictionary. please don't tell me to add this property because I can't and that not the target.