提交 53a7803a 编写于 作者: T tanghai

Entity和Component继承ISupportInitialize,实现EndInit,反序列化后进行初始化

上级 6dd31f36
......@@ -4,18 +4,6 @@ namespace Model
{
public class Buff: Object
{
private BuffType type;
public BuffType Type
{
get
{
return this.type;
}
set
{
this.type = value;
}
}
public BuffType Type { get; set; }
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.ComponentModel;
using Common.Base;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using Component = Common.Base.Component;
namespace Model
{
public class BuffComponent: Component, ISupportInitialize
public class BuffComponent: Component
{
public HashSet<Buff> Buffs { get; set; }
[BsonElement]
private HashSet<Buff> Buffs { get; set; }
private Dictionary<ObjectId, Buff> buffGuidDict { get; set; }
......@@ -21,11 +22,7 @@ namespace Model
this.buffTypeMMap = new MultiMap<BuffType, Buff>();
}
void ISupportInitialize.BeginInit()
{
}
void ISupportInitialize.EndInit()
public override void EndInit()
{
foreach (var buff in this.Buffs)
{
......
......@@ -9,18 +9,6 @@ namespace Model
public class GameObject: Entity
{
private GameObjectType type;
public GameObjectType Type
{
get
{
return this.type;
}
set
{
this.type = value;
}
}
public GameObjectType Type { get; set; }
}
}
\ No newline at end of file
using MongoDB.Bson.Serialization.Attributes;
using System.ComponentModel;
using MongoDB.Bson.Serialization.Attributes;
namespace Common.Base
{
public class Component: Object
public abstract class Component : Object, ISupportInitialize
{
private Entity owner;
[BsonIgnore]
public Entity Owner {
public Entity Owner
{
get
{
return owner;
......@@ -19,7 +21,11 @@ namespace Common.Base
}
}
protected Component()
public virtual void BeginInit()
{
}
public virtual void EndInit()
{
}
}
......
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using MongoDB.Bson.Serialization.Attributes;
namespace Common.Base
{
public class Entity: Object
public abstract class Entity : Object, ISupportInitialize
{
public Dictionary<string, Component> Components { get; private set; }
[BsonElement]
private HashSet<Component> Components { get; set; }
private Dictionary<Type, Component> ComponentDict { get; set; }
protected Entity()
{
this.Components = new Dictionary<string, Component>();
this.Components = new HashSet<Component>();
this.ComponentDict = new Dictionary<Type, Component>();
}
public void AddComponent<T>() where T : Component, new()
{
T t = new T { Owner = this };
this.Components.Add(typeof(T).Name, t);
this.Components.Add(t);
this.ComponentDict.Add(typeof (T), t);
}
public void RemoveComponent<T>() where T : Component
{
this.Components.Remove(typeof(T).Name);
Component t;
this.ComponentDict.TryGetValue(typeof(T), out t);
this.Components.Remove(t);
this.ComponentDict.Remove(typeof(T));
}
public T GetComponent<T>() where T : Component
{
Component t;
if (!this.Components.TryGetValue(typeof(T).Name, out t))
if (!this.ComponentDict.TryGetValue(typeof (T), out t))
{
return null;
}
......@@ -35,7 +46,20 @@ namespace Common.Base
public Component[] GetComponents()
{
return this.Components.Values.ToArray();
return this.Components.ToArray();
}
public virtual void BeginInit()
{
}
public virtual void EndInit()
{
foreach (Component component in this.Components)
{
this.ComponentDict.Add(component.GetType(), component);
component.Owner = this;
}
}
}
}
\ No newline at end of file
......@@ -4,14 +4,14 @@ using MongoDB.Bson.Serialization.Attributes;
namespace Common.Base
{
public class Object
public abstract class Object
{
[BsonId]
public ObjectId Guid { get; protected set; }
[BsonElement]
[BsonIgnoreIfNull]
public Dictionary<string, object> Values;
private Dictionary<string, object> Values;
protected Object()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册