void OnWeaponSwitched(BasicWeapon newWeapon) { if (newWeapon != null) { newWeapon.ShowWeapon(true); } }
// Adds a weapon to our inventory public bool AddWeapon(BasicWeapon weaponPrefab) { // if we already hold this weapon type (a weapon coming from the same source prefab), don't add the weapon if (HasWeapon(weaponPrefab)) { return(false); } // search our weapon slots for the first free one, assign the weapon to it, and return true if we found one. Return false otherwise for (int i = 0; i < m_WeaponSlots.Length; i++) { // only add the weapon if the slot is free if (m_WeaponSlots[i] == null) { // spawn the weapon prefab as child of the weapon socket BasicWeapon weaponInstance = Instantiate(weaponPrefab, weaponParentSocket); weaponInstance.transform.localPosition = Vector3.zero; weaponInstance.transform.localRotation = Quaternion.identity; // Set owner to this gameObject so the weapon can alter projectile/damage logic accordingly weaponInstance.owner = gameObject; weaponInstance.sourcePrefab = weaponPrefab.gameObject; weaponInstance.ShowWeapon(false); // Assign the first person layer to the weapon int layerIndex = Mathf.RoundToInt(Mathf.Log(FPSWeaponLayer.value, 2)); // This function converts a layermask to a layer index foreach (Transform t in weaponInstance.gameObject.GetComponentsInChildren <Transform>(true)) { t.gameObject.layer = layerIndex; } m_WeaponSlots[i] = weaponInstance; if (onAddedWeapon != null) { onAddedWeapon.Invoke(weaponInstance, i); } return(true); } } // Handle auto-switching to weapon if no weapons currently if (GetActiveWeapon() == null) { SwitchWeapon(true); } return(false); }
// Updates the animated transition of switching weapons void UpdateWeaponSwitching() { // Calculate the time ratio (0 to 1) since weapon switch was triggered float switchingTimeFactor = 0f; if (weaponSwitchDelay == 0f) { switchingTimeFactor = 1f; } else { switchingTimeFactor = Mathf.Clamp01((Time.time - m_TimeStartedWeaponSwitch) / weaponSwitchDelay); } // Handle transiting to new switch state if (switchingTimeFactor >= 1f) { if (m_WeaponSwitchState == WeaponSwitchState.PutDownPrevious) { // Deactivate old weapon BasicWeapon oldWeapon = GetWeaponAtSlotIndex(activeWeaponIndex); if (oldWeapon != null) { oldWeapon.ShowWeapon(false); } activeWeaponIndex = m_WeaponSwitchNewWeaponIndex; switchingTimeFactor = 0f; // Activate new weapon BasicWeapon newWeapon = GetWeaponAtSlotIndex(activeWeaponIndex); if (onSwitchedToWeapon != null) { onSwitchedToWeapon.Invoke(newWeapon); } if (newWeapon) { m_TimeStartedWeaponSwitch = Time.time; m_WeaponSwitchState = WeaponSwitchState.PutUpNew; } else { // if new weapon is null, don't follow through with putting weapon back up m_WeaponSwitchState = WeaponSwitchState.Down; } } else if (m_WeaponSwitchState == WeaponSwitchState.PutUpNew) { m_WeaponSwitchState = WeaponSwitchState.Up; } } // Handle moving the weapon socket position for the animated weapon switching if (m_WeaponSwitchState == WeaponSwitchState.PutDownPrevious) { m_WeaponMainLocalPosition = Vector3.Lerp(defaultWeaponPosition.localPosition, downWeaponPosition.localPosition, switchingTimeFactor); } else if (m_WeaponSwitchState == WeaponSwitchState.PutUpNew) { m_WeaponMainLocalPosition = Vector3.Lerp(downWeaponPosition.localPosition, defaultWeaponPosition.localPosition, switchingTimeFactor); } }