Arsene 4 vuotta sitten
vanhempi
commit
a4225b5430

+ 5 - 3
Assets/FrameWorkDesign/Example/Scripts/Command/KillEnemyCommand.cs

@@ -5,9 +5,11 @@ namespace FrameWorkDesign.Example
     {
         public void Execute()
         {
-            GameModel.killCount.Value++;
-            Debug.Log(GameModel.killCount.Value);
-            if (GameModel.killCount.Value == 6)
+            var gameModel = PointGame.Get<IGameModel>();
+
+            gameModel.killCount.Value++;
+           /* Debug.Log(gameModel.killCount.Value);*/
+            if (gameModel.killCount.Value == 6)
             {
                 GamePassEvent.Trigger();
             }

Assets/FrameWorkDesign/Example/Scripts/Enemy.cs → Assets/FrameWorkDesign/Example/Scripts/Game/Enemy.cs


Assets/FrameWorkDesign/Example/Scripts/Enemy.cs.meta → Assets/FrameWorkDesign/Example/Scripts/Game/Enemy.cs.meta


+ 18 - 6
Assets/FrameWorkDesign/Example/Scripts/Game/Model/GameModel.cs

@@ -5,27 +5,39 @@ using UnityEngine;
 
 namespace FrameWorkDesign.Example
 {
-   public class GameModel
+    public interface IGameModel
     {
-        public static BindableProperty<int> killCount = new BindableProperty<int>()
+
+        BindableProperty<int> killCount { get; }
+
+        BindableProperty<int> Gold { get; }
+
+        BindableProperty<int> Score { get; }
+
+        BindableProperty<int> BestScore { get; }
+
+
+    }
+    public class GameModel : IGameModel
+    {
+        public BindableProperty<int> killCount { get; } = new BindableProperty<int>()
         {
             Value = 0
         };
 
-        public static BindableProperty<int> Gold = new BindableProperty<int>()
+        public BindableProperty<int> Gold { get; } = new BindableProperty<int>()
         {
             Value = 0
         };
 
-        public static BindableProperty<int> Score = new BindableProperty<int>()
+        public BindableProperty<int> Score { get; } = new BindableProperty<int>()
         {
             Value = 0
         };
 
-        public static BindableProperty<int> BestScore = new BindableProperty<int>()
+        public BindableProperty<int> BestScore { get; } = new BindableProperty<int>()
         {
             Value = 0
         };
     }
-
 }

+ 15 - 0
Assets/FrameWorkDesign/Example/Scripts/PointGame.cs

@@ -0,0 +1,15 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace FrameWorkDesign.Example
+{
+    public class PointGame : Architecture<PointGame>
+    {
+        protected override void Init()
+        {
+            Register<IGameModel>(new GameModel());
+        }
+    }
+}
+

+ 1 - 1
Assets/FrameWorkDesign/Example/Scripts/Enemy.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 03dab518e674c124e98b668b805aec4e
+guid: 5cd643ac9a3510c499c5391dfa59fcb3
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 8 - 0
Assets/FrameWorkDesign/FrameWork/Architecture.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: e288e16af1b95c0469c90ba5afb5629a
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 38 - 0
Assets/FrameWorkDesign/FrameWork/Architecture/Architecture.cs

@@ -0,0 +1,38 @@
+using FrameWorkDesign.Example;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace FrameWorkDesign
+{
+    public abstract class Architecture<T> where T : Architecture<T>, new()
+    {
+        private static T mArchitecture;
+        static void MakeSureArchitecture()
+        {
+            if (mArchitecture == null)
+            {
+                mArchitecture = new T();
+                mArchitecture.Init();
+            }
+        }
+        protected abstract void Init();
+
+        private IOCContainer mContainer = new IOCContainer();
+
+        public static T Get<T>() where T : class
+        {
+            MakeSureArchitecture();
+            return mArchitecture.mContainer.Get<T>();
+        }
+
+        public void Register<T>(T instance)
+        {
+            MakeSureArchitecture();
+
+            mArchitecture.mContainer.Register<T>(instance);
+        }
+    }
+
+}
+

+ 1 - 1
Assets/FrameWorkDesign/Example/Scripts/Enemy.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 03dab518e674c124e98b668b805aec4e
+guid: 1eb3e27d15270f348b8a9e099e368e64
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 8 - 0
Assets/FrameWorkDesign/FrameWork/IOC.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: a5dffbe2b02f87e4e8fff6843b5c0998
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 39 - 0
Assets/FrameWorkDesign/FrameWork/IOC/IOCContainer.cs

@@ -0,0 +1,39 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace FrameWorkDesign.Example
+{
+    public class IOCContainer
+    {
+        private Dictionary<Type, object> mInstances = new Dictionary<Type, object>();
+
+        public void Register<T>(T instance)
+        {
+            var key = typeof(T);
+
+            if (mInstances.ContainsKey(key))
+            {
+                mInstances[key] = instance;
+            }
+            else
+            {
+                mInstances.Add(key, instance);
+            }
+        }
+
+        public T Get<T>() where T : class
+        {
+            var key = typeof(T);
+
+            if (mInstances.TryGetValue(key,out var retInstance))
+            {
+                return retInstance as T;
+            }
+
+            return null;
+        }
+    }
+
+}

+ 1 - 1
Assets/FrameWorkDesign/Example/Scripts/Enemy.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 03dab518e674c124e98b668b805aec4e
+guid: be3fc6bca1be59d4d9924b67bc397fb0
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 8 - 0
Assets/FrameWorkDesign/FrameWork/Singleton.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 147e71e3e14583a4eb65c2d1cbf5dc27
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 34 - 0
Assets/FrameWorkDesign/FrameWork/Singleton/Singleton.cs

@@ -0,0 +1,34 @@
+using UnityEngine;
+using System.Reflection;
+using System;
+
+namespace FrameWorkDesign
+{
+    public class Singleton <T>where T:Singleton<T>
+    {
+        private static T mInstance;
+
+        public static T Instance
+        {
+            get
+            {
+                if (mInstance == null)
+                {
+                    var type = typeof(T);
+                    var ctors = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
+                    var ctor = Array.Find(ctors, c => c.GetParameters().Length == 0);
+
+                    if (ctor == null)
+                    {
+                        throw new Exception("Non Public Constructor Not Fount in " + type.Name);
+                    }
+
+                    mInstance= ctor.Invoke(null) as T;
+
+                }
+                return mInstance;
+            }
+        }
+    }
+
+}

+ 1 - 1
Assets/FrameWorkDesign/Example/Scripts/Enemy.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 03dab518e674c124e98b668b805aec4e
+guid: 82108e2163f64b043986a9a0e92fb17a
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2