Example #1
0
        public UnitView(Unit unit)
        {
            Unit = unit;
            Attack = unit.AttackTotal;
            Defense = unit.DefenseTotal;
            HitPoints = unit.HitPoints;
            MovementPoints = unit.MovementPoints;
            RemainingHitPoints = unit.RemainingHitPoints;
            RemainingMovementPoints = unit.RemainingMovementPoints;
            Type = unit.ToString();

            unit.PropertyChanged += new PropertyChangedEventHandler(delegate(object sender, PropertyChangedEventArgs args)
            {
                switch (args.PropertyName)
                {
                    case "RemainingMovementPoints":
                        RemainingMovementPoints = ((Unit)sender).RemainingMovementPoints;
                        break;
                    case "RemainingHitPoints":
                        RemainingHitPoints = ((Unit)sender).RemainingHitPoints;
                        break;
                    default:
                        break;
                }
            });
        }
Example #2
0
 /// <summary>
 /// Removes a unit from this case.
 /// </summary>
 /// <param name="unit"></param>
 public virtual void RemoveUnit(Unit unit)
 {
     units.Remove(unit);
     OnPropertyChanged("Units");
 }
Example #3
0
 /// <summary>
 /// Returns true if the case contains the given unit.
 /// </summary>
 /// <param name="unit"></param>
 /// <returns></returns>
 public virtual bool Contains(Unit unit)
 {
     return units.Contains(unit);
 }
Example #4
0
        /// <summary>
        /// Adds a unit on the case.
        /// </summary>
        /// <param name="unit">The new unit to occupy the case.</param>
        public virtual void AddUnit(Unit unit)
        {
            if (HasCity && Occupant != unit.Player)
            {
                // If an enemy city is built on this case, invade it.
                _city.Invade(unit.Player);
            }
            else if (IsUsed && Occupant != unit.Player)
            {
                // If the case is used as a field by an enemy, sack it.
                _city.RemoveField(this);
                Free();
            }

            this.units.Add(unit);
            OnPropertyChanged("Units");
            Occupant = unit.Player;
        }
Example #5
0
        /// <summary>
        /// Builds a city on the case on which the given teacher stands.
        /// Note: this should not be in this class. Refactoring needed.
        /// </summary>
        /// <param name="name">Name of the city.</param>
        /// <param name="teacher">Teacher building the city.</param>
        public void BuildCity(Unit teacher)
        {
            Player player = teacher.Player;
            Case position = teacher.Location;

            City city = new City(position, player, map.TerritoryAround(position, City.Radius));

            position.BuildCity(city);
            player.AddCity(city);

            // The teacher sacrifices himself to build the city (may he rest in peace).
            teacher.Kill();
        }
Example #6
0
 /// <summary>
 /// The player's unit that must attack.
 /// </summary>
 /// <param name="u"></param>
 public AttackCommand(Unit u)
 {
     _unit = u;
 }
Example #7
0
 /// <summary>
 /// Removes a unit.
 /// </summary>
 /// <param name="unit"></param>
 public void RemoveUnit(Unit unit)
 {
     units.Remove(unit);
     OnPropertyChanged("Units");
     if (LoseCondition())
     {
         Lose();
     }
 }
Example #8
0
 /// <summary>
 /// Adds a unit.
 /// </summary>
 /// <param name="unit"></param>
 public void AddUnit(Unit unit)
 {
     units.Add(unit);
     OnPropertyChanged("Units");
 }
Example #9
0
 /// <summary>
 /// Note: ideally, this class should not be coupled to Game. Refactoring needed.
 /// </summary>
 /// <param name="game"></param>
 /// <param name="unit"></param>
 public MoveUnitCommand(Game game, Unit unit)
 {
     _unit = unit;
     _game = game;
 }
Example #10
0
        /// <summary>
        /// Starts an attack on another unit.
        /// </summary>
        /// <param name="opponent"></param>
        /// <returns></returns>
        protected bool Attack(Unit opponent)
        {
            if (opponent.DefenseTotal == 0)
            {
                opponent.Kill();
            }

            int hits = Math.Max(RemainingHitPoints, opponent.RemainingHitPoints) + 2;
            int rounds = Game.Game.random.Next(3, hits);

            for (int i = 0; i < rounds; i++)
            {
                double ratio = AttackTotal / opponent.DefenseTotal;
                double proba = 0.5 * ratio;

                if (Game.Game.random.NextDouble() < proba)
                {
                    if (--opponent.RemainingHitPoints == 0)
                    {
                        opponent.Kill();
                        return true;
                    }
                }
                else
                {
                    if (--RemainingHitPoints == 0)
                    {
                        Kill();
                        return false;
                    }
                }
            }
            return false;
        }