//Return all valid collisions between objects in a given list internal static void CollisionChecks() { var collisionList = new List <Collision>(); //Prep a list for (int i = 0; i < ObjectList.Count; i++) //For each object { SceneObject currObj = ObjectList[i]; { if (currObj.GetType() == typeof(Bullet)) //If it is a bullet { if (currObj.GlobalPosition.x < 0 || currObj.GlobalPosition.x > WindowWidth || currObj.GlobalPosition.y < 0 || currObj.GlobalPosition.y > WindowHeight) { //If it has left the window boundaries collisionList.Add(new Collision(currObj)); //Add a null collision to the list continue; //Continue to next object } } } for (int j = i + 1; j < ObjectList.Count; j++) //For each object beyond the current { SceneObject otherObj = ObjectList[j]; if (currObj.typeIgnore.Contains(otherObj.GetType()) || otherObj.typeIgnore.Contains(currObj.GetType())) //If either object ignores the others' type { continue; //Skip } if (currObj.specificIgnore.Contains(otherObj) || otherObj.specificIgnore.Contains(currObj)) //If either object ignores the other specifically { continue; //Skip } if (DistanceBetweenObjs(currObj, otherObj) > currObj.maxBoxDimension + otherObj.maxBoxDimension) //If they are too far apart to actually touch, { continue; //Skip } if (!Collides(currObj.Box, otherObj.Box)) //If oriented bounding boxes don't collide { continue; //Skip } collisionList.Add(new Collision(currObj, otherObj)); //Add pair to final list if all checks passed } } Collisions = collisionList; //Update the list of Collisions }
//Processes the list of collisions internal static void CollisionProcess() { foreach (Collision coll in Collisions) { SceneObject a = coll.a; SceneObject b = coll.b; string container; //Contains info on what types are colliding if (b == null) //If it is a null collision { container = a.GetType().Name + ",NULL"; //Note as such } else //Otherwise { container = a.GetType().Name + "," + b.GetType().Name; //Note both types } switch (container) //Look at the info { case "Bullet,NULL": //case "x,y": Read these cases as "I am an x colliding with a y" { a.Destroy(); } break; case "Tank,Tank": //If two tanks collide { Bounce(a, b); //Bounce the tanks off of each other (Easy to stop repeat collisions) (a as Tank).Damage(); //Damage both tanks (b as Tank).Damage(); } break; case "Bullet,Bullet": //If two bullets collide, destroy them both { a.Destroy(); b.Destroy(); } break; case "Turret,Tank": //If a turret collides with a tank, bounce the tanks, and damage the turret's tank. { Bounce(a.Parent, b); (a.Parent as Tank).Damage(); } break; case "Tank,Turret": //Inverted case of previous { Bounce(a, b.Parent); (b.Parent as Tank).Damage(); } break; case "Turret,Bullet": //If a turret collides with a bullet, damage the turret's tank, destroy the bullet { (a.Parent as Tank).Damage(10); b.Destroy(); } break; case "Bullet,Turret": //Inverted case of previous { a.Destroy(); (b.Parent as Tank).Damage(10); } break; case "Tank,Bullet": //If a tank collides with a bullet, damage the tank, destroy the bullet { (a as Tank).Damage(10); b.Destroy(); } break; case "Bullet,Tank": //Inverted case of previous { a.Destroy(); (b as Tank).Damage(10); } break; default: //If anything else Console.WriteLine("Unexpected collision between " + a.GetType().Name + " and " + b.GetType().Name); //Record unexepected collision break; } ; } Collisions = null; //Clear the current list of collisions }