public AutomaticCommander(string commanderName, CommanderType commanderNewType, Unit unit) { name = commanderName; commanderType = commanderNewType; commandUnit = unit; currentBattleLosses = 0; firstComposition = unit.Properties.ActiveCount(); }
/// <summary> /// Делает выводы о целесообразности продолжения сражения. /// </summary> /// <param name="enemy"> Юнит, с которым идет сражение</param> /// <returns>true если сражение надо продолжить.</returns> public bool ContinueBattleDecision(Unit enemy) { if ((double)currentBattleLosses / (double)firstComposition > GetParameters()[0]) { return false; } else return true; }
/// <summary> /// Начинает бой между Unit'ами /// </summary> /// <param name="enemy">Юнит, с которым будет начато сражение</param> public void Attack(Unit enemy) { var attack_action = new AttackAction(commandUnit, enemy); if (enemy.CurrentAction != null) enemy.CurrentAction = null; GameEngine.Game.SendOrder(commandUnit, attack_action); }
/// <summary> /// Отступление /// </summary> /// <param name="enemy">Unit отступает по направлению, противоположную к направлению на противника.</param> public void Retreat(Unit enemy) { var point = new Geometry.Figures.Point(commandUnit.Position.X - enemy.Position.X, commandUnit.Position.Y - enemy.Position.Y); double t = 0; t = Math.Sqrt((commandUnit.Properties.CurSpeed * commandUnit.Properties.CurSpeed) / (point.X * point.X + point.Y * point.Y)); Geometry.Figures.Polyline poly = new Geometry.Figures.Polyline(commandUnit.Position.X, commandUnit.Position.Y, commandUnit.Position.X + t * point.X, commandUnit.Position.Y + t * point.Y); if (this.commandUnit.CurrentAction != null) { this.commandUnit.CurrentAction.Completed = true; this.commandUnit.CurrentAction = null; } Reset(); GameEngine.Game.SendOrder(commandUnit, new MoveAction(commandUnit, poly)); }
public List<UnitState> Leaving(Unit enemy) { var point = new Geometry.Figures.Point(commandUnit.Position.X - enemy.Position.X, commandUnit.Position.Y - enemy.Position.Y); double t = 0; t = Math.Sqrt((commandUnit.Properties.CurSpeed * commandUnit.Properties.CurSpeed) / (point.X * point.X + point.Y * point.Y)); Geometry.Figures.Polyline poly = new Geometry.Figures.Polyline(commandUnit.Position.X, commandUnit.Position.Y, commandUnit.Position.X + t * point.X, commandUnit.Position.Y + t * point.Y); //GameEngine.Game.SendOrder(adm, new MoveAction(adm, poly)); //return new UnitState return new List<UnitState> { new UnitState(poly.Points.Last(), 0, StateAction.Move), new UnitState(poly.Points.Last(), 0, StateAction.Move) }; }
/// <summary> /// Атака войска противника несколькими дружественными войсками /// </summary> /// <param name="allies"></param> /// <param name="enemy"></param> public void MassAttack(List<Unit> allies, Unit enemy) { allies.Add(commandUnit); var combAttack = new CombinedAttackAction(allies, enemy); GameEngine.Game.SendOrder(commandUnit, combAttack); }
public static void Build(Unit unit) { MouseOrderBuild(unit); }
/// <summary> /// Метод десерилизации для Местности /// Определяет местность по имени и считывает все фигуры этой местности. /// Если местность не может быть определена или не существует, то бросает исключительную ситуацию. /// </summary> /// <param name="xelement">Xml Element</param> /// <returns>Местность</returns> public static object Deserialize(this XElement xelement) { var name = xelement.Name.ToString(); switch (name) { case "City": return new City(xelement.Elements().Deserialize()); case "Sand": return new Sand(xelement.Elements().Deserialize()); case "Road": return new Road(xelement.Elements().Deserialize()); case "Mountains": return new Mountains(xelement.Elements().Deserialize()); case "Water": return new Water(xelement.Elements().Deserialize()); case "Lowland": return new Lowland(xelement.Elements().Deserialize()); case "Field": return new Field(xelement.Elements().Deserialize()); case "Forest": return new Forest(xelement.Elements().Deserialize()); case "Unit": var xpolygon = xelement.Element("ConvexPolygon"); var polygon = ((new List<XElement> { xpolygon }).Deserialize()[0]) as ConvexPolygon; var xgroups = xelement.Element("Groups"); var groups = new List<Group>(); if (xgroups != null) { foreach (var xgroup in xgroups.Elements()) { var rank = (Rank)((Enum.Parse(typeof(Rank), (string)xgroup.Attribute("Rank")))); var specialization = (Specialization)((Enum.Parse(typeof(Specialization), (string)xgroup.Attribute("Specialization")))); var qualification = (Qualification)((Enum.Parse(typeof(Qualification), (string)xgroup.Attribute("Qualification")))); var experience = (Qualification)((Enum.Parse(typeof(Qualification), (string)xgroup.Attribute("Experience")))); var vitality = (Vitality)((Enum.Parse(typeof(Vitality), (string)xgroup.Attribute("Vitality")))); groups.Add(new Group((int)xgroup.Attribute("Count"), rank, specialization, qualification, experience, vitality)); } } var xitems = xelement.Element("Items"); var items = new List<Item>(); if (xitems != null) { foreach (var xitem in xitems.Elements()) { switch (xitem.Name.ToString()) { case "Goods": var objecttype = (ObjectType)((Enum.Parse(typeof(ObjectType), (string)xitem.Attribute("ItemType")))); items.Add(new Goods(objecttype, (int)xitem.Attribute("Count"), new Circle(0, 0, 0))); break; case "EquipmentMark": var weapon = (Caliber)((Enum.Parse(typeof(Caliber), (string)xitem.Attribute("Weapon")))); var armor = (Caliber)((Enum.Parse(typeof(Caliber), (string)xitem.Attribute("Armor")))); items.Add(new EquipmentMark(weapon, armor, (double)(xitem.Attribute("MoveSpeed")), (double)(xitem.Attribute("FireRate")), (int)(xitem.Attribute("Crew")), (int)(xitem.Attribute("Count")))); break; } } } var unit = new Unit(polygon); var features = new UnitFeatures(groups, items); features.CurSpeed = (double)xelement.Attribute("CurSpeed"); features.Visible = (double)xelement.Attribute("Visible"); features.CurVisible = (double)xelement.Attribute("CurVisible"); features.Speed = (double)xelement.Attribute("Speed"); unit.Properties = features; unit.SetSide((int)xelement.Attribute("Side"), Countries.USSR, null); // todo do this better var unittype = xelement.Attribute("UnitType"); unit.UnitType = (UnitType)((Enum.Parse(typeof(UnitType), (string)unittype))); // unit. return unit; default: throw new Exception("Not existing Landscape used as parameter in deserializing function. Exception name: " + name); } }
private static void MouseOrderBuild(Unit unit) { GameEngine.Game.SendOrder(unit, new BuildAction(unit, new Goods(ObjectType.Structures, 1, new Geometry.Figures.Circle(unit.Position, 1)))); }
public UnitPresenter(Unit u) { this.original = u; Unit = u; Country = (u.Side as Team).Country; Position = new FigurePresenter(u.Polygon, u); Name = u.Properties.Name; Abbreviation = Name; // todo correct this Update(); }
/// <summary> /// Проверка возможности начать атаку по противнику. /// </summary> /// <param name="defender">Цель нападения.</param> /// <returns>true если возможность напасть есть.</returns> public bool CanAttack(Unit defender) { var distance = 0.0; var positionAtUnit = Position; var positionDefUnit = defender.Position; distance = Point.Length(positionAtUnit, positionDefUnit); return Properties.Visible > distance; }
/// <summary> /// Метод посылки приказа Юниту /// </summary> /// <param name="unit">Кому приказ</param> /// <param name="task">Приказ</param> /// <returns>Успешно ли получен приказ</returns> public static bool SendOrder(Unit unit, Action task) { if (unit.CurrentAction == null) { unit.CurrentAction = task; return true; } else { if (unit.CurrentAction.Completed) { unit.CurrentAction = task; return true; } } return false; }
/// <summary> /// Находит все юниты, нападающие на defender /// </summary> /// <param name="defender">Обороняющийся юнит</param> /// <returns>Список атакующих юнитов, нападающих на defender</returns> public static List<Unit> Attackers(Unit defender) { //for (int i = 0; i < List<Unit> attackers = new List<Unit>(); for (int i = 0; i < mainMap.Units.Count; i++) { var unit = mainMap.Units[i]; if (unit.CurrentAction != null) { if (unit.CurrentAction.GetType().Name == "AttackAction") { if ((unit.CurrentAction as AttackAction).Defender == defender) attackers.Add(unit); } } } return attackers; }