/// <summary> /// upon success, drops all the player's weapons /// </summary> protected virtual void TryDropAllWeapons() { if (WeaponHandler == null) { return; } foreach (KeyValuePair <vp_Weapon, vp_ItemIdentifier> w in Inventory.WeaponIdentifiers) { vp_Weapon weapon = w.Key; vp_ItemIdentifier weaponIdentifier = w.Value; // skip if weapon identifier has no item type vp_ItemType itemType = weaponIdentifier.GetItemType(); if (itemType == null) { continue; } // for unitbank weapons, try and set the weapon pickup's ammo // to the amount of ammo in the dropped weapon int units = 0; vp_UnitBankInstance unitBank = Inventory.GetUnitBankInstanceOfWeapon(weapon); if (unitBank != null) { units = unitBank.Count; } // drop the weapon TryDropWeapon(itemType, units, weaponIdentifier.ID); } }
/// <summary> /// /// </summary> protected virtual vp_ItemIdentifier GetWeaponIdentifier(int index) { if (!Application.isPlaying) { return(null); } if (!m_WeaponIdentifiers.TryGetValue(index, out m_WeaponIdentifierResult)) { if ((index < 1) || index > (WeaponHandler.Weapons.Count)) { return(null); } if (WeaponHandler.Weapons[index - 1] == null) { return(null); } m_WeaponIdentifierResult = WeaponHandler.Weapons[index - 1].GetComponent <vp_ItemIdentifier>(); if (m_WeaponIdentifierResult == null) { return(null); } if (m_WeaponIdentifierResult.Type == null) { return(null); } m_WeaponIdentifiers.Add(index, m_WeaponIdentifierResult); } return(m_WeaponIdentifierResult); }
/// <summary> /// /// </summary> protected virtual vp_ItemIdentifier GetWeaponIdentifier(int index) { if (!Application.isPlaying) return null; if (!m_WeaponIdentifiers.TryGetValue(index, out m_WeaponIdentifierResult)) { if ((index < 1) || index > (WeaponHandler.Weapons.Count)) return null; if (WeaponHandler.Weapons[index - 1] == null) return null; m_WeaponIdentifierResult = WeaponHandler.Weapons[index - 1].GetComponent<vp_ItemIdentifier>(); if (m_WeaponIdentifierResult == null) return null; if (m_WeaponIdentifierResult.Type == null) return null; m_WeaponIdentifiers.Add(index, m_WeaponIdentifierResult); } return m_WeaponIdentifierResult; }
/// <summary> /// upon success, unwields the current weapon of the player and waits /// for a little bit before tossing the weapon /// </summary> public virtual void TryDropCurrentWeapon(float pauseForUnwield = 0.3f) { if (WeaponHandler == null) { return; } if (WeaponHandler.CurrentWeapon == null) { return; } vp_ItemIdentifier identifier = WeaponHandler.CurrentWeapon.GetComponent <vp_ItemIdentifier>(); if (identifier == null) { return; } vp_ItemType itemType = identifier.GetItemType(); if (itemType == null) { return; } int units = Inventory.GetAmmoInCurrentWeapon(); Player.Unwield.Send(); vp_Timer.In(Mathf.Max(pauseForUnwield, 0.0f), () => { TryDropWeapon(itemType, units); }); }
/// <summary> /// tries to drop the currenlty loaded throwing weapon unit in the form /// of an ammo unit (rather than in the form of a weapon) /// </summary> protected virtual void TryDropLoadedThrowingUnit(vp_UnitType unitType) { vp_UnitBankType unitBankType = Inventory.GetThrowingWeaponUnitBankType(unitType); if (unitBankType == null) { return; } for (int v = (WeaponHandler.Weapons.Count - 1); v > -1; v--) { if (WeaponHandler.Weapons[v] == null) { continue; } vp_ItemIdentifier identifier = WeaponHandler.Weapons[v].GetComponent <vp_ItemIdentifier>(); if (identifier == null) { continue; } vp_ItemType iType = identifier.GetItemType(); if (iType == null) { continue; } if (iType != unitBankType) { continue; } for (int u = (Inventory.UnitBankInstances.Count - 1); u > -1; u--) { //Debug.Log("unittype: " + Inventory.UnitBankInstances[u].Type + ", count: " + Inventory.UnitBankInstances[u].Count); if (Inventory.UnitBankInstances[u].UnitType == unitBankType.Unit) { // if the throwing weapon is currently loaded with one unit (likely), move that unit // from the throwing weapon to the internal unitbank and drop it as an ammo clip if (Inventory.UnitBankInstances[u].Count == 1) { // move unit to internal unitbank Inventory.UnitBankInstances[u].DoRemoveUnits(1); vp_UnitBankInstance internalUnitBank = Inventory.GetInternalUnitBank(Inventory.UnitBankInstances[u].UnitType); internalUnitBank.DoAddUnits(1); // drop the moved unit if (TryDropAmmoClip(Inventory.UnitBankInstances[u].UnitType.name, 1)) { //Debug.Log("Dropped: " + Inventory.UnitBankInstances[v].UnitType.name + ",1"); } continue; } } } } }
/// <summary> /// analyzes the player weapons and inventory unitbank and unit types to /// figure out what weapons are throwing weapons, and caches this info /// </summary> protected virtual void StoreThrowingWeaponInfo() { foreach (vp_Weapon weapon in WeaponHandler.Weapons) { // if the weapon's animation type is 'thrown' ... if (!(weapon.AnimationType == (int)vp_Weapon.Type.Thrown)) { continue; } // ... and it has an item identifier ... vp_ItemIdentifier identifier = weapon.GetComponent <vp_ItemIdentifier>(); if (identifier == null) { continue; } // ... and the identifier is for a unitbank ... vp_UnitBankType unitBankType = (identifier.GetItemType() as vp_UnitBankType); if (unitBankType == null) { continue; } // --- then consider it a throwing weapon unitbank type --- // store the unitbank type under its unit type if (!m_ThrowingWeaponUnitBankTypes.ContainsKey(unitBankType.Unit)) { m_ThrowingWeaponUnitBankTypes.Add(unitBankType.Unit, unitBankType); } // store the unit type as known throwing weapon ammo if (!m_ThrowingWeaponUnitTypes.Contains(unitBankType.Unit)) { m_ThrowingWeaponUnitTypes.Add(unitBankType.Unit); } // if the inventory has a unitbank instance for this weapon ... vp_UnitBankInstance unitBankInstance = GetUnitBankInstanceOfWeapon(weapon); if (unitBankInstance == null) { continue; } // ... then store it by its unitbank type if (!m_ThrowingWeaponUnitBankInstances.ContainsKey(unitBankType)) { m_ThrowingWeaponUnitBankInstances.Add(unitBankType, unitBankInstance); } } m_HaveThrowingWeaponInfo = true; }
/// <summary> /// /// </summary> public vp_ItemInstance GetItemInstanceOfWeapon(vp_Weapon weapon) { vp_ItemIdentifier itemIdentifier = GetWeaponIdentifier(weapon); if (itemIdentifier == null) { return(null); } vp_ItemInstance ii = GetItem(itemIdentifier.Type); return(ii); }
/// <summary> /// returns true if the inventory contains a weapon by the index /// fed as an argument to the 'SetWeapon' activity. false if not. /// this is used to regulate which weapons the player currently /// has access to. /// </summary> protected virtual bool CanStart_SetWeapon() { int index = (int)Player.SetWeapon.Argument; if (index == 0) { return(true); } if ((index < 1) || index > (WeaponHandler.Weapons.Count)) { return(false); } vp_ItemIdentifier weaponIdentifier = GetWeaponIdentifier(WeaponHandler.Weapons[index - 1]); if (weaponIdentifier == null) { return(MissingIdentifierError(index)); } bool haveItem = HaveItem(weaponIdentifier.Type, weaponIdentifier.ID); // see if weapon is thrown if (haveItem && (vp_Weapon.Type)WeaponHandler.Weapons[index - 1].AnimationType == vp_Weapon.Type.Thrown) { if (GetAmmoInWeapon(WeaponHandler.Weapons[index - 1]) < 1) { vp_UnitBankType uType = weaponIdentifier.Type as vp_UnitBankType; if (uType == null) { Debug.LogError("Error (" + this + ") Tried to wield thrown weapon " + WeaponHandler.Weapons[index - 1] + " but its item identifier does not point to a UnitBank."); return(false); } else { if (!TryReload(uType, weaponIdentifier.ID)) // NOTE: ID might not work for identification here because of multiplayer add-on pickup logic { //Debug.Log("uType: " + uType + ", weapon.ID: " + weapon.ID); //Debug.Log("failed because: no thrower wielded and no extra ammo"); return(false); } } //Debug.Log("success because: no thrower wielded but we have extra ammo"); } //else //Debug.Log("success because: thrower wielded"); } return(haveItem); }
/// <summary> /// returns true if the inventory contains a weapon by the /// index fed as an argument to the 'SetWeapon' activity. /// false if not. this is used to regulate which weapons the /// player currently has access to. /// </summary> protected virtual bool CanStart_SetWeapon() { int index = (int)Player.SetWeapon.Argument; if (index == 0) { return(true); } vp_ItemIdentifier weapon = GetWeaponIdentifier(index); if (weapon == null) { if ((index < 1) || index > (WeaponHandler.Weapons.Count)) { return(false); } return(MissingIdentifierError(index)); } return(HaveItem(weapon.Type, weapon.ID)); }
/// <summary> /// /// </summary> protected virtual vp_ItemIdentifier GetWeaponIdentifier(vp_Weapon weapon) { if (!Application.isPlaying) { return(null); } if (weapon == null) { return(null); } if (!WeaponIdentifiers.TryGetValue(weapon, out m_WeaponIdentifierResult)) { if (weapon == null) { return(null); } m_WeaponIdentifierResult = weapon.GetComponent <vp_ItemIdentifier>(); if (m_WeaponIdentifierResult == null) { return(null); } if (m_WeaponIdentifierResult.Type == null) { return(null); } WeaponIdentifiers.Add(weapon, m_WeaponIdentifierResult); } return(m_WeaponIdentifierResult); }
private void AddAmmo() { vp_ItemIdentifier item = inventory.CurrentWeaponIdentifier; if (item == null) { return; } if (currentClip) { vp_UnitBankInstance bank = (inventory.GetItem(item.Type, item.ID) as vp_UnitBankInstance); bank.Count += 1; bank.ClampToCapacity(); } else { vp_UnitBankType type = (vp_UnitBankType)item.Type; if (m_Player.AddAmmo.Try(type.Unit)) { inventory.TryGiveUnits(type.Unit, 1); } } }
/// <summary> /// /// </summary> protected virtual vp_ItemIdentifier GetWeaponIdentifier(vp_Weapon weapon) { if (!Application.isPlaying) return null; if (weapon == null) return null; if (!WeaponIdentifiers.TryGetValue(weapon, out m_WeaponIdentifierResult)) { if (weapon == null) return null; m_WeaponIdentifierResult = weapon.GetComponent<vp_ItemIdentifier>(); if (m_WeaponIdentifierResult == null) return null; if (m_WeaponIdentifierResult.Type == null) return null; WeaponIdentifiers.Add(weapon, m_WeaponIdentifierResult); } return m_WeaponIdentifierResult; }
/// <summary> /// sets up simplified 3rd person weapon child objects based on the /// current first person weapons under the vp_FPCamera component /// </summary> public static void ConvertWeaponsTo3rdPerson(GameObject player) { int weaponsConverted = 0; Transform oldWeaponGroup = null; // typically the 'FPSCamera' object GameObject newWeaponGroup = new GameObject("Weapons"); foreach (vp_FPWeapon fpWeapon in player.transform.GetComponentsInChildren <vp_FPWeapon>()) { // detect old weapons parent and put the new one alongside it in hierarchy if (oldWeaponGroup == null) { oldWeaponGroup = fpWeapon.transform.parent; newWeaponGroup.transform.parent = oldWeaponGroup.parent; newWeaponGroup.transform.localPosition = Vector3.zero; newWeaponGroup.transform.localEulerAngles = Vector3.zero; newWeaponGroup.transform.localScale = Vector3.one; } // create a new gameobject for each weapon. this way we get rid of // any custom components that may be hard for the editor code to // delete because of component interdependencies GameObject newWeaponGO = new GameObject(fpWeapon.gameObject.name); newWeaponGO.transform.parent = newWeaponGroup.transform; // NOTE: position of this transform is irrelevant because a 3rd // person vp_Weapon is just an invisible, locigal entity, represented // by its '3rdPersonWeapon' gameobject reference in the 3d world. // still, let's place the weapons in a fairly neutral position for // clarity: say, the center of the player facing forwards newWeaponGO.transform.localPosition = Vector3.up; newWeaponGO.transform.localEulerAngles = Vector3.zero; newWeaponGO.transform.localScale = Vector3.one; // convert vp_FPWeapon component into a vp_Weapon component on the new gameobject vp_Weapon tdpWeapon = newWeaponGO.AddComponent <vp_Weapon>(); vp_EditorUtility.CopyValuesFromDerivedComponent(fpWeapon, tdpWeapon, true, true, null); if (m_CopyStates && !string.IsNullOrEmpty(m_StatePath)) { vp_EditorUtility.GenerateStatesAndPresetsFromDerivedComponent(fpWeapon, tdpWeapon, m_StatePath); } // FP shooters (used for projectile weapons) vp_FPWeaponShooter fpShooter = fpWeapon.GetComponent <vp_FPWeaponShooter>(); if (fpShooter != null) { vp_WeaponShooter tdpFpShooter = newWeaponGO.AddComponent <vp_WeaponShooter>(); vp_EditorUtility.CopyValuesFromDerivedComponent(fpShooter, tdpFpShooter, true, true, null); if (m_CopyStates && !string.IsNullOrEmpty(m_StatePath)) { vp_EditorUtility.GenerateStatesAndPresetsFromDerivedComponent(fpShooter, tdpFpShooter, m_StatePath); } } // regular shooters (used for melee weapons) vp_WeaponShooter shooter = fpWeapon.GetComponent <vp_WeaponShooter>(); if ((shooter != null) && !(shooter is vp_FPWeaponShooter)) { vp_WeaponShooter tdpShooter = newWeaponGO.AddComponent <vp_WeaponShooter>(); vp_EditorUtility.CopyValuesFromDerivedComponent(shooter, tdpShooter, true, true, null); if (m_CopyStates && !string.IsNullOrEmpty(m_StatePath)) { vp_EditorUtility.GenerateStatesAndPresetsFromDerivedComponent(shooter, tdpShooter, m_StatePath); } } // reloaders vp_FPWeaponReloader fpReloader = fpWeapon.GetComponent <vp_FPWeaponReloader>(); if (fpReloader != null) { vp_WeaponReloader tdpReloader = newWeaponGO.AddComponent <vp_WeaponReloader>(); vp_EditorUtility.CopyValuesFromDerivedComponent(fpReloader, tdpReloader, true, true, null); // reloader is no vp_Component so don't generate states & presets } // copy weapon thrower vp_FPWeaponThrower fpThrower = fpWeapon.GetComponent <vp_FPWeaponThrower>(); if (fpThrower != null) { vp_WeaponThrower tdpThrower = newWeaponGO.AddComponent <vp_WeaponThrower>(); vp_EditorUtility.CopyValuesFromDerivedComponent(fpWeapon, tdpThrower, true, true, null); } // copy item identifier vp_ItemIdentifier identifier = fpWeapon.GetComponent <vp_ItemIdentifier>(); if (identifier != null) { vp_ItemIdentifier newIdentifier = newWeaponGO.AddComponent <vp_ItemIdentifier>(); newIdentifier.Type = identifier.Type; } weaponsConverted++; } // delete the old weapon group (FPSCamera object) if (oldWeaponGroup != null) { GameObject.DestroyImmediate(oldWeaponGroup.gameObject); } EditorUtility.DisplayDialog( weaponsConverted + " weapons converted", (weaponsConverted > 0 ? ( ((m_CopyStates && !string.IsNullOrEmpty(m_StatePath)) ? ("Any states with compatible values have been retained and their presets can be found in the folder:\n\t\t'" + m_StatePath + "'") : "") ) : ""), "OK"); }
/// <summary> /// upon success, drops a weapon of the passed 'itemType' /// </summary> public virtual void TryDropWeapon(vp_ItemType itemType, int units = 0, int id = 0) { if (WeaponHandler == null) { return; } if (itemType == null) { return; } if (Player == null) { return; } if (Player.Reload.Active) { return; } if (Player.Attack.Active) { Player.Attack.Stop(); } for (int v = (WeaponHandler.Weapons.Count - 1); v > -1; v--) { if (WeaponHandler.Weapons[v] == null) { continue; } vp_ItemIdentifier identifier = WeaponHandler.Weapons[v].GetComponent <vp_ItemIdentifier>(); if (identifier == null) { continue; } vp_ItemType iType = identifier.GetItemType(); if (iType == null) { continue; } if (iType != itemType) { continue; } if (id <= 0) { TryDropItem(itemType, units, 0); return; } else { TryDropItem(itemType, units, identifier.ID); return; } } }