NBTExplorer/Substrate/SubstrateCS/Source/EntityCollection.cs

134 lines
3.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Text;
namespace Substrate
{
using NBT;
public class EntityCollection
{
private TagList _entities;
private bool _dirty;
public bool IsDirty
{
get { return _dirty; }
set { _dirty = value; }
}
public EntityCollection (TagList entities)
{
_entities = entities;
}
public List<Entity> FindEntities (string id)
{
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;
}
public List<Entity> FindEntities (Predicate<Entity> match)
{
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;
}
public bool AddEntity (Entity ent)
{
/*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;
}
public int RemoveEntities (string id)
{
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;
}
return rem;
}
public int RemoveEntities (Predicate<Entity> match)
{
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;
}
}
}