コード例 #1
0
 public Collision(SceneObject a, SceneObject b)
 {
     this.a = a;
     this.b = b;
 }
コード例 #2
0
 public Collision(SceneObject a)
 {
     this.a = a;
     this.b = null;
 }
コード例 #3
0
        //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
        }