diff --git a/AudioEvent/Editor/CustomInspectors/AudioEventInspector.cs b/AudioEvent/Editor/CustomInspectors/AudioEventInspector.cs index 4ff5c52..4d0e7a9 100644 --- a/AudioEvent/Editor/CustomInspectors/AudioEventInspector.cs +++ b/AudioEvent/Editor/CustomInspectors/AudioEventInspector.cs @@ -1,34 +1,37 @@ using UnityEditor; using UnityEngine; -[CustomEditor(typeof(AudioEvent), true)] -public class AudioEventInspector : Editor +namespace AudioEvent { - [SerializeField] private AudioSource audioSource; - - private void OnEnable() + [CustomEditor(typeof(AudioEvent), true)] + public class AudioEventInspector : Editor { - audioSource = - EditorUtility.CreateGameObjectWithHideFlags("Audio preview", HideFlags.HideAndDontSave, - typeof(AudioSource)).GetComponent(); - } + [SerializeField] private AudioSource _audioSource; - private void OnDisable() - { - DestroyImmediate(audioSource.gameObject); - } - - - public override void OnInspectorGUI() - { - DrawDefaultInspector(); - - EditorGUI.BeginDisabledGroup(serializedObject.isEditingMultipleObjects); - if (GUILayout.Button("Preview")) + private void OnEnable() { - ((AudioEvent) target).Play(audioSource); + _audioSource = + EditorUtility.CreateGameObjectWithHideFlags("Audio preview", HideFlags.HideAndDontSave, + typeof(AudioSource)).GetComponent(); } - EditorGUI.EndDisabledGroup(); + private void OnDisable() + { + DestroyImmediate(_audioSource.gameObject); + } + + + public override void OnInspectorGUI() + { + DrawDefaultInspector(); + + EditorGUI.BeginDisabledGroup(serializedObject.isEditingMultipleObjects); + if (GUILayout.Button("Preview")) + { + ((AudioEvent) target).Play(_audioSource); + } + + EditorGUI.EndDisabledGroup(); + } } } \ No newline at end of file diff --git a/AudioEvent/Runtime/AudioEvent.cs b/AudioEvent/Runtime/AudioEvent.cs index 034a246..0ec07f2 100644 --- a/AudioEvent/Runtime/AudioEvent.cs +++ b/AudioEvent/Runtime/AudioEvent.cs @@ -1,6 +1,9 @@ using UnityEngine; -public abstract class AudioEvent : ScriptableObject +namespace AudioEvent { - public abstract void Play(AudioSource source); + public abstract class AudioEvent : ScriptableObject + { + public abstract void Play(AudioSource source); + } } \ No newline at end of file diff --git a/AudioEvent/Runtime/SimpleAudioEvent.cs b/AudioEvent/Runtime/SimpleAudioEvent.cs index d634304..7291bca 100644 --- a/AudioEvent/Runtime/SimpleAudioEvent.cs +++ b/AudioEvent/Runtime/SimpleAudioEvent.cs @@ -1,20 +1,24 @@ -using UnityEngine; +using RangedValue; +using UnityEngine; -[CreateAssetMenu(menuName = "ScriptableObject/SimpleAudioEvent")] -public class SimpleAudioEvent : AudioEvent +namespace AudioEvent { - public AudioClip[] clips; - public RangedFloat volume; - - [MinMaxRange(0, 2)] public RangedFloat pitch; - - public override void Play(AudioSource source) + [CreateAssetMenu(menuName = "ScriptableObject/SimpleAudioEvent")] + public class SimpleAudioEvent : AudioEvent { - if (clips.Length == 0) return; + public AudioClip[] Clips; + public RangedFloat Volume = new RangedFloat(0.9f,1f); - source.clip = clips[Random.Range(0, clips.Length)]; - source.volume = Random.Range(volume.minValue, volume.maxValue); - source.pitch = Random.Range(pitch.minValue, pitch.maxValue); - source.Play(); + [MinMaxRange(0, 2)] public RangedFloat Pitch = new RangedFloat(0.95f,1.05f); + + public override void Play(AudioSource source) + { + if (Clips.Length == 0) return; + + source.clip = Clips[Random.Range(0, Clips.Length)]; + source.volume = Random.Range(Volume.MinValue, Volume.MaxValue); + source.pitch = Random.Range(Pitch.MinValue, Pitch.MaxValue); + source.Play(); + } } } \ No newline at end of file diff --git a/ChildUpdator/UpdateChildObjects.cs b/ChildUpdator/UpdateChildObjects.cs deleted file mode 100644 index c98e0dd..0000000 --- a/ChildUpdator/UpdateChildObjects.cs +++ /dev/null @@ -1,20 +0,0 @@ -using UnityEngine; - -public class UpdateChild{ - - public static void recursiveCallOnChild (GameObject _obj) - { - if (_obj == null) - return; - - //Changes to the current object - - foreach (Transform _child in _obj.transform) - { - if (_child == null) - continue; - - recursiveCallOnChild(_child.gameObject); - } - } -} \ No newline at end of file diff --git a/DiceRolling/ApplyRandForce.cs b/DiceRolling/ApplyRandForce.cs deleted file mode 100644 index a918af3..0000000 --- a/DiceRolling/ApplyRandForce.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class ApplyRandForce : MonoBehaviour { - - public string buttonName = "Fire1"; - public float forceAmount = 10.0f; - public float torqueAmount = 10.0f; - public ForceMode forceMode; - public float maxAngularVelocity; - public Rigidbody rb; - - void Start() { - - rb.maxAngularVelocity = maxAngularVelocity; - } - - void FixedUpdate () - { - if(Input.GetButtonDown(buttonName)) - { - rb.AddForce(Random.onUnitSphere*forceAmount,forceMode); - rb.AddTorque(Random.onUnitSphere * torqueAmount, forceMode); - } - } -} diff --git a/DiceRolling/DieValue.cs b/DiceRolling/DieValue.cs deleted file mode 100644 index df466e9..0000000 --- a/DiceRolling/DieValue.cs +++ /dev/null @@ -1,11 +0,0 @@ -using UnityEngine; - -public class DieValue : MonoBehaviour -{ - public int value; - - public int getValue() { - - return value; - } -} diff --git a/DiceRolling/DisplayCurrentDieValue.cs b/DiceRolling/DisplayCurrentDieValue.cs deleted file mode 100644 index 5427942..0000000 --- a/DiceRolling/DisplayCurrentDieValue.cs +++ /dev/null @@ -1,31 +0,0 @@ -using UnityEngine; - -public class DisplayCurrentDieValue : MonoBehaviour -{ - public LayerMask dieValueColliderLayer; - public Rigidbody rb; - - private int currentValue; - private bool rollComplete = false; - - void Update () { - - if (rb.IsSleeping() && !rollComplete) - { - rollComplete = true; - Debug.Log("Dice stopped rolling, result is: " + currentValue.ToString()); - } - else if(!rb.IsSleeping()) - { - rollComplete = false; - } - - RaycastHit hit; - - if(Physics.Raycast(transform.position,Vector3.up,out hit,Mathf.Infinity,dieValueColliderLayer)){ - - // Reading the value of the collider on the die top face - currentValue = hit.collider.GetComponent().getValue(); - } - } -} \ No newline at end of file diff --git a/Extensions/GameObjectExtensions.cs b/Extensions/GameObjectExtensions.cs new file mode 100644 index 0000000..e20f1d8 --- /dev/null +++ b/Extensions/GameObjectExtensions.cs @@ -0,0 +1,19 @@ +using UnityEngine; + +public static class GameObjectExtensions +{ + public static int GetNumberOfComponents(this GameObject gameObject) + { + return gameObject.GetComponentsInChildren().Length - 1; + } + + public static void Show(this GameObject gameObject) + { + gameObject.SetActive(true); + } + + public static void Hide(this GameObject gameObject) + { + gameObject.SetActive(false); + } +} \ No newline at end of file diff --git a/Grid System/GridSystem.cs b/Grid System/GridSystem.cs deleted file mode 100644 index a64fa67..0000000 --- a/Grid System/GridSystem.cs +++ /dev/null @@ -1,226 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class GridSystem : MonoBehaviour { - - public LayerMask mask; - public GameObject prefab; - public float nodeSize = 1f; - - private Grid grid; - private GameObject currentPrefab; - private int gridSizeX; - private int gridSizeZ; - private Boolean currentPrefabOnMat = false; - - // Use this for initialization - void Start () { - - //Get Size of the gameMat - GetGridSize(); - - //Create the grid of nodes to the size of the mat - CreateGrid(); - } - - void Update () { - - CheckKeyDown(); - - CheckMouseDown(); - - currentPrefabOnMat = MovePrefabToMouse(); - } - - private Boolean MovePrefabToMouse() { - - int x; - int y; - - if (MouseOnTableMat(out x, out y)) { - - if (currentPrefab != null) { - - if (x != -1 && y != -1) { - - MoveCurrentPrefab(x, y); - return prefabOnMat(); - } - } - } - - return false; - } - - private bool prefabOnMat() { - - Boolean onMat = true; - - foreach (Transform child in currentPrefab.transform) { - - int x = Mathf.FloorToInt(currentPrefab.transform.position.x - - nodeSize / 2 + - child.localPosition.x / nodeSize + - gridSizeX / 2 - - transform.position.x); - - int y = Mathf.FloorToInt(currentPrefab.transform.position.z - - nodeSize / 2 + - child.localPosition.z / nodeSize + - gridSizeZ / 2 - - transform.position.z); - - if (!(Mathf.Clamp(x, 0, gridSizeX - 1) == x && - Mathf.Clamp(y, 0, gridSizeZ - 1) == y)) { - - onMat = false; - } else { - - if (grid.grid[x, y].occupied) { - - onMat = false; - } - } - } - - return onMat; - } - - private void CheckMouseDown() { - - if (Input.GetMouseButtonDown(0) && currentPrefabOnMat) { - - foreach (Transform child in currentPrefab.transform) { - - int x = Mathf.Clamp( - Mathf.FloorToInt(currentPrefab.transform.position.x - - nodeSize / 2 + - child.localPosition.x / nodeSize + - gridSizeX / 2 - - transform.position.x), - 0, gridSizeX - 1); - - int y = Mathf.Clamp( - Mathf.FloorToInt(currentPrefab.transform.position.z - - nodeSize / 2 + - child.localPosition.z / nodeSize + - gridSizeZ / 2 - - transform.position.z), - 0, gridSizeZ - 1); - - grid.grid[x, y].occupied = true; - } - - currentPrefab = null; - } - } - - private void CheckKeyDown() { - - if (Input.GetKeyDown(KeyCode.A)) { - - if (currentPrefab == null) { - - currentPrefab = Instantiate(prefab) - } else { - - Destroy(currentPrefab); - } - } - } - - private void MoveCurrentPrefab(int x, int y) { - - currentPrefab.transform.position = grid.grid[x,y].centerWorldPosition; - } - - private bool MouseOnTableMat(out int x, out int y) { - - Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); - RaycastHit hitPoint; - - if (Physics.Raycast(ray, out hitPoint, Mathf.Infinity, mask)) { - - if (hitPoint.collider == GetComponent()) { - - x = Mathf.Clamp( - Mathf.FloorToInt(hitPoint.point.x - transform.position.x + gridSizeX / 2), - 0, gridSizeX - 1); - - y = Mathf.Clamp( - Mathf.FloorToInt(hitPoint.point.z - transform.position.z + gridSizeZ / 2), - 0, gridSizeZ - 1); - - return true; - } - } - x = -1; - y = -1; - return false; - } - - private void GetGridSize() { - - Bounds borders = GetComponent().bounds; - gridSizeX = Mathf.RoundToInt(borders.size.x); - gridSizeZ = Mathf.RoundToInt(borders.size.z); - - } - - private void CreateGrid() { - - grid = new Grid(transform.position, gridSizeX, gridSizeZ, nodeSize); - - } -} - -public class Node{ - - public Vector3 centerWorldPosition; - public bool occupied; - - public Node(Vector3 centerWorldPosition) - { - this.centerWorldPosition = centerWorldPosition; - occupied = false; - } -} - -public class Grid{ - - public Node[,] grid; - - public Grid(Vector3 gridCenter, int gridSizeX, int gridSizeZ, float NodeSize){ - - grid = new Node[gridSizeX, gridSizeZ]; - - for (int row = 0 ; row < gridSizeX; row++){ - for (int collumn = 0; collumn < gridSizeZ ; collumn++){ - - Vector3 centerWorldPosition = gridCenter - (Vector3.right * gridSizeX / 2) - - (Vector3.forward * gridSizeZ / 2) + - (Vector3.right * NodeSize * row) + - (Vector3.forward * NodeSize * collumn) + - (Vector3.right * NodeSize / 2) + - (Vector3.forward * NodeSize / 2); - - grid[row,collumn] = new Node(centerWorldPosition); - } - } - } - - public String toString() { - - String str = ""; - - for (int i = 0; i < 8; i++) { - for (int y = 0; y < 4; y++) { - str += $"\n[{i},{y}] : {grid[i, y].centerWorldPosition} - {grid[i, y].occupied}"; - } - } - - return str; - } -} \ No newline at end of file diff --git a/RangedFloat/Editor/PropertyDrawers/RangedFloatDrawer.cs b/RangedFloat/Editor/PropertyDrawers/RangedFloatDrawer.cs index df8573e..c770012 100644 --- a/RangedFloat/Editor/PropertyDrawers/RangedFloatDrawer.cs +++ b/RangedFloat/Editor/PropertyDrawers/RangedFloatDrawer.cs @@ -1,50 +1,53 @@ using UnityEditor; using UnityEngine; -[CustomPropertyDrawer(typeof(RangedFloat), true)] -public class RangedFloatDrawer : PropertyDrawer +namespace RangedValue { - public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + [CustomPropertyDrawer(typeof(RangedFloat), true)] + public class RangedFloatDrawer : PropertyDrawer { - label = EditorGUI.BeginProperty(position, label, property); - position = EditorGUI.PrefixLabel(position, label); - - var minProp = property.FindPropertyRelative("minValue"); - var maxProp = property.FindPropertyRelative("maxValue"); - - var minValue = minProp.floatValue; - var maxValue = maxProp.floatValue; - - float rangeMin = 0; - float rangeMax = 1; - - var ranges = (MinMaxRangeAttribute[]) fieldInfo.GetCustomAttributes(typeof(MinMaxRangeAttribute), true); - - if (ranges.Length > 0) + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { - rangeMin = ranges[0].Min; - rangeMax = ranges[0].Max; + label = EditorGUI.BeginProperty(position, label, property); + position = EditorGUI.PrefixLabel(position, label); + + var minProp = property.FindPropertyRelative("MinValue"); + var maxProp = property.FindPropertyRelative("MaxValue"); + + var minValue = minProp.floatValue; + var maxValue = maxProp.floatValue; + + float rangeMin = 0; + float rangeMax = 1; + + var ranges = (MinMaxRangeAttribute[]) fieldInfo.GetCustomAttributes(typeof(MinMaxRangeAttribute), true); + + if (ranges.Length > 0) + { + rangeMin = ranges[0].Min; + rangeMax = ranges[0].Max; + } + + const float rangeBoundsLabelWidth = 40f; + + var rangeBoundsLabel1Rect = new Rect(position) {width = rangeBoundsLabelWidth}; + GUI.Label(rangeBoundsLabel1Rect, new GUIContent(minValue.ToString("F2"))); + position.xMin += rangeBoundsLabelWidth; + + var rangeBoundsLabel2Rect = new Rect(position); + rangeBoundsLabel2Rect.xMin = rangeBoundsLabel2Rect.xMax - rangeBoundsLabelWidth + 5; + GUI.Label(rangeBoundsLabel2Rect, new GUIContent(maxValue.ToString("F2"))); + position.xMax -= rangeBoundsLabelWidth; + + EditorGUI.BeginChangeCheck(); + EditorGUI.MinMaxSlider(position, ref minValue, ref maxValue, rangeMin, rangeMax); + if (EditorGUI.EndChangeCheck()) + { + minProp.floatValue = minValue; + maxProp.floatValue = maxValue; + } + + EditorGUI.EndProperty(); } - - const float rangeBoundsLabelWidth = 40f; - - var rangeBoundsLabel1Rect = new Rect(position) {width = rangeBoundsLabelWidth}; - GUI.Label(rangeBoundsLabel1Rect, new GUIContent(minValue.ToString("F2"))); - position.xMin += rangeBoundsLabelWidth; - - var rangeBoundsLabel2Rect = new Rect(position); - rangeBoundsLabel2Rect.xMin = rangeBoundsLabel2Rect.xMax - rangeBoundsLabelWidth + 5; - GUI.Label(rangeBoundsLabel2Rect, new GUIContent(maxValue.ToString("F2"))); - position.xMax -= rangeBoundsLabelWidth; - - EditorGUI.BeginChangeCheck(); - EditorGUI.MinMaxSlider(position, ref minValue, ref maxValue, rangeMin, rangeMax); - if (EditorGUI.EndChangeCheck()) - { - minProp.floatValue = minValue; - maxProp.floatValue = maxValue; - } - - EditorGUI.EndProperty(); } } \ No newline at end of file diff --git a/RangedFloat/Runtime/MinMaxRangeAttribute.cs b/RangedFloat/Runtime/MinMaxRangeAttribute.cs index e1aa92f..cbed5b6 100644 --- a/RangedFloat/Runtime/MinMaxRangeAttribute.cs +++ b/RangedFloat/Runtime/MinMaxRangeAttribute.cs @@ -1,13 +1,16 @@ using System; -public class MinMaxRangeAttribute : Attribute +namespace RangedValue { - public float Min { get; private set; } - public float Max { get; private set; } - - public MinMaxRangeAttribute(float min, float max) + public class MinMaxRangeAttribute : Attribute { - Min = min; - Max = max; + public float Min { get; } + public float Max { get; } + + public MinMaxRangeAttribute(float min, float max) + { + Min = min; + Max = max; + } } } \ No newline at end of file diff --git a/RangedFloat/Runtime/RangedFloat.cs b/RangedFloat/Runtime/RangedFloat.cs index 4e66fe0..1b30925 100644 --- a/RangedFloat/Runtime/RangedFloat.cs +++ b/RangedFloat/Runtime/RangedFloat.cs @@ -1,8 +1,22 @@ using System; -[Serializable] -public struct RangedFloat +namespace RangedValue { - public float minValue; - public float maxValue; + [Serializable] + public struct RangedFloat + { + public float MinValue; + public float MaxValue; + + public RangedFloat(float minValue, float maxValue) + { + MinValue = minValue; + MaxValue = maxValue; + } + + public float RandomValueInRange() + { + return UnityEngine.Random.Range(MinValue, MaxValue); + } + } } \ No newline at end of file diff --git a/SaveSystem/Editor/CustomInspectors/SaveManagerInspector.cs b/SaveSystem/Editor/CustomInspectors/SaveManagerInspector.cs new file mode 100644 index 0000000..8fcf9ab --- /dev/null +++ b/SaveSystem/Editor/CustomInspectors/SaveManagerInspector.cs @@ -0,0 +1,43 @@ +using System.Diagnostics; +using System.IO; +using UnityEditor; +using UnityEngine; + +namespace SaveSystem +{ + [CustomEditor(typeof(SaveManager), true)] + public class SaveManagerInspector : Editor + { + public override void OnInspectorGUI() + { + DrawDefaultInspector(); + + SaveManager saveManager = (SaveManager) target; + GUI.enabled = EditorApplication.isPlaying; + EditorGUI.BeginDisabledGroup(serializedObject.isEditingMultipleObjects); + EditorGUILayout.BeginHorizontal(); + if (GUILayout.Button("Save", EditorStyles.miniButtonLeft)) + { + saveManager.Save(); + } + + if (GUILayout.Button("Load", EditorStyles.miniButtonRight)) + { + saveManager.Load(); + } + EditorGUILayout.EndHorizontal(); + + if (GUILayout.Button("Save to Json")) + { + saveManager.SaveExplicit(); + } + + GUI.enabled = true; + if (GUILayout.Button("Open explicit save in notepad++")) + { + Process.Start("notepad++.exe", Path.Combine(Application.dataPath, "../") + "saveData01.json"); + } + EditorGUI.EndDisabledGroup(); + } + } +} \ No newline at end of file diff --git a/SaveSystem/ISaveData.cs b/SaveSystem/ISaveData.cs new file mode 100644 index 0000000..8de4f48 --- /dev/null +++ b/SaveSystem/ISaveData.cs @@ -0,0 +1,8 @@ +namespace SaveSystem +{ + public interface ISaveData + { + public void Save(ref GameData gameData); + public void Load(ref GameData gameData); + } +} \ No newline at end of file diff --git a/SaveSystem/SaveManager.cs b/SaveSystem/SaveManager.cs new file mode 100644 index 0000000..9b61038 --- /dev/null +++ b/SaveSystem/SaveManager.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Core; +using UnityEngine; +using UnityEngine.Events; + +namespace SaveSystem +{ + public class SaveManager : Singleton + { + public static UnityAction OnLoadCompleted; + + private static readonly List SaveDataList = new List(); + + private GameData _saveGame; + private int _loadedGameNumber = -1; + + protected override void Awake() + { + base.Awake(); + _saveGame = new GameData(); + } + + private void Start() => Load(); + + public void Save(int saveNumber = 1) + { + foreach (ISaveData saveData in SaveDataList) + { + saveData.Save(ref _saveGame); + } + + string json = JsonUtility.ToJson(_saveGame); + byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes(json); + string b64 = Convert.ToBase64String(plainTextBytes); + using StreamWriter sw = new StreamWriter($"saveData{saveNumber:00}.sgd"); + sw.Write(b64); + _loadedGameNumber = -1; + } + + public void SaveExplicit() + { + foreach (ISaveData saveData in SaveDataList) + { + saveData.Save(ref _saveGame); + } + string json = JsonUtility.ToJson(_saveGame); + using StreamWriter sw = new StreamWriter($"saveData01.json"); + sw.Write(json); + } + + public void Load(int saveNumber = 1) + { + if (saveNumber != _loadedGameNumber) + { + try + { + using StreamReader sr = new StreamReader($"saveData{saveNumber:00}.sgd"); + string b64 = sr.ReadToEnd(); + byte[] plainTextBytes = Convert.FromBase64String(b64); + string json = System.Text.Encoding.UTF8.GetString(plainTextBytes); + _saveGame = JsonUtility.FromJson(json); + } + catch (FileNotFoundException) + { + Debug.LogWarning($"Tried to load saveData{saveNumber:00}.sgd but file doesn't exist"); + } + _loadedGameNumber = saveNumber; + } + + foreach (ISaveData saveData in SaveDataList) + { + saveData.Load(ref _saveGame); + } + + OnLoadCompleted?.Invoke(_saveGame); + } + + public static void Register(ISaveData saveData) + { + if (!SaveDataList.Contains(saveData)) + { + SaveDataList.Add(saveData); + } + } + + public static void Unregister(ISaveData saveData) + { + SaveDataList.Remove(saveData); + } + + protected override void OnDestroy() + { + base.OnDestroy(); + Save(); + } + } +} diff --git a/SerializableDictionary.cs b/SerializableDictionary.cs new file mode 100644 index 0000000..5dab809 --- /dev/null +++ b/SerializableDictionary.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +[Serializable] +public class SerializableDictionary : Dictionary, ISerializationCallbackReceiver +{ + [SerializeField] private List _keys = new List(); + [SerializeField] private List _values = new List(); + + public void OnBeforeSerialize() + { + _keys.Clear(); + _values.Clear(); + + foreach (KeyValuePair pair in this) + { + _keys.Add(pair.Key); + _values.Add(pair.Value); + } + } + + public void OnAfterDeserialize() + { + Clear(); + + if (_keys.Count != _values.Count) + { + throw new Exception( + $"There are {_keys.Count} keys and {_values.Count} values after deserialization." + + " Make sure that both key and value types are serializable."); + } + + for (int i = 0; i < _keys.Count; ++i) + { + Add(_keys[i], _values[i]); + } + } +} \ No newline at end of file diff --git a/Singleton.cs b/Singleton.cs new file mode 100644 index 0000000..3657cd6 --- /dev/null +++ b/Singleton.cs @@ -0,0 +1,77 @@ +using System; +using UnityEngine; + +namespace Core +{ + public abstract class Singleton : MonoBehaviour where T : Singleton + { + private static T _instance; + + public static T Instance + { + get + { + if (!HasInstance) + { + Debug.LogWarning( + $"Trying to access a singleton of type {typeof(T).Name} that is not in the scene. " + + "If you are trying to see if the Instance exists, you should use the HasInstance static property instead."); + } + + return _instance; + } + } + + private bool _destroyedByScript; + + public static bool HasInstance => _instance != null; + + protected virtual void Awake() + { + _destroyedByScript = false; + + if (HasInstance) + { + Debug.Log($"Two or more instances of a singleton of type {typeof(T).Name} were found in the scene. " + + "The new instance of the singleton trying to register will be removed."); + + DestroyInstance(); + return; + } + + try + { + _instance = (T) Convert.ChangeType(this, typeof(T)); + } + catch (InvalidCastException) + { + Debug.Assert(false, "Singleton's T type should be the derived class."); + } + } + + private void DestroyInstance() + { + _destroyedByScript = true; + if (gameObject.GetNumberOfComponents() == 1) + { + Destroy(gameObject); + } + else + { + Destroy(this); + } + } + + protected virtual void OnDestroy() + { + if (_instance == this) + { + _instance = null; + } + else if (!_destroyedByScript) + { + Debug.LogWarning($"Instance of {typeof(T).Name} deleted, but was not the singleton instance."); + } + } + } +} \ No newline at end of file diff --git a/Timer.cs b/Timer.cs new file mode 100644 index 0000000..71acd86 --- /dev/null +++ b/Timer.cs @@ -0,0 +1,49 @@ +using UnityEngine; +using UnityEngine.Events; + +namespace Core +{ + public class Timer : MonoBehaviour + { + public UnityAction onTimeOver; + + public bool IsRunning { get; private set; } + + public float CurrentTime { get; private set; } + + [SerializeField] private float _startingTime = 600; + [SerializeField] private bool _isCountDown; + + private void Start() + { + CurrentTime = _startingTime; + } + + private void Update() + { + if (IsRunning) + { + if (_isCountDown) + { + if (CurrentTime > 0) + { + CurrentTime -= Time.deltaTime; + } + else + { + CurrentTime = 0; + IsRunning = false; + onTimeOver?.Invoke(); + } + } + else + { + CurrentTime += Time.deltaTime; + } + } + } + + public void StartTimer() => IsRunning = true; + public void StopTimer() => IsRunning = false; + } +} diff --git a/UI/Components/ButtonUIComponent.cs b/UI/Components/ButtonUIComponent.cs new file mode 100644 index 0000000..60f907a --- /dev/null +++ b/UI/Components/ButtonUIComponent.cs @@ -0,0 +1,21 @@ +using AudioEvent; +using UnityEngine; +using UnityEngine.Events; +using UnityEngine.UI; + +namespace UI +{ + public class ButtonUIComponent : UIComponentBase + { + [SerializeField] private Button _button; + [SerializeField] private SimpleAudioEvent _clickSound; + [SerializeField] private AudioSource _audioSource; + + private void Awake() => _button.onClick.AddListener(() => _clickSound.Play(_audioSource)); + + public void OnClick(UnityAction callback) => _button.onClick.AddListener(callback); + + private void OnDestroy() => _button.onClick.RemoveAllListeners(); + + } +} \ No newline at end of file diff --git a/UI/Components/ComponentBase.cs b/UI/Components/ComponentBase.cs deleted file mode 100644 index 276fe48..0000000 --- a/UI/Components/ComponentBase.cs +++ /dev/null @@ -1,17 +0,0 @@ -using UnityEngine; - -namespace Util.UiComponents -{ - public class ComponentBase : MonoBehaviour - { - public void Show() - { - gameObject.SetActive(true); - } - - public void Hide() - { - gameObject.SetActive(false); - } - } -} \ No newline at end of file diff --git a/UI/Components/DoubleText.cs b/UI/Components/DoubleText.cs deleted file mode 100644 index 3db554e..0000000 --- a/UI/Components/DoubleText.cs +++ /dev/null @@ -1,15 +0,0 @@ -using TMPro; -using UnityEngine; - -namespace Util.UiComponents -{ - public class TextComponent : ComponentBase - { - [SerializeField] private TMP_Text text; - - public void SetText(string newText) - { - text.text = newText; - } - } -} \ No newline at end of file diff --git a/UI/Components/SliderComponent.cs b/UI/Components/SliderComponent.cs deleted file mode 100644 index 929b797..0000000 --- a/UI/Components/SliderComponent.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using UnityEngine; -using UnityEngine.Events; -using UnityEngine.UI; - -namespace Util.UiComponents -{ - public class SliderComponent : ComponentBase - { - [SerializeField] private Slider slider; - - public void SetValue(float value, float max) - { - slider.maxValue = max; - slider.value = value; - } - - public float GetValue() - { - return slider.value; - } - - public void SetOnValueChanged(UnityAction callBack) - { - slider.onValueChanged.AddListener(callBack); - } - - private void OnDestroy() - { - slider.onValueChanged?.RemoveAllListeners(); - } - } -} \ No newline at end of file diff --git a/UI/Components/SliderUIComponent.cs b/UI/Components/SliderUIComponent.cs new file mode 100644 index 0000000..4af4dbd --- /dev/null +++ b/UI/Components/SliderUIComponent.cs @@ -0,0 +1,25 @@ +using UnityEngine; +using UnityEngine.Events; +using UnityEngine.UI; + +namespace UI +{ + public class SliderUIComponent : UIComponentBase + { + [SerializeField] private Slider _slider; + + public void SetValue(float value, float max) + { + _slider.maxValue = max; + _slider.value = value; + } + + public void SetValue(float value) => _slider.value = value; + + public float GetValue() => _slider.value; + + public void OnValueChanged(UnityAction callBack) => _slider.onValueChanged.AddListener(callBack); + + private void OnDestroy() => _slider.onValueChanged?.RemoveAllListeners(); + } +} \ No newline at end of file diff --git a/UI/Components/TextComponent.cs b/UI/Components/TextComponent.cs deleted file mode 100644 index 3db554e..0000000 --- a/UI/Components/TextComponent.cs +++ /dev/null @@ -1,15 +0,0 @@ -using TMPro; -using UnityEngine; - -namespace Util.UiComponents -{ - public class TextComponent : ComponentBase - { - [SerializeField] private TMP_Text text; - - public void SetText(string newText) - { - text.text = newText; - } - } -} \ No newline at end of file diff --git a/UI/Components/TextUIComponent.cs b/UI/Components/TextUIComponent.cs new file mode 100644 index 0000000..a816c70 --- /dev/null +++ b/UI/Components/TextUIComponent.cs @@ -0,0 +1,22 @@ +using TMPro; +using UnityEngine; + +namespace UI +{ + public class TextUIComponent : UIComponentBase + { + [SerializeField] private TMP_Text _text; + + private string _initialText; + + private void Awake() => _initialText = _text.text; + + public void SetText(string newText) => _text.text = string.IsNullOrEmpty(newText) ? string.Empty : newText; + + public void ResetText() => _text.text = _initialText; + + public void AddText(string textToAdd) => _text.text += textToAdd; + + public void HideText() => _text.text = string.Empty; + } +} \ No newline at end of file diff --git a/UI/Components/UIComponentBase.cs b/UI/Components/UIComponentBase.cs new file mode 100644 index 0000000..a6b0b6b --- /dev/null +++ b/UI/Components/UIComponentBase.cs @@ -0,0 +1,28 @@ +using UnityEngine; + +namespace UI +{ + public class UIComponentBase : MonoBehaviour + { + + public void Show() + { + gameObject.Show(); + } + + public void Hide() + { + gameObject.Hide(); + } + + public void ToggleDisplay() + { + gameObject.SetActive(!gameObject.activeInHierarchy); + } + + public void SetActive(bool active) + { + gameObject.SetActive(active); + } + } +} \ No newline at end of file