public void SwitchWeapon(InventoryWeaponMount theWeaponMount, int whichRifle) { //yield return new WaitForSeconds(2f); // Then we force the player to switch to the picked weapon: // We get the weapon index first int weaponIndex = 1; if (theWeaponMount.item.weaponType == WeaponType.Handgun) { weaponIndex = 3; } else if (theWeaponMount.item.weaponType == WeaponType.Melee) { weaponIndex = 4; } else if (theWeaponMount.item.weaponType == WeaponType.Rifle) { weaponIndex = whichRifle; } // Then switch to the weapon GoneWrong.Player.instance.SwitchWeapon(weaponIndex); }
IEnumerator SwitchWeaponCoroutine(int weaponIndex) { _weaponIndex = -1; InventoryWeaponMount weaponMount = null; if (weaponIndex == 1) { weaponMount = _playerInventory.rifle1; } else if (weaponIndex == 2) { weaponMount = _playerInventory.rifle2; } if (weaponIndex == 3) { weaponMount = _playerInventory.handgun; } if (weaponIndex == 4) { weaponMount = _playerInventory.melee; } // If player inventory doesn't contain the weapon identified by weaponIndex, then we leave if (weaponIndex != 0 && weaponMount.item == null) { _weaponSwitchCoroutine = null; yield break; } // When we switch the weapon and the weapon is not of type melee, we stop looking with the flashlight: if (Flashlight.instance != null) { if (Flashlight.instance.looking && weaponIndex != 4 && weaponIndex != 0) { Flashlight.instance.Look(false); } if (weaponIndex == 4) { Flashlight.instance.Look(true); } } float deslectionClipLength = 0f; _equippedWeapon = weaponIndex; // Then we set the previously selected weapon to inactive // We need to check if the weapon hasn't been destroyed in a weapon replacement case if (_equippedWeaponControl != null) { _equippedWeaponControl.gameObject.SetActive(false); } // If the new weapon is nothing, we select the flashlight, otherwise, we deactivate the light if (Flashlight.instance != null && !Flashlight.instance.looking) { Flashlight.instance.Look(_weaponIndex == 0); Flashlight.instance.ActivateDeactivateLight(false, 0); } // Equipped weapons starts from 0 with 0 representing the melee (no weapon) index. // Shared int on equipped weapon is equal to 0 when the first weapon is selected. // Shared int's 0 is, by identification equal to 1 in our equipped weapons if (_equippedWeaponSharedInt != null) { _equippedWeaponSharedInt.value = weaponIndex - 1; } WeaponControl newEquippedWeaponControl = null; // If the newly equipped weapon is no weapon, we access it directly if (weaponIndex == 0) { newEquippedWeaponControl = _weaponHolder.transform.GetChild(0).GetComponent <WeaponControl>(); } // else, we access it through the player inventory else if (_playerInventory != null) { WeaponControl weaponControl = weaponMount.item.collectableWeapon.weaponControl; foreach (Transform child in weaponHolder.transform) { if (child.GetComponent <WeaponControl>().inventoryWeapon == weaponMount.item) { newEquippedWeaponControl = child.GetComponent <WeaponControl>(); } } } if (_playerInventory != null && newEquippedWeaponControl != null) { // We activate the new weapons and animate it // But we only activate the weapon when it's not just the hands if (weaponIndex != 0) { newEquippedWeaponControl.transform.gameObject.SetActive(true); } newEquippedWeaponControl.WeaponSelectOrDeselect(true); _equippedWeaponControl = newEquippedWeaponControl; // We wait for some time before we deactivate the unequipped weapon (all the weapons) yield return(new WaitForSeconds(deslectionClipLength - 1f)); // Now, we deselect all other weapons foreach (Transform child in _weaponHolder.transform) { if (child.GetComponent <WeaponControl>() != newEquippedWeaponControl) { child.transform.gameObject.SetActive(false); } } } _weaponSwitchCoroutine = null; }
public void Drop() { switch (_clickedItemType) { case ItemType.Weapon: // We instantiate the weapon in front of us: InventoryWeaponMount clickedWeaponMount = null; if (_clickedItemIndex == 0) { clickedWeaponMount = _inventory.rifle1; } else if (_clickedItemIndex == 1) { clickedWeaponMount = _inventory.rifle2; } else if (_clickedItemIndex == 2) { clickedWeaponMount = _inventory.handgun; } else if (_clickedItemIndex == 3) { clickedWeaponMount = _inventory.melee; } if (clickedWeaponMount.item.collectableWeapon != null) { Instantiate(clickedWeaponMount.item.collectableWeapon, GoneWrong.Player.instance.transform.position + GoneWrong.Player.instance.transform.forward, Quaternion.Euler(clickedWeaponMount.item.collectableWeapon.instantiateRotation)); } // We play the drop weapon sound clickedWeaponMount.item.collectableWeapon.PlayDropSound(); // We find the weapon control and we destroy it GoneWrong.WeaponControl weaponControl = clickedWeaponMount.item.collectableWeapon.weaponControl; foreach (Transform child in GoneWrong.Player.instance.weaponHolder.transform) { if (child.GetComponent <GoneWrong.WeaponControl>().inventoryWeapon == clickedWeaponMount.item) { Destroy(child.gameObject); } } // If the equipped weapon is the weapon that we just dropped, we switch back to no equipped weapon if (GoneWrong.Player.instance.equippedWeapon == _clickedItemIndex + 1) { GoneWrong.Player.instance.SwitchWeapon(0); } clickedWeaponMount.rounds = 0; clickedWeaponMount.item = null; break; case ItemType.Ammo: // We instantiate the ammo in front of us if (_inventory.ammo[_clickedItemIndex].item.collectableAmmo != null) { Instantiate(_inventory.ammo[_clickedItemIndex].item.collectableAmmo, GoneWrong.Player.instance.transform.position + GoneWrong.Player.instance.transform.forward, Quaternion.identity); } // We play the drop ammo sound _inventory.ammo[_clickedItemIndex].item.collectableAmmo.PlayDropSound(); _inventory.ammo[_clickedItemIndex].rounds = 0; _inventory.ammo[_clickedItemIndex].item = null; _inventory.ammo.RemoveAt(_clickedItemIndex); break; case ItemType.Consumable: // We get the index of the intem in the inventory consumable list // Because consumables are stored after ammo. int indexAtConsumableList = _clickedItemIndex - _inventory.ammo.Count; // We instantiate the consumable in front of us DropConsumableByIndex(indexAtConsumableList); break; } _clickedItemIndex = -1; _clickedItemType = ItemType.None; // Deselect All weapons for (int i = 0; i < _weaponSlots.Count; i++) { OnWeaponPointerExit(i); } // Deselect all items for (int i = 0; i < _itemInfos.Count; i++) { OnItemPointerExit(i); } Repaint(false); }
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) //{ // //} // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { GoneWrong.WeaponControl weaponControl = animator.GetComponent <GoneWrong.WeaponControl>(); SharedInt equippedWeapon = weaponControl.equippedWeapon; Inventory playerInventory = weaponControl.playerInventory; // Get the weapon mount of the current equipped weapon 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; } // We need to keep track of weather we found rounds in our invventory if the weapon is partial // So we could stop the reload animation bool foundRounds = false; for (int i = 0; i < playerInventory.ammo.Count; i++) { // We get the ammo mount for each ammo we have InventoryAmmoMount ammoMount = playerInventory.ammo[i]; if (ammoMount == null || ammoMount.item == null) { continue; } // We check if the ammo applies to the equipped weapon if (ammoMount.item.weapon == weaponMount.item) { // If the weapon reload type is partial, we just add one single bullet if (weaponMount.item.partialReload) { // If we have attained the last round that can be loaded, then we force ourselves to stop reloading if (weaponMount.rounds >= weaponMount.item.ammoCapacity) { animator.SetBool("Reload", false); } foundRounds = true; break; } } } if (weaponMount.item.partialReload && !foundRounds) { animator.SetBool("Reload", false); } }
public void DoReload() { // Get the weapon mount 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; } // Rounds needed int roundsNeeded = weaponMount.item.ammoCapacity - weaponMount.rounds; for (int i = 0; i < _playerInventory.ammo.Count; i++) { // We get the ammo mount for each ammo we have InventoryAmmoMount ammoMount = _playerInventory.ammo[i]; if (ammoMount == null || ammoMount.item == null) { continue; } // We check if the ammo applies to the equipped weapon if (ammoMount.item.weapon == weaponMount.item) { // If the weapon reload type is partial, we just add one single bullet if (weaponMount.item.partialReload) { weaponMount.rounds++; ammoMount.rounds--; // If the ammo mount no longer contains ammo, then we remove the ammo mount from our inventory if (ammoMount.rounds == 0) { _playerInventory.ammo.RemoveAt(i); // Repainte the playerInventory canvas if (PlayerInventoryUI.instance != null) { PlayerInventoryUI.instance.Repaint(false); } } // If we have attained the last round that can be loaded, then we force ourselves to stop reloading if (weaponMount.rounds >= weaponMount.item.ammoCapacity) { _animator.SetBool(_realoadWeaponHash, false); } break; } // If the weapon reload type is non partial (a whole magazine at a time) else { if (ammoMount.rounds > roundsNeeded) { weaponMount.rounds += roundsNeeded; ammoMount.rounds -= roundsNeeded; roundsNeeded = 0; break; } else { roundsNeeded -= ammoMount.rounds; weaponMount.rounds += ammoMount.rounds; _playerInventory.ammo.RemoveAt(i); if (PlayerInventoryUI.instance != null) { PlayerInventoryUI.instance.Repaint(false); } } } } } }
public bool Reload() { // Get the weapon mount 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; } // Check if we have more ammo int bestFitAmmoIndex = -1; for (int i = 0; i < _playerInventory.ammo.Count; i++) { InventoryAmmoMount ammoMount = _playerInventory.ammo[i]; if (ammoMount == null || ammoMount.item == null) { continue; } if (ammoMount.item.weapon == weaponMount.item) { if (ammoMount.rounds > bestFitAmmoIndex) { bestFitAmmoIndex = i; } } } // Return if we don't find ammo in our backpack if (bestFitAmmoIndex == -1) { return(false); } _animator.SetBool(_isShootingHash, false); // We check if the number of rounds inside the weapon is inferior to the weapon capacity if (weaponMount.rounds < weaponMount.item.ammoCapacity && !_animator.GetCurrentAnimatorStateInfo(0).IsName("Reload")) { // If the weapon is of partial reload, we set realod to true because it's a loop animation if (weaponMount.item.partialReload) { _animator.SetBool(_realoadWeaponHash, true); } // Else, we just trigger the loop animation else { _animator.SetTrigger(_realoadWeaponHash); } return(true); } return(false); }
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(); } } }
private void Update() { // Get the weapon mount of the current equipped weapon 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; } // For Screen ammo if (_playerInventory != null && _equippedWeapon != null && _equippedWeapon.value >= 0 && _equippedWeapon.value < 3 ) { if (weaponMount.item != null) { // Activating ammo text _ammoPanel.gameObject.SetActive(true); _equippedRoundsText.text = weaponMount.rounds + ""; // Walk through all the remaining ammo in our backpack an add them to the remaining amm text int remainingRounds = 0; for (int i = 0; i < _playerInventory.ammo.Count; i++) { if (_playerInventory.ammo[i] != null && _playerInventory.ammo[i].item != null && _playerInventory.ammo[i].item.weapon == weaponMount.item) { remainingRounds += _playerInventory.ammo[i].rounds; } } _remainingRoundsText.text = remainingRounds + ""; } } else { // Deactivating ammo text _ammoPanel.gameObject.SetActive(false); } // For screan health if (_healthSlider != null && _healthSharedFloat != null) { _healthSlider.value = _healthSharedFloat.value / 100f; if (_healthText != null) { _healthText.text = "Health: " + _healthSharedFloat.value + "%"; } } // For screan stamina if (_staminaSlider != null && _staminaSharedFloat != null) { _staminaSlider.value = _staminaSharedFloat.value / 100f; int staminaValue = (int)_staminaSharedFloat.value; if (_staminaText != null) { _staminaText.text = "Stamina: " + staminaValue.ToString() + "%"; } } // For screan infection if (_infectionSlider != null && _infectionSharedFloat != null) { _infectionSlider.value = _infectionSharedFloat.value / 100f; if (_infectionText != null) { _infectionText.text = "Infection: " + _infectionSharedFloat.value.ToString() + "%"; } } }
public void InitializeInventory() { _rifle1 = new InventoryWeaponMount(); _rifle2 = new InventoryWeaponMount(); _handgun = new InventoryWeaponMount(); _melee = new InventoryWeaponMount(); _ammo.Clear(); _consumables.Clear(); _messages.Clear(); if (_rifle1Mount.item != null) { _rifle1.rounds = _rifle1Mount.rounds; _rifle1.item = _rifle1Mount.item; } if (_rifle2Mount.item != null) { _rifle2.rounds = _rifle2Mount.rounds; _rifle2.item = _rifle2Mount.item; } if (_handgunMount.item != null) { _handgun.rounds = _handgunMount.rounds; _handgun.item = _handgunMount.item; } if (_meleeMount.item != null) { _melee.item = _meleeMount.item; } for (int i = 0; i < _ammoMounts.Count; i++) { if (_ammoMounts[i] == null) { continue; } InventoryAmmoMount ammoMount = new InventoryAmmoMount(); ammoMount.rounds = _ammoMounts[i].rounds; ammoMount.item = _ammoMounts[i].item; if (!_ammo.Contains(_ammoMounts[i])) { _ammo.Add(ammoMount); } } for (int i = 0; i < _consumableMounts.Count; i++) { if (_consumableMounts[i] == null) { continue; } InventoryConsumableMount consumableMount = new InventoryConsumableMount(); consumableMount.item = _consumableMounts[i].item; if (!_consumables.Contains(_consumableMounts[i])) { _consumables.Add(consumableMount); } } for (int i = 0; i < _messagesMounts.Count; i++) { if (_messagesMounts[i] == null) { continue; } if (!_messages.Contains(_messagesMounts[i])) { _messages.Add(_messagesMounts[i]); } } }
public void ReplaceWeapon(bool atStart = false, InventoryWeaponMount theWeaponMount = null, int whichRifle = -1) { if (theWeaponMount.item == null) { return; } // We first get the weapon holder that we are gonna need GameObject weaponHolder = GoneWrong.Player.instance.weaponHolder; // Deciding which rifle we are gonna replace in case the weaponmount item is of type rifle if (whichRifle == -1) { whichRifle = _playerInventory.rifle1.item == null ? 1 : 2; } // Get the weapon to drop InventoryWeaponMount weaponToDrop = null; // We don't drop any weapon if we are the start of the game if (!atStart) { if (theWeaponMount.item.weaponType == WeaponType.Handgun) { weaponToDrop = _playerInventory.handgun; } else if (theWeaponMount.item.weaponType == WeaponType.Melee) { weaponToDrop = _playerInventory.melee; } else if (theWeaponMount.item.weaponType == WeaponType.Rifle) { weaponToDrop = whichRifle == 1 ? _playerInventory.rifle1 : _playerInventory.rifle2; } } // Then we drop the weapon here: if (weaponToDrop != null && weaponToDrop.item != null && weaponToDrop.item.collectableWeapon != null /* We only replace when the weapon that's about to be equipped is different from the existing one*/ && weaponToDrop.item != this._weaponMount.item // We don't have to drop anything if it's the start of the game && !atStart) { // We instantiate the collectable weapon CollectableWeapon collectableWeapon = Instantiate(weaponToDrop.item.collectableWeapon, transform.position, Quaternion.identity); // Then we assign the number of rounds to the collectable weapon collectableWeapon._weaponMount.rounds = weaponToDrop.rounds; // Then we unregister the weapon in player and remove it from the hierarchy // Then we find the weapon to dismiss from the hierarchy through its name string weaponName = collectableWeapon._weaponControl.name; if (weaponHolder != null) { Transform weaponControlTransform = weaponHolder.transform.Find(weaponName + "(Clone)"); // Then once it is unregistered, we just delete it from the hierarchy if (weaponControlTransform != null) { Destroy(weaponControlTransform.gameObject); } } } // We play the pick weapon audio sound if (GoneWrong.AudioManager.instance != null && _pickSound != null) { GoneWrong.AudioManager.instance.PlayOneShotSound(_pickSound, 1, 0, 0); } // Then we replace the weapon mount with the new one if (theWeaponMount.item.weaponType == WeaponType.Handgun) { _playerInventory.handgun = theWeaponMount; } else if (theWeaponMount.item.weaponType == WeaponType.Melee) { _playerInventory.melee = theWeaponMount; } else if (theWeaponMount.item.weaponType == WeaponType.Rifle) { if (whichRifle == 1) { _playerInventory.rifle1 = theWeaponMount; } else { _playerInventory.rifle2 = theWeaponMount; } } // Then we add the weaponControl to the player weaponHolder and register it // We instantiate the weaponControl anywhere in the scene first if (_weaponControl != null) { // Get the temporary position and rotation of the weaponcontrol Vector3 position = _weaponControl.transform.position; Quaternion rotation = _weaponControl.transform.rotation; GoneWrong.WeaponControl weaponControl = Instantiate(_weaponControl); // We make of the weapon holder the parent of the weapon control that we just instantiated if (weaponHolder != null) { weaponControl.transform.parent = weaponHolder.transform; weaponControl.transform.localPosition = position; weaponControl.transform.localRotation = rotation; // If we are at the start of the game (loading the weapon from the saved game or having it in the inventory by default), // then we deactivate the weapon (to increase game performance) if (atStart) { weaponControl.gameObject.SetActive(false); } } } // Then we switch to the raplacing weapon // We don't have to switch to anything if it's the start of the game if (!atStart) { SwitchWeapon(theWeaponMount, whichRifle); /*IEnumerator coroutine = SwitchWeapon(theWeaponMount, whichRifle); * StartCoroutine(coroutine);*/ } // Then we make the collectable weapon disappear from the scene if (!atStart) { Destroy(gameObject); } }