2011-05-13 03:09:57 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Substrate
|
|
|
|
|
{
|
|
|
|
|
using NBT;
|
|
|
|
|
|
2011-06-01 06:11:50 +00:00
|
|
|
|
public class EntityCollection : IEnumerable<Entity>
|
2011-05-13 03:09:57 +00:00
|
|
|
|
{
|
|
|
|
|
private TagList _entities;
|
|
|
|
|
|
|
|
|
|
private bool _dirty;
|
|
|
|
|
|
|
|
|
|
public bool IsDirty
|
|
|
|
|
{
|
|
|
|
|
get { return _dirty; }
|
|
|
|
|
set { _dirty = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EntityCollection (TagList entities)
|
|
|
|
|
{
|
|
|
|
|
_entities = entities;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-01 06:11:50 +00:00
|
|
|
|
public List<Entity> FindAll (string id)
|
2011-05-13 03:09:57 +00:00
|
|
|
|
{
|
|
|
|
|
List<Entity> set = new List<Entity>();
|
|
|
|
|
|
|
|
|
|
foreach (TagCompound ent in _entities) {
|
|
|
|
|
TagValue eid;
|
|
|
|
|
if (!ent.TryGetValue("id", out eid)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (eid.ToTagString().Data != id) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Entity obj = EntityFactory.Create(ent);
|
|
|
|
|
if (obj != null) {
|
|
|
|
|
set.Add(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-01 06:11:50 +00:00
|
|
|
|
public List<Entity> FindAll (Predicate<Entity> match)
|
2011-05-13 03:09:57 +00:00
|
|
|
|
{
|
|
|
|
|
List<Entity> set = new List<Entity>();
|
|
|
|
|
|
|
|
|
|
foreach (TagCompound ent in _entities) {
|
|
|
|
|
Entity obj = EntityFactory.Create(ent);
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (match(obj)) {
|
|
|
|
|
set.Add(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-01 06:11:50 +00:00
|
|
|
|
public bool Add (Entity ent)
|
2011-05-13 03:09:57 +00:00
|
|
|
|
{
|
|
|
|
|
/*double xlow = _cx * XDim;
|
|
|
|
|
double xhigh = xlow + XDim;
|
|
|
|
|
double zlow = _cz * ZDim;
|
|
|
|
|
double zhigh = zlow + ZDim;
|
|
|
|
|
|
|
|
|
|
Entity.Vector3 pos = ent.Position;
|
|
|
|
|
if (!(pos.X >= xlow && pos.X < xhigh && pos.Z >= zlow && pos.Z < zhigh)) {
|
|
|
|
|
return false;
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
_entities.Add(ent.BuildTree());
|
|
|
|
|
_dirty = true;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-01 06:11:50 +00:00
|
|
|
|
public int RemoveAll (string id)
|
2011-05-13 03:09:57 +00:00
|
|
|
|
{
|
|
|
|
|
int rem = _entities.RemoveAll(val =>
|
|
|
|
|
{
|
|
|
|
|
TagCompound cval = val as TagCompound;
|
|
|
|
|
if (cval == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TagValue sval;
|
|
|
|
|
if (!cval.TryGetValue("id", out sval)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (sval.ToTagString().Data == id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (rem > 0) {
|
|
|
|
|
_dirty = true;
|
|
|
|
|
}
|
2011-06-01 06:11:50 +00:00
|
|
|
|
|
2011-05-13 03:09:57 +00:00
|
|
|
|
return rem;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-01 06:11:50 +00:00
|
|
|
|
public int RemoveAll (Predicate<Entity> match)
|
2011-05-13 03:09:57 +00:00
|
|
|
|
{
|
|
|
|
|
int rem = _entities.RemoveAll(val =>
|
|
|
|
|
{
|
|
|
|
|
TagCompound cval = val as TagCompound;
|
|
|
|
|
if (cval == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Entity obj = EntityFactory.Create(cval);
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return match(obj);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (rem > 0) {
|
|
|
|
|
_dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rem;
|
|
|
|
|
}
|
2011-06-01 06:11:50 +00:00
|
|
|
|
|
|
|
|
|
#region IEnumerable<Entity> Members
|
|
|
|
|
|
|
|
|
|
public IEnumerator<Entity> GetEnumerator ()
|
|
|
|
|
{
|
|
|
|
|
return new EntityEnumerator(_entities);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IEnumerable Members
|
|
|
|
|
|
|
|
|
|
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
|
|
|
|
|
{
|
|
|
|
|
return new EntityEnumerator(_entities);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public class EntityEnumerator : IEnumerator<Entity>
|
|
|
|
|
{
|
|
|
|
|
private TagList _entities;
|
|
|
|
|
private IEnumerator<TagValue> _enum;
|
|
|
|
|
|
|
|
|
|
private Entity _cur;
|
|
|
|
|
|
|
|
|
|
public EntityEnumerator (TagList entities)
|
|
|
|
|
{
|
|
|
|
|
_entities = entities;
|
|
|
|
|
_enum = entities.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IEnumerator<Entity> Members
|
|
|
|
|
|
|
|
|
|
public Entity Current
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_cur == null) {
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
return _cur;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
|
|
|
|
|
|
public void Dispose () { }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IEnumerator Members
|
|
|
|
|
|
|
|
|
|
object System.Collections.IEnumerator.Current
|
|
|
|
|
{
|
|
|
|
|
get { return Current; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool MoveNext ()
|
|
|
|
|
{
|
|
|
|
|
if (!_enum.MoveNext()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_cur = EntityFactory.Create(_enum.Current.ToTagCompound());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reset ()
|
|
|
|
|
{
|
|
|
|
|
_cur = null;
|
|
|
|
|
_enum.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
2011-05-13 03:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|