Classes Enemy
and Ore
are childs of Entity
class. Class Asteroid
is child of Enemy
. Code below is fired when my ship touches any of entities.
Code in a couple sentences is: When ship touches an ore, add ore to ship's inventory and remove that ore from screen. When ship touches enemy, check what enemy it is. If it's asteroid, push ship back, lower it's health and delete that asteroid.
if (entities[j] is Ore)
{ pilots[i].Ship.InventoryAdd(entities[j]); entities.RemoveAt(j--); }
if (entities[j] is Enemy)
{
if(entities[j] is Asteroid)
{
pilots[i].Ship.AddForce(entities[j].Force * (entities[j] as Enemy).Damage);
pilots[i].Ship.HP -= (entities[j] as Enemy).Damage;
entities.RemoveAt(j);
}
}
NOTE that i see the problem that can be triggered when in one update or frame, both collisions are happening (ship vs asteroid & ship vs ore), and I will fix this. Also, this is the whole code, not just a part of all cases. For now, I do only have ores and asteroids (and a ship) on screen.
Problem is happening in ship vs ore collision. My ship is pushed back. But I do that only on ship vs asteroid. That means that the program sees ore as asteroid, wich it is not. I checked all these things multiple times. Yes, they both have the same parent, but if I ask if an instance is asteroid, how on earth can he say "yes, and it's an ore too", when it's not.
I get that if I asked for an Asteroid instance, if it is of class Entity and Enemy and Asteroid, I would always get an answer "yes", but how can Ore be Asteroid?
Question is: what is logic behind this and how do I check this in proper way? Also note that line for adding force will fire for all Enemies, not just for Asteroids, which is the case for now.
You are dealing with two different elements in the entities
array. Because you remove the entry from the array, the second if
works on a different element.
Resolve this by using else if
if (entities[j] is Ore)
{
pilots[i].Ship.InventoryAdd(entities[j]); entities.RemoveAt(j--);
}
else if (entities[j] is Enemy)
{
if(entities[j] is Asteroid)
{
pilots[i].Ship.AddForce(entities[j].Force * (entities[j] as Enemy).Damage);
pilots[i].Ship.HP -= (entities[j] as Enemy).Damage;
entities.RemoveAt(j);
}
}