/// <summary>
        /// Checks whether a Unit can attack a Unit at the specified tile, and attacks it if it's possible.
        /// </summary>
        /// <param name="newPos">The position for the Unit to attack.</param>
        /// <param name="unit">The Unit which attacks.</param>
        /// <returns>True if the Unit attacked, false otherwise.</returns>
        public bool CheckAttackSoldier(Point tileToAttack, Soldier attackingUnit)
        {
            // Cannot attack more than once a turn.
            if (attackingUnit.HasAttacked)
            {
                return(false);
            }

            Tile toAttack = this[tileToAttack] as Tile;

            // Make sure the attack square is occupied by an enemy unit
            if (!toAttack.Occupied || toAttack.Unit.Owner == attackingUnit.Owner)
            {
                return(false); // nothing to attack
            }

            // Make sure the attack square is in range of the attacking unit
            if (!attackingUnit.IsInRange(tileToAttack))
            {
                return(false); // Enemy not in range
            }

            // We can actually attack this? Nice :D
            attackingUnit.Attack(toAttack, false);

            // After a battle, check if there are dead Units, and remove these if they are dead
            Soldier defender = toAttack.Unit as Soldier;


            return(true);
        }
Exemple #2
0
    public void UpdateState()
    {
        if (!mSoldier.IsDead)
        {
            mSoldier.MakeDecision();
            if (mSoldier.AttackTarget != null)
            {
                if (!mSoldier.AttackTarget.mBI.IsDestroyed && mSoldier.IsTargetInAttackRange())
                {
                    mSoldier.Attack();
                }
                else
                {
                    ToMoveState();
                }
            }
            else
            {
                ToDeadState();
            }

            /*
             * mSoldier.AttackTimer += Time.deltaTime;
             * if (mSoldier.AttackTimer >= mSoldier.mAttackInterval) {
             *      mSoldier.AttackTimer = 0.0f;
             */
            //}
        }
        else
        {
            ToDeadState();
        }
    }
    public override void Proceed(ControlledDivision Host)
    {
        if (!IsFinished && Host.FindVisibleDivision(RememberedTargetId, out ControlledDivision division))
        {
            var distanceToTarget = (division.Controller.transform.position - Host.Controller.transform.position).magnitude;
            if (distanceToTarget > Host.MaxHitRange)
            {
                IsFinished = true;
                return;
            }

            float totalAttackerDamage = 0;
            for (int i = 0; i < Host.Soldiers.Count; i++)
            {
                Soldier soldier = Host.Soldiers[i];

                if (distanceToTarget > soldier.MinRange && distanceToTarget < soldier.MaxRange)
                {
                    var result = soldier.Attack(ref division, distanceToTarget);
                    totalAttackerDamage += result.DamageToDefender;
                }
            }


            bool isDestroyed     = division.CheckDamageDone(Host);
            bool isHostDestroyed = Host.CheckDamageDone(division);
            if (isDestroyed)
            {
                RememberedDivision RememberedTarget = GetRememberedDivisionFromHost(Host, RememberedTargetId);
                RememberedTarget.HasBeenDestroyed = true;
                RememberedTarget.TimeStamp        = GameManager.Instance.GameTime;
                IsFinished = true;
            }
        }
    }
Exemple #4
0
    void Update()
    {
        if (!isAlive)
        {
            return;
        }

        time             = maxTime - (Time.time - startTime);
        towerBars.time   = time;
        towerBars.health = health;

        if (time < maxTime / 2.0f)
        {
            bool show = (int)((Time.time - startTime) * blinkSpeed) % 2 == 0;
            icon.enabled = show;

            if (!hasPlayedSFX)
            {
                hasPlayedSFX = true;
                unitSelection.sfxManager.PlaySound(unitSelection.sfxManager.halfwaySFX);
            }
        }
        else
        {
            icon.enabled = true;
        }

        if (time <= 0)
        {
            unitSelection.GameOver();
            isAlive = false;
        }

        AquireTarget();

        if (currentTarget)
        {
            laser.enabled       = true;
            laser.positionCount = 2;
            laser.SetPositions(new Vector3[] { laserStart.position, currentTarget.transform.position });
            currentTarget.Attack(attackPower * Time.deltaTime);
        }
        else
        {
            laser.enabled = false;
        }

        if (health <= 0)
        {
            isAlive      = false;
            icon.enabled = true;
            OnDeath();
            Destroy(towerBars.gameObject);
            Destroy(this.gameObject);
        }
    }
    public override void Exit()
    {
        elapsed_time = 0f;

        Soldier context = state_machine.GetContext();

        context.Attack();
        context.ChangeMaterial(false);
        first_time_using = false;
    }
Exemple #6
0
    IEnumerator RemoteAttack()
    {
        float time = soldier.Frequency;

        while (true)
        {
            time -= Time.deltaTime;
            while (RemoteTarget.Count > 0 && (RemoteTarget[0] == null || !RemoteTarget[0].activeSelf))
            {
                RemoteTarget.RemoveAt(0);
            }
            if (time <= 0)
            {
                if (RemoteTarget.Count > 0 && RemoteTarget[0].activeSelf)
                {
                    GameObject Bullet = Resources.Load("Prefabs/Bullet", typeof(GameObject)) as GameObject;
                    var        bullet = Instantiate(Bullet, transform.position, new Quaternion(0, 0, 0, 0), transform);
                    bullet.GetComponent <BulletManager>().Init(RemoteTarget[0]);
                    bullet.GetComponent <SpriteRenderer>().sprite = Resources.Load("Sprites/Bullet/" + soldier.bulletType, typeof(Sprite)) as Sprite;
                    soldier.Attack(RemoteTarget[0].GetComponent <SoldierManager>().soldier);
                    //如果死亡
                    if (RemoteTarget[0].GetComponent <SoldierManager>().soldier.Healthy <= 0)
                    {
                        if (RemoteTarget[0].GetComponent <SoldierManager>().soldier.IsAlive)
                        {
                            RemoteTarget[0].GetComponent <SoldierManager>().soldier.IsAlive = false;
                            StartCoroutine(RemoteTarget[0].GetComponent <SoldierManager>().Death());
                        }
                    }
                }

                time = soldier.Frequency;
                //重置攻击cd
            }
            yield return(null);
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (m_btree != null)
        {
            for (int i = 0; i < m_soldierList.Count; i++)
            {
                Soldier soldier = m_soldierList [i] as Soldier;
                if (soldier.IsDied)
                {
                    continue;
                }
                Input         input   = soldier.CurrentState;
                List <string> actions = m_btree.Run(input);
                for (int j = 0; j < actions.Count; j++)
                {
                    string action = actions [j];
                    switch (action)
                    {
                    case "escape":
                        soldier.Escape();
                        break;

                    case "attack":
                        soldier.Attack();
                        break;

                    case "patrol":
                        soldier.Patrol();
                        break;
                    }
                }
            }
        }

        if (recoverTime <= 0)
        {
            for (int i = 0; i < m_soldierList.Count; i++)
            {
                Soldier soldier = m_soldierList [i] as Soldier;
                if (soldier.IsDied)
                {
                    continue;
                }
                soldier.Recover(5);
            }
            recoverTime = 50;
        }
        recoverTime--;
    }
Exemple #8
0
        public void Can_attack_target()
        {
            // Arrange
            Soldier player = new Soldier {
                Health = 100
            };
            Soldier enemy = new Soldier {
                Health = 100
            };

            // Act
            player.Attack(enemy);

            // Assert
            Assert.AreEqual(0, enemy.Health);
        }
 public void AnimationAttack()
 {
     _soldier.Attack();
 }
Exemple #10
0
 public void HandleClick(GameObject hitObject, Vector3 hitPoint)
 {
     if (hitObject && hitObject.name != "Ground" && hitObject.name != "LOS")          //clicked on something that was not the ground plane
     {
         Soldier clickedSoldier = hitObject.transform.parent.GetComponent <Soldier>();
         if (clickedSoldier)                          //clicked on soldier
         {
             if (clickedSoldier.IsControlledBy(this)) // soldier is controlled by player
             {
                 bool selectSoldier = false, deselectSoldier = false;
                 if (selectedSoldier)                       //already have soldier selected
                 {
                     if (clickedSoldier == selectedSoldier) // clicked on selected soldier
                     {
                         if (selectedSoldier.IsSelected())
                         {
                             deselectSoldier = true;
                         }
                         else
                         {
                             selectSoldier = true;
                         }
                     }
                     else                             // clicked on another soldier
                     {
                         deselectSoldier = true;
                         selectSoldier   = true;
                     }
                 }
                 else                         // no soldier selected
                 {
                     selectSoldier = true;
                 }
                 if (deselectSoldier)
                 {
                     DeselectSoldier();
                 }
                 if (selectSoldier)
                 {
                     clickedSoldier.Select();
                     selectedSoldier = clickedSoldier;
                 }
             }
             else                     // soldier is controlled by another player
             {
                 bool selectEnemy = false, deselectEnemy = false;;
                 if (selectedEnemySoldier)                       // already have enemy soldier selected
                 {
                     if (clickedSoldier == selectedEnemySoldier) // clicked on selected enemy soldier
                     {
                         if (selectedEnemySoldier.IsSelected())
                         {
                             deselectEnemy = true;
                         }
                         else                                 //
                         {
                             selectEnemy = true;
                         }
                     }
                     else                             // clicked on another enemy soldier
                     {
                         deselectEnemy = true;
                         selectEnemy   = true;
                     }
                 }
                 else                         // clicked on an enemy soldier
                 {
                     selectEnemy = true;
                 }
                 if (deselectEnemy)
                 {
                     selectedEnemySoldier.Deselect();
                     selectedEnemySoldier = null;
                 }
                 if (selectedSoldier)
                 {
                     if (currentState == State.Attack)
                     {
                         if (selectedSoldier.Attack(clickedSoldier))
                         {
                             selectEnemy = true;
                             StartMove(selectedSoldier);
                         }
                     }
                     else
                     {
                         //prompt user to click on attack first somehow?
                     }
                 }
                 if (selectEnemy)
                 {
                     selectedEnemySoldier = clickedSoldier;
                     selectedEnemySoldier.Select();
                 }
             }
         }
     }
     else if (selectedSoldier)
     {
         if (hitPoint != Resources.InvalidPosition)
         {
             if (currentState == State.Move)
             {
                 float distance = Vector3.Distance(hitPoint, selectedSoldier.transform.position);
                 if (distance < selectedSoldier.GetRange())
                 {
                     selectedSoldier.SetDestination(new Vector3(hitPoint.x, 0, hitPoint.z));
                     StartMove(selectedSoldier);
                 }
             }
             else
             {
                 // prompt user to click on move first somehow?
             }
         }
     }
 }