提交 d668c194 编写于 作者: T tanghai

重新设计了Entity的层次关系api

上级 5864e5b0
fileFormatVersion: 2
guid: ddebf2498a1f6984bbd3290b28570092
TextScriptImporter:
userData:
fileFormatVersion: 2
guid: edf597891a39c89458673c2ff0bf977d
timeCreated: 1463827830
licenseType: Pro
TextScriptImporter:
userData:
assetBundleName:
assetBundleVariant:
......@@ -10,16 +10,33 @@ namespace Base
{
[BsonElement, BsonIgnoreIfNull]
private HashSet<Component<T>> components = new HashSet<Component<T>>();
private Dictionary<Type, Component<T>> componentDict = new Dictionary<Type, Component<T>>();
[BsonIgnore]
public T Parent { get; set; }
public string Name { get; }
[BsonElement, BsonIgnoreIfNull]
private Dictionary<long, T> idChildren;
[BsonElement, BsonIgnoreIfNull]
private Dictionary<string, T> nameChildren;
protected Entity()
{
this.Name = "";
ObjectManager.Add(this);
}
protected Entity(long id): base(id)
protected Entity(string name)
{
this.Name = name;
ObjectManager.Add(this);
}
protected Entity(long id, string name): base(id)
{
this.Name = name;
ObjectManager.Add(this);
}
......@@ -28,6 +45,59 @@ namespace Base
return MongoHelper.FromBson<T>(MongoHelper.ToBson(this));
}
public int Count
{
get
{
return this.idChildren.Count;
}
}
public void Add(T t)
{
t.Parent = (T)this;
if (this.idChildren == null)
{
this.idChildren = new Dictionary<long, T>();
this.nameChildren = new Dictionary<string, T>();
}
this.idChildren.Add(t.Id, t);
this.nameChildren.Add(t.Name, t);
}
private void Remove(T t)
{
this.idChildren.Remove(t.Id);
this.nameChildren.Remove(t.Name);
if (this.idChildren.Count == 0)
{
this.idChildren = null;
this.nameChildren = null;
}
t.Dispose();
}
public void Remove(long id)
{
T t;
if (!this.idChildren.TryGetValue(id, out t))
{
return;
}
this.Remove(t);
}
public void Remove(string name)
{
T t;
if (!this.nameChildren.TryGetValue(name, out t))
{
return;
}
this.Remove(t);
}
public override void Dispose()
{
if (this.Id == 0)
......@@ -37,6 +107,11 @@ namespace Base
base.Dispose();
foreach (T t in this.idChildren.Values.ToArray())
{
t.Dispose();
}
foreach (Component<T> component in this.GetComponents())
{
try
......@@ -48,6 +123,7 @@ namespace Base
Log.Error(e.ToString());
}
}
this.Parent?.Remove(this.Id);
ObjectManager.Remove(this.Id);
}
......
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using MongoDB.Bson.Serialization.Attributes;
namespace Base
{
public abstract class Object: ISupportInitialize, IDisposable, IEnumerable
public abstract class Object: IDisposable, ISupportInitialize
{
public static ObjectManager ObjectManager = new ObjectManager();
[BsonId]
public long Id { get; private set; }
public Dictionary<string, object> Values
{
get
{
return values;
}
}
[BsonElement, BsonIgnoreIfNull]
private Dictionary<string, object> values = new Dictionary<string, object>();
protected Object()
{
Id = IdGenerater.GenerateId();
......@@ -34,106 +21,27 @@ namespace Base
this.Id = id;
}
public virtual void BeginInit()
{
if (this.values == null)
{
this.values = new Dictionary<string, object>();
}
}
public virtual void EndInit()
{
if (this.values.Count == 0)
{
this.values = null;
}
}
public object this[string key]
{
get
{
return this.values[key];
}
set
{
if (this.values == null)
{
this.values = new Dictionary<string, object>();
}
this.values[key] = value;
}
}
public T Get<T>(string key)
{
if (this.values == null || !this.values.ContainsKey(key))
{
return default(T);
}
object value = values[key];
return (T)value;
}
public void Set(string key, object obj)
{
if (this.values == null)
{
this.values = new Dictionary<string, object>();
}
this.values[key] = obj;
}
public bool ContainKey(string key)
public bool IsDisposed()
{
if (this.values == null)
{
return false;
}
return this.values.ContainsKey(key);
return this.Id == 0;
}
public void Remove(string key)
public virtual void Dispose()
{
if (this.values == null)
if (this.Id == 0)
{
return;
}
this.values.Remove(key);
if (this.values.Count == 0)
{
this.values = null;
}
}
public void Add(string key, object value)
{
if (this.values == null)
{
this.values = new Dictionary<string, object>();
}
this.values[key] = value;
}
public IEnumerator GetEnumerator()
{
return this.values.GetEnumerator();
this.Id = 0;
}
public bool IsDisposed()
public virtual void BeginInit()
{
return this.Id == 0;
}
public virtual void Dispose()
public virtual void EndInit()
{
if (this.Id == 0)
{
return;
}
this.Id = 0;
}
}
}
\ No newline at end of file
......@@ -18,88 +18,13 @@ namespace Base
public sealed class Scene: Entity<Scene>
{
public Scene Parent { get; set; }
public string Name { get; set; }
public SceneType SceneType { get; }
private readonly Dictionary<long, Scene> Children = new Dictionary<long, Scene>();
private readonly Dictionary<SceneType, HashSet<Scene>> SceneTypeChildren = new Dictionary<SceneType, HashSet<Scene>>();
public Scene(string name, SceneType sceneType)
public Scene(string name, SceneType sceneType): base(name)
{
this.Name = name;
this.SceneType = sceneType;
}
public int Count
{
get
{
return this.Children.Count;
}
}
public void Add(Scene scene)
{
scene.Parent = this;
this.Children.Add(scene.Id, scene);
HashSet<Scene> listScene;
if (!this.SceneTypeChildren.TryGetValue(scene.SceneType, out listScene))
{
listScene = new HashSet<Scene>();
this.SceneTypeChildren.Add(scene.SceneType, listScene);
}
listScene.Add(scene);
}
public Scene Get(SceneType sceneType)
{
HashSet<Scene> scenes;
if (!this.SceneTypeChildren.TryGetValue(sceneType, out scenes))
{
return null;
}
if (scenes.Count == 0)
{
return null;
}
return scenes.First();
}
public void Remove(SceneType sceneType)
{
HashSet<Scene> scenes;
if (!this.SceneTypeChildren.TryGetValue(sceneType, out scenes))
{
return;
}
foreach (Scene scene in scenes)
{
Children.Remove(scene.Id);
scene.Dispose();
}
this.SceneTypeChildren.Remove(sceneType);
}
public void Remove(long id)
{
Scene scene;
if (!this.Children.TryGetValue(id, out scene))
{
return;
}
HashSet<Scene> scenes;
if (!this.SceneTypeChildren.TryGetValue(scene.SceneType, out scenes))
{
return;
}
scenes.Remove(scene);
scene.Dispose();
}
public override void Dispose()
{
if (this.Id == 0)
......@@ -108,8 +33,6 @@ namespace Base
}
base.Dispose();
this.Parent?.Remove(this.Id);
}
}
}
\ No newline at end of file
namespace Base
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Bson.Serialization.Attributes;
namespace Base
{
public class Env: Object
{
[BsonElement, BsonIgnoreIfNull]
private Dictionary<string, object> values = new Dictionary<string, object>();
public object this[string key]
{
get
{
return this.values[key];
}
set
{
if (this.values == null)
{
this.values = new Dictionary<string, object>();
}
this.values[key] = value;
}
}
public T Get<T>(string key)
{
if (this.values == null || !this.values.ContainsKey(key))
{
return default(T);
}
object value = values[key];
try
{
return (T)value;
}
catch (InvalidCastException e)
{
throw new GameException($"不能把{value.GetType()}转换为{typeof(T)}", e);
}
}
public void Set(string key, object obj)
{
if (this.values == null)
{
this.values = new Dictionary<string, object>();
}
this.values[key] = obj;
}
public bool ContainKey(string key)
{
if (this.values == null)
{
return false;
}
return this.values.ContainsKey(key);
}
public void Remove(string key)
{
if (this.values == null)
{
return;
}
this.values.Remove(key);
if (this.values.Count == 0)
{
this.values = null;
}
}
public void Add(string key, object value)
{
if (this.values == null)
{
this.values = new Dictionary<string, object>();
}
this.values[key] = value;
}
public IEnumerator GetEnumerator()
{
return this.values.GetEnumerator();
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册