From 7364a1cf5ff9e828e0fda903e7645291f008995c Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Wed, 6 Apr 2011 18:57:05 +0000 Subject: [PATCH] Mob entities all added, entity factory created. --- Substrate/SubstrateCS/Source/Entity.cs | 910 ++++++++++++++++-- Substrate/SubstrateCS/Source/EntityFactory.cs | 73 ++ .../SubstrateCS/Source/TileEntityFactory.cs | 4 +- 3 files changed, 885 insertions(+), 102 deletions(-) create mode 100644 Substrate/SubstrateCS/Source/EntityFactory.cs diff --git a/Substrate/SubstrateCS/Source/Entity.cs b/Substrate/SubstrateCS/Source/Entity.cs index 5721c3a..7ee19fc 100644 --- a/Substrate/SubstrateCS/Source/Entity.cs +++ b/Substrate/SubstrateCS/Source/Entity.cs @@ -9,7 +9,7 @@ namespace Substrate.Map public class Entity : INBTObject, ICopyable { - public class Vector3 + public class Vector3 { public double X { get; set; } public double Y { get; set; } @@ -44,7 +44,7 @@ namespace Substrate.Map private short _air; private byte _onGround; - private string? _world; + private string _world; public string ID { @@ -124,100 +124,9 @@ namespace Substrate.Map } - #region Predefined Schemas - - public static readonly NBTCompoundNode MobSchema = BaseSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Mob"), - new NBTScalerNode("AttackTime", NBT_Type.TAG_SHORT), - new NBTScalerNode("DeathTime", NBT_Type.TAG_SHORT), - new NBTScalerNode("Health", NBT_Type.TAG_SHORT), - new NBTScalerNode("HurtTime", NBT_Type.TAG_SHORT), - }); - - public static readonly NBTCompoundNode MonsterSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Monster"), - }); - - public static readonly NBTCompoundNode CreeperSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Creeper"), - }); - - public static readonly NBTCompoundNode SkeletonSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Skeleton"), - }); - - public static readonly NBTCompoundNode SpiderSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Spider"), - }); - - public static readonly NBTCompoundNode GiantSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Giant"), - }); - - public static readonly NBTCompoundNode ZombieSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Zombie"), - }); - - public static readonly NBTCompoundNode PigZombieSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "PigZombie"), - }); - - public static readonly NBTCompoundNode GhastSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Ghast"), - }); - - public static readonly NBTCompoundNode PigSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Pig"), - new NBTScalerNode("Saddle", NBT_Type.TAG_BYTE), - }); - - public static readonly NBTCompoundNode SheepSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Sheep"), - new NBTScalerNode("Sheared", NBT_Type.TAG_BYTE), - new NBTScalerNode("Color", NBT_Type.TAG_BYTE), - }); - - public static readonly NBTCompoundNode CowSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Cow"), - }); - - public static readonly NBTCompoundNode ChickenSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Chicken"), - }); - - public static readonly NBTCompoundNode Slimechema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Slime"), - new NBTScalerNode("Size", NBT_Type.TAG_INT), - }); - - public static readonly NBTCompoundNode WolfSchema = MobSchema.MergeInto(new NBTCompoundNode("") - { - new NBTStringNode("id", "Wolf"), - new NBTScalerNode("Owner", NBT_Type.TAG_STRING), - new NBTScalerNode("Sitting", NBT_Type.TAG_BYTE), - new NBTScalerNode("Angry", NBT_Type.TAG_BYTE), - }); - - #endregion - - #region INBTObject Members - public Entity LoadTree (NBT_Value tree) + public virtual Entity LoadTree (NBT_Value tree) { NBT_Compound ctree = tree as NBT_Compound; if (ctree == null) { @@ -257,7 +166,7 @@ namespace Substrate.Map return this; } - public Entity LoadTreeSafe (NBT_Value tree) + public virtual Entity LoadTreeSafe (NBT_Value tree) { if (!ValidateTree(tree)) { return null; @@ -266,7 +175,7 @@ namespace Substrate.Map return LoadTree(tree); } - public NBT_Value BuildTree () + public virtual NBT_Value BuildTree () { NBT_Compound tree = new NBT_Compound(); tree["id"] = new NBT_String(_id); @@ -294,13 +203,13 @@ namespace Substrate.Map tree["OnGround"] = new NBT_Byte(_onGround); if (_world != null) { - tree["World"] = new NBT_String(_world.GetValueOrDefault()); + tree["World"] = new NBT_String(_world); } return tree; } - public bool ValidateTree (NBT_Value tree) + public virtual bool ValidateTree (NBT_Value tree) { return new NBTVerifier(tree, BaseSchema).Verify(); } @@ -310,7 +219,7 @@ namespace Substrate.Map #region ICopyable Members - public Entity Copy () + public virtual Entity Copy () { return new Entity(this); } @@ -320,6 +229,807 @@ namespace Substrate.Map public class EntityMob : Entity { + public static readonly NBTCompoundNode MobSchema = BaseSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Mob"), + new NBTScalerNode("AttackTime", NBT_Type.TAG_SHORT), + new NBTScalerNode("DeathTime", NBT_Type.TAG_SHORT), + new NBTScalerNode("Health", NBT_Type.TAG_SHORT), + new NBTScalerNode("HurtTime", NBT_Type.TAG_SHORT), + }); + private short _attackTime; + private short _deathTime; + private short _health; + private short _hurtTime; + + public int AttackTime + { + get { return _attackTime; } + set { _attackTime = (short)value; } + } + + public int DeathTime + { + get { return _deathTime; } + set { _deathTime = (short)value; } + } + + public int Health + { + get { return _health; } + set { _health = (short)value; } + } + + public int HurtTime + { + get { return _hurtTime; } + set { _hurtTime = (short)value; } + } + + public EntityMob () + : base("Mob") + { + } + + public EntityMob (string id) + : base(id) + { + } + + public EntityMob (Entity e) + : base(e) + { + EntityMob e2 = e as EntityMob; + if (e2 != null) { + _attackTime = e2._attackTime; + _deathTime = e2._deathTime; + _health = e2._health; + _hurtTime = e2._hurtTime; + } + } + + + #region INBTObject Members + + public override Entity LoadTree (NBT_Value tree) + { + NBT_Compound ctree = tree as NBT_Compound; + if (ctree == null || base.LoadTree(tree) == null) { + return null; + } + + _attackTime = ctree["AttackTime"].ToNBTShort(); + _deathTime = ctree["DeathTime"].ToNBTShort(); + _health = ctree["Health"].ToNBTShort(); + _hurtTime = ctree["HurtTime"].ToNBTShort(); + + return this; + } + + public override NBT_Value BuildTree () + { + NBT_Compound tree = base.BuildTree() as NBT_Compound; + tree["AttackTime"] = new NBT_Short(_attackTime); + tree["DeathTime"] = new NBT_Short(_deathTime); + tree["Health"] = new NBT_Short(_health); + tree["HurtTime"] = new NBT_Short(_hurtTime); + + return tree; + } + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, MobSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityMob(this); + } + + #endregion } -} + + public class EntityMonster : EntityMob + { + public static readonly NBTCompoundNode MonsterSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Monster"), + }); + + public EntityMonster () + : base("Monster") + { + } + + public EntityMonster (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, MonsterSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityMonster(this); + } + + #endregion + } + + public class EntityCreeper : EntityMob + { + public static readonly NBTCompoundNode CreeperSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Creeper"), + }); + + public EntityCreeper () + : base("Creeper") + { + } + + public EntityCreeper (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, CreeperSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityCreeper(this); + } + + #endregion + } + + public class EntitySkeleton : EntityMob + { + public static readonly NBTCompoundNode SkeletonSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Skeleton"), + }); + + public EntitySkeleton () + : base("Skeleton") + { + } + + public EntitySkeleton (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, SkeletonSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntitySkeleton(this); + } + + #endregion + } + + public class EntitySpider : EntityMob + { + public static readonly NBTCompoundNode SpiderSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Spider"), + }); + + public EntitySpider () + : base("Spider") + { + } + + public EntitySpider (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, SpiderSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntitySpider(this); + } + + #endregion + } + + public class EntityGiant : EntityMob + { + public static readonly NBTCompoundNode GiantSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Giant"), + }); + + public EntityGiant () + : base("Giant") + { + } + + public EntityGiant (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, GiantSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityGiant(this); + } + + #endregion + } + + public class EntityZombie : EntityMob + { + public static readonly NBTCompoundNode ZombieSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Zombie"), + }); + + public EntityZombie () + : base("Zombie") + { + } + + public EntityZombie (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, ZombieSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityZombie(this); + } + + #endregion + } + + public class EntitySlime : EntityMob + { + public static readonly NBTCompoundNode SlimeSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Slime"), + new NBTScalerNode("Size", NBT_Type.TAG_INT), + }); + + private int _size; + + public int Size + { + get { return _size; } + set { _size = value; } + } + + public EntitySlime () + : base("Slime") + { + } + + public EntitySlime (Entity e) + : base(e) + { + EntitySlime e2 = e as EntitySlime; + if (e2 != null) { + _size = e2._size; + } + } + + + #region INBTObject Members + + public override Entity LoadTree (NBT_Value tree) + { + NBT_Compound ctree = tree as NBT_Compound; + if (ctree == null || base.LoadTree(tree) == null) { + return null; + } + + _size = ctree["Size"].ToNBTInt(); + + return this; + } + + public override NBT_Value BuildTree () + { + NBT_Compound tree = base.BuildTree() as NBT_Compound; + tree["Size"] = new NBT_Int(_size); + + return tree; + } + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, SlimeSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntitySlime(this); + } + + #endregion + } + + public class EntityPigZombie : EntityMob + { + public static readonly NBTCompoundNode PigZombieSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "PigZombie"), + }); + + public EntityPigZombie () + : base("PigZombie") + { + } + + public EntityPigZombie (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, PigZombieSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityPigZombie(this); + } + + #endregion + } + + public class EntityGhast : EntityMob + { + public static readonly NBTCompoundNode GhastSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Ghast"), + }); + + public EntityGhast () + : base("Ghast") + { + } + + public EntityGhast (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, GhastSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityGhast(this); + } + + #endregion + } + + public class EntityPig : EntityMob + { + public static readonly NBTCompoundNode PigSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Pig"), + new NBTScalerNode("Saddle", NBT_Type.TAG_BYTE), + }); + + private bool _saddle; + + public bool HasSaddle + { + get { return _saddle; } + set { _saddle = value; } + } + + public EntityPig () + : base("Pig") + { + } + + public EntityPig (Entity e) + : base(e) + { + EntityPig e2 = e as EntityPig; + if (e2 != null) { + _saddle = e2._saddle; + } + } + + + #region INBTObject Members + + public override Entity LoadTree (NBT_Value tree) + { + NBT_Compound ctree = tree as NBT_Compound; + if (ctree == null || base.LoadTree(tree) == null) { + return null; + } + + _saddle = ctree["Saddle"].ToNBTByte() == 1; + + return this; + } + + public override NBT_Value BuildTree () + { + NBT_Compound tree = base.BuildTree() as NBT_Compound; + tree["Saddle"] = new NBT_Byte((byte)(_saddle ? 1 : 0)); + + return tree; + } + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, PigSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityPig(this); + } + + #endregion + } + + public class EntitySheep : EntityMob + { + public static readonly NBTCompoundNode SheepSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Pig"), + new NBTScalerNode("Sheared", NBT_Type.TAG_BYTE), + new NBTScalerNode("Color", NBT_Type.TAG_BYTE), + }); + + private bool _sheared; + private byte _color; + + public bool IsSheared + { + get { return _sheared; } + set { _sheared = value; } + } + + public int Color + { + get { return _color; } + set { _color = (byte)value; } + } + + public EntitySheep () + : base("Sheep") + { + } + + public EntitySheep (Entity e) + : base(e) + { + EntitySheep e2 = e as EntitySheep; + if (e2 != null) { + _sheared = e2._sheared; + _color = e2._color; + } + } + + + #region INBTObject Members + + public override Entity LoadTree (NBT_Value tree) + { + NBT_Compound ctree = tree as NBT_Compound; + if (ctree == null || base.LoadTree(tree) == null) { + return null; + } + + _sheared = ctree["Sheared"].ToNBTByte() == 1; + _color = ctree["Color"].ToNBTByte(); + + return this; + } + + public override NBT_Value BuildTree () + { + NBT_Compound tree = base.BuildTree() as NBT_Compound; + tree["Sheared"] = new NBT_Byte((byte)(_sheared ? 1 : 0)); + tree["Color"] = new NBT_Byte(_color); + + return tree; + } + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, SheepSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntitySheep(this); + } + + #endregion + } + + public class EntityCow : EntityMob + { + public static readonly NBTCompoundNode CowSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Cow"), + }); + + public EntityCow () + : base("Cow") + { + } + + public EntityCow (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, CowSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityCow(this); + } + + #endregion + } + + public class EntityChicken : EntityMob + { + public static readonly NBTCompoundNode ChickenSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Chicken"), + }); + + public EntityChicken () + : base("Chicken") + { + } + + public EntityChicken (Entity e) + : base(e) + { + } + + + #region INBTObject Members + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, ChickenSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityChicken(this); + } + + #endregion + } + + public class EntityWolf : EntityMob + { + public static readonly NBTCompoundNode WolfSchema = MobSchema.MergeInto(new NBTCompoundNode("") + { + new NBTStringNode("id", "Wolf"), + new NBTScalerNode("Owner", NBT_Type.TAG_STRING), + new NBTScalerNode("Sitting", NBT_Type.TAG_BYTE), + new NBTScalerNode("Angry", NBT_Type.TAG_BYTE), + }); + + private string _owner; + private bool _sitting; + private bool _angry; + + public string Owner + { + get { return _owner; } + set { _owner = value; } + } + + public bool IsSitting + { + get { return _sitting; } + set { _sitting = value; } + } + + public bool IsAngry + { + get { return _angry; } + set { _angry = value; } + } + + public EntityWolf () + : base("Wolf") + { + } + + public EntityWolf (Entity e) + : base(e) + { + EntityWolf e2 = e as EntityWolf; + if (e2 != null) { + _owner = e2._owner; + _sitting = e2._sitting; + _angry = e2._angry; + } + } + + + #region INBTObject Members + + public override Entity LoadTree (NBT_Value tree) + { + NBT_Compound ctree = tree as NBT_Compound; + if (ctree == null || base.LoadTree(tree) == null) { + return null; + } + + _owner = ctree["Owner"].ToNBTString(); + _sitting = ctree["Sitting"].ToNBTByte() == 1; + _angry = ctree["Angry"].ToNBTByte() == 1; + + return this; + } + + public override NBT_Value BuildTree () + { + NBT_Compound tree = base.BuildTree() as NBT_Compound; + tree["Owner"] = new NBT_String(_owner); + tree["Sitting"] = new NBT_Byte((byte)(_sitting ? 1 : 0)); + tree["Angry"] = new NBT_Byte((byte)(_angry ? 1 : 0)); + + return tree; + } + + public override bool ValidateTree (NBT_Value tree) + { + return new NBTVerifier(tree, WolfSchema).Verify(); + } + + #endregion + + + #region ICopyable Members + + public override Entity Copy () + { + return new EntityPig(this); + } + + #endregion + } +} \ No newline at end of file diff --git a/Substrate/SubstrateCS/Source/EntityFactory.cs b/Substrate/SubstrateCS/Source/EntityFactory.cs new file mode 100644 index 0000000..848dd4b --- /dev/null +++ b/Substrate/SubstrateCS/Source/EntityFactory.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Substrate.Map +{ + using NBT; + + public class EntityFactory + { + private static Dictionary _registry; + + public static Entity Create (string type) + { + Type t; + if (!_registry.TryGetValue(type, out t)) { + return null; + } + + return Activator.CreateInstance(t, new object[] { type} ) as Entity; + } + + public static Entity Create (NBT_Compound tree) + { + string type = tree["id"].ToNBTString(); + + Type t; + if (!_registry.TryGetValue(type, out t)) { + return null; + } + + Entity te = Activator.CreateInstance(t) as Entity; + + return te.LoadTreeSafe(tree); + } + + public static Type Lookup (string type) + { + Type t; + if (!_registry.TryGetValue(type, out t)) { + return null; + } + + return t; + } + + public static void Register (string id, Type subtype) + { + _registry[id] = subtype; + } + + static EntityFactory () + { + _registry = new Dictionary(); + + _registry["Chicken"] = typeof(EntityChicken); + _registry["Cow"] = typeof(EntityCow); + _registry["Creeper"] = typeof(EntityCreeper); + _registry["Ghast"] = typeof(EntityGhast); + _registry["Giant"] = typeof(EntityGiant); + _registry["Mob"] = typeof(EntityMob); + _registry["Monster"] = typeof(EntityMonster); + _registry["Pig"] = typeof(EntityPig); + _registry["PigZombie"] = typeof(EntityPigZombie); + _registry["Sheep"] = typeof(EntitySheep); + _registry["Skeleton"] = typeof(EntitySkeleton); + _registry["Slime"] = typeof(EntitySlime); + _registry["Spider"] = typeof(EntitySpider); + _registry["Wolf"] = typeof(EntityWolf); + _registry["Zombie"] = typeof(EntityZombie); + } + } +} diff --git a/Substrate/SubstrateCS/Source/TileEntityFactory.cs b/Substrate/SubstrateCS/Source/TileEntityFactory.cs index 0d25715..56726cf 100644 --- a/Substrate/SubstrateCS/Source/TileEntityFactory.cs +++ b/Substrate/SubstrateCS/Source/TileEntityFactory.cs @@ -17,7 +17,7 @@ namespace Substrate.Map return null; } - return Activator.CreateInstance(t) as TileEntity; + return Activator.CreateInstance(t, new object[] { type }) as TileEntity; } public static TileEntity Create (NBT_Compound tree) @@ -29,7 +29,7 @@ namespace Substrate.Map return null; } - TileEntity te = Activator.CreateInstance(t, new object[] { tree }) as TileEntity; + TileEntity te = Activator.CreateInstance(t) as TileEntity; return te.LoadTreeSafe(tree); }