public override void CopyFrom(Weapon weapon) { base.CopyFrom(weapon); if (weapon.weaponType == WeaponType.Gun) { Shoot tempShoot = (Shoot)weapon; try { ChargeShoot shoot = (ChargeShoot)tempShoot; chargeSound = shoot.chargeSound; isProjectile = shoot.isProjectile; projectileToBeShot = shoot.projectileToBeShot; numberOfPellets = shoot.numberOfPellets; timeToFire = shoot.timeToFire; rampUpDamageAmount = shoot.rampUpDamageAmount; rampUpSpeedAmount = shoot.rampUpSpeedAmount; fireEarly = shoot.fireEarly; projectileSpeed = shoot.projectileSpeed; } catch (System.InvalidCastException) { Debug.LogError("Weapon passed to CopyFrom is not the same as the weapon copying! Could not pass derived class variables"); } } }
void ThrowWeapon(GameObject weaponToThrow) { GameObject tempWeapon = Instantiate(weaponToThrow.GetComponent <PickUpProperties>().actualGunPrefab, weaponSlot.position, weaponSlot.rotation); tempWeapon.GetComponent <Rigidbody>().AddForce(4 * weaponSlot.transform.forward, ForceMode.Impulse); tempWeapon.GetComponent <Rigidbody>().AddTorque(2 * weaponSlot.transform.up, ForceMode.Impulse); Weapon weaponValues = tempWeapon.GetComponent <Weapon>(); weaponValues.CopyFrom(weaponToThrow.GetComponent <Weapon>()); weaponValues.isPickup = true; if (weaponValues.weaponType == Weapon.WeaponType.Gun) { Shoot tempShoot = (Shoot)weaponValues; tempShoot.CopyFrom(weaponToThrow.GetComponent <Shoot>()); switch (tempShoot.shootType) { case Shoot.ShootType.GunShoot: GunShoot tempGunShoot = (GunShoot)(tempShoot); tempGunShoot.CopyFrom(weaponToThrow.GetComponent <GunShoot>()); break; case Shoot.ShootType.ShotgunShoot: ShotgunShoot tempShotgun = (ShotgunShoot)(tempShoot); tempShotgun.CopyFrom(weaponToThrow.GetComponent <ShotgunShoot>()); break; case Shoot.ShootType.SemiAutoShoot: break; case Shoot.ShootType.ChargeShoot: ChargeShoot chargeShoot = (ChargeShoot)tempShoot; chargeShoot.CopyFrom(weaponToThrow.GetComponent <ChargeShoot>()); break; case Shoot.ShootType.ProjectileShoot: break; } } else { MeleeAttack tempMelee = (MeleeAttack)weaponValues; tempMelee.CopyFrom(weaponToThrow.GetComponent <MeleeAttack>()); } Destroy(weaponToThrow); UpdateEquippedWeapons(); }
void Update() { if ((Input.GetKey(control.pauseMenu) || Input.GetKey(control.inventory)) && isCoroutineRunning) { gameObject.GetComponent <PauseGame>().HideAttachmentMenu(); StopCoroutine("AttachWeapon"); attachmentToAttach = null; isCoroutineRunning = false; } Ray ray = new Ray(cam.transform.position, cam.transform.forward); RaycastHit hit; Debug.DrawRay(ray.origin, ray.direction); if (Physics.Raycast(ray, out hit, 5)) { if (hit.collider.CompareTag("Chest") || hit.collider.gameObject.GetComponent <ChestContents>()) { useText.SetActive(true); chest = hit.collider.GetComponent <ChestContents>(); useText.GetComponent <Text>().text = chest.ammoChest ? "Pick up Ammo" : "Open Chest"; if (Input.GetKeyDown(control.use)) { if (chest.ammoChest) { currentAmmo = weaponSwitch.equippedWeapon.GetComponent <Weapon>().ammo.GetComponent <Amount>(); currentAmmo.amountOf += currentAmmo.limit; } else { pause.ShowChestAndInventory(); chestDisplay.SetChestContents(chest); inventoryDisplay.SetOpenChest(chest); } } } else if (hit.collider.gameObject.CompareTag("Weapon")) { useText.SetActive(true); useText.GetComponent <Text>().text = "Pick up " + hit.collider.gameObject.GetComponent <PickUpProperties>().actualGunPrefab.name; string weaponName = hit.collider.gameObject.GetComponent <PickUpProperties>().actualGunPrefab.name; if (weaponSlot.childCount >= weaponSwitch.maxWeapons) { useText.GetComponent <Text>().text = "Replace current weapon with " + weaponName; } UpdateEquippedWeapons(); if (Input.GetKeyDown(control.use)) { if (weaponSlot.childCount >= weaponSwitch.maxWeapons) { GameObject tempWeapon = Instantiate(hit.collider.gameObject.GetComponent <PickUpProperties>().actualGunPrefab, weaponSlot.position, weaponSlot.rotation, weaponSlot); tempWeapon.name = weaponName; equippedWeapons[weaponSwitch.currentWeapon].name = hit.collider.gameObject.GetComponent <PickUpProperties>().actualGunPrefab.name; equippedWeapons[weaponSwitch.currentWeapon].weapon = tempWeapon; Weapon weaponValues = tempWeapon.GetComponent <Weapon>(); weaponValues.CopyFrom(hit.collider.gameObject.GetComponent <Weapon>()); if (weaponValues.weaponType == Weapon.WeaponType.Gun) { Shoot tempShoot = (Shoot)weaponValues; tempShoot.CopyFrom(hit.collider.gameObject.GetComponent <Shoot>()); switch (tempShoot.shootType) { case Shoot.ShootType.GunShoot: GunShoot gunShoot = (GunShoot)tempShoot; gunShoot.CopyFrom(hit.collider.gameObject.GetComponent <GunShoot>()); break; case Shoot.ShootType.ShotgunShoot: ShotgunShoot shotgun = (ShotgunShoot)tempShoot; shotgun.CopyFrom(hit.collider.gameObject.GetComponent <ShotgunShoot>()); break; case Shoot.ShootType.SemiAutoShoot: break; case Shoot.ShootType.ChargeShoot: ChargeShoot chargeShoot = (ChargeShoot)tempShoot; chargeShoot.CopyFrom(hit.collider.gameObject.GetComponent <ChargeShoot>()); break; case Shoot.ShootType.ProjectileShoot: break; } } else { MeleeAttack tempMelee = (MeleeAttack)weaponValues; tempMelee.CopyFrom(hit.collider.GetComponent <MeleeAttack>()); } tempWeapon.transform.SetSiblingIndex(weaponSwitch.currentWeapon); GameObject weaponToDestroy = weaponSlot.GetChild(weaponSwitch.currentWeapon + 1).gameObject; hit.collider.gameObject.GetComponent <MiniMapIndicator>().DestroyCurrentIcon(); ThrowWeapon(weaponToDestroy); Destroy(weaponToDestroy); UpdateEquippedWeapons(); Destroy(hit.collider.gameObject); } else { GameObject weaponPickedUp = hit.collider.gameObject; GameObject tempWeapon = Instantiate(weaponPickedUp.GetComponent <PickUpProperties>().actualGunPrefab, weaponSlot.position, weaponSlot.rotation, weaponSlot); tempWeapon.name = weaponName; UpdateEquippedWeapons(); Weapon weaponValues = tempWeapon.GetComponent <Weapon>(); weaponValues.CopyFrom(weaponPickedUp.GetComponent <Weapon>()); weaponValues.isPickup = false; if (weaponValues.weaponType == Weapon.WeaponType.Gun) { Shoot tempShoot = (Shoot)weaponValues; tempShoot.CopyFrom(hit.collider.gameObject.GetComponent <Shoot>()); switch (tempShoot.shootType) { case Shoot.ShootType.GunShoot: GunShoot gunShoot = (GunShoot)tempShoot; gunShoot.CopyFrom(hit.collider.gameObject.GetComponent <GunShoot>()); break; case Shoot.ShootType.ShotgunShoot: ShotgunShoot shotgun = (ShotgunShoot)tempShoot; shotgun.CopyFrom(hit.collider.gameObject.GetComponent <ShotgunShoot>()); break; case Shoot.ShootType.SemiAutoShoot: break; case Shoot.ShootType.ChargeShoot: ChargeShoot chargeShoot = (ChargeShoot)tempShoot; chargeShoot.CopyFrom(hit.collider.gameObject.GetComponent <ChargeShoot>()); break; case Shoot.ShootType.ProjectileShoot: break; } } else { MeleeAttack tempMelee = (MeleeAttack)weaponValues; tempMelee.CopyFrom(hit.collider.GetComponent <MeleeAttack>()); } weaponSwitch.DisableWeapon(tempWeapon); hit.collider.gameObject.GetComponent <MiniMapIndicator>().DestroyCurrentIcon(); Destroy(hit.collider.gameObject); } } } else if (hit.collider.gameObject.CompareTag("Door")) { Door tempDoor = hit.collider.gameObject.GetComponent <Door>(); useText.SetActive(true); Text text = useText.GetComponent <Text>(); if (tempDoor.GetIsLocked()) { bool hasKey = false; int keyLoc = 0; for (int i = 0; i < keyNumbers.Count; i++) { hasKey = tempDoor.IsCorrectKey(keyNumbers[i]); if (hasKey) { keyLoc = i; } } text.text = (hasKey) ? "Unlock door" : "Door requires key!"; if (Input.GetKeyDown(control.use) && hasKey) { tempDoor.UnlockDoor(keyNumbers[keyLoc]); keyNumbers.RemoveAt(keyLoc); } } else if ((tempDoor.doorType == Door.DoorType.Use || tempDoor.doorType == Door.DoorType.RequiresKeyAfter || tempDoor.doorType == Door.DoorType.RequiresKeyInit) && !tempDoor.isOpen) { text.text = "Open door"; if (Input.GetKeyDown(control.use)) { tempDoor.OpenDoorWithUse(); } } else if (tempDoor.isOpen && tempDoor.doorType == Door.DoorType.Use) { text.text = "Close door"; if (Input.GetKeyDown(control.use)) { tempDoor.CloseDoorWithUse(); } } else { useText.SetActive(false); } } else if (hit.collider.gameObject.CompareTag("Key")) { useText.SetActive(true); Text text = useText.GetComponent <Text>(); text.text = "Pickup key"; if (Input.GetKeyDown(control.use)) { try { Key key = hit.collider.gameObject.GetComponent <Key>(); keyNumbers.Add(key.keyNumber); Destroy(hit.collider.gameObject); } catch (MissingComponentException e) { Debug.LogException(e); } } } else { useText.SetActive(false); } } else { useText.SetActive(false); } if (Input.GetKeyDown(control.throwWeapon)) { if (weaponSlot.GetChild(weaponSwitch.currentWeapon).gameObject != null) { ThrowWeapon(weaponSlot.GetChild(weaponSwitch.currentWeapon).gameObject); weaponSwitch.SwitchWeaponUp(); } UpdateEquippedWeapons(); } }