private void UnitRemovedFromCell(Object sender, UnitArgs args)
 {
     for (int i = 0; i < Controls.Count; )
     {
         if (Controls[0] != null)
             Controls[0].Dispose();
     }
 }
 private void UnitAddedToCell(Object sender, UnitArgs args)
 {
     for (int i = 0; i < Controls.Count; )
     {
         if (Controls[0] != null)
             Controls[0].Dispose();
     }
     UnitUI unitUI = new UnitUI(controller, args.Unit);
     Controls.Add(unitUI);
     unitUI.MouseClick += TileUI_MouseDown;
 }
 private void handleUnitAddedToCell(object obj, UnitArgs e)
 {
     if (e.Unit == this) // I'm seeing my own move event.
     {}
     else // Saw another Unit added to a CellComponent.
     {
         if (unitIsAnEnemy(e.Unit) && this.AttackStance == UnitAttackStance.Aggressive && this.actionQueue.GetChildren().Count == 0)
         {
             ModelComponent temp = Parent;
             while (!(temp is Gameworld))
             {
                 temp = temp.Parent;
             }
             AttackAction attackAction = new AttackAction(this, e.Unit,(Gameworld)temp);
             actionQueue.AddChild(attackAction);
         }
     }
 }
        /// <summary>
        /// Adds a Component to this CellComponent.
        /// </summary>
        /// <param name="child">The Component to add</param>
        public override void AddChild(ModelComponent child)
        {
            // Ensure that we only have one tile or resource
            List<ModelComponent> toRemove =  new List<ModelComponent>();
            if (child is Tile)
            {
                foreach (ModelComponent tile in GetChildren())
                {
                    if (tile is Tile)
                    {
                        toRemove.Add(tile);
                    }
                }
            }
            else if (child is MapResource)
            {
                foreach (ModelComponent resource in GetChildren())
                {
                    if (resource is MapResource)
                    {
                        toRemove.Add(resource);
                        if (entitiesContainedWithin.Count != 0)
                        {
                            UnitArgs args = new UnitArgs();
                            args.Unit = (UnitComponent)entitiesContainedWithin[0];
                            entitiesContainedWithin.Clear();
                            if (UnitRemovedEvent != null)
                                UnitRemovedEvent(this, args);
                        }
                    }
                }
                entitiesContainedWithin.Add(child);
            }
            foreach (ModelComponent component in toRemove)
            {
                RemoveChild(component);
            }
            base.AddChild(child);

            // Handle notifications
            if (child is Tile)
            {
                if (TileChangedEvent != null)
                {
                    TileChangedEvent(this, new TileChangedEventArgs((Tile)child));
                }
            }
        }
 /// <summary>
 /// Removes an Entity from this Cell.
 /// </summary>
 /// <param name="entity">The Entity to remove</param>
 public void RemoveEntity(ModelComponent entity)
 {
     entitiesContainedWithin.Remove(entity);
     if (entity is UnitComponent)
     {
         UnitArgs args = new UnitArgs();
         args.Unit = (UnitComponent)entity;
         args.Unit.UnitAttackedEnemyHanlders -= new UnitAttackedEnemyHandler(handleUnitInCellAttackingEnemy);
         if (UnitRemovedEvent != null)
             UnitRemovedEvent(this, args);
     }
 }
        /// <summary>
        /// Add game entity to this cell
        /// </summary>
        /// <param name="entity">The Entity to add</param>
        public void AddEntity(ModelComponent entity)
        {
            if (entitiesContainedWithin.Count == 0)
            {
                if (entity is UnitComponent || entity is MapResource || entity is Building)
                {
                    entitiesContainedWithin.Add(entity);

                    if (entity is UnitComponent)
                    {
                        UnitArgs args = new UnitArgs();
                        args.Unit = (UnitComponent)entity;
                        args.Unit.UnitAttackedEnemyHanlders += new UnitAttackedEnemyHandler(handleUnitInCellAttackingEnemy);
                        if (UnitAddedEvent != null)
                        {
                            UnitAddedEvent(this, args);
                        }
                    }
                    if (entity is Building)
                    {
                        // do nothing special?
                    }
                }
            }
        }