public Button(float x, float y, float width, float height, string name)
 {
     location = new PointF(x, y);
     this.width = width;
     this.height = height;
     this.name = name;
 }
 /// <summary>
 /// Give selected units a move command
 /// </summary>
 /// <param name="point">Destination</param>
 public void MoveSelectedUnitsToPoint(PointF point)
 {
     List<ModelComponent> selectedEntities = ((XnaUITestGame)Game).Model.GetSelectionState().SelectedEntities;
     // Ensure each component is a Unit.
     bool allAreUnits = true;
     bool playerEntities = false;
     foreach (ModelComponent component in selectedEntities)
     {
         if (entityBelongsToPlayer(component))
         {
             playerEntities = true;
             if (!(component is UnitComponent))
             {
                 allAreUnits = false;
                 break;
             }
         }
     }
     if (allAreUnits && playerEntities)
     {
         foreach (UnitComponent unit in selectedEntities)
         {
             MoveAction moveAction = new MoveAction(point.X, point.Y,getMap() , unit);
             ActionQueue aq = unit.GetActionQueue();
             aq.GetChildren().Clear();
             aq.AddChild(moveAction);
         }
     }
 }
        /// <summary>
        /// find the closest cell from the specified destination
        /// </summary>
        /// <param name="point">Destination</param>
        /// <returns>Closest cell</returns>
        private CellComponent findClosestCell(PointF point)
        {
            double distanceSquared = Math.Pow(map.GetWidth(), 2.0) + Math.Pow(map.GetHeight(), 2.0);
            CellComponent cell = null;
            for (int i = Math.Max((int)building.PointLocation.X - 1, 0); i <= building.PointLocation.X + building.Width + 1; i++)
            {
                for (int j = Math.Max((int)building.PointLocation.Y - 1, 0); j <= building.PointLocation.Y + building.Height + 1; j++)
                {
                    // Ignore cases in the middle of the proposed building site.
                    if (!(i >= (int)building.PointLocation.X && i < (int)building.PointLocation.X + building.Width && j >= (int)building.PointLocation.Y && j < (int)building.PointLocation.Y + building.Height))
                    {
                        double calculatedDistanceSquared = findDistanceSquared(point.X, point.Y, i, j);
                        if (calculatedDistanceSquared <= distanceSquared)
                        {
                            if (map.GetCellAt(i, j) != null)
                            {
                                cell = map.GetCellAt(i, j);
                                distanceSquared = calculatedDistanceSquared;
                            }
                        }
                    }
                }
            }

            return cell;
        }
 /// <summary>
 /// Tell selected units to harvest (TODO)
 /// </summary>
 public void TellSelectedUnitsToHarvest(PointF destination)
 {
     List<ModelComponent> selectedEntities = getGameModel().GetSelectionState().SelectedEntities;
     foreach (ModelComponent entity in selectedEntities)
     {
         if (entityBelongsToPlayer(entity))
         {
             if (!(entity is UnitComponent))
             {
                 break;
             }
             else
             {
                 UnitComponent u = entity as UnitComponent;
                 if (!u.CanHarvest)
                 {
                     continue;
                 }
                 else
                 {
                     System.Console.Out.WriteLine("NOT IMPLEMENT YET: harvest at " + destination.X + " : " + destination.Y);
                 }
             }
         }// if
     }
 }
        private void stopListeningToCells(PointF oldPoint)
        {
            int startX = getStartX();
            int endX = getEndX();
            int startY = getStartX();
            int endY = getEndY();

            ModelComponent gameWorldComponent = location;
            while (!(gameWorldComponent is Gameworld))
                gameWorldComponent = gameWorldComponent.Parent;
            Map map = ((Gameworld)(gameWorldComponent)).GetMap();

            for (int i = startX; i < endX; i++)
            {
                for (int j = startY; j < endY; j++)
                {
                    CellComponent cell = map.GetCellAt(i, j);
                    cell.UnitAddedEvent -= new EntityInCellChangedHandler(handleUnitAddedToCell);
                }
            }
        }
 /// <summary>
 /// Determines if two entities will overlap each other.  This is called before committing any change to the model to ensure that the moving/new entity
 /// does not in any way overlap with any other entities on the map.
 /// </summary>
 /// <param name="e"></param>
 /// <returns></returns>
 public bool Overlaps(Entity e)
 {
     PointF offset = new PointF(e.PointLocation.X - pointLocation.X, e.PointLocation.Y - pointLocation.Y);
     return shape.Overlaps(e.Shape, offset);
 }
 /// <summary>
 /// Trigger function in the event of selected unit is told to harvest
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void tellSelectedUnitsToHarvestAt(object sender, XnaMouseEventArgs e)
 {
     if (e.Bubbled && !e.Handled && e.ButtonPressed == MouseButton.Right)
     {
         PointF gamePoint = new PointF((float)(e.ClickLocation.X + ScrollX) / (float)cellDimension, (float)(e.ClickLocation.Y + ScrollY) / (float)cellDimension);
         ((XnaUITestGame)Game).Controller.TellSelectedUnitsToHarvest(gamePoint);
         e.Handled = true;
     }
 }
 /// <summary>
 /// Tell selected units to harvest (TODO)
 /// </summary>
 public void TellSelectedUnitsToHarvest(PointF destination)
 {
     List<ModelComponent> selectedEntities = getGameModel().GetSelectionState().SelectedEntities;
     foreach (ModelComponent entity in selectedEntities)
     {
         if (entityBelongsToPlayer(entity))
         {
             if (!(entity is UnitComponent))
             {
                 break;
             }
             else
             {
                 UnitComponent u = entity as UnitComponent;
                 if (!u.CanHarvest)
                 {
                     continue;
                 }
                 else
                 {
                     // DO SOMETHING TO HARVEST!!!!!
                     System.Console.Out.WriteLine("NOT IMPLEMENT YET: harvest at " + destination.X + " : " + destination.Y);
                     //u.GetActionQueue().GetChildren().Clear();
                     //u.GetActionQueue().AddChild(new AttackAction
                 }
             }
         }// if
     }
     //throw new NotImplementedException();
 }
 private void updateUnitOrientation()
 {
     PointF directionVector = new PointF((target.PointLocation.X - unit.PointLocation.X), (target.PointLocation.Y - unit.PointLocation.Y));
     //unit.Orientation = (int)Math.Atan2((double)directionVector.Y, (double)directionVector.X);
 }
        /// <summary>
        /// Move the unit towards the center of targetCell.
        /// </summary>
        private void moveMiddle(UnitComponent unit)
        {
            float speed = (unit.Speed);

            if (path[0].Y + CENTER > unit.PointLocation.Y)
            {
                if (path[0].X + CENTER> unit.PointLocation.X)
                {
                    unit.UnitOrient = UnitComponent.Orient.SE;
                }
                else if (path[0].X + CENTER < unit.PointLocation.X)
                {
                    unit.UnitOrient = UnitComponent.Orient.SW;
                }
                else
                {
                    unit.UnitOrient = UnitComponent.Orient.S;
                }
            }
            else if (path[0].Y + CENTER < unit.PointLocation.Y)
            {
                if (path[0].X + CENTER> unit.PointLocation.X)
                {
                    unit.UnitOrient = UnitComponent.Orient.NE;
                }
                else if (path[0].X + CENTER< unit.PointLocation.X)
                {
                    unit.UnitOrient = UnitComponent.Orient.NW;
                }
                else
                {
                    unit.UnitOrient = UnitComponent.Orient.N;
                }
            }
            else if (path[0].X + CENTER< unit.PointLocation.X)
            {
                unit.UnitOrient = UnitComponent.Orient.W;
            }
            else if (path[0].X + CENTER> unit.PointLocation.X)
            {
                unit.UnitOrient = UnitComponent.Orient.E;
            }

            // If we are within the range of the destination point, simply move there
            if (Math.Sqrt(Math.Pow(path[0].Y + CENTER - unit.PointLocation.Y, 2) + Math.Pow(path[0].X + CENTER - unit.PointLocation.X, 2)) <= speed)
            {
                PointF directionVector = new PointF(path[0].X + CENTER - unit.PointLocation.X, path[0].Y + CENTER - unit.PointLocation.Y);
                // We are within |unit.speed| of the targetCell's center, set unit's position to center.
                unit.PointLocation = new PointF(path[0].X + CENTER, path[0].Y + CENTER);

            }
            else
            {
                PointF directionVector = new PointF(path[0].X + CENTER - unit.PointLocation.X, path[0].Y + CENTER - unit.PointLocation.Y);
                float magnitude = (float)Math.Sqrt(Math.Pow((double)directionVector.X, 2.0) + Math.Pow((double)directionVector.Y, 2.0));
                directionVector.X = directionVector.X / magnitude * unit.Speed;
                directionVector.Y = directionVector.Y / magnitude * unit.Speed;
                unit.PointLocation = new PointF(unit.PointLocation.X + directionVector.X, unit.PointLocation.Y + directionVector.Y);
            }
        }