// -------------------------------------------------------------------------------------------- // Name : GetAvailableAmmo // Desc : Returns the number of bullets in the inventory for the passed ammo type. If true // is passed for the second parameter this will include bullet at the weapon mount // as well. // -------------------------------------------------------------------------------------------- public override int GetAvailableAmmo(InventoryItemAmmo ammo, bool includeWeapon) { if (!ammo) { return(0); } // Sum all ammo of the correct type on the ammo belt int roundCount = 0; for (int i = 0; i < _ammo.Count; i++) { InventoryAmmoMountInfo ammoMountInfo = _ammo[i]; if (ammoMountInfo.Ammo != ammo) { continue; } roundCount += ammoMountInfo.Rounds; } // Also include ammo at the weapon mount if requested and applicable if (includeWeapon) { for (int i = 0; i < 2; i++) { InventoryWeaponMountInfo weaponMountInfo = _weapons[i]; if (weaponMountInfo.Weapon == null || weaponMountInfo.Weapon.ammo != ammo) { continue; } roundCount += weaponMountInfo.InGunRounds; } } return(roundCount); }
protected bool AddAmmoItem(InventoryItemAmmo inventoryItemAmmo, CollectableItem collectableAmmo) { foreach (var a in _ammo) { if (a.Ammo == null) { a.Ammo = inventoryItemAmmo; inventoryItemAmmo.Pickup(collectableAmmo.transform.position); return(true); } } return(false); }
// -------------------------------------------------------------------------------------------- // Name : AddAmmoItem // Desc : Searches for the first available Ammo Mount and assigns the passed ammo to it. // -------------------------------------------------------------------------------------------- protected bool AddAmmoItem(InventoryItemAmmo inventoryItemAmmo, CollectableAmmo collectableAmmo, bool playAudio) { // Search for empty mount on ammo belt for (int i = 0; i < _ammo.Count; i++) { // A free mount is one with no Ammo assigned (.Ammo==null) if (_ammo[i].Ammo == null) { // Store this Ammo type at the mount _ammo[i].Ammo = inventoryItemAmmo; // Copy over instance data _ammo[i].Rounds = collectableAmmo.rounds; // Pickup inventoryItemAmmo.Pickup(collectableAmmo.transform.position, playAudio); // Broadcast that attempt was successful if (_notificationQueue) { _notificationQueue.Enqueue("Added " + inventoryItemAmmo.inventoryName + " to Ammo Belt"); } // Success so return return(true); } } // Broadcast that attempt was NOT successful if (_notificationQueue) { _notificationQueue.Enqueue("Could not pickup " + inventoryItemAmmo.inventoryName + "\nNo room in Ammo Belt"); } return(false); }
public abstract int GetAvailableAmmo(InventoryItemAmmo ammo, bool includeWeapon = false);
// -------------------------------------------------------------------------------------------- // Name : Invalidate // Desc : This function updates the UI so all elements reflect the current state of the // user's inventory. This function also resets the Inventory to an unselected // state. // -------------------------------------------------------------------------------------------- protected virtual void Invalidate() { // Make sure its initialized before its is rendered for the first time if (!_isInitialized) { Initialize(); } // Reset Selections _selectedPanelType = InventoryPanelType.None; _selectedMount = -1; // Deactivate Description Panels if (_generalDescriptionLayout.LayoutContainer != null) { _generalDescriptionLayout.LayoutContainer.SetActive(false); } if (_weaponDescriptionLayout.LayoutContainer != null) { _weaponDescriptionLayout.LayoutContainer.SetActive(false); } // Deactivate the action buttons if (_actionButton1.GameObject != null) { _actionButton1.GameObject.SetActive(false); } if (_actionButton2.GameObject != null) { _actionButton2.GameObject.SetActive(false); } // Clear the Weapon Mounts for (int i = 0; i < _weaponMounts.Count; i++) { if (_weaponMounts[i] != null) { if (_weaponMountImages[i] != null) { _weaponMountImages[i].sprite = null; } if (_weaponMountNames[i] != null) { _weaponMountNames[i].text = ""; } if (_weaponMountSliders[i] != null) { _weaponMountSliders[i].enabled = false; } _weaponMounts[i].SetActive(false); _weaponMounts[i].transform.GetComponent <Image>().fillCenter = false; } } // Iterate over the UI Backpack mounts and set all to empty and unselected for (int i = 0; i < _backpackMounts.Count; i++) { // Clear sprite and deactivate mount if (_backpackMountImages[i] != null) { _backpackMountImages[i].gameObject.SetActive(false); _backpackMountImages[i].sprite = null; } // Enable the text for this slot that says "EMPTY" if (_backpackMountText[i] != null) { _backpackMountText[i].gameObject.SetActive(true); } // Make all mounts look unselected if (_backpackMounts[i] != null) { // Get the image of the mount itself (the frame) Image img = _backpackMounts[i].GetComponent <Image>(); if (img) { img.fillCenter = false; img.color = _backpackMountColor; } } } // Configure the ammo slots for (int i = 0; i < _ammoMounts.Count; i++) { // Clear Sprite and deactivate mount if (_ammoMounts[i] != null) { if (_ammoMountImages[i]) { _ammoMountImages[i].gameObject.SetActive(false); _ammoMountImages[i].sprite = null; } } // Enable the text for this slot that says "EMPTY" if (_ammoMountEmptyText[i] != null) { _ammoMountEmptyText[i].gameObject.SetActive(true); } if (_ammoMountRoundsText[i] != null) { _ammoMountRoundsText[i].gameObject.SetActive(false); } // Give Mount Frame unselected look if (_ammoMounts[i] != null) { Image img = _ammoMounts[i].GetComponent <Image>(); if (img) { img.fillCenter = false; img.color = _ammoMountColor; } } } // Other PDA things if (_pDAReferences._autoplayOnPickup) { if (_audioPlayOnPickup) { _pDAReferences._autoplayOnPickup.isOn = true; } else { _pDAReferences._autoplayOnPickup.isOn = false; } } // Finally update the status panel if (_statusPanelUI.HealthSlider) { _statusPanelUI.HealthSlider.value = _health.value; } if (_statusPanelUI.InfectionSlider) { _statusPanelUI.InfectionSlider.value = _infection.value; } if (_statusPanelUI.StaminaSlider) { _statusPanelUI.StaminaSlider.value = _stamina.value; } if (_statusPanelUI.FlashlightSlider) { _statusPanelUI.FlashlightSlider.value = _flashlight.value; } if (_statusPanelUI.NightVisionSlider) { _statusPanelUI.NightVisionSlider.value = _nightvision.value; } // DO we have a valid Inventory Referenc. If so...let's paint it if (_inventory != null) { // Configure Weapons Panel by iterating through each mount for (int i = 0; i < _weaponMounts.Count; i++) { // Do we have a weapon mount here if (_weaponMounts[i] != null) { // Get the matching mount and weapon data from the inventory InventoryWeaponMountInfo weaponMountInfo = _inventory.GetWeapon(i); InventoryItemWeapon weapon = null; if (weaponMountInfo != null) { weapon = weaponMountInfo.Weapon; } // No weapon info here to skip this mount if (weapon == null) { continue; } // Set sprite and name of weapon if (_weaponMountImages[i] != null) { _weaponMountImages[i].sprite = weapon.inventoryImage; } if (_weaponMountNames[i] != null) { _weaponMountNames[i].text = weapon.inventoryName; } // If its a melee weapon then deactivate the entire AmmoInfo section of the UI // otherwise Enabled it and show the Reload Type and Rounds in Gun if (_weaponMountAmmoInfo[i] != null) { if (weapon.weaponFeedType == InventoryWeaponFeedType.Melee) { _weaponMountAmmoInfo[i].SetActive(false); } else { // Activate Mount _weaponMountAmmoInfo[i].SetActive(true); // Display Reload Type if (_weaponMountReloadType[i] != null) { _weaponMountReloadType[i].text = weapon.reloadType.ToString(); } if (_weaponMountRounds[i] != null) { _weaponMountRounds[i].text = weaponMountInfo.InGunRounds + " / " + weapon.ammoCapacity; } } } // Update the condition slider if (_weaponMountSliders[i] != null) { _weaponMountSliders[i].enabled = true; _weaponMountSliders[i].value = weaponMountInfo.Condition; } _weaponMounts[i].SetActive(true); } } // Configure Ammo Mounts for (int i = 0; i < _ammoMounts.Count; i++) { // Clear Sprite and deactivate mount if (_ammoMounts[i] != null) { // Get the ammo and it's mount info for this mount InventoryAmmoMountInfo ammoMountInfo = _inventory.GetAmmo(i); InventoryItemAmmo ammo = null; if (ammoMountInfo != null) { ammo = ammoMountInfo.Ammo; } // No weapon at this mount so skip if (ammo == null) { continue; } // Set image if (_ammoMountImages[i]) { _ammoMountImages[i].gameObject.SetActive(true); _ammoMountImages[i].sprite = ammoMountInfo.Ammo.inventoryImage; } // Set and Enable Rounds Text if (_ammoMountRoundsText[i] != null) { _ammoMountRoundsText[i].gameObject.SetActive(true); _ammoMountRoundsText[i].text = ammoMountInfo.Rounds.ToString(); } // Disable Empty text if (_ammoMountEmptyText[i] != null) { _ammoMountEmptyText[i].gameObject.SetActive(false); } } } // Iterate over the UI Backpack mounts and set all to empty and unselected for (int i = 0; i < _backpackMounts.Count; i++) { if (_backpackMounts[i] != null) { InventoryBackpackMountInfo backpackMountInfo = _inventory.GetBackpack(i); InventoryItem item = null; if (backpackMountInfo != null) { item = backpackMountInfo.Item; } if (item != null) { // Set sprite and activate mount if (_backpackMountImages[i] != null) { _backpackMountImages[i].gameObject.SetActive(true); _backpackMountImages[i].sprite = item.inventoryImage; } // Disable the text for this slot that says "EMPTY" if (_backpackMountText[i] != null) { _backpackMountText[i].gameObject.SetActive(false); } } } } } }
// -------------------------------------------------------------------------------------------- // Name : ReloadWeapon // Desc : This function can be called from the InventoryUI and the main game code. Usually, // We will want to play the audio when at the inventory screen but pass false to this // function when calling from real time. // -------------------------------------------------------------------------------------------- public override bool ReloadWeapon(int mountIndex, bool playAudio = true) { // Invalid mount index (This invetory supports only 2 weapon mounts if (mountIndex < 0 || mountIndex > 1) { return(false); } // Get weapon mount and return if no weapon assigned InventoryWeaponMountInfo weaponMountInfo = _weapons[mountIndex]; if (weaponMountInfo.Weapon == null) { return(false); } // To reduce typing :) InventoryItemWeapon weapon = weaponMountInfo.Weapon; InventoryItemAmmo ammo = weapon.ammo; // If no ammo assigned or this mount is already at the gun's capacity...just bail if (ammo == null || weaponMountInfo.InGunRounds >= weapon.ammoCapacity) { return(false); } // If its a Non-Partial Reload type then simply search the belt for a clip // with the most bullets if (weapon.reloadType == InventoryWeaponReloadType.NonPartial) { // Search for a clip in our belt that has the highest round count // that matches the ammoID of the gun int ammoMountCandidate = -1; int roundCount = -1; for (int i = 0; i < _ammo.Count; i++) { InventoryAmmoMountInfo ammoMountInfo = _ammo[i]; // If ammo at this mount is not the correct type of ammo skip it if (ammoMountInfo.Ammo != ammo) { continue; } // If the ammo we have found has more rounds than currently stored // at then record this as the best candidate for a swap if (ammoMountInfo.Rounds > roundCount) { roundCount = ammoMountInfo.Rounds; ammoMountCandidate = i; } } // If we didn't find any matching ammo or if we found no ammo // with more rounds in then is currently in the gun ... bail if (ammoMountCandidate == -1 || roundCount <= weaponMountInfo.InGunRounds) { return(false); } // We have found some ammo we can use so consume it. First // remember how many rounds were in the clip that is in the gun currently int oldInGunRounds = weaponMountInfo.InGunRounds; // Set the round count to that of the new clip weaponMountInfo.InGunRounds = _ammo[ammoMountCandidate].Rounds; // In terms of the inventory...using a weapon is reloading it. Firing the weapon // is handled somewhere else entirely weapon.Use(Vector3.zero, playAudio); // If no rounds where in the gun before the reload, we have nothing to // swap with the mount so remove the item from the mount if (oldInGunRounds == 0) { // Clear the mount _ammo[ammoMountCandidate].Ammo = null; // Lets now create an instance of the ammo type in the scene // to simulate the clip we have ejected from the gun Vector3 position = _playerPosition != null ? _playerPosition.value : Vector3.zero; position += _playerDirection != null ? _playerDirection.value : Vector3.zero; CollectableAmmo sceneAmmo = ammo.Drop(position, playAudio) as CollectableAmmo; // Now we must configure the scene ammo we just instantiated to reflect the number // of rounds that was in the gun if (sceneAmmo) { sceneAmmo.rounds = 0; } } // Otherwise simply swap the the clip else { _ammo[ammoMountCandidate].Rounds = oldInGunRounds; } } else if (weapon.reloadType == InventoryWeaponReloadType.Partial) { // If the gun is full, abort reload int roundsWanted = weapon.ammoCapacity - weaponMountInfo.InGunRounds; if (roundsWanted < 1) { return(false); } // Loop through items on ammo belt searching for the correct ammo type for (int i = 0; i < _ammo.Count; i++) { // If not the right type of ammo then continue InventoryAmmoMountInfo ammoMountInfo = _ammo[i]; if (ammoMountInfo.Ammo != weapon.ammo) { continue; } // Otherwise we have found some ammo so lets remove // some items from it int ammoTaken = Mathf.Min(roundsWanted, ammoMountInfo.Rounds); weaponMountInfo.InGunRounds += ammoTaken; ammoMountInfo.Rounds -= ammoTaken; roundsWanted -= ammoTaken; // Use the weapon to reload it weapon.Use(Vector3.zero, playAudio); // If we have emptied this ammo item then remove from belt if (ammoMountInfo.Rounds == 0) { ammoMountInfo.Ammo = null; } // If the gun is full then return and don't continue to seek if (roundsWanted <= 0) { break; } } } // Weapon Successfully Reload return(true); }
protected virtual void Invalidate() { _selectedPanelType = InventoryPanelType.None; if (_weaponMount != null) { if (_weaponMountImage != null) { _weaponMountImage.sprite = null; } if (_weaponMountName != null) { _weaponMountName.text = ""; } if (_weaponMountWeight != null) { _weaponMountWeight.text = ""; } if (_weaponMountID != null) { _weaponMountID.text = ""; } if (_weaponMountRounds != null) { _weaponMountRounds.text = ""; } _weaponMount.SetActive(false); _weaponMount.transform.GetComponent <Image>().fillCenter = false; } if (_foodMount != null) { if (_foodMountImage != null) { _foodMountImage.sprite = null; } if (_foodMountName != null) { _foodMountName.text = ""; } if (_foodMountWeight != null) { _foodMountWeight.text = ""; } if (_foodMountID != null) { _foodMountID.text = ""; } _foodMount.SetActive(false); _foodMount.transform.GetComponent <Image>().fillCenter = false; } if (_ammoMount != null) { if (_ammoMountImage != null) { _ammoMountImage.sprite = null; } if (_ammoMountName != null) { _ammoMountName.text = ""; } if (_ammoMountWeight != null) { _ammoMountWeight.text = ""; } if (_ammoMountID != null) { _ammoMountID.text = ""; } _ammoMount.SetActive(false); _ammoMount.transform.GetComponent <Image>().fillCenter = false; } if (_inventory != null) { if (_weaponMount != null) { var weaponMountInfo = _inventory.GetWeapon(0); InventoryItemWeapon weapon = null; if (weaponMountInfo != null) { weapon = weaponMountInfo.Weapon; } if (weapon != null) { if (_weaponMountImage != null) { _weaponMountImage.sprite = weapon.Image; } if (_weaponMountName != null) { _weaponMountName.text = weapon.Name; } if (_weaponMountWeight != null) { _weaponMountWeight.text = weapon.Weight.ToString() + " kg"; } if (_weaponMountID != null) { _weaponMountID.text = weapon.Identifier; } if (_weaponMountRounds != null) { _weaponMountRounds.text = weaponMountInfo.Rounds.ToString(); } _weaponMount.SetActive(true); } } if (_foodMount != null) { var foodMountInfo = _inventory.GetFood(0); InventoryItemFood food = null; if (foodMountInfo != null) { food = foodMountInfo.Item; } if (food != null) { if (_foodMountImage != null) { _foodMountImage.sprite = food.Image; } if (_foodMountName != null) { _foodMountName.text = food.Name; } if (_foodMountWeight != null) { _foodMountWeight.text = food.Weight.ToString() + " kg"; } if (_foodMountID != null) { _foodMountID.text = food.Identifier; } _foodMount.SetActive(true); } } if (_ammoMount != null) { var ammoMountInfo = _inventory.GetAmmo(0); InventoryItemAmmo ammo = null; if (ammoMountInfo != null) { ammo = ammoMountInfo.Ammo; } if (ammo != null) { if (_ammoMountImage != null) { _ammoMountImage.sprite = ammo.Image; } if (_ammoMountName != null) { _ammoMountName.text = ammo.Name; } if (_ammoMountWeight != null) { _ammoMountWeight.text = ammo.Weight.ToString() + " kg"; } if (_ammoMountID != null) { _ammoMountID.text = ammo.Identifier; } _ammoMount.SetActive(true); } } } }