Explorar el Código

Added Statistics GamModule

James Peret hace 2 años
padre
commit
3e1bcc6cc5

+ 19 - 8
Runtime/Statistics/Statistics.cs

@@ -27,8 +27,7 @@ namespace KairoEngine.Utilities.Statistics
         }
         #endregion
 
-        [ListDrawerSettings(ShowPaging = false)]
-        public List<StatisticData> data = new List<StatisticData>();
+        [InlineEditor(InlineEditorObjectFieldModes.Boxed)] public StatisticsList db;
 
         void Awake()
         {
@@ -37,6 +36,15 @@ namespace KairoEngine.Utilities.Statistics
                 Destroy(this.gameObject);
                 return;
             }
+        }
+
+        void Start()
+        {
+            if(db == null)
+            {
+                Debug.LogError("Statistics component is missing the StatisticsList.\nPlease configure the statistics module in the Game config file.");
+                return;
+            }
             LoadStatistics();
         }
 
@@ -48,9 +56,10 @@ namespace KairoEngine.Utilities.Statistics
         public static StatisticData GetData(string title)
         {
             if(instance == null) return null;
-            for (int i = 0; i < instance.data.Count; i++)
+            if(instance.db == null) return null;
+            for (int i = 0; i < instance.db.data.Count; i++)
             {
-                if(instance.data[i].title == title) return instance.data[i];
+                if(instance.db.data[i].title == title) return instance.db.data[i];
             }
             return null;
         }
@@ -58,9 +67,10 @@ namespace KairoEngine.Utilities.Statistics
         private void LoadStatistics()
         {
             if(instance == null) return;
-            for (int i = 0; i < instance.data.Count; i++)
+            if(instance.db == null) return;
+            for (int i = 0; i < instance.db.data.Count; i++)
             {
-                StatisticData stat = instance.data[i];
+                StatisticData stat = instance.db.data[i];
                 if(stat.persistent == false) continue;
                 switch (stat.category)
                 {
@@ -85,9 +95,10 @@ namespace KairoEngine.Utilities.Statistics
         private void SaveStatistics()
         {
             if(instance == null) return;
-            for (int i = 0; i < instance.data.Count; i++)
+            if(instance.db == null) return;
+            for (int i = 0; i < instance.db.data.Count; i++)
             {
-                StatisticData stat = instance.data[i];
+                StatisticData stat = instance.db.data[i];
                 if(stat.persistent == false) continue;
                 switch (stat.category)
                 {

+ 1 - 1
Runtime/Statistics/StatisticsConsoleCommands.cs

@@ -26,7 +26,7 @@ namespace KairoEngine.Utilities.Statistics
                 QuantumTheme theme = console.GetTheme();
                 color = theme ? theme.SuccessColor : Color.white;
             }
-            foreach (var stat in Statistics.instance.data)
+            foreach (var stat in Statistics.instance.db.data)
             {
                 result += stat.title;
                 switch (stat.category)

+ 14 - 0
Runtime/Statistics/StatisticsList.cs

@@ -0,0 +1,14 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using Sirenix.OdinInspector;
+
+namespace KairoEngine.Utilities.Statistics
+{
+    [CreateAssetMenu(fileName = "StatisticsList", menuName = "KairoEngine/StatisticsList"), HideMonoScript]
+    public class StatisticsList : ScriptableObject
+    {
+        [ListDrawerSettings(ShowPaging = false)]
+        public List<StatisticData> data = new List<StatisticData>();
+    }
+}

+ 11 - 0
Runtime/Statistics/StatisticsList.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 48fb955243a877d4ea4cbe3debd8bcb1
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 66 - 0
Runtime/Statistics/StatisticsModule.cs

@@ -0,0 +1,66 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using KairoEngine.Core;
+using KairoEngine.Core.ModuleSystem;
+using Sirenix.OdinInspector;
+
+namespace KairoEngine.Utilities.Statistics
+{
+    [Serializable, HideReferenceObjectPicker]
+    public class StatisticsModule : GameModuleBase
+    {
+        public override string name => "Statistics Module";
+        [FoldoutGroup("@name"), NonSerialized, ShowInInspector, InlineEditor(InlineEditorObjectFieldModes.Boxed)] public StatisticsList database;
+        public StatisticsModule(GameConfig config) : base(config)
+        {
+            this.gameConfig = config;
+            this.className = this.GetType().AssemblyQualifiedName;
+            this.typeName = "StatisticsModule";
+        }
+
+        public override void Load(Transform parent)
+        {
+            if(database == null)
+            {
+                Debug.LogError("Missing statistics database.\nPlease configure the statistics module in the Game config file.");
+                return;
+            }
+            var obj = new GameObject();
+            obj.transform.parent = parent;
+            obj.name = "Statistics";
+            var comp = obj.AddComponent<Statistics>();
+            comp.db = database;
+        }
+
+        public override void Reset()
+        {
+            
+        }
+
+        public override void Destroy() { }
+
+        public static StatisticsModule JSONToStatisticsModule(string data)
+        {
+            try
+            {
+                return JsonUtility.FromJson<StatisticsModule>(data);
+            }
+            catch (System.Exception e)
+            {
+                Debug.LogError($"Could not deserialize StatisticsModule: \n{e}");
+                return new StatisticsModule(null);
+            }
+        }
+
+        public override void OnBeforeSerialize(ObjectSerializer serializer) 
+        {
+            if(database != null) serializer.AddScriptableObject("Statistics_Database", database);
+        }
+        public override void OnBeforeDeserialize(ObjectSerializer serializer)
+        { 
+            database = (StatisticsList)serializer.GetScriptableObject("Statistics_Database");
+        }
+    }
+}

+ 11 - 0
Runtime/Statistics/StatisticsModule.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: a3fea9601b9d8e8419adff3354bb0180
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 16 - 0
Runtime/Utilities.cs

@@ -62,6 +62,22 @@ namespace KairoEngine.Utility
             else return Vector3.zero;
         }
 
+        public static Vector3 GetMouseWorldPosition(int mouseColliderLayerMask, ref bool pointerHit)
+        {
+            if(Camera.main == null) return new Vector3();
+            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
+            if(Physics.Raycast(ray, out RaycastHit raycastHit, 999f, mouseColliderLayerMask))
+            {
+                pointerHit = true;
+                return raycastHit.point;
+            }
+            else
+            {
+                pointerHit = false;
+                return Vector3.zero;
+            } 
+        }
+
         // Create a Text Popup in the World, no parent
         public static void CreateWorldTextPopup(string text, Vector3 localPosition) {
             CreateWorldTextPopup(null, text, localPosition, 40, Color.white, localPosition + new Vector3(0, 20), 1f, true);