using System.Collections.Generic; using Microsoft.Xna.Framework; using DWGame.Globals; namespace DWGame.Player { public class Collision { //player ship public static Human.playerShip human; //enviroment rectangles used for collision decection public static Rectangle[] frgrnd; //lists of player missles, enemy missles, and enemies fighters public static List hMissles; public static List enemies; public static List eMissles; //constructor public Collision() { human = Human.getShip(); hMissles = Human.getMissles(); frgrnd = Background.getFrgrnd(); enemies = AI.getFighters(); eMissles = AI.getMissles(); } //method used to detect collisions between player and world, player and enemies, //enemies and player missles, player and enemy missles, and missle to missle. // Upon detection, this method removes enemies and missles from their lists, as well //as changing player state to dead if player collides with other objects public static void Update() { human = Human.getShip(); hMissles = Human.getMissles(); frgrnd = Background.getFrgrnd(); enemies = AI.getFighters(); eMissles = AI.getMissles(); for (int i = 0; i < frgrnd.Length; i++) { for (int j = 0; j < hMissles.Count; j++) { if (frgrnd[i].Intersects(hMissles[j].mBox)) hMissles.RemoveAt(j); } for (int j = 0; j < eMissles.Count; j++) { if (frgrnd[i].Intersects(eMissles[j].mBox)) eMissles.RemoveAt(j); } for (int k = 0; k < enemies.Count; k++) { for (int l = 0; l < hMissles.Count; l++) { if ( k < enemies.Count && (hMissles[l].mBox.Intersects(enemies[k].enemyBox)) ) { enemies.RemoveAt(k); hMissles.RemoveAt(l); human.score = human.score + 100; } } if (k < enemies.Count && (enemies[k].enemyBox.Intersects(human.playerBox))) { enemies.RemoveAt(k); Statics.playerState = Enums_Engine.PlayerState.DEAD; } } if (frgrnd[i].Intersects(human.playerBox)) { Statics.playerState = Enums_Engine.PlayerState.DEAD; } } for (int i = 0; i < eMissles.Count; i++) { if (eMissles[i].mBox.Intersects(human.playerBox)) { eMissles.RemoveAt(i); Statics.playerState = Enums_Engine.PlayerState.DEAD; } for (int j = 0; j < hMissles.Count; j++) { if (i < eMissles.Count && eMissles[i].mBox.Intersects(hMissles[j].mBox)) { eMissles.RemoveAt(i); hMissles.RemoveAt(j); human.score = human.score + 1; } } } } //method to pass missle list back to Human public static List getHMissles() { return hMissles; } //method to pass enemy missle list back to AI public static List getEMissles() { return eMissles; } //method used to pass player score back to Human public static int getScore() { return human.score; } } }