提交 ede8c8db 编写于 作者: T tanghai

1.简单的逻辑代码可以放到Component

2.增加Component继承模式,比如怪物的死亡和玩家的死亡不一样,这样可以设计PlayerDeadComponent和MonsterDeadComponent继承于DeadComponent,实现不同的死亡.但是带来一个问题,这部分逻辑将无法reload
上级 9ebe4e6a
using System;
using System.Collections.Generic;
using Common.Event;
using Model;
using MongoDB.Bson;
namespace Controller
{
/// <summary>
/// 控制复杂的buff逻辑,可以reload
/// </summary>
public static class BuffComponentExtension
{
public static void Add(this BuffComponent buffComponent, Buff buff)
{
if (buffComponent.buffs.Contains(buff))
{
throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
}
if (buffComponent.idBuff.ContainsKey(buff.Id))
{
throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
}
Env env = new Env();
env[EnvKey.Unit] = buffComponent.Owner;
env[EnvKey.Buff] = buff;
World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.BeforeAddBuff, env);
buffComponent.buffs.Add(buff);
buffComponent.idBuff.Add(buff.Id, buff);
buffComponent.typeBuff.Add(buff.Config.Type, buff);
World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.AfterAddBuff, env);
}
public static Buff GetById(this BuffComponent buffComponent, ObjectId id)
{
if (!buffComponent.idBuff.ContainsKey(id))
{
return null;
}
return buffComponent.idBuff[id];
}
public static Buff GetOneByType(this BuffComponent buffComponent, BuffType type)
{
return buffComponent.typeBuff.GetOne(type);
}
public static Buff[] GetByType(this BuffComponent buffComponent, BuffType type)
{
return buffComponent.typeBuff.GetByKey(type);
}
private static bool Remove(this BuffComponent buffComponent, Buff buff)
{
if (buff == null)
{
return false;
}
Env env = new Env();
env[EnvKey.Unit] = buffComponent.Owner;
env[EnvKey.Buff] = buff;
World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.BeforeRemoveBuff, env);
buffComponent.buffs.Remove(buff);
buffComponent.idBuff.Remove(buff.Id);
buffComponent.typeBuff.Remove(buff.Config.Type, buff);
World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.AfterRemoveBuff, env);
return true;
}
public static bool RemoveById(this BuffComponent buffComponent, ObjectId id)
{
Buff buff = buffComponent.GetById(id);
return buffComponent.Remove(buff);
}
public static void RemoveByType(this BuffComponent buffComponent, BuffType type)
{
Buff[] allbuffs = buffComponent.GetByType(type);
foreach (Buff buff in allbuffs)
{
buffComponent.Remove(buff);
}
}
}
}
\ No newline at end of file
......@@ -49,8 +49,6 @@
<Compile Include="BehaviorTreeNode\Selector.cs" />
<Compile Include="BehaviorTreeNode\Sequence.cs" />
<Compile Include="EnvKey.cs" />
<Compile Include="UnitComponentExtension.cs" />
<Compile Include="BuffComponentExtension.cs" />
<Compile Include="ConfigCategory\BuffCategory.cs" />
<Compile Include="ConfigCategory\GlobalCategory.cs" />
<Compile Include="ConfigCategory\NodeCategory.cs" />
......
......@@ -8,6 +8,7 @@ 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);
......
using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using MongoDB.Bson;
namespace Controller
{
/// <summary>
/// 控制复杂的unit逻辑,可以reload
/// </summary>
public static class UnitComponentExtension
{
public static void Add(this UnitComponent unitComponent, Unit unit)
{
unitComponent.units.Add(unit.Id, unit);
if (!unitComponent.typeUnits.ContainsKey(unit.Config.Type))
{
unitComponent.typeUnits.Add(unit.Config.Type, new Dictionary<ObjectId, Unit>());
}
unitComponent.typeUnits[unit.Config.Type].Add(unit.Id, unit);
}
public static Unit Get(this UnitComponent unitComponent, ObjectId id)
{
Unit unit = null;
unitComponent.units.TryGetValue(id, out unit);
return unit;
}
public static Unit[] GetOneType(this UnitComponent unitComponent, int type)
{
Dictionary<ObjectId, Unit> oneTypeUnits = null;
if (!unitComponent.typeUnits.TryGetValue(type, out oneTypeUnits))
{
return new Unit[0];
}
return oneTypeUnits.Values.ToArray();
}
public static bool Remove(this UnitComponent unitComponent, Unit unit)
{
if (unit == null)
{
throw new ArgumentNullException("unit");
}
if (!unitComponent.units.Remove(unit.Id))
{
return false;
}
if (!unitComponent.typeUnits[unit.Config.Type].Remove(unit.Id))
{
return false;
}
return true;
}
public static bool Remove(this UnitComponent unitComponent, ObjectId id)
{
Unit unit = unitComponent.Get(id);
if (unit == null)
{
return false;
}
return unitComponent.Remove(unit);
}
}
}
\ No newline at end of file
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Common.Base;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Model
{
public class BuffComponent: Component<Unit>
public class BuffComponent : Component<Unit>
{
[BsonElement]
public HashSet<Buff> buffs;
private HashSet<Buff> buffs;
[BsonIgnore]
public Dictionary<ObjectId, Buff> idBuff;
private Dictionary<ObjectId, Buff> idBuff;
[BsonIgnore]
public MultiMap<BuffType, Buff> typeBuff;
private MultiMap<BuffType, Buff> typeBuff;
public BuffComponent()
{
......@@ -42,5 +41,71 @@ namespace Model
this.typeBuff.Add(buff.Config.Type, buff);
}
}
public void Add(Buff buff)
{
if (this.buffs.Contains(buff))
{
throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
}
if (this.idBuff.ContainsKey(buff.Id))
{
throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id));
}
this.buffs.Add(buff);
this.idBuff.Add(buff.Id, buff);
this.typeBuff.Add(buff.Config.Type, buff);
}
public Buff GetById(ObjectId id)
{
if (!this.idBuff.ContainsKey(id))
{
return null;
}
return this.idBuff[id];
}
public Buff GetOneByType(BuffType type)
{
return this.typeBuff.GetOne(type);
}
public Buff[] GetByType(BuffType type)
{
return this.typeBuff.GetByKey(type);
}
private bool Remove(Buff buff)
{
if (buff == null)
{
return false;
}
this.buffs.Remove(buff);
this.idBuff.Remove(buff.Id);
this.typeBuff.Remove(buff.Config.Type, buff);
return true;
}
public bool RemoveById(ObjectId id)
{
Buff buff = this.GetById(id);
return this.Remove(buff);
}
public void RemoveByType(BuffType type)
{
Buff[] allbuffs = this.GetByType(type);
foreach (Buff buff in allbuffs)
{
this.Remove(buff);
}
}
}
}
\ No newline at end of file
using System;
using Common.Base;
namespace Model
{
public abstract class DeadComponent: Component<Unit>
{
public override Type GetComponentType()
{
return typeof (DeadComponent);
}
public abstract void Dead();
}
}
namespace Model
{
public class MonsterDeadComponent: DeadComponent
{
public override void Dead()
{
}
}
}
using System;
namespace Model
{
public class PlayerDeadComponent: DeadComponent
{
public override void Dead()
{
Console.WriteLine("Player {0} Dead!", this.Owner.Id);
}
}
}
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Common.Base;
using MongoDB.Bson;
namespace Model
{
public class UnitComponent: Component<World>
public class UnitComponent : Component<World>
{
public readonly Dictionary<ObjectId, Unit> units =
private readonly Dictionary<ObjectId, Unit> units =
new Dictionary<ObjectId, Unit>();
public readonly Dictionary<int, Dictionary<ObjectId, Unit>> typeUnits =
private readonly Dictionary<int, Dictionary<ObjectId, Unit>> typeUnits =
new Dictionary<int, Dictionary<ObjectId, Unit>>();
public void Add(Unit unit)
{
this.units.Add(unit.Id, unit);
if (!this.typeUnits.ContainsKey(unit.Config.Type))
{
this.typeUnits.Add(unit.Config.Type, new Dictionary<ObjectId, Unit>());
}
this.typeUnits[unit.Config.Type].Add(unit.Id, unit);
}
public Unit Get(ObjectId id)
{
Unit unit = null;
this.units.TryGetValue(id, out unit);
return unit;
}
public Unit[] GetOneType(int type)
{
Dictionary<ObjectId, Unit> oneTypeUnits = null;
if (!this.typeUnits.TryGetValue(type, out oneTypeUnits))
{
return new Unit[0];
}
return oneTypeUnits.Values.ToArray();
}
public bool Remove(Unit unit)
{
if (unit == null)
{
throw new ArgumentNullException("unit");
}
if (!this.units.Remove(unit.Id))
{
return false;
}
if (!this.typeUnits[unit.Config.Type].Remove(unit.Id))
{
return false;
}
return true;
}
public bool Remove(ObjectId id)
{
Unit unit = this.Get(id);
if (unit == null)
{
return false;
}
return this.Remove(unit);
}
}
}
\ No newline at end of file
......@@ -52,10 +52,13 @@
<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="MessageAttribute.cs" />
......
......@@ -34,10 +34,14 @@ 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));
......
using MongoDB.Bson.Serialization.Attributes;
using System;
using MongoDB.Bson.Serialization.Attributes;
namespace Common.Base
{
......@@ -22,5 +23,14 @@ namespace Common.Base
this.Id = this.owner.Id;
}
}
/// <summary>
/// 用于父类的Component需要重写此方法
/// </summary>
/// <returns></returns>
public virtual Type GetComponentType()
{
return this.GetType();
}
}
}
\ No newline at end of file
......@@ -15,10 +15,12 @@ namespace Common.Base
public T AddComponent<T>() where T : Component<K>, new()
{
if (this.componentDict.ContainsKey(typeof (T)))
T t = new T { Owner = (K) this };
if (this.componentDict.ContainsKey(t.GetComponentType()))
{
throw new Exception(
string.Format("AddComponent, component already exist, id: {0}, component: {1}",
string.Format("AddComponent, component already exist, id: {0}, component: {1}",
this.Id, typeof(T).Name));
}
......@@ -27,12 +29,28 @@ namespace Common.Base
this.components = new HashSet<Component<K>>();
}
T t = new T { Owner = (K)this };
this.components.Add(t);
this.componentDict.Add(typeof (T), t);
this.componentDict.Add(t.GetComponentType(), t);
return t;
}
public void AddComponent(Component<K> component)
{
if (this.componentDict.ContainsKey(component.GetComponentType()))
{
throw new Exception(
string.Format("AddComponent, component already exist, id: {0}, component: {1}",
this.Id, component.GetComponentType().Name));
}
if (this.components == null)
{
this.components = new HashSet<Component<K>>();
}
this.components.Add(component);
this.componentDict.Add(component.GetComponentType(), component);
}
public void RemoveComponent<T>() where T : Component<K>
{
Component<K> t;
......@@ -85,7 +103,7 @@ namespace Common.Base
foreach (var component in this.components)
{
component.Owner = (K)this;
this.componentDict.Add(component.GetType(), component);
this.componentDict.Add(component.GetComponentType(), component);
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册