Explorar o código

删除了Component继承的设计,reload不好处理,既然都是一类了没必要再继承处理,使用if else走不同的逻辑就好了

tanghai %!s(int64=11) %!d(string=hai) anos
pai
achega
4171e82f36

+ 0 - 2
CSharp/Game/Controller/Controller.csproj

@@ -48,14 +48,12 @@
     <Compile Include="BehaviorTreeNode\Not.cs" />
     <Compile Include="BehaviorTreeNode\Selector.cs" />
     <Compile Include="BehaviorTreeNode\Sequence.cs" />
-    <Compile Include="EnvKey.cs" />
     <Compile Include="ConfigCategory\BuffCategory.cs" />
     <Compile Include="ConfigCategory\GlobalCategory.cs" />
     <Compile Include="ConfigCategory\NodeCategory.cs" />
     <Compile Include="ConfigCategory\UnitCategory.cs" />
     <Compile Include="NodeType.cs" />
     <Compile Include="Factory\UnitFactory.cs" />
-    <Compile Include="EventType.cs" />
     <Compile Include="UnitType.cs" />
   </ItemGroup>
   <ItemGroup>

+ 0 - 1
CSharp/Game/Controller/Factory/UnitFactory.cs

@@ -8,7 +8,6 @@ namespace Controller
         public Unit Create(int configId)
         {
             Unit player = new Unit(configId);
-            player.AddComponent<PlayerDeadComponent>();
             player.AddComponent<BuffComponent>();
             Buff buff = new Buff(1);
             player.GetComponent<BuffComponent>().Add(buff);

+ 17 - 0
CSharp/Game/Model/Component/BuffComponent.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using Common.Base;
+using Common.Event;
 using MongoDB.Bson;
 using MongoDB.Bson.Serialization.Attributes;
 
@@ -54,9 +55,17 @@ namespace Model
                 throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
             }
 
+            Env env = new Env();
+            env[EnvKey.Owner] = this.Owner;
+            env[EnvKey.Buff] = buff;
+
+            World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.BeforeAddBuff, env);
+
             this.buffs.Add(buff);
             this.idBuff.Add(buff.Id, buff);
             this.typeBuff.Add(buff.Config.Type, buff);
+
+            World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.AfterAddBuff, env);
         }
 
         public Buff GetById(ObjectId id)
@@ -86,10 +95,18 @@ namespace Model
                 return false;
             }
 
+            Env env = new Env();
+            env[EnvKey.Owner] = this.Owner;
+            env[EnvKey.Buff] = buff;
+
+            World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.BeforeRemoveBuff, env);
+
             this.buffs.Remove(buff);
             this.idBuff.Remove(buff.Id);
             this.typeBuff.Remove(buff.Config.Type, buff);
 
+            World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.AfterRemoveBuff, env);
+
             return true;
         }
 

+ 0 - 15
CSharp/Game/Model/Component/DeadComponent.cs

@@ -1,15 +0,0 @@
-using System;
-using Common.Base;
-
-namespace Model
-{
-    public abstract class DeadComponent: Component<Unit>
-    {
-        public override Type GetComponentType()
-        {
-            return typeof (DeadComponent);
-        }
-
-        public abstract void Dead();
-    }
-}

+ 1 - 1
CSharp/Game/Model/Component/EventComponent.cs

@@ -20,7 +20,7 @@ namespace Model
             {
                 object[] attrs = t.GetCustomAttributes(typeof (AttributeType), false);
                 if (attrs.Length == 0)
-                {
+                { 
                     continue;
                 }
                 object obj = Activator.CreateInstance(t);

+ 1 - 1
CSharp/Game/Model/Component/FactoryComponent.cs

@@ -42,7 +42,7 @@ namespace Model
 
         public T Create(int configId)
         {
-            int type = (int) World.Instance.GetComponent<ConfigComponent>().Get<UnitConfig>(configId).Type;
+            int type = World.Instance.GetComponent<ConfigComponent>().Get<UnitConfig>(configId).Type;
             return this.allConfig[type].Create(configId);
         }
     }

+ 0 - 9
CSharp/Game/Model/Component/MonsterDeadComponent.cs

@@ -1,9 +0,0 @@
-namespace Model
-{
-    public class MonsterDeadComponent: DeadComponent
-    {
-        public override void Dead()
-        {
-        }
-    }
-}

+ 0 - 12
CSharp/Game/Model/Component/PlayerDeadComponent.cs

@@ -1,12 +0,0 @@
-using System;
-
-namespace Model
-{
-    public class PlayerDeadComponent: DeadComponent
-    {
-        public override void Dead()
-        {
-            Console.WriteLine("Player {0} Dead!", this.Owner.Id);
-        }
-    }
-}

+ 2 - 2
CSharp/Game/Controller/EnvKey.cs → CSharp/Game/Model/EnvKey.cs

@@ -1,8 +1,8 @@
-namespace Controller
+namespace Model
 {
     public static class EnvKey
     {
-        public const string Unit = "Unit";
+        public const string Owner = "Owner";
         public const string Buff = "Buff";
     }
 }

+ 1 - 1
CSharp/Game/Controller/EventType.cs → CSharp/Game/Model/EventType.cs

@@ -1,4 +1,4 @@
-namespace Controller
+namespace Model
 {
     public static class EventType
     {

+ 2 - 3
CSharp/Game/Model/Model.csproj

@@ -52,15 +52,14 @@
     <Compile Include="BehaviorTree\Node.cs" />
     <Compile Include="BehaviorTree\NodeAttribute.cs" />
     <Compile Include="Buff.cs" />
-    <Compile Include="Component\DeadComponent.cs" />
     <Compile Include="Component\MessageComponent.cs" />
     <Compile Include="Component\BuffComponent.cs" />
     <Compile Include="Component\ConfigComponent.cs" />
     <Compile Include="Component\EventComponent.cs" />
-    <Compile Include="Component\MonsterDeadComponent.cs" />
-    <Compile Include="Component\PlayerDeadComponent.cs" />
     <Compile Include="Config\UnitConfig.cs" />
     <Compile Include="Config\BuffConfig.cs" />
+    <Compile Include="EnvKey.cs" />
+    <Compile Include="EventType.cs" />
     <Compile Include="MessageAttribute.cs" />
     <Compile Include="FactoryAttribute.cs" />
     <Compile Include="Component\FactoryComponent.cs" />

+ 1 - 1
CSharp/Game/Model/Unit.cs

@@ -6,7 +6,7 @@ namespace Model
     public class Unit : Entity<Unit>
     {
         [BsonElement]
-        public int configId { get; set; }
+        private int configId { get; set; }
 
         [BsonIgnore]
         public UnitConfig Config

+ 7 - 5
CSharp/Game/MongoDBTest/MongoDBTest.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Threading;
 using Common.Helper;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Model;
@@ -33,18 +34,19 @@ namespace MongoDBTest
 
             Unit player1 = world.GetComponent<FactoryComponent<Unit>>().Create(1);
             player1["hp"] = 10;
-
-            player1.GetComponent<DeadComponent>().Dead();
-
+            
             collection.Insert(player1);
 
             var query = Query<Unit>.EQ(e => e.Id, player1.Id);
             Unit player2 = collection.FindOne(query);
-
-            player2.GetComponent<DeadComponent>().Dead();
             
             Console.WriteLine(MongoHelper.ToJson(player2));
             Assert.AreEqual(MongoHelper.ToJson(player1), MongoHelper.ToJson(player2));
+
+            Thread.Sleep(20 * 1000);
+            world.Load();
+
+            Assert.AreEqual(MongoHelper.ToJson(player1), MongoHelper.ToJson(player2));
         }
     }
 }

+ 1 - 1
CSharp/Platform/Common/Base/Component.cs

@@ -13,7 +13,7 @@ namespace Common.Base
         [BsonIgnore]
         public T Owner
         {
-            get
+            protected get
             {
                 return owner;
             }