public void SpawnUnit(int PlayerIndex, UnitMap NewUnit, Vector3 SpawnPosition, Vector3 SpawnDestination) { NewUnit.Unit3D = new UnitMap3D(GameScreen.GraphicsDevice, Content.Load<Effect>("Shaders/Squad shader 3D"), NewUnit.ActiveUnit.SpriteMap, 1); NewUnit.ActiveUnit.Init(); ListPlayer[PlayerIndex].ListUnit.Add(NewUnit); NewUnit.SetPosition(SpawnPosition); Vector3 FinalPosition; GetEmptyPosition(SpawnDestination, out FinalPosition); MovementAnimation.Add(NewUnit.X, NewUnit.Y, NewUnit); NewUnit.SetPosition(FinalPosition); }
/// <summary> /// Try to attack with Weapon 1. /// </summary> /// <returns>Returns true if success.</returns> public bool AIAttackWithWeapon1(UnitConquest ActiveUnit) { int PosX = (int)ActiveUnit.X; int PosY = (int)ActiveUnit.Y; Attack ActiveWeapon = ActiveUnit.ListAttack[0]; List <Tuple <int, int> > ListDefendingSquad = CanSquadAttackWeapon1(PosX, PosY, ActivePlayerIndex, ActiveUnit.FullName, ActiveUnit.ListAttack[1]); if (!ActiveUnit.CanMove && !((ActiveWeapon.Sec & WeaponSecondaryProperty.PostMovement) == WeaponSecondaryProperty.PostMovement || ActiveUnit.Boosts.PostMovementModifier.Attack)) { return(false); } //Define the minimum and maximum value of the attack range. int MinRange = ActiveWeapon.RangeMinimum; int MaxRange = ActiveWeapon.RangeMaximum; float Distance; if (MaxRange > 1) { MaxRange += ActiveUnit.Boosts.RangeModifier; } #region Can attack if (ListDefendingSquad.Count > 0) { UnitConquest TargetSquad = ListPlayer[ListDefendingSquad[0].Item1].ListUnit[ListDefendingSquad[0].Item2]; //Prepare the Cursor to move. CursorPosition.X = ActiveUnit.X; CursorPosition.Y = ActiveUnit.Y; GetAttackChoice(ActiveUnit, ActiveUnit.Position); ActiveUnit.BattleDefenseChoice = Unit.BattleDefenseChoices.Attack; GetAttackDamageWithWeapon1(ActiveUnit, TargetSquad, ActiveUnit.HP); return(true);//Exit the weapon loop. } #endregion #region Can't attack directly //If it's a post-movement weapon or you can still move. else if (ActiveUnit.CanMove && ((ActiveWeapon.Sec & WeaponSecondaryProperty.PostMovement) == WeaponSecondaryProperty.PostMovement || ActiveUnit.Boosts.PostMovementModifier.Attack)) {//check if there is an enemy too close to be attacked but that could be attacked after moving. for (int P = 0; P < ListPlayer.Count; P++) { //If the player is from the same team as the current player or is dead, skip it. if (ListPlayer[P].Team == ListPlayer[ActivePlayerIndex].Team || !ListPlayer[P].IsAlive) { continue; } for (int TargetSelect = 0; TargetSelect < ListPlayer[P].ListUnit.Count; TargetSelect++) { UnitConquest TargetSquad = ListPlayer[P].ListUnit[TargetSelect]; Distance = Math.Abs(PosX - TargetSquad.X) + Math.Abs(PosY - TargetSquad.Y); //Check if you can attack it if you moved. if (Distance >= MinRange - GetSquadMaxMovement(ActiveUnit) && Distance <= MaxRange + GetSquadMaxMovement(ActiveUnit)) { ListDefendingSquad.Add(new Tuple <int, int>(P, TargetSelect)); } } } //If something was found. if (ListDefendingSquad.Count > 0) { int RandomNumber = RandomHelper.Next(ListDefendingSquad.Count); //Select a target. UnitConquest TargetSquad = ListPlayer[ListDefendingSquad[RandomNumber].Item1].ListUnit[ListDefendingSquad[RandomNumber].Item2]; float DistanceUnit = Math.Abs(PosX - TargetSquad.X) + Math.Abs(PosY - TargetSquad.Y); //Move to be in range. List <Vector3> ListRealChoice = new List <Vector3>(GetMVChoice(ActiveUnit)); for (int M = 0; M < ListRealChoice.Count; M++) {//Remove every MV that would make it impossible to attack. Distance = Math.Abs(ListRealChoice[M].X - TargetSquad.X) + Math.Abs(ListRealChoice[M].Y - TargetSquad.Y); //Remove every MV that would bring the Unit too close to use its weapon. if (DistanceUnit <= MinRange) { if (Distance <= MinRange) { ListRealChoice.RemoveAt(M--); } } //Check if you can attack it if you moved. else if (Distance < MinRange || Distance > MaxRange) { ListRealChoice.RemoveAt(M--); } } //Must find a spot to move if got there, just to make sure it won't crash in case of logic error. if (ListRealChoice.Count != 0) { int Choice = RandomHelper.Next(ListRealChoice.Count); //Movement initialisation. MovementAnimation.Add(ActiveUnit.X, ActiveUnit.Y, ActiveUnit.Components); //Prepare the Cursor to move. CursorPosition.X = ListRealChoice[Choice].X; CursorPosition.Y = ListRealChoice[Choice].Y; ActiveUnit.SetPosition(CursorPosition); ActiveUnit.SetPosition(ListRealChoice[Choice]); FinalizeMovement(ActiveUnit); } else { //Something is blocking the path. ActiveUnit.EndTurn(); } //Unit should be in attack range next time the AI is called. return(true);//Exit the weapon loop. } } #endregion return(false);//Can't attack at all. }