From 2eb12b1cd07fad67da495957de7de643448d5e05 Mon Sep 17 00:00:00 2001 From: Jens Pitkanen Date: Sun, 19 Apr 2020 02:12:29 +0300 Subject: [PATCH] Add GameState and mixer snapshots --- Assets/Audio Mixers/MainMixer.mixer | 85 ++++++++++++++++++-- Assets/Scenes/MainScene.unity | 118 ++++++++++++++++++++++++++-- Assets/Scripts/AudioMaster.cs | 26 ++++++ Assets/Scripts/AudioMaster.cs.meta | 11 +++ Assets/Scripts/CameraController.cs | 9 ++- Assets/Scripts/Campfire.cs | 7 +- Assets/Scripts/GameState.cs | 27 +++++++ Assets/Scripts/GameState.cs.meta | 11 +++ Assets/Scripts/PauseMenu.cs | 12 +-- 9 files changed, 282 insertions(+), 24 deletions(-) create mode 100644 Assets/Scripts/AudioMaster.cs create mode 100644 Assets/Scripts/AudioMaster.cs.meta create mode 100644 Assets/Scripts/GameState.cs create mode 100644 Assets/Scripts/GameState.cs.meta diff --git a/Assets/Audio Mixers/MainMixer.mixer b/Assets/Audio Mixers/MainMixer.mixer index b76c8ff..6b42883 100644 --- a/Assets/Audio Mixers/MainMixer.mixer +++ b/Assets/Audio Mixers/MainMixer.mixer @@ -1,5 +1,53 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: +--- !u!244 &-6119931374106344343 +AudioMixerEffectController: + m_ObjectHideFlags: 3 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: + m_EffectID: 86b1dc2b2b693394caad6dfdf7d2acdd + m_EffectName: Attenuation + m_MixLevel: 6520c254fe97e8d4585afad68d351162 + m_Parameters: [] + m_SendTarget: {fileID: 0} + m_EnableWetMix: 0 + m_Bypass: 0 +--- !u!243 &-6009061206453669325 +AudioMixerGroupController: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: Gameplay + m_AudioMixer: {fileID: 24100000} + m_GroupID: 01ed36fd4259f874d8e152aa9f5cbdb4 + m_Children: + - {fileID: -267709887311468707} + - {fileID: -2967980135314984734} + - {fileID: 586271417499195193} + m_Volume: 3b873ffd7476b574ab79c605a05a50ff + m_Pitch: db09271ae0508dd4fab1979ce5ca6572 + m_Send: 00000000000000000000000000000000 + m_Effects: + - {fileID: -6119931374106344343} + m_UserColorIndex: 0 + m_Mute: 0 + m_Solo: 0 + m_BypassEffects: 0 +--- !u!245 &-3105936107480203619 +AudioMixerSnapshotController: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: Playing + m_AudioMixer: {fileID: 24100000} + m_SnapshotID: ac9cc10b0c59e3b4682dc5089372ae81 + m_FloatValues: + 3b873ffd7476b574ab79c605a05a50ff: 0 + m_TransitionOverrides: {} --- !u!243 &-2967980135314984734 AudioMixerGroupController: m_ObjectHideFlags: 0 @@ -19,6 +67,18 @@ AudioMixerGroupController: m_Mute: 0 m_Solo: 0 m_BypassEffects: 0 +--- !u!245 &-815558828361742615 +AudioMixerSnapshotController: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: GameOver + m_AudioMixer: {fileID: 24100000} + m_SnapshotID: dd433c999a0f1de4b8d7ae8353027f93 + m_FloatValues: + 3b873ffd7476b574ab79c605a05a50ff: -60 + m_TransitionOverrides: {} --- !u!243 &-267709887311468707 AudioMixerGroupController: m_ObjectHideFlags: 0 @@ -49,10 +109,13 @@ AudioMixerController: m_MasterGroup: {fileID: 24300002} m_Snapshots: - {fileID: 24500006} + - {fileID: -3105936107480203619} + - {fileID: 7009156351606643568} + - {fileID: -815558828361742615} m_StartSnapshot: {fileID: 24500006} m_SuspendThreshold: -80 m_EnableSuspend: 1 - m_UpdateMode: 0 + m_UpdateMode: 1 m_ExposedParameters: [] m_AudioMixerGroupViews: - guids: @@ -60,9 +123,10 @@ AudioMixerController: - 1bdb8c14a13a7064e887fd8c05bd43e7 - ecc30646201221445ab5a1cd672c78d8 - 440e1443b72e9e449a0e30e04101552d + - 01ed36fd4259f874d8e152aa9f5cbdb4 name: View m_CurrentViewIndex: 0 - m_TargetSnapshot: {fileID: 24500006} + m_TargetSnapshot: {fileID: -3105936107480203619} --- !u!243 &24300002 AudioMixerGroupController: m_ObjectHideFlags: 0 @@ -73,9 +137,7 @@ AudioMixerGroupController: m_AudioMixer: {fileID: 24100000} m_GroupID: cce571dfddcbc694fa5856a9866157f8 m_Children: - - {fileID: -267709887311468707} - - {fileID: -2967980135314984734} - - {fileID: 586271417499195193} + - {fileID: -6009061206453669325} m_Volume: 1dc5725402baaca40b189e10927dfd5f m_Pitch: 91120b7d67bf787459cb1cee82330d2f m_Send: 00000000000000000000000000000000 @@ -171,3 +233,16 @@ AudioMixerEffectController: m_SendTarget: {fileID: 0} m_EnableWetMix: 0 m_Bypass: 0 +--- !u!245 &7009156351606643568 +AudioMixerSnapshotController: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: Paused + m_AudioMixer: {fileID: 24100000} + m_SnapshotID: 99acc46f1b1d7c24b861966f8835044d + m_FloatValues: + fd01f6bdeaa870c4688fa6fcad7c3e41: 0 + 3b873ffd7476b574ab79c605a05a50ff: 0 + m_TransitionOverrides: {} diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index d9109e0..8a02aa4 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -123,6 +123,58 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} +--- !u!1 &176857073 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 176857075} + - component: {fileID: 176857074} + m_Layer: 0 + m_Name: Audio Master + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &176857074 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 176857073} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 499ed37d51869ba44bbe6f61ffe9688e, type: 3} + m_Name: + m_EditorClassIdentifier: + GameState: {fileID: 834592896} + Mixer: {fileID: 24100000, guid: 032de345950918c4e89684078c6a7213, type: 2} + PlayingSnapshot: {fileID: -3105936107480203619, guid: 032de345950918c4e89684078c6a7213, + type: 2} + PausedSnapshot: {fileID: 7009156351606643568, guid: 032de345950918c4e89684078c6a7213, + type: 2} + GameoverSnapshot: {fileID: -815558828361742615, guid: 032de345950918c4e89684078c6a7213, + type: 2} + TransitionDuration: 0.5 +--- !u!4 &176857075 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 176857073} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &183132683 GameObject: m_ObjectHideFlags: 0 @@ -230,6 +282,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d53e1b3547ca29043b4a527f784da518, type: 3} m_Name: m_EditorClassIdentifier: + GameState: {fileID: 834592896} DynamicLight: {fileID: 876239992} BurnEffectSource: {fileID: 783793205} GoodFuelAmount: 20 @@ -628,6 +681,50 @@ Terrain: m_GroupingID: 0 m_RenderingLayerMask: 1 m_AllowAutoConnect: 1 +--- !u!1 &834592895 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 834592897} + - component: {fileID: 834592896} + m_Layer: 0 + m_Name: Game State + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &834592896 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 834592895} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ea392b45677427546a64c81b04afbc11, type: 3} + m_Name: + m_EditorClassIdentifier: + Current: 0 +--- !u!4 &834592897 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 834592895} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &876239990 GameObject: m_ObjectHideFlags: 0 @@ -1003,7 +1100,7 @@ Transform: - {fileID: 1161135096} - {fileID: 1545147752} m_Father: {fileID: 0} - m_RootOrder: 2 + m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1849724584 GameObject: @@ -1275,7 +1372,7 @@ RectTransform: - {fileID: 1364366412} - {fileID: 1156509021} m_Father: {fileID: 0} - m_RootOrder: 0 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0} @@ -1594,7 +1691,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 1 + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1001 &5400695019531174117 PrefabInstance: @@ -1603,11 +1700,6 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 1921606960} m_Modifications: - - target: {fileID: 5400695020334462788, guid: 2cd64e49ad7ad1c4bb4a4264703d7915, - type: 3} - propertyPath: m_Alpha - value: 1 - objectReference: {fileID: 0} - target: {fileID: 5400695020334462789, guid: 2cd64e49ad7ad1c4bb4a4264703d7915, type: 3} propertyPath: m_LocalPosition.x @@ -1713,6 +1805,11 @@ PrefabInstance: propertyPath: m_Pivot.y value: 0.5 objectReference: {fileID: 0} + - target: {fileID: 5400695020334462791, guid: 2cd64e49ad7ad1c4bb4a4264703d7915, + type: 3} + propertyPath: GameState + value: + objectReference: {fileID: 834592896} - target: {fileID: 5400695020334462842, guid: 2cd64e49ad7ad1c4bb4a4264703d7915, type: 3} propertyPath: m_Name @@ -1727,6 +1824,11 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 1660514110} m_Modifications: + - target: {fileID: 6125707630481988370, guid: 558201eae20fa5540a826edb23937665, + type: 3} + propertyPath: GameState + value: + objectReference: {fileID: 834592896} - target: {fileID: 6125707630481988396, guid: 558201eae20fa5540a826edb23937665, type: 3} propertyPath: m_LocalPosition.x diff --git a/Assets/Scripts/AudioMaster.cs b/Assets/Scripts/AudioMaster.cs new file mode 100644 index 0000000..9afc997 --- /dev/null +++ b/Assets/Scripts/AudioMaster.cs @@ -0,0 +1,26 @@ +using UnityEngine; +using UnityEngine.Audio; + +public class AudioMaster : MonoBehaviour { + public GameState GameState; + + public AudioMixer Mixer; + public AudioMixerSnapshot PlayingSnapshot; + public AudioMixerSnapshot PausedSnapshot; + public AudioMixerSnapshot GameoverSnapshot; + public float TransitionDuration; + + void Update() { + float[] Weights = { + GameState.Current == State.Playing ? 1 : 0, + GameState.Current == State.Paused ? 1 : 0, + GameState.Current == State.GameOver ? 1 : 0, + }; + AudioMixerSnapshot[] Snapshots = { + PlayingSnapshot, + PausedSnapshot, + GameoverSnapshot, + }; + Mixer.TransitionToSnapshots(Snapshots, Weights, TransitionDuration); + } +} diff --git a/Assets/Scripts/AudioMaster.cs.meta b/Assets/Scripts/AudioMaster.cs.meta new file mode 100644 index 0000000..af71f15 --- /dev/null +++ b/Assets/Scripts/AudioMaster.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 499ed37d51869ba44bbe6f61ffe9688e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/CameraController.cs b/Assets/Scripts/CameraController.cs index a0ddc91..4837341 100644 --- a/Assets/Scripts/CameraController.cs +++ b/Assets/Scripts/CameraController.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using UnityEngine; public class CameraController : MonoBehaviour { + public GameState GameState; public Transform BodyTransform; public Transform HeadTransform; @@ -13,10 +14,7 @@ public class CameraController : MonoBehaviour { } private void Update() { - if (Menu.Paused) { - Cursor.visible = true; - Cursor.lockState = CursorLockMode.None; - } else { + if (GameState.Current == State.Playing) { Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; @@ -33,6 +31,9 @@ public class CameraController : MonoBehaviour { Eulers.x = 270; } HeadTransform.localEulerAngles = Eulers; + } else { + Cursor.visible = true; + Cursor.lockState = CursorLockMode.None; } } } diff --git a/Assets/Scripts/Campfire.cs b/Assets/Scripts/Campfire.cs index 9938167..e6f8a86 100644 --- a/Assets/Scripts/Campfire.cs +++ b/Assets/Scripts/Campfire.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using UnityEngine; public class Campfire : MonoBehaviour { + public GameState GameState; public Light DynamicLight; [Tooltip("This audiosource will play the clips to indicate that an object has been thrown in the fire.")] public AudioSource BurnEffectSource; @@ -35,8 +36,12 @@ public class Campfire : MonoBehaviour { RandomVariance = Mathf.Lerp(RandomVariance, NextRandomVariance, (Time.time - LastRandomVarianceChange) / RandomVarianceDuration); Fuel -= Time.deltaTime; - DynamicLight.range = Mathf.Log(Fuel + 2f, 10) / 2f * FullRange + RandomVariance; + DynamicLight.range = Mathf.Log(Fuel + 1f, 10) / 2f * FullRange + RandomVariance; DynamicLight.color = Color.Lerp(DynamicLight.color, Fuel < GoodFuelAmount ? TooLowFuelColor : EnoughFuelColor, 10f * Time.deltaTime); + + if (Fuel <= 0) { + GameState.Current = State.GameOver; + } } private void OnCollisionEnter(Collision c) { diff --git a/Assets/Scripts/GameState.cs b/Assets/Scripts/GameState.cs new file mode 100644 index 0000000..853a892 --- /dev/null +++ b/Assets/Scripts/GameState.cs @@ -0,0 +1,27 @@ +using UnityEngine; + +[System.Serializable] +public enum State { + Playing, + Paused, + GameOver +} + +public class GameState : MonoBehaviour { + public State Current = State.Playing; + + private void Update() { + if (Input.GetKeyDown(KeyCode.Escape)) { + switch (Current) { + case State.Playing: + Current = State.Paused; + break; + case State.Paused: + Current = State.Playing; + break; + } + } + + Time.timeScale = Mathf.Lerp(Time.timeScale, Current == State.Paused ? 0 : 1, 10f * Time.unscaledDeltaTime); + } +} diff --git a/Assets/Scripts/GameState.cs.meta b/Assets/Scripts/GameState.cs.meta new file mode 100644 index 0000000..63deb67 --- /dev/null +++ b/Assets/Scripts/GameState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ea392b45677427546a64c81b04afbc11 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PauseMenu.cs b/Assets/Scripts/PauseMenu.cs index f024695..ddeb383 100644 --- a/Assets/Scripts/PauseMenu.cs +++ b/Assets/Scripts/PauseMenu.cs @@ -2,9 +2,14 @@ [RequireComponent(typeof(CanvasGroup))] public class PauseMenu : MonoBehaviour { - public bool Paused = false; + public GameState GameState; private CanvasGroup Canvas; + private bool Paused { + get { + return GameState.Current == State.Paused; + } + } private void Awake() { Canvas = GetComponent(); @@ -12,11 +17,6 @@ public class PauseMenu : MonoBehaviour { } private void Update() { - if (Input.GetKeyDown(KeyCode.Escape)) { - Paused = !Paused; - } - - Time.timeScale = Mathf.Lerp(Time.timeScale, Paused ? 0 : 1, 10f * Time.unscaledDeltaTime); Canvas.alpha = Mathf.Lerp(Canvas.alpha, Paused ? 1 : 0, 10f * Time.unscaledDeltaTime); } }