コード例 #1
0
ファイル: EnemyScript.cs プロジェクト: duckonomy/skyfort
    void Update()
    {
        Debug.Log("Health is: " + enemy.GetHealth());
        // if (enemy.GetHealth() < 0)
        // {
        //     Destroy(enemy.GetGameObject());

        // }
        if (enemy.GetAttackState())
        {
            //    Destroy(hitTower.gameObject);
            //    if (hitTower.NotDead())
            //    {
            //        hitTower.SendMessage("AddHealth", -enemy.GetDamage());
            //    }
        }
        else
        {
            float step = speed * Time.deltaTime; // calculate distance to move
            if (towerTarget != null)
            {
                float distance = Vector3.Distance(transform.position, towerTarget.position);
                if (distance < 4.0f)
                {
                    enemy.SetAttackState(true);
                }
                transform.position = Vector3.MoveTowards(transform.position, towerTarget.position, step);
            }
            else
            {
                transform.position = Vector3.MoveTowards(transform.position, new Vector3(90, 7, 90), step);
            }
        }
    }
コード例 #2
0
ファイル: EnemyScript.cs プロジェクト: cs352-iap-mrn/skyfort
    void FixedUpdate()
    {
        if (enemy.IsDead())
        {
            System.Random r = new System.Random();
            if (r.NextDouble() < chance)
            {
                Game.AddMP((int)Math.Round(enemy.GetMaxHealth() * .20));
            }

            Enemies.KillEnemy(enemy);

            Destroy(healthBar);

            GameObject.Destroy(gameObject);

            // Make this dependent on enemy type
            Game.IncreaseScore(1);
        }

        enemy.Update();

        if (enemy.GetAttackState())
        {
            if (hitTower == null)
            {
                enemy.SetAttackState(false);
            }
            else if (enemy.GetCooldown() <= 0)
            {
                if (enemy.GetTypeName() == Enemy.EnemyType.Boomer)
                {
                    hitTower.SendMessageUpwards("DestroyAll");
                }
                else
                {
                    enemy.SetCooldown((int)Math.Round(120 / ((30 + enemy.GetAttackSpeed() / 3) * 0.01)));
                    hitTower.SendMessageUpwards("AddHealth", -enemy.GetDamage());
                }
            }
        }
        else
        {
            float step = enemy.GetSpeed() * Time.deltaTime; // calculate distance to move
            if (towerTarget != null)
            {
                float distance = Vector3.Distance(transform.position, towerTarget.position + new Vector3(0, 8f, 0));
                if (distance < enemy.GetAttackRange())
                {
                    enemy.SetAttackState(true);
                }
                transform.position = Vector3.MoveTowards(transform.position, towerTarget.position + new Vector3(0, 8f, 0), step);
            }
            else
            {
                transform.position = Vector3.MoveTowards(transform.position, Game.baseTower.GetPosition() + new Vector3(0, 8f, 0), step);
            }

            Collider[] hitColliders = Physics.OverlapSphere(transform.position, enemy.GetDetectionRange());

            double highest = -9999;
            for (int i = 0; i < hitColliders.Length; i++)
            {
                if (hitColliders[i].tag == "tower" || hitColliders[i].tag == "base")
                {
                    TowerInstance ti = hitColliders[i].GetComponentInParent <TowerScript>().tower;
                    if (ti != null)
                    {
                        double thisValue = enemy.GetPriority(ti, enemy.GetPosition());
                        if (thisValue > highest)
                        {
                            towerTarget = ti.GetGameObject().transform;
                            highest     = thisValue;
                            hitTower    = hitColliders[i];
                        }
                    }
                }
            }
        }
    }