// 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) { GoneWrong.WeaponControl weaponControl = animator.GetComponent <GoneWrong.WeaponControl>(); if (weaponControl != null) { weaponControl.isRunning = true; } }
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; }
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) //{ // //} // OnStateExit is called when a transition ends and the state machine finishes evaluating this state override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { GoneWrong.WeaponControl weaponControl = animator.GetComponent <GoneWrong.WeaponControl>(); weaponControl.isReloading = false; }
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); } }
// 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) { GoneWrong.WeaponControl weaponControl = animator.GetComponent <GoneWrong.WeaponControl>(); weaponControl.HandleSound(true); }
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); } }