/* * This function resizeds the array to a certain size. It takes the new size of the array * as its argument. */ void ResizeDefenseTower(int newSize) { //create a copy for the current list DefenseTower[] copy = new DefenseTower[newSize]; if (newSize > defenseTowers.Length) { for (int i = HEAD, n = defenseTowers.Length; i < n; i++) { copy[i] = defenseTowers[i]; } } else { for (int i = HEAD; i < newSize; i++) { copy[i] = defenseTowers[i]; } } //resize the original array defenseTowers = new DefenseTower[newSize]; //transfer what's on the copy to the original array for (int i = HEAD; i < newSize; i++) { defenseTowers[i] = copy[i]; } }
/* * This function does the responds when the user clicks on the from after moving the mouse. It takes * the mouse evnet argument from main as its argument. */ public void MouseClickMoving(MouseEventArgs e) { //here, the user must been moving the defense tower if (movingDefenseTower == true) { //if the user clicks on the place that cannot place items, terminte the function if (defenseTowerList.CheckPlacingItems(initialRectangle.Location) == false) { return; } if (UI.background.CheckPlacingItem(e.Location) == false) { return; } //if the user places item on an approprate position, set the tower, so the tower won't be //moving anymore Background.placingDefenseTower = false; //set the projectile information of the temporary tower tempTower.SetProjectile(); //set the temporary tower to placed tempTower.towerPlaced = true; //copy the temporary tower to an element of the defense tower list defenseTowerList.AddDefenseTower(tempTower); //clear the temporary tower tempTower = null; //now, the user must not be moving the defense tower movingDefenseTower = false; } }
/* * This function adds one projectile to the list. It takes the new projectile that user * wants to add as its argument */ public void AddDefenseTower(DefenseTower toPlace) { //resize the list to one more of its length ResizeDefenseTower(defenseTowers.Length + 1); //initialize the lastest elemnt defenseTowers[defenseTowers.Length - 1] = toPlace; //update the money the user has GUI.UI.user.MoneyLoss(toPlace.cost); }
/* * This function allows user to click for a new defense tower */ public void MouseClickNew(MouseEventArgs e, Widget button) { //if the defense tower is not moving if (movingDefenseTower == false) { //let the defense tower to move movingDefenseTower = true; //initialize the temperory tower depending on the type user clicks if ((button as BosenQuFelixLiuFinalCulminatingProject.GUI.Button).ButtonFunction == UIButtonFunctions.Tower1) { tempTower = new DefenseTower(new RectangleF(e.Location, tileSize), DefenseTowerType.Normal); } else if ((button as BosenQuFelixLiuFinalCulminatingProject.GUI.Button).ButtonFunction == UIButtonFunctions.Tower2) { tempTower = new DefenseTower(new RectangleF(e.Location, tileSize), DefenseTowerType.LongRange); } else if ((button as BosenQuFelixLiuFinalCulminatingProject.GUI.Button).ButtonFunction == UIButtonFunctions.Tower3) { tempTower = new DefenseTower(new RectangleF(e.Location, tileSize), DefenseTowerType.Missile); } //tell the background class the user is placing tower Background.placingDefenseTower = true; } }