Ejemplo n.º 1
0
        public void Attack(int fromRight = 1)
        {
            // We are gonna attack here by casting a ray in front of the player
            Player player = GoneWrong.Player.instance;

            if (player != null)
            {
                RaycastHit hit;
                Ray        ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
                if (Physics.Raycast(ray, out hit, _hitRange, LayerMask.GetMask("BodyPart")))
                {
                    if (GoneWrong.AudioManager.instance != null && _hitSounds.Count > 0)
                    {
                        AudioClip clip = _hitSounds[Random.Range(0, _hitSounds.Count)];
                        if (clip != null)
                        {
                            GoneWrong.AudioManager.instance.PlayOneShotSound(clip, 1, 0, 1);
                        }
                    }

                    EmitBlood(hit);

                    AIStateMachine stateMachine = hit.transform.GetComponentInParent <AIStateMachine>();
                    if (stateMachine != null)
                    {
                        stateMachine.TakeDamage(_damage, player.transform.position, hit, false, fromRight);

                        // Check if we can decapitate the part
                        AIDecapitation part = hit.transform.GetComponent <AIDecapitation>();
                        if (_decapitator && part != null)
                        {
                            part.DecapitatePart(hit);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Fire()
        {
            InventoryWeaponMount weaponMount = null;

            if (_equippedWeapon.value == 0)
            {
                weaponMount = _playerInventory.rifle1;
            }
            else if (_equippedWeapon.value == 1)
            {
                weaponMount = _playerInventory.rifle2;
            }
            else if (_equippedWeapon.value == 2)
            {
                weaponMount = _playerInventory.handgun;
            }
            if (_equippedWeapon.value == 3)
            {
                weaponMount = _playerInventory.melee;
            }

            if (_playerInventory != null && _equippedWeapon != null)
            {
                // If we have rounds left in our weapon, we fire.
                if (weaponMount.rounds > 0)
                {
                    // Muzzle flash activation
                    if (_muzzleFlashTimer <= 0 && _muzzleFlash != null)
                    {
                        _muzzleFlashTimer = _muzzleFlashActiveDelay;
                        _muzzleFlash.gameObject.SetActive(!_muzzleFlash.gameObject.activeSelf);
                    }

                    // Bullet sound
                    if (_bulletSound != null && AudioManager.instance != null && !_gazWeapon)
                    {
                        AudioManager.instance.PlayOneShotSound(_bulletSound, 1, 0, 0);
                    }

                    // Emit Smoke
                    if (_bulletPosition != null)
                    {
                        if (_weaponSmoke != null)
                        {
                            _weaponSmoke.transform.position = _bulletPosition.position;
                            _weaponSmoke.Emit(_smokeEmitted);
                        }

                        if (_impact != null)
                        {
                            _impact.transform.position = _bulletPosition.position;
                            _impact.Emit(10);
                        }

                        if (_multipleImpactObject != null && !_gazWeapon)
                        {
                            _multipleImpactObject.transform.position = _bulletPosition.position;
                            foreach (Transform child in transform)
                            {
                                ParticleSystem childParticleSystem = child.GetComponent <ParticleSystem>();
                                if (childParticleSystem != null)
                                {
                                    childParticleSystem.Emit(10);
                                }
                            }
                        }
                    }

                    // Instantiate a real bullet in the scene (case of a rocket launcher or a heavy weapon with slower ammo speed)
                    if (_bulletPosition != null && _bullet != null)
                    {
                        Bullet bullet = Instantiate(_bullet, _bulletPosition.position, Quaternion.identity);
                        bullet.transform.forward = -transform.forward;
                    }
                    // We make a simple ray cast.
                    else
                    {
                        // If it's a normal fire weapon, we fire a bullet with a raycast
                        if (!_gazWeapon)
                        {
                            // We create a ray
                            Ray        ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
                            RaycastHit hit;
                            if (Physics.Raycast(ray, out hit, Mathf.Infinity, _bulletLayerMask) && _bulletHole != null)
                            {
                                // If we hit an enemy, we are going to provoke damage
                                if (hit.transform.gameObject.layer == LayerMask.NameToLayer("BodyPart"))
                                {
                                    if (GoneWrong.AudioManager.instance != null && _hitSounds.Count > 0)
                                    {
                                        AudioClip clip = _hitSounds[Random.Range(0, _hitSounds.Count)];
                                        if (clip != null)
                                        {
                                            GoneWrong.AudioManager.instance.PlayOneShotSound(clip, 1, 0, 1, hit.transform.position);
                                        }
                                    }

                                    // We emit blood particles at the hit point
                                    EmitBlood(hit);

                                    // Try getting the ai state machine
                                    AIStateMachine stateMachine = hit.transform.GetComponentInParent <AIStateMachine>();
                                    if (stateMachine != null)
                                    {
                                        float damage = _damage;
                                        if (_damageDependsOnDistance && _damages.Count > 0)
                                        {
                                            // By default, the damage should be equal to the least amount of damage
                                            damage = _damages[_damages.Count - 1].damage;

                                            float distance = (stateMachine.transform.position - GoneWrong.Player.instance.transform.position).magnitude;
                                            for (int i = 0; i < _damages.Count; i++)
                                            {
                                                if (distance < _damages[i].distance)
                                                {
                                                    damage = _damages[i].damage;
                                                    break;
                                                }
                                            }
                                        }

                                        // Check if we can decapitate the part
                                        AIDecapitation part = hit.transform.GetComponent <AIDecapitation>();

                                        if (_decapitator && part != null)
                                        {
                                            // Only decapitate the parts that don't cause for an instant kill
                                            if (!part.instantKill || part.CompareTag("Head"))
                                            {
                                                part.DecapitatePart(hit);
                                            }
                                        }

                                        stateMachine.TakeDamage(damage, Player.instance.transform.position, hit);
                                    }
                                }
                                // Else, we mot likely hit default geometry, so we instantiate the bullet hole
                                else
                                {
                                    GameObject tmp = Instantiate(_bulletHole, hit.point, Quaternion.identity);
                                    tmp.transform.forward = hit.normal;
                                    tmp.transform.parent  = hit.transform;
                                }
                            }
                        }
                        else
                        {
                            // This is a gaz weapon
                            // Here, we cast a sphere for the gaz
                            Ray        ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
                            RaycastHit hitInfo;
                            if (Physics.Raycast(ray, out hitInfo, 5f, LayerMask.GetMask("Barrier")))
                            {
                                Barrier barrier = hitInfo.transform.GetComponent <Barrier>();
                                if (barrier != null)
                                {
                                    barrier.TakeDamage(_damage);
                                }
                            }
                        }
                    }

                    // We reduce the number of rounds in our player inventory
                    weaponMount.rounds--;
                }
                else
                {
                    // We play out of ammo sound
                    if (_outOfAmmoSound != null && AudioManager.instance != null)
                    {
                        AudioManager.instance.PlayOneShotSound(_outOfAmmoSound, 1, 0, 0);
                    }

                    // We try to reload.
                    Reload();
                }
            }
        }