private void OnTriggerStay(Collider other) { if (other.gameObject.CompareTag("Gun") || other.gameObject.CompareTag("GunWall") || other.gameObject.CompareTag("GeneratedGun")) { if (Input.GetAxis("EquipWeapon") > 0 && pickupTime > pickupTimeLimit) { pickupTime = 0.0f; GunParameters gunToEquip = other.gameObject.GetComponent <GunScript>().GetGunParameters(); GameObject.FindGameObjectWithTag("Inventory").GetComponent <InventoryScript>().AddWeaponToInventory(gunToEquip); if (other.gameObject.CompareTag("Gun")) { Destroy(other.gameObject); } else if (other.gameObject.CompareTag("GeneratedGun")) { try { other.gameObject.transform.parent.gameObject.GetComponent <WeaponSpawner>().weaponSpawned = false; } catch { Debug.Log("Can't reset weapon spawner, object cannot be found"); } Destroy(other.gameObject); } } } }
IEnumerator CreateNewGun() { GunParameters newGunParamters = GenerateNewGunParameters(); SpawnNewGun(newGunParamters); yield return(null); }
public void EquipWeapon() { try { GameObject currentWeaponGameObject = gameObject.transform.GetChild(0).gameObject; Destroy(currentWeaponGameObject); } catch { Debug.Log("No weapon equipped, cannot destroy child object"); } GunParameters weaponToEquip = weaponsInInventory[currentWeaponIndex]; if (weaponToEquip != null) { GameObject weaponToEquipObject = Instantiate(weaponToEquip.currentGunObject, gameObject.transform.position, gameObject.transform.rotation, gameObject.transform); currentWeapon = weaponToEquipObject; weaponToEquipObject.GetComponent <GunScript>().Equip(); gameObject.transform.localPosition = currentWeapon.GetComponent <GunScript>().GetGunParameters().camOffset; weaponToEquipObject.GetComponent <GunScript>().SetGunParameters(weaponToEquip); } else { Debug.Log("No weapon to equip"); } }
public void AddWeaponToInventory(GunParameters weaponToAdd) { for (int i = 0; i < weaponsInInventory.Count; i++) { if (weaponsInInventory[i] == null) { weaponsInInventory[i] = weaponToAdd; if (i == currentWeaponIndex) { EquipWeapon(); } return; } } if (weaponsInInventory.Count >= inventorySize) { UnequipCurrentWeapon(); weaponsInInventory[currentWeaponIndex] = weaponToAdd; EquipWeapon(); } else { weaponsInInventory.Add(weaponToAdd); } }
// Update is called once per frame void Update() { if (Input.GetAxis("WeaponSlot1") > 0 && currentWeaponIndex != 0) { currentWeaponIndex = 0; GunParameters gunToEquip = weaponsInInventory[currentWeaponIndex]; if (gunToEquip != null) { EquipWeapon(); } return; } if (Input.GetAxis("WeaponSlot2") > 0 && currentWeaponIndex != 1) { currentWeaponIndex = 1; GunParameters gunToEquip = weaponsInInventory[currentWeaponIndex]; if (gunToEquip != null) { EquipWeapon(); } return; } if (Input.GetAxis("UnequipWeapon") > 0) { UnequipCurrentWeapon(); } }
GunParameters GenerateNewGunParameters() { GameObject inventoryObject = GameObject.FindGameObjectWithTag("Inventory"); GunParameters gunParameters1; GunParameters gunParameters2; GunParameters newParameters = null; if (inventoryObject != null) { InventoryScript inventory = inventoryObject.GetComponent <InventoryScript>(); List <GunParameters> listOfGunParameters = inventory.GetWeaponsInInventory(); int numOfEquippedGuns = 0; foreach (GunParameters gp in listOfGunParameters) { if (gp != null) { numOfEquippedGuns += 1; } } if (numOfEquippedGuns >= 2) { gunParameters1 = listOfGunParameters[0]; gunParameters2 = listOfGunParameters[1]; GunType newGunType = GenerateNewGunType(); int newDamage = GenerateNewGunDamage(gunParameters1.damage, gunParameters2.damage); int newMaxAmmo = GenerateNewGunMaxAmmo(gunParameters1.maxAmmo, gunParameters2.maxAmmo); float newRateOfFire = GenerateNewGunRateOfFire(gunParameters1.rateOfFire, gunParameters2.rateOfFire); float newReloadSpeed = GenerateNewGunReloadSpeed(gunParameters1.reloadSpeed, gunParameters2.reloadSpeed); float newHitscanRange = GenerateNewGunHitscanRange(gunParameters1.hitscanRange, gunParameters2.hitscanRange); GameObject newGameObject = GenerateNewGameObject(gunParameters1.currentGunObject, gunParameters2.currentGunObject); ProjectileParameters newProjectileParams = GenerateNewProjectileParameters(gunParameters1.projectileParameters, gunParameters2.projectileParameters); newParameters = new GunParameters(newGunType, newDamage, newMaxAmmo, newRateOfFire, newReloadSpeed, newHitscanRange, newGameObject, newProjectileParams); } else if (numOfEquippedGuns == 1) { foreach (GunParameters gp in listOfGunParameters) { if (gp != null) { newParameters = gp; } } } } return(newParameters); }
void SpawnNewGun(GunParameters parametersToUse) { if (parametersToUse != null) { GameObject gunToSpawn = Instantiate(parametersToUse.currentGunObject, Vector3.zero, Quaternion.identity); gunToSpawn.transform.parent = this.gameObject.transform; gunToSpawn.transform.localPosition = Vector3.zero; gunToSpawn.GetComponent <GunScript>().SetGunParameters(parametersToUse); gunToSpawn.tag = "GeneratedGun"; weaponSpawned = true; } else { Debug.Log("NULL NEW PARAMTERS, CANNOT CREATE NEW GUN"); weaponSpawned = false; } }
public void SetGunParameters(GunParameters parametersToSet) { gunParameters = parametersToSet; }