public void ChangePlayerPosition(Transform newPlayerPosition) { GoneWrong.Player player = GoneWrong.Player.instance; if (player != null && newPlayerPosition != null) { // We deactivate the player character controller first player.characterController.enabled = false; player.transform.position = newPlayerPosition.position; player.transform.rotation = newPlayerPosition.rotation; player.characterController.enabled = true; } }
private void Start() { // Getting cache variables _particleSystem = GetComponent <ParticleSystem>(); _audioSource = GetComponent <AudioSource>(); _player = GoneWrong.Player.instance; _particleSystem.Stop(); _audioSource.Stop(); _activated = false; if (_stateMachine != null) { _stateMachine.transform.gameObject.SetActive(false); } }
private void Update() { if (_destination != null) { transform.position = _destination.position; } if (GoneWrong.Player.instance != null && _distanceSharedFloat != null) { GoneWrong.Player player = GoneWrong.Player.instance; _distanceSharedFloat.value = (transform.position - player.transform.position).magnitude; float angleWithForward = Vector3.Angle(player.transform.forward, transform.position - player.transform.position); float angleWithRight = Vector3.Angle(player.transform.right, transform.position - player.transform.position); //Debug.Log("Angle with forward: " + angleWithForward); //Debug.Log("Angle with right: " + angleWithRight); if (_angleWithHorizontalShredFloat != null) { Vector3 horizontallProjectedVector = Vector3.ProjectOnPlane(transform.position - player.transform.position, player.transform.up); float sign = Mathf.Sign(Vector3.Cross(player.transform.forward, horizontallProjectedVector).y); float angleWithHorizontal = Vector3.Angle(horizontallProjectedVector, player.transform.forward) * sign; _angleWithHorizontalShredFloat.value = angleWithHorizontal; } if (_angleWithCameraUpSharedFloat != null) { if (_camera == null) { _camera = FindObjectOfType <Camera>(); } float angleWithCameraUp = Vector3.Angle(_camera.transform.up, transform.position - _camera.transform.position); _angleWithCameraUpSharedFloat.value = angleWithCameraUp; } } }
public void HandleCarReversal() { GoneWrong.Player player = GoneWrong.Player.instance; if (player == null) { return; } // The dot product of vector a and b is equal to: a.magnitude * b.magnitude * cos(angle) // In this case, the dot product is equal to con(angle) only because the vectors are normalized // the dot product's value is between -1 and 1. // -1 when the car up vector makes a 180 angle with the world down vector // 1 when the car up vector makes a 0 angle with the world down vector // When the angle is between -90 and 90, cos(angle) is superior to 0 if (Vector3.Dot(transform.up, Vector3.down) > -0.5f) { player.inReversedCar = true; } else { player.inReversedCar = false; } }
public IEnumerator Sit() { GoneWrong.Player player = GoneWrong.Player.instance; // Reset blood screen if (player != null) { player.ResetBloodScreen(); } if (player == null || _playerSit == null || _carController == null || _playerOut == null) { _sitCoroutine = null; yield break; } _playerIn = !_playerIn; player.canMove = !_playerIn; player.transform.parent = _playerIn ? _playerSit : _playerOut; Vector3 playerInitialPosition = player.transform.localPosition; Quaternion playerInitialRotation = player.transform.localRotation; float duration = _playerIn ? _getInCarDuration : _getOutOfCarDuration; if (!_playerIn) { // We open the door before getting outside the car if (_carDoor != null) { _carDoor.Interact(transform); } // We deactivate the car controller if we are getting out _carController.enabled = false; } else { // If we are getting inside the car, we deactivate the player controls player.GetComponent <CharacterController>().enabled = !_playerIn; // We also tell the player that we are inside a car player.insideACar = true; } float time = 0; while (time < duration) { time += Time.deltaTime; float normalizedTime = time / duration; player.transform.localPosition = Vector3.Lerp(playerInitialPosition, Vector3.zero, normalizedTime); player.transform.localRotation = Quaternion.Slerp(playerInitialRotation, Quaternion.Euler(Vector3.zero), normalizedTime); yield return(null); } // Make sure we got the right values player.transform.localPosition = Vector3.zero; player.transform.localRotation = Quaternion.Euler(Vector3.zero); // Make sure the main camera gets set to rotation zero after we get out if (!_playerIn) { Camera.main.transform.localRotation = Quaternion.Euler(Vector3.zero); } // We close the door after getting inside the car if (_carDoor != null && _playerIn) { _carDoor.Interact(transform); } if (_playerIn) { // We activate the char controller after it is enabled _carController.enabled = true; } else { player.transform.parent = null; player.GetComponent <CharacterController>().enabled = true; player.insideACar = false; // Then we close the door after we are out. if (_carDoor != null) { _carDoor.Interact(transform); } } // Now we change the text _text = _playerIn ? _getOutText : _interactiveText; // Force the player to go back to his initial local scale after we get him out of the car if (!_playerIn) { player.transform.localScale = _playerLocalScale; } // Now we activate or deactive playerHUD or carHUD if (CarHUD.instance != null) { CarHUD.instance.gameObject.SetActive(_playerIn); } if (PlayerHUD.instance != null) { PlayerHUD.instance.gameObject.SetActive(!_playerIn); } // Set the player drived vehicle player.drivedVehicle = _playerIn ? GetComponentInParent <Vehicle>() : null; // Activate the flashlight the moment the player gets out of the car if (!_playerIn && Flashlight.instance != null) { Flashlight.instance.Look(!Flashlight.instance.looking); } // Play the vehicle's startup sound: if (_playerIn && player.drivedVehicle.startUpSound != null && GoneWrong.AudioManager.instance != null) { GoneWrong.AudioManager.instance.PlayOneShotSound(player.drivedVehicle.startUpSound, 1, 0, 0, transform.position); } _sitCoroutine = null; }
private void Start() { player = GoneWrong.Player.instance; }
public void Save(int nextSceneIndex = -1) { SavedData savedData = new SavedData(); // We save the current scene (or the last scene) instead of the next one when the scene index is either not set or when we are going back to main menu // Or when the next scene is nightmare // Or when it's the end chapter or the final suicide scene // When the next scene index is not set, it means we are saving data in wasteland, so we save the data for the current scene. int nightmareSceneIndex = SceneManager.GetSceneByName("Nightmare").buildIndex; int chapter1EndSceneIndex = SceneManager.GetSceneByName("Chapter 1 End").buildIndex; int suicideSceneIndex = SceneManager.GetSceneByName("Suicide Scene ").buildIndex; if (nextSceneIndex == -1 || nextSceneIndex == 0 || nextSceneIndex == nightmareSceneIndex || nextSceneIndex == chapter1EndSceneIndex || nextSceneIndex == suicideSceneIndex) { savedData.currentScene = SceneManager.GetActiveScene().buildIndex; } else { savedData.currentScene = nextSceneIndex; } // We get the corresponding scene items first SceneData sceneData = null; switch (SceneManager.GetActiveScene().name) { case "Hospital": sceneData = savedData.hospitalSceneData; break; case "Wasteland": sceneData = savedData.wasteLandData; break; case "Tunnels": sceneData = savedData.tunnelsSceneData; break; case "Underground": sceneData = savedData.undergroundSceneData; break; } if (_playerInventory != null) { List <InventoryWeaponMount> weaponMounts = new List <InventoryWeaponMount>(); weaponMounts.Add(_playerInventory.rifle1); weaponMounts.Add(_playerInventory.rifle2); weaponMounts.Add(_playerInventory.handgun); weaponMounts.Add(_playerInventory.melee); for (int i = 0; i < weaponMounts.Count; i++) { if (weaponMounts[i].item == null) { continue; } bool foundWeapon = false; int index = 0; do { if (weaponMounts[i].item == _inventoryWeapons[index]) { foundWeapon = true; SavedSingleData savedSingleData = new SavedSingleData(); savedSingleData.type = DataType.Weapon; savedSingleData.index = index; savedSingleData.rounds = weaponMounts[i].rounds; if (i == 0) { savedData.rifle1 = savedSingleData; } else if (i == 1) { savedData.rifle2 = savedSingleData; } else if (i == 2) { savedData.handgun = savedSingleData; } else if (i == 3) { savedData.melee = savedSingleData; } } index++; } while (!foundWeapon && index < _inventoryWeapons.Count); } foreach (InventoryAmmoMount ammoMount in _playerInventory.ammo) { bool foundAmmo = false; int index = 0; do { if (ammoMount.item == _inventoryAmmos[index]) { foundAmmo = true; if (ammoMount.item.collectableAmmo.canSave) { SavedSingleData savedSingleData = new SavedSingleData(); savedSingleData.type = DataType.Ammo; savedSingleData.index = index; savedSingleData.rounds = ammoMount.rounds; savedData.inventoryList.Add(savedSingleData); } } index++; } while (!foundAmmo && index < _inventoryAmmos.Count); } foreach (InventoryConsumableMount consumableMount in _playerInventory.consumables) { bool foundConsumable = false; int index = 0; do { if (consumableMount.item == _inventoryConsumables[index]) { foundConsumable = true; if (consumableMount.item.collectableConsumable.canSave) { SavedSingleData savedSingleData = new SavedSingleData(); savedSingleData.type = DataType.Consumable; savedSingleData.index = index; savedData.inventoryList.Add(savedSingleData); } } index++; } while (!foundConsumable && index < _inventoryConsumables.Count); } foreach (Messages message in _playerInventory.messages) { bool foundMessage = false; int index = 0; do { if (message == _messages[index]) { foundMessage = true; SavedSingleData savedSingleData = new SavedSingleData(); savedSingleData.type = DataType.Message; savedSingleData.index = index; savedData.inventoryList.Add(savedSingleData); } index++; } while (!foundMessage && index < _messages.Count); } if (_healthSharedFloat != null) { savedData.health = _healthSharedFloat.value; } if (_staminaSharedFloat != null) { savedData.stamina = _staminaSharedFloat.value; } if (_infectionSharedFloat != null) { savedData.infection = _infectionSharedFloat.value; } } // Now we are about to save the scene items: if (sceneData != null && sceneData.collectableItems != null) { // Now saving the night sky and the fog sceneData.dayTime = RenderSettings.fog ? DayTime.Mist : DayTime.Night; CollectableConsumable[] consumables = FindObjectsOfType <CollectableConsumable>(); // We save the remaining collectable consumables foreach (CollectableConsumable consumable in consumables) { // Some key items are set to never be saved. They are managed by progress states if (!consumable.canSave) { continue; } SceneCollectableItem sceneCollectableItem = new SceneCollectableItem(); sceneCollectableItem.type = DataType.Consumable; sceneCollectableItem.localPosition = consumable.transform.localPosition; sceneCollectableItem.localRotation = consumable.transform.localRotation; if (consumable.transform.parent != null) { sceneCollectableItem.parentName = consumable.transform.parent.name; } bool foundConsumable = false; int index = 0; do { if (consumable.consumableMount.item == _inventoryConsumables[index]) { foundConsumable = true; sceneCollectableItem.index = index; } index++; } while (!foundConsumable && index < _inventoryConsumables.Count); // Now that we got our scene collectable item data stored in an object, we add them to the corresponding scene sceneData.collectableItems.Add(sceneCollectableItem); } // Now we save the remaining collectable ammos: CollectableAmmo[] ammos = FindObjectsOfType <CollectableAmmo>(); foreach (CollectableAmmo ammo in ammos) { SceneCollectableItem sceneCollectableItem = new SceneCollectableItem(); sceneCollectableItem.type = DataType.Ammo; sceneCollectableItem.localPosition = ammo.transform.localPosition; sceneCollectableItem.localRotation = ammo.transform.localRotation; if (ammo.transform.parent != null) { sceneCollectableItem.parentName = ammo.transform.parent.name; } bool foundAmmo = false; int index = 0; do { if (ammo.ammoMount.item == _inventoryAmmos[index]) { foundAmmo = true; sceneCollectableItem.index = index; } index++; } while (!foundAmmo && index < _inventoryAmmos.Count); // Now that we got our scene collectable item data stored in an object, we add them to the corresponding scene sceneData.collectableItems.Add(sceneCollectableItem); } // Now we check for the remaining zombies: if (_saveZombies) { AIStateMachine[] stateMachines = FindObjectsOfType <AIStateMachine>(); foreach (AIStateMachine stateMachine in stateMachines) { // If the zombie is activated and he is not dead, then we save him if (stateMachine.gameObject.activeSelf && !stateMachine.dead) { sceneData.remainingZombies.Add(stateMachine.transform.name); } } } // Now saving progress objects for in sceneData for (int i = 0; i < _progressObjects.Count; i++) { ProgressObject progressObject = new ProgressObject(); progressObject.index = i; if (_progressObjects[i] != null) { progressObject.active = _progressObjects[i].gameObject.activeSelf; progressObject.position = _progressObjects[i].transform.position; progressObject.rotation = _progressObjects[i].transform.rotation; } else { progressObject.active = false; } sceneData.progressObjects.Add(progressObject); } // Now saving the next level objective text if (PlayerHUD.instance != null) { sceneData.nextObjective = PlayerHUD.instance.nextObjective; } // Then we set the scene saved state to true sceneData.savedBefore = true; Notifications.instance.EnqueNotification("Progress Saved!"); if (ProgressSavedUI.instance != null) { ProgressSavedUI.instance.Show(); } } // Now we save the player position // We only save the player's position when we are in wasteland GoneWrong.Player player = GoneWrong.Player.instance; if (player != null && SceneManager.GetActiveScene().name == "Wasteland") { savedData.playerPosition = player.transform.position; savedData.playerRotation = player.transform.rotation; } // Now we save in player prefs: string data = JsonUtility.ToJson(savedData); PlayerPrefs.SetString("data", data); }
private void Awake() { if (SaveGame.instance != null) { return; } instance = this; if (_deleteSaveData) { DeleteSaveGame(); return; } SavedData savedData = JsonUtility.FromJson <SavedData>(PlayerPrefs.GetString("data")); _savedData = savedData; List <InventoryAmmoMount> ammos = new List <InventoryAmmoMount>(); List <InventoryConsumableMount> consumables = new List <InventoryConsumableMount>(); List <Messages> messages = new List <Messages>(); if (_playerInventory != null && savedData != null) { if (savedData.rifle1.index != -1) { _playerInventory.rifle1.item = _inventoryWeapons[savedData.rifle1.index]; _playerInventory.rifle1.rounds = savedData.rifle1.rounds; } if (savedData.rifle2.index != -1 && _inventoryWeapons.Count > savedData.rifle2.index) { _playerInventory.rifle2.item = _inventoryWeapons[savedData.rifle2.index]; _playerInventory.rifle2.rounds = savedData.rifle2.rounds; } if (savedData.handgun.index != -1 && _inventoryWeapons.Count > savedData.handgun.index) { _playerInventory.handgun.item = _inventoryWeapons[savedData.handgun.index]; _playerInventory.handgun.rounds = savedData.handgun.rounds; } if (savedData.melee.index != -1 && _inventoryWeapons.Count > savedData.melee.index) { _playerInventory.melee.item = _inventoryWeapons[savedData.melee.index]; } foreach (SavedSingleData savedSingleData in savedData.inventoryList) { switch (savedSingleData.type) { case DataType.Ammo: if (_inventoryAmmos.Count > savedSingleData.index) { InventoryAmmoMount ammoMount = new InventoryAmmoMount(); ammoMount.rounds = savedSingleData.rounds; ammoMount.item = _inventoryAmmos[savedSingleData.index]; ammos.Add(ammoMount); } break; case DataType.Consumable: if (_inventoryConsumables.Count > savedSingleData.index && _inventoryConsumables[savedSingleData.index] != null && _inventoryConsumables[savedSingleData.index].collectableConsumable.canSave) { InventoryConsumableMount consumableMount = new InventoryConsumableMount(); consumableMount.item = _inventoryConsumables[savedSingleData.index]; // Now we alter the game progress // Before adding each consumable, we need to make sure that the progress states associated with the mare registered List <GameProgress> progressStates = consumableMount.item.collectableConsumable.progressStates; ProgressManager progressManager = FindObjectOfType <ProgressManager>(); if (progressStates.Count > 0 && progressManager != null) { foreach (GameProgress gameProgress in progressStates) { progressManager.SetProgress(gameProgress.key, gameProgress.value); } } consumables.Add(consumableMount); } break; case DataType.Message: if (_messages.Count > savedSingleData.index) { Messages message = _messages[savedSingleData.index]; messages.Add(message); } break; } _playerInventory.ammo = ammos; _playerInventory.consumables = consumables; _playerInventory.messages = messages; } if (_healthSharedFloat != null) { _healthSharedFloat.value = savedData.health; } if (_staminaSharedFloat != null) { _staminaSharedFloat.value = savedData.stamina; } if (_infectionSharedFloat != null) { _infectionSharedFloat.value = savedData.infection; } // We only load the player position if the last saved scene is the same as the current scene // So that we dont load the player in a random position when he just entered the map from another map // We can only load the player's positon when we are in wasteland GoneWrong.Player player = FindObjectOfType <GoneWrong.Player>(); if (player != null && _savedData.currentScene == SceneManager.GetActiveScene().buildIndex && SceneManager.GetActiveScene().name == "Wasteland") { CharacterController playerCharacterController = player.GetComponent <CharacterController>(); playerCharacterController.enabled = false; if (savedData.playerPosition != Vector3.zero) { FlashlightLight flashLight = FindObjectOfType <FlashlightLight>(); // We should also put the flash light in place Vector3 differenceWithFlashLight = player.transform.position - flashLight.transform.position; player.transform.position = savedData.playerPosition; flashLight.transform.position = player.transform.position - differenceWithFlashLight; } if (savedData.playerRotation != Quaternion.identity) { player.transform.rotation = savedData.playerRotation; } playerCharacterController.enabled = true; } // Now for the collectable items // We check if we already saved the data for the current scene: SceneData sceneData = null; switch (SceneManager.GetActiveScene().name) { case "Hospital": sceneData = savedData.hospitalSceneData; break; case "Wasteland": sceneData = savedData.wasteLandData; break; case "Tunnels": sceneData = savedData.tunnelsSceneData; break; case "Underground": sceneData = savedData.undergroundSceneData; break; } if (sceneData != null && sceneData.savedBefore) { // We change the skybox and the fog depending on what is stored in the sceneData switch (sceneData.dayTime) { case DayTime.Night: RenderSettings.fogDensity = 0f; RenderSettings.skybox = null; break; case DayTime.Mist: if (_fogSkyBox != null) { RenderSettings.fogDensity = 0.35f; RenderSettings.skybox = _fogSkyBox; } break; } // We already saved the scene before. So we destroy all the collectableItems in the scene CollectableConsumable[] collectableConsumables = FindObjectsOfType <CollectableConsumable>(); foreach (CollectableConsumable collectableConsumable in collectableConsumables) { if (collectableConsumable.canSave) { Destroy(collectableConsumable.gameObject); } } CollectableAmmo[] colectableAmmos = FindObjectsOfType <CollectableAmmo>(); foreach (CollectableAmmo collectableAmmo in colectableAmmos) { if (collectableAmmo.canSave) { Destroy(collectableAmmo.gameObject); } } // Then we instantiate all the remaining collectable Items that were present when saving the data foreach (SceneCollectableItem sceneCollectableItem in sceneData.collectableItems) { // If we have the collectable item in list of collectable items if (sceneCollectableItem.index != -1) { // Then we instantiate the collectable item from the inventory scriptable object in our list if (sceneCollectableItem.type == DataType.Consumable) { if (_inventoryConsumables[sceneCollectableItem.index].collectableConsumable != null // check if we can instantiate it in the first place && _inventoryConsumables[sceneCollectableItem.index].collectableConsumable.canSave) { CollectableConsumable tmp = Instantiate(_inventoryConsumables[sceneCollectableItem.index].collectableConsumable); if (sceneCollectableItem.parentName != "") { Transform parent = GameObject.Find(sceneCollectableItem.parentName).transform; if (parent != null) { tmp.transform.parent = parent; } tmp.transform.localPosition = sceneCollectableItem.localPosition; tmp.transform.localRotation = sceneCollectableItem.localRotation; } } } else if (sceneCollectableItem.type == DataType.Ammo) { if (_inventoryConsumables[sceneCollectableItem.index].collectableConsumable != null) { CollectableAmmo tmp = Instantiate(_inventoryAmmos[sceneCollectableItem.index].collectableAmmo); if (sceneCollectableItem.parentName != "") { Transform parent = GameObject.Find(sceneCollectableItem.parentName).transform; if (parent != null) { tmp.transform.parent = parent; } tmp.transform.localPosition = sceneCollectableItem.localPosition; tmp.transform.localRotation = sceneCollectableItem.localRotation; } } } } } // Now we check the remaining zombies // Each zombie whose name doesn't belong to the list of the remaining zombies, will be deactivated if (_saveZombies) { AIStateMachine[] stateMachines = FindObjectsOfType <AIStateMachine>(); foreach (AIStateMachine stateMachine in stateMachines) { int index = 0; bool foundZombie = false; do { if (sceneData.remainingZombies.Count > index && stateMachine.transform.name == sceneData.remainingZombies[index]) { foundZombie = true; } index++; } while (index < sceneData.remainingZombies.Count && !foundZombie); // If we didn't find the zombie in our list, we destroy him if (!foundZombie) { Destroy(stateMachine.gameObject); //stateMachine.gameObject.SetActive(false); } } } // Handling progress objects (activating and deactivating them, then setting their position) foreach (ProgressObject progressObject in sceneData.progressObjects) { if (progressObject.index != -1 && _progressObjects.Count > progressObject.index) { Transform sceneProgressObject = _progressObjects[progressObject.index]; if (sceneProgressObject != null) { sceneProgressObject.gameObject.SetActive(progressObject.active); sceneProgressObject.transform.position = progressObject.position; sceneProgressObject.transform.rotation = progressObject.rotation; } } } PlayerHUD playerHud = PlayerHUD.instance; if (playerHud == null) { playerHud = FindObjectOfType <PlayerHUD>(); } if (playerHud != null) { playerHud.ChangeLevelObjectiveText(sceneData.nextObjective); } CarHUD carHUD = CarHUD.instance; if (carHUD == null) { carHUD = FindObjectOfType <CarHUD>(); } if (carHUD != null) { carHUD.ChangeLevelObjectiveText(sceneData.nextObjective); } } } }