Ejemplo n.º 1
0
    // If AttackTarget needs to be called from another thread (besides main), use this method.
    // You need to pass in a randomly generated dodge int (from 0 to 100).
    public bool AttackTarget(Unit selectedTarget, int damage, int dodge)
    {
        try
        {
            unitAccuracy = selectedUnit.accuracy;

            if (selectedTarget != null && !selectedTarget.isPlayer)
            {
                if (unitAccuracy >= selectedTarget.dodgeChance + dodge)
                {
                    GameObject   enemy  = selectedTarget.gameObject;
                    HealthSystem health = (HealthSystem)enemy.GetComponent(typeof(HealthSystem));
                    health.takeDamageAndDie(damage);

                    return(true);
                }
                else
                {
                    HealthSystem health = (HealthSystem)selectedTarget.gameObject.GetComponent(typeof(HealthSystem));
                    health.ConfirmHit(null);
                    Debug.Log("Player Missed!");

                    return(true);
                }
            }
            else
            {
                return(false);
            }
        }catch { return(false); }
    }
Ejemplo n.º 2
0
    // Primary AttackTarget method, return true if the method executes successfully, return false if something goes wrong.
    // An attack executing successfully but missing its target does not constitute something going wrong.
    public bool AttackTarget(Unit selectedTarget, int damage)
    {
        try
        {
            unitAccuracy = selectedUnit.accuracy;
            bool didHit = false;
            if (selectedTarget != null && selectedUnit.timeStampAttack <= Time.time && !selectedTarget.isPlayer)
            {
                if (unitAccuracy >= selectedTarget.dodgeChance + random.Next(100))
                {
                    GameObject   enemy  = selectedTarget.gameObject;
                    HealthSystem health = (HealthSystem)enemy.GetComponent(typeof(HealthSystem));
                    health.takeDamageAndDie(damage);
                    didHit = true;
                }
                else
                {
                    HealthSystem health = (HealthSystem)selectedTarget.gameObject.GetComponent(typeof(HealthSystem));
                    health.ConfirmHit(null);
                    Debug.Log("Player Missed!");
                    didHit = true;
                }
            }
            else
            {
                return(false);
            }

            try { selectedUnit.GetComponent <PlayerUnit>().ResetAttackRanges(); } catch { }
            selectedAbility = 0;
            BoardHighlights.Instance.Hidehighlights();
            return(didHit);
        }
        catch { return(false); }
    }
Ejemplo n.º 3
0
 public void BuffTarget(Unit selectedTarget, int buff)
 {
     try{
         if (selectedTarget != null && selectedUnit.timeStampAttack <= Time.time && selectedTarget.isPlayer == selectedUnit.isPlayer)
         {
             GameObject   ally   = selectedTarget.gameObject;
             HealthSystem health = (HealthSystem)ally.GetComponent(typeof(HealthSystem));
             if (health.currentHealth <= health.startingHealth)
             {
                 health.takeDamageAndDie(0 - buff);
             }
             else
             {
                 health.ConfirmHit(0, "Full Health!");
             }
         }
         else
         {
             return;
         }
         BoardHighlights.Instance.Hidehighlights();
     }catch { }
 }
Ejemplo n.º 4
0
    private void Update()
    {
        int  damage   = this.GetComponent <PlayerUnit>().damageAmount;
        int  accuracy = this.GetComponent <PlayerUnit> ().accuracy;
        int  targetDodgeChance;
        Unit closestPlayer  = null;
        int  playerDistance = 100;

        //find closest player
        for (int i = 0; i < playerUnits.Count; i++)
        {
            PlayerUnit currentPlayer = playerUnits[i].GetComponent <PlayerUnit>();
            int        xDist         = Math.Abs(enemyUnit.CurrentX - currentPlayer.CurrentX);
            int        yDist         = Math.Abs(enemyUnit.CurrentY - currentPlayer.CurrentY);
            if (xDist + yDist < playerDistance)
            {
                closestPlayer  = currentPlayer;
                playerDistance = xDist + yDist;
            }
        }

        //If Attack cooldown over, then attack
        if (enemyUnit.timeStampAttack <= Time.time && timeStampDelay <= Time.time)
        {
            bool[,] allowedEnemyAttacks = enemyUnit.PossibleAttack();
            //BoardHighlights.Instance.Hidehighlights();
            //BoardHighlights.Instance.HighlightAllowedAttacks(allowedEnemyAttacks);

            //Determine if player is in Attack distance
            if (allowedEnemyAttacks [closestPlayer.CurrentX, closestPlayer.CurrentY])
            {
                //If yes then attack
                HealthSystem health = (HealthSystem)BoardManager.Units[closestPlayer.CurrentX, closestPlayer.CurrentY].GetComponent(typeof(HealthSystem));
                targetDodgeChance = closestPlayer.dodgeChance + UnityEngine.Random.Range(0, 100);
                if (accuracy >= targetDodgeChance)
                {
                    health.takeDamageAndDie(damage);
                    source.PlayOneShot(attack);
                }
                else
                {
                    Debug.Log("Golem Missed!");
                    health.ConfirmHit(null);
                }
                enemyUnit.timeStampAttack = Time.time + enemyUnit.cooldownAttackSeconds;
                return;
            }
        }



        //If Movemvent cooldown over, then Move
        if (enemyUnit.timeStampMove <= Time.time)
        {
            List <int[]> allowedEnemyMoves = getTrueMoves();
            Shuffle(allowedEnemyMoves);
            //BoardHighlights.Instance.Hidehighlights();
            //BoardHighlights.Instance.HighlightAllowedMoves(enemyUnit.PossibleMove());
            foreach (int[] move in allowedEnemyMoves)
            {
                //Check if this will move enemy closer to a player
                if (Math.Abs(enemyUnit.CurrentX - closestPlayer.CurrentX) > Math.Abs(move[0] - closestPlayer.CurrentX) || Math.Abs(enemyUnit.CurrentY - closestPlayer.CurrentY) > Math.Abs(move[1] - closestPlayer.CurrentY))
                {
                    if (BoardManager.Units[move[0], move[1]] == null)
                    {
                        BoardManager.Units[enemyUnit.CurrentX, enemyUnit.CurrentY] = null;
                        enemyUnit.transform.position = BoardManager.Instance.GetTileCenter(move[0], move[1], 0);
                        enemyUnit.SetPosition(move[0], move[1]);
                        BoardManager.Units[move[0], move[1]] = enemyUnit;
                        enemyUnit.timeStampMove = Time.time + enemyUnit.cooldownMoveSeconds;
                        return;
                    }
                }
            }
        }
    }