Exemplo n.º 1
0
    public override void Execute(Enemy enemy)
    {
        rigidbody = enemy.gameObject.GetComponent <Rigidbody>();
        if (rigidbody == null)
        {
            rigidbody            = enemy.gameObject.AddComponent <Rigidbody>();
            rigidbody.useGravity = false;
        }
        if (startPositionSet == false)
        {
            startPosition = rigidbody.position;
            ChangeMovementDirection(enemy);
            startPositionSet = true;
        }
        if (Vector3.Distance(startPosition, enemy.transform.position) < moveDistance)
        {
            Collider[] colliders = Physics.OverlapBox(enemy.transform.position, enemy.transform.localScale / 2);

            foreach (Collider collider in colliders)
            {
                if (collider.tag != "Player" && collider.tag != "Enemy" && collider.tag != "Bullet")
                {
                    ChangeMovementDirection(enemy);
                    break;
                }
            }
            rigidbody.velocity = movementDirection * enemy.MovementSpeed;
        }
        else
        {
            rigidbody.velocity = Vector3.zero;
            startPositionSet   = false;
            enemy.ActionCompleted();
        }
    }
Exemplo n.º 2
0
    public override void Execute(Enemy enemy)
    {
        if (player == null)
        {
            player = GameObject.FindGameObjectWithTag("Player").GetComponentInChildren <Player>();
        }

        RaycastHit raycastHit;

        if (Physics.Raycast(enemy.transform.position, player.transform.position - enemy.transform.position, out raycastHit))
        {
            if (raycastHit.collider.tag != "Player")
            {
                Debug.Log("Can't see the sheriff!");
            }
            else
            {
                Debug.Log("I'd like to shoot the sheriff!");

                enemy.Gun.transform.LookAt(player.transform);

                if (enemy.CurrentAmmo - enemy.EquippedGun.CostPerShot > 0 || !enemy.takeDamageWhenFiring)
                {
                    for (int i = 0; i < enemy.EquippedGun.ShotsAndDirections.Length; i++)
                    {
                        Bullet bullet = Instantiate(enemy.EquippedGun.Projectile, enemy.Gun.transform.GetChild(1).transform.position, enemy.Gun.transform.rotation).GetComponent <Bullet>();
                        bullet.speed        = enemy.EquippedGun.ProjectileSpeed;
                        bullet.bulletSource = enemy.tag;
                        bullet.damage       = enemy.EquippedGun.ProjectileDamage;
                        bullet.transform.Rotate(new Vector3(0, enemy.EquippedGun.ShotsAndDirections[i] + Random.Range(-enemy.EquippedGun.RandomizedAngle, enemy.EquippedGun.RandomizedAngle), 0));
                        //bullet.audioSource.clip = enemy.EquippedGun.SoundEffect;
                        if (i == 0)
                        {
                            bullet.audioClip = enemy.EquippedGun.SoundEffect;
                        }
                    }
                    if (enemy.takeDamageWhenFiring)
                    {
                        enemy.ProcessDamage(enemy.EquippedGun.CostPerShot);
                    }

                    Debug.Log($"{enemy.name} has {enemy.CurrentAmmo} ammo left.");
                }
            }
        }
        enemy.ActionCompleted();
    }
 private void MoveCompleted(Enemy enemy)
 {
     rigidbody.velocity = Vector3.zero;
     startPositionSet   = false;
     enemy.ActionCompleted();
 }