Example #1
0
    public void CreateProjectile(GameObject ProjectileOBJ, TroopAI theArcher, TroopAI theEnemy, float damage)
    {
        if (theProjectile != null)
        {
            Destroy(theProjectile);
        }
        enemy               = theEnemy;
        thisArcher          = theArcher;
        thisArcher.fireProj = true;
        projDmg             = damage;
        dir = (theEnemy.transform.position - theArcher.transform.position).normalized;
        Vector3 offset = new Vector3(0, theArcher.range / 4, 0);

        if (dir.y < 0)
        {
            theProjectile = Instantiate(ProjectileOBJ, theArcher.transform.position - offset, theArcher.transform.rotation);
        }
        else
        {
            theProjectile = Instantiate(ProjectileOBJ, theArcher.transform.position + offset, theArcher.transform.rotation);
        }

        GameObject theCanvas        = GameObject.FindGameObjectWithTag("Canvas");
        Vector2    canvasLocalScale = theCanvas.transform.localScale;


        theProjectile.transform.SetParent(theCanvas.transform, true);
        theProjectile.transform.localScale = new Vector3(theProjectile.transform.localScale.x * canvasLocalScale.x, theProjectile.transform.localScale.y * canvasLocalScale.y, 0);
    }
Example #2
0
    bool Collided(GameObject projectile, TroopAI theEnemy)
    {
        if (projectile.transform.position.x < theEnemy.transform.position.x + (99 * 0.5f) &&
            projectile.transform.position.x > theEnemy.transform.position.x - (99 * 0.5f) &&
            projectile.transform.position.y < theEnemy.transform.position.y + (99 * 0.5f) &&
            projectile.transform.position.y > theEnemy.transform.position.y - (99 * 0.5f))
        {
            return(true);
        }

        return(false);
    }
Example #3
0
 //Does stuff based on the bool that are set to true, takes in the affected gameobject as parameter
 public void activateTrap(TroopAI go)
 {
     if (instantKill)
     {
         //kills/destroy go
         //Destroy(go);
         HealthSystem goHealthSystem = go.health;
         goHealthSystem.addHealth(-goHealthSystem.getHealth());
     }
     if (dealsDamage)
     {
         //Deals set damage to gameobject
         go.health.addHealth(-damageAmount);
     }
     if (stun)
     {
         go.speed = 0;
     }
 }
Example #4
0
    void Update()
    {
        if (PauseAnimator.GetBool("PauseEnabled") == true)
        {
            return;
        }
        if (health.getHealth() <= 0)
        {
            if (team == -1)
            {
                thePlayer.GetComponent <InGameCash>().addAmount(10);
            }
            activ = false;
        }
        if (activ)
        {
            if (state == (int)States.CHARGE)
            {
                float dist = 0;
                if (team == 1)
                {
                    for (uint j = 0; j < enemyGridSystem.GridSize; ++j)
                    {
                        float yDist = enemyGridSystem.grid[j].transform.position.y - originPos.y;
                        //Debug.Log("TEST");

                        if (Mathf.Abs(enemyGridSystem.grid[j].transform.position.x - originPos.x) < 20 && !enemyGridSystem.IsGreyedOut(j) && Mathf.Abs(yDist) > dist)
                        {
                            //Debug.Log("TEST");
                            dist        = Mathf.Abs(yDist);
                            targetPos   = enemyGridSystem.grid[j].transform.position;
                            targetIndex = j;
                        }
                    }
                }
                else if (team == -1)
                {
                    for (uint j = 0; j < theGridSystem.GridSize; ++j)
                    {
                        float yDist = theGridSystem.grid[j].transform.position.y - originPos.y;
                        if (Mathf.Abs(theGridSystem.grid[j].transform.position.x - originPos.x) < 20 && !theGridSystem.IsGreyedOut(j) && Mathf.Abs(yDist) > dist)
                        {
                            dist        = Mathf.Abs(yDist);
                            targetPos   = theGridSystem.grid[j].transform.position;
                            targetIndex = j;
                        }
                    }
                }


                Vector3 hello = targetPos - Pos;
                //hello.x = 0;
                //hello.y = team;
                //hello.z = 0;
                hello.Normalize();
                bool collided = false;
                for (int i = 0; i < game.objects.Count; ++i)
                {
                    Vector3 nextPosition = Pos + hello * speed;
                    if (!(Mathf.Abs(game.objects[i].transform.position.x - Pos.x) < 10 * canvasLocalScale.x && Mathf.Abs(game.objects[i].transform.position.y - Pos.y) < 10 * canvasLocalScale.y))
                    {
                        if (Collided(nextPosition, game.objects[i].transform.position))
                        {
                            collided = true;
                        }
                    }
                }
                if (!collided)
                {
                    Pos += hello * speed;
                }

                transform.position = Pos;
                if (/*prevhealth != health*/ health.isHealthModified())
                {
                    // state = (int)States.CHASE;
                    aggrotimer = 0;
                }
                //prevhealth = health;
                health.setHealthModifiedToFalse();
                float minNearest = 1000000;
                nearest = null;
                for (int i = 0; i < game.objects.Count; ++i)
                {
                    nearestAI = game.objects[i].GetComponent <TroopAI>();
                    if (nearestAI.activ && nearestAI.team != team)
                    {
                        //Vector2 hello1 = new Vector2(game.objects[i].transform.position.x - transform.position.x, game.objects[i].transform.position.y - transform.position.y);
                        //float dist = hello1.SqrMagnitude();
                        //if (dist <= vision * vision && dist < minNearest)
                        //{
                        //    minNearest = dist;
                        //    nearest = game.objects[i];
                        //}
                        if (game.objects[i].transform.position.x > transform.position.x - attackWidth && game.objects[i].transform.position.x < transform.position.x + attackWidth && (game.objects[i].transform.position - transform.position).magnitude < range + 10 * canvasLocalScale.x && Mathf.Abs(game.objects[i].transform.position.y - transform.position.y) < attackHeight + 10 * canvasLocalScale.y)
                        {
                            nearest = game.objects[i];
                        }
                    }
                }
                if (nearest != null)
                {
                    state = (int)States.ATTACK;
                }
            }

            if (state == (int)States.ATTACK)
            {
                attacktimer += Time.deltaTime;
                if (nearest != null)
                {
                    nearestAI = nearest.GetComponent <TroopAI>();
                    if (nearestAI.activ)
                    {
                        if (attacktimer > (attckSpd))
                        {
                            //Class 1 = Infantry, Class 2 = Bowmen, Class 3 = Cavalry
                            attacktimer = 0;
                            if (_class == nearestAI._class)
                            {
                                if (!fireProj && _class == 2 && nearestAI)
                                {
                                    theProjectile.CreateProjectile(projectileOBJ, this, nearestAI, attckDmg);
                                }
                                else if (_class != 2)
                                {
                                    nearestAI.health.addHealth(-attckDmg);
                                }
                            }
                            if (_class == 1 && nearestAI._class == 3)
                            {
                                nearestAI.health.addHealth(-attckDmg * 3);
                            }
                            if (_class == 1 && nearestAI._class == 2)
                            {
                                nearestAI.health.addHealth(-attckDmg);
                            }
                            if (_class == 2 && nearestAI._class == 1)
                            {
                                if (!fireProj && nearestAI)
                                {
                                    theProjectile.CreateProjectile(projectileOBJ, this, nearestAI, attckDmg + 10);
                                }
                                else
                                {
                                    nearestAI.health.addHealth(-attckDmg * 5);
                                }
                            }
                            if (_class == 2 && nearestAI._class == 3)
                            {
                                if (!fireProj && nearestAI)
                                {
                                    theProjectile.CreateProjectile(projectileOBJ, this, nearestAI, attckDmg);
                                }
                                else
                                {
                                    nearestAI.health.addHealth(-attckDmg);
                                }
                            }
                            if (_class == 3 && nearestAI._class == 1)
                            {
                                nearestAI.health.addHealth(-attckDmg);
                            }
                            if (_class == 3 && nearestAI._class == 2)
                            {
                                nearestAI.health.addHealth(-attckDmg * 3);
                            }
                            attacktimer = 0;
                        }
                    }
                    else
                    {
                        state = (int)States.CHARGE;
                    }
                }
                else
                {
                    state = (int)States.CHARGE;
                }
            }

            //Powerups
            if (thePowerupsSystem && thePowerupsSystem.PowerupsIsActive == true)
            {
                //Search player powerups list
                if (team == 1)
                {
                    for (int i = 0; i < thePowerupsSystem.PlayerGridPowerups.Count; ++i)
                    {
                        if (Collided(thePowerupsSystem.PlayerGridPowerups[i].powerupPosition, transform.position))
                        {
                            switch (thePowerupsSystem.PlayerGridPowerups[i].powerType)
                            {
                            case PowerupsSystem.POWERUP_TYPE.POWERUP_ATTACKDAMAGE:
                            {
                                attckDmg += thePowerupsSystem.PlayerGridPowerups[i].AddedAttackDamage;
                                break;
                            }

                            case PowerupsSystem.POWERUP_TYPE.POWERUP_ATTACKSPEED:
                            {
                                attckSpd -= thePowerupsSystem.PlayerGridPowerups[i].AddedAttackSpeed;
                                break;
                            }

                            case PowerupsSystem.POWERUP_TYPE.POWERUP_MOVESPEED:
                            {
                                speed += thePowerupsSystem.PlayerGridPowerups[i].AddedMoveSpeed;
                                break;
                            }

                            default:
                                break;
                            }

                            Destroy(thePowerupsSystem.PlayerGridPowerups[i].PowerUpTexture);
                            thePowerupsSystem.PlayerGridPowerups.RemoveAt(i);
                        }
                    }
                }
                else  //Search enemy player grid list
                {
                    for (int i = 0; i < thePowerupsSystem.EnemyGridPowerups.Count; ++i)
                    {
                        if (Collided(thePowerupsSystem.EnemyGridPowerups[i].powerupPosition, transform.position))
                        {
                            switch (thePowerupsSystem.EnemyGridPowerups[i].powerType)
                            {
                            case PowerupsSystem.POWERUP_TYPE.POWERUP_ATTACKDAMAGE:
                            {
                                attckDmg += thePowerupsSystem.EnemyGridPowerups[i].AddedAttackDamage;
                                break;
                            }

                            case PowerupsSystem.POWERUP_TYPE.POWERUP_ATTACKSPEED:
                            {
                                attckSpd += thePowerupsSystem.EnemyGridPowerups[i].AddedAttackSpeed;
                                break;
                            }

                            case PowerupsSystem.POWERUP_TYPE.POWERUP_MOVESPEED:
                            {
                                speed += thePowerupsSystem.EnemyGridPowerups[i].AddedMoveSpeed;
                                break;
                            }

                            default:
                                break;
                            }

                            Destroy(thePowerupsSystem.EnemyGridPowerups[i].PowerUpTexture);
                            thePowerupsSystem.EnemyGridPowerups.RemoveAt(i);
                        }
                    }
                }
            }
            if (theTrapSystem && theTrapSystem.trapSystemActive)
            {
                foreach (GameObject go in theTrapSystem.myTraps)
                {
                    Trap theTrap = go.GetComponent <Trap>();
                    if (!theTrap.isactive)
                    {
                        continue;
                    }
                    if (theTrap.team == team)
                    {
                        continue;
                    }
                    if (Collided(go.transform.position, transform.position))
                    {
                        theTrap.activateTrap(this);
                        //Destroy(theTrap);
                        theTrap.isactive = false;
                        go.SetActive(false);
                    }
                }
                theTrapSystem.cleanUpTraps();
            }
        }
    }