IEnumerator DelayedForSeconds(float delay, AI_Follower_Script unit)
    {
        yield return(new WaitForSeconds(delay));

        TakeAction(unit);
        Debug.Log("Delay Complete");
    }
    public void TakeAction(AI_Follower_Script unit)
    {
        float distance = 0;

        AI_Follower_Script closestEnemy = null;

        foreach (AI_Follower_Script fol in turnScript.goodList)
        {
            float tempDist = Vector3.Distance(unit.gameObject.transform.position, fol.gameObject.transform.position);

            if (distance == 0)
            {
                distance     = tempDist;
                closestEnemy = fol;
            }
            else if (tempDist < distance)
            {
                distance     = tempDist;
                closestEnemy = fol;
            }
        }

        if (distance > unit.maxRange)
        {
            aiScript.SetGrid();
            MoveUnit(unit, closestEnemy);
            unit.TakeAction(1);
        }
        else
        {
            unit.unitModel.SetActive(true);
            aiScript.ConfirmShot(unit.gameObject, closestEnemy.gameObject);
        }
    }
    IEnumerator DelayedSpawn(float delay, AI_Follower_Script shooterUnit)
    {
        yield return(new WaitForSeconds(delay));

        GameObject bulletTemp = Instantiate(bulletPrefab, shooterUnit.transform);
        Quaternion random     = new Quaternion(shooterUnit.transform.rotation.x + Random.Range(-0.05f, 0.05f), shooterUnit.transform.rotation.y + Random.Range(-0.05f, 0.05f), shooterUnit.transform.rotation.z, shooterUnit.transform.rotation.w);

        bulletTemp.transform.position = shooterUnit.transform.position + (shooterUnit.transform.up);
        bulletTemp.transform.rotation = random;
        bulletTemp.GetComponent <Bullet_Mover_Script>().DestroyIn30();
    }
    public void DealDamage(AI_Follower_Script target)
    {
        gTarget = target.gameObject;

        if (gTarget.tag == "Good Guy")
        {
            if (target.armour > damage)
            {
            }
            else
            {
                float nDamage = damage - (target.armour - armourPiercing);
                target.currentHealth -= nDamage;
                target.slider.value   = target.currentHealth;
                //Was interacting with this unit's slider, not the target's slider, so health won't change on damage
                if (target.currentHealth <= 0)
                {
                    turnScript.allList.Remove(target);
                    turnScript.goodList.Remove(target);
                    target.animManager.Die();
                    Invoke("DestroyUnit", 2.5f);
                }
            }
        }

        else if (gTarget.tag == "Bad Guy")
        {
            if (target.armour > damage)
            {
            }
            else
            {
                float nDamage = damage - (target.armour - armourPiercing);
                target.currentHealth -= nDamage;
                target.slider.value   = target.currentHealth;
                if (target.currentHealth <= 0)
                {
                    turnScript.allList.Remove(target);
                    turnScript.badList.Remove(target);
                    target.animManager.Die();
                    Invoke("DestroyUnit", 2.5f);
                }
            }
        }
    }
    public void MoveUnit(AI_Follower_Script unit, AI_Follower_Script closestEnemy)
    {
        if (closestEnemy != null)
        {
            path = aiScript.FindPathAI(closestEnemy.GetChunkUnder().gameObject, unit.gameObject);

            truePath = new List <Chunk_Script>();

            if (path != null)
            {
                int currentGCost = 0;
                int currentID    = -1;



                path = aiScript.CalculatePath(closestEnemy.GetChunkUnder());
                foreach (Chunk_Script chunk in path)
                {
                    currentID += 1;
                    int tentativeGCost = currentGCost + aiScript.CalculateDistanceCost(path[currentID], path[currentID + 1]);
                    if (tentativeGCost > unit.GetComponent <AI_Follower_Script>().maxDistance)
                    {
                        break;
                    }
                    else
                    {
                        currentGCost = tentativeGCost;
                        truePath.Add(chunk);
                    }
                }
                unit.GetComponent <AI_Follower_Script>().SetPath(truePath);
            }
            else
            {
                Debug.Log("Couldn't Find Path");
            }
        }
        else
        {
            Debug.Log("Something went wrong with finding the closest unit");
        }
    }
示例#6
0
    public AI_Follower_Script GetNextUnit()
    {
        newUnit = null;

        if (currentTeam == 0)
        {
            foreach (AI_Follower_Script fol in badList)
            {
                if (fol.GetActions() > 0)
                {
                    newUnit           = fol;
                    currentTeam       = 1;
                    enemyTurn.enabled = true;
                    break;
                }
            }
        }
        else if (currentTeam == 1)
        {
            foreach (AI_Follower_Script fol in goodList)
            {
                if (fol.GetActions() > 0)
                {
                    newUnit           = fol;
                    currentTeam       = 0;
                    enemyTurn.enabled = false;
                    break;
                }
            }
        }

        if (newUnit == null)
        {
            if (goodList.Count > 0 && badList.Count > 0)
            {
                if (currentTeam == 1)
                {
                    foreach (AI_Follower_Script fol in badList)
                    {
                        if (fol.GetActions() > 0)
                        {
                            newUnit           = fol;
                            currentTeam       = 1;
                            enemyTurn.enabled = true;
                            break;
                        }
                    }
                }
                else if (currentTeam == 0)
                {
                    foreach (AI_Follower_Script fol in goodList)
                    {
                        if (fol.GetActions() > 0)
                        {
                            newUnit           = fol;
                            currentTeam       = 0;
                            enemyTurn.enabled = false;
                            break;
                        }
                    }
                }
            }
            else
            {
                Debug.Log("Somebody Won!");

                if (badList.Count == 0)
                {
                    winScreen.gameObject.SetActive(true);
                }
                else if (goodList.Count == 0)
                {
                    loseScreen.gameObject.SetActive(true);
                }
            }
        }

        return(newUnit);
    }
    public void ShootRound(GameObject target, GameObject shooter, Vector3 newShooterTransform)
    {
        AI_Follower_Script shooterUnit = shooter.GetComponent <AI_Follower_Script>();

        Vector3 up1 = new Vector3(0, 0.5f);

        RaycastHit[] hits;
        bool         shot = false;

        float distance = Vector3.Distance(newShooterTransform, target.transform.position);

        if (distance < shooterUnit.maxRange)
        {
            hits = Physics.RaycastAll(newShooterTransform + up1, (target.transform.position - newShooterTransform), distance);
            Vector3 lookDirection = target.transform.position;
            lookDirection.y = 0;
            shooter.transform.LookAt(lookDirection);
            foreach (RaycastHit h in hits)
            {
                if (h.collider.tag == "Tall Cover")
                {
                    if (Vector3.Distance(shooter.transform.position, h.transform.position) <= 2 && target != h.collider.gameObject)
                    {
                    }
                    else
                    {
                        Debug.Log("Hit Tall Cover");
                        shot = false;
                        shooterUnit.aiScript.GetComponent <AudioSource>().Play();
                        StartCoroutine(DelayedDamage(1.0f, h.collider.gameObject));
                        break;
                    }
                }
                else if (h.collider.tag == "Low Cover")
                {
                    if (distance - Vector3.Distance(shooter.transform.position, h.transform.position) <= 0.5)
                    {
                        Debug.Log("Hit Low Cover");
                        shot = false;
                        shooterUnit.aiScript.GetComponent <AudioSource>().Play();
                        StartCoroutine(DelayedDamage(1.0f, h.collider.gameObject));
                        break;
                    }
                    else if (distance - Vector3.Distance(shooter.transform.position, h.transform.position) <= 1.5)
                    {
                        if (target.tag == "Good Guy" || target.tag == "Bad Guy")
                        {
                            Debug.Log("Hit Low Cover");
                            shot = false;
                            shooterUnit.aiScript.GetComponent <AudioSource>().Play();
                            StartCoroutine(DelayedDamage(1.0f, h.collider.gameObject));
                            break;
                        }
                        else if (distance - Vector3.Distance(shooter.transform.position, h.transform.position) <= 0.5 && target.tag == "Low Cover")
                        {
                            Debug.Log("Hit Low Cover");
                            shot = false;
                            shooterUnit.aiScript.GetComponent <AudioSource>().Play();
                            StartCoroutine(DelayedDamage(1.0f, h.collider.gameObject));
                            break;
                        }
                        else
                        {
                            Debug.Log("Went Over Low Cover");
                        }
                    }
                    else
                    {
                        Debug.Log("Went Over Low Cover");
                    }
                }
                else if (h.collider.tag == "Good Guy")
                {
                    if (shooter.tag == "Bad Guy")
                    {
                        if (target == h.collider.gameObject)
                        {
                            shot        = true;
                            damagedUnit = h.collider.gameObject.GetComponent <AI_Follower_Script>();
                        }
                    }
                }
                else if (h.collider.tag == "Bad Guy")
                {
                    if (shooter.tag == "Good Guy")
                    {
                        if (target == h.collider.gameObject)
                        {
                            shot        = true;
                            damagedUnit = h.collider.gameObject.GetComponent <AI_Follower_Script>();
                        }
                    }
                }
            }
        }

        if (shot == true)
        {
            shooterUnit.DealDamage(damagedUnit);
            Debug.Log("Hit Unit");
        }
    }
    public Vector3 Shoot(GameObject target, GameObject shooter)
    {
        AI_Follower_Script shooterUnit = shooter.GetComponent <AI_Follower_Script>();

        Vector3 newPos = new Vector3(0, 0, 0);

        shooterUnit.aiScript.unitCamera.GetComponent <AudioSource>().Play();

        for (float i = 0; i < 2; i += 0.3f)
        {
            StartCoroutine(DelayedSpawn(i, shooterUnit));
        }

        Vector3 up1 = new Vector3(0, 0.5f);

        RaycastHit[] hits;

        float distance = Vector3.Distance(shooter.transform.position, target.transform.position);
        bool  shot     = false;

        if (distance < shooterUnit.maxRange)
        {
            hits = Physics.RaycastAll(shooter.transform.position + up1, (target.transform.position - shooter.transform.position), distance);
            Vector3 lookDirection = target.transform.position;
            lookDirection.y = 0;
            shooter.transform.LookAt(lookDirection);
            foreach (RaycastHit h in hits)
            {
                if (h.collider.tag == "Tall Cover")
                {
                    if (Vector3.Distance(shooter.transform.position, h.transform.position) <= 2 && target != h.collider.gameObject)
                    {
                        RaycastHit hit;
                        shooter.transform.position += shooter.transform.right;
                        Chunk_Script avoidChunk = shooter.GetComponent <AI_Follower_Script>().GetChunkUnder();
                        if (avoidChunk.impassable == false)
                        {
                            if (Physics.Raycast(shooter.transform.position + up1, shooter.transform.forward, out hit, 1.5f))
                            {
                                if (hit.collider.tag == ("Tall Cover"))
                                {
                                    Debug.Log("Attempted and hit cover still");
                                }
                                else
                                {
                                    ShootRound(target, shooter, shooter.transform.position);
                                    newPos = shooter.transform.position;
                                    Debug.Log("Attempted and didn't hit cover");
                                    shooter.transform.position += -shooter.transform.right;
                                    break;
                                }
                            }
                            else
                            {
                                ShootRound(target, shooter, shooter.transform.position);
                                newPos = shooter.transform.position;
                                Debug.Log("Attempted and didn't hit cover");
                                shooter.transform.position += -shooter.transform.right;
                                break;
                            }
                        }

                        shooter.transform.position += -shooter.transform.right;
                        shooter.transform.position += -shooter.transform.right;
                        avoidChunk = shooter.GetComponent <AI_Follower_Script>().GetChunkUnder();
                        if (avoidChunk.impassable == false)
                        {
                            if (Physics.Raycast(shooter.transform.position + up1, shooter.transform.forward, out hit, 1.5f))
                            {
                                if (hit.collider.tag == ("Tall Cover"))
                                {
                                    Debug.Log("Attempted and hit cover still");
                                    Debug.Log("Hit Tall Cover");
                                    shot = false;
                                    shooterUnit.aiScript.GetComponent <AudioSource>().Play();
                                    StartCoroutine(DelayedDamage(1.0f, h.collider.gameObject));
                                    break;
                                }
                                else
                                {
                                    ShootRound(target, shooter, shooter.transform.position);
                                    newPos = shooter.transform.position;
                                    Debug.Log("Attempted and didn't hit cover");
                                    shooter.transform.position += shooter.transform.right;
                                    break;
                                }
                            }
                            else
                            {
                                ShootRound(target, shooter, shooter.transform.position);
                                newPos = shooter.transform.position;
                                Debug.Log("Attempted and didn't hit cover");
                                shooter.transform.position += shooter.transform.right;
                                break;
                            }
                        }
                    }
                    else
                    {
                        Debug.Log("Hit Tall Cover");
                        shooterUnit.aiScript.GetComponent <AudioSource>().Play();
                        StartCoroutine(DelayedDamage(1.0f, h.collider.gameObject));
                        shot   = false;
                        newPos = shooterUnit.transform.position;
                        break;
                    }
                }
                else if (h.collider.tag == "Low Cover")
                {
                    if (Vector3.Distance(target.transform.position, h.transform.position) <= 0.5)
                    {
                        Debug.Log("Hit Low Cover");
                        shooterUnit.aiScript.GetComponent <AudioSource>().Play();
                        StartCoroutine(DelayedDamage(1.0f, h.collider.gameObject));
                        shot   = false;
                        newPos = shooterUnit.transform.position;
                        break;
                    }
                    else if (Vector3.Distance(target.transform.position, h.transform.position) <= 1.5)
                    {
                        if (target.tag == "Good Guy" || target.tag == "Bad Guy")
                        {
                            Debug.Log("Hit Low Cover");
                            shooterUnit.aiScript.GetComponent <AudioSource>().Play();
                            StartCoroutine(DelayedDamage(1.0f, h.collider.gameObject));
                            shot   = false;
                            newPos = shooterUnit.transform.position;
                            break;
                        }
                        else if (distance - Vector3.Distance(shooter.transform.position, h.transform.position) <= 0.5 && target.tag == "Low Cover")
                        {
                            Debug.Log("Hit Low Cover");
                            shooterUnit.aiScript.GetComponent <AudioSource>().Play();
                            StartCoroutine(DelayedDamage(1.0f, h.collider.gameObject));
                            shot   = false;
                            newPos = shooterUnit.transform.position;
                            break;
                        }
                        else
                        {
                            Debug.Log("Went Over Low Cover");
                        }
                    }
                    else if (target.transform == h.collider.gameObject.transform)
                    {
                        Debug.Log("Hit Low Cover");
                        shooterUnit.aiScript.GetComponent <AudioSource>().Play();
                        StartCoroutine(DelayedDamage(1.0f, h.collider.gameObject));
                        shot   = false;
                        newPos = shooterUnit.transform.position;
                        break;
                    }
                    else
                    {
                        Debug.Log("Went Over Low Cover");
                    }
                }
                else if (h.collider.tag == "Good Guy")
                {
                    if (shooter.tag == "Bad Guy")
                    {
                        if (target == h.collider.gameObject)
                        {
                            shot        = true;
                            damagedUnit = h.collider.gameObject.GetComponent <AI_Follower_Script>();
                        }
                    }
                }
                else if (h.collider.tag == "Bad Guy")
                {
                    if (shooter.tag == "Good Guy")
                    {
                        if (target == h.collider.gameObject)
                        {
                            shot        = true;
                            damagedUnit = h.collider.gameObject.GetComponent <AI_Follower_Script>();
                        }
                    }
                }
            }
        }
        if (shot == true)
        {
            shooterUnit.DealDamage(damagedUnit);
            Debug.Log("Hit Unit");
            newPos = shooterUnit.transform.position;
        }

        return(newPos);
    }