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; } }); }
/// <summary> /// Removes a unit from this case. /// </summary> /// <param name="unit"></param> public virtual void RemoveUnit(Unit unit) { units.Remove(unit); OnPropertyChanged("Units"); }
/// <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); }
/// <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; }
/// <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(); }
/// <summary> /// The player's unit that must attack. /// </summary> /// <param name="u"></param> public AttackCommand(Unit u) { _unit = u; }
/// <summary> /// Removes a unit. /// </summary> /// <param name="unit"></param> public void RemoveUnit(Unit unit) { units.Remove(unit); OnPropertyChanged("Units"); if (LoseCondition()) { Lose(); } }
/// <summary> /// Adds a unit. /// </summary> /// <param name="unit"></param> public void AddUnit(Unit unit) { units.Add(unit); OnPropertyChanged("Units"); }
/// <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; }
/// <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; }