Exemplo n.º 1
0
 public void TestComponentDoubleRemoval()
 {
     Entity entity = new Entity();
     entity.Add(new ComponentA());
     entity.Remove<ComponentA>();
     entity.Remove<ComponentA>();
     Assert.AreEqual(0, entity.ComponentCount);
 }
Exemplo n.º 2
0
        public void TestContains()
        {
            Entity entity = new Entity();
            entity.Add(new ComponentA());
            Assert.AreEqual(true, entity.Contains<ComponentA>());

            entity.Remove<ComponentA>();
            Assert.AreEqual(false, entity.Contains<ComponentA>());
        }
Exemplo n.º 3
0
        public static void MakeAttachable(Entity entity, Main main)
        {
            Transform transform = entity.Get<Transform>();
            Property<float> attachOffset = entity.GetOrMakeProperty<float>("AttachmentOffset", true);
            Property<Entity.Handle> map = entity.GetOrMakeProperty<Entity.Handle>("AttachedMap");
            Property<Map.Coordinate> coord = entity.GetOrMakeProperty<Map.Coordinate>("AttachedCoordinate");

            if (main.EditorEnabled)
                return;

            Binding<Matrix> attachmentBinding = null;
            CommandBinding deleteBinding = null;
            CommandBinding<IEnumerable<Map.Coordinate>, Map> cellEmptiedBinding = null;

            entity.Add(new NotifyBinding(delegate()
            {
                if (attachmentBinding != null)
                {
                    entity.Remove(attachmentBinding);
                    entity.Remove(deleteBinding);
                    entity.Remove(cellEmptiedBinding);
                }

                Map m = map.Value.Target.Get<Map>();
                coord.Value = m.GetCoordinate(Vector3.Transform(new Vector3(0, 0, attachOffset), transform.Matrix));

                Matrix offset = transform.Matrix * Matrix.Invert(Matrix.CreateTranslation(m.Offset) * m.Transform);

                attachmentBinding = new Binding<Matrix>(transform.Matrix, () => offset * Matrix.CreateTranslation(m.Offset) * m.Transform, m.Transform, m.Offset);
                entity.Add(attachmentBinding);

                deleteBinding = new CommandBinding(m.Delete, entity.Delete);
                entity.Add(deleteBinding);

                cellEmptiedBinding = new CommandBinding<IEnumerable<Map.Coordinate>, Map>(m.CellsEmptied, delegate(IEnumerable<Map.Coordinate> coords, Map newMap)
                {
                    foreach (Map.Coordinate c in coords)
                    {
                        if (c.Equivalent(coord))
                        {
                            if (newMap == null)
                                entity.Delete.Execute();
                            else
                                map.Value = newMap.Entity;
                            break;
                        }
                    }
                });
                entity.Add(cellEmptiedBinding);
            }, map));

            entity.Add(new PostInitialization
            {
                delegate()
                {
                    if (map.Value.Target == null)
                    {
                        Map closestMap = null;
                        int closestDistance = 3;
                        float closestFloatDistance = 3.0f;
                        Vector3 target = Vector3.Transform(new Vector3(0, 0, attachOffset), transform.Matrix);
                        foreach (Map m in Map.Maps)
                        {
                            Map.Coordinate targetCoord = m.GetCoordinate(target);
                            Map.Coordinate? c = m.FindClosestFilledCell(targetCoord, closestDistance);
                            if (c.HasValue)
                            {
                                float distance = (m.GetRelativePosition(c.Value) - m.GetRelativePosition(targetCoord)).Length();
                                if (distance < closestFloatDistance)
                                {
                                    closestFloatDistance = distance;
                                    closestDistance = (int)Math.Floor(distance);
                                    closestMap = m;
                                }
                            }
                        }
                        if (closestMap == null)
                            entity.Delete.Execute();
                        else
                            map.Value = closestMap.Entity;
                    }
                    else
                        map.Reset();
                }
            });
        }
Exemplo n.º 4
0
        public static void Bind(Entity result, Main main, Func<BEPUphysics.Entities.Entity, BEPUphysics.Entities.Entity, Vector3, Vector3, Vector3, ISpaceObject> createJoint, bool allowRotation, bool creating = false)
        {
            Transform mapTransform = result.GetOrCreate<Transform>("MapTransform");

            Transform transform = result.GetOrCreate<Transform>("Transform");

            Factory.Get<DynamicMapFactory>().InternalBind(result, main, creating, mapTransform);

            DynamicMap map = result.Get<DynamicMap>();

            Property<Entity.Handle> parentMap = result.GetOrMakeProperty<Entity.Handle>("Parent");
            Property<Map.Coordinate> coord = result.GetOrMakeProperty<Map.Coordinate>("Coord");
            Property<Direction> dir = result.GetOrMakeProperty<Direction>("Direction", true);

            Action refreshMapTransform = delegate()
            {
                Entity parent = parentMap.Value.Target;
                if (parent != null)
                {
                    if (!parent.Active)
                        parent = null;
                    else
                    {
                        Map staticMap = parent.Get<Map>();
                        coord.Value = staticMap.GetCoordinate(transform.Position);
                        mapTransform.Position.Value = staticMap.GetAbsolutePosition(staticMap.GetRelativePosition(coord) - new Vector3(0.5f) + staticMap.Offset + map.Offset);
                        if (!allowRotation)
                            mapTransform.Orientation.Value = parent.Get<Transform>().Orientation;
                    }
                }
                else
                    mapTransform.Matrix.Value = transform.Matrix;
            };
            if (main.EditorEnabled)
                result.Add(new NotifyBinding(refreshMapTransform, transform.Matrix, map.Offset));

            ISpaceObject joint = null;
            CommandBinding jointDeleteBinding = null, physicsUpdateBinding = null;

            Action rebuildJoint = null;
            rebuildJoint = delegate()
            {
                if (joint != null)
                {
                    if (joint.Space != null)
                        main.Space.Remove(joint);
                    result.Remove(jointDeleteBinding);
                    if (physicsUpdateBinding != null)
                        result.Remove(physicsUpdateBinding);
                    physicsUpdateBinding = null;
                    joint = null;
                    jointDeleteBinding = null;
                }

                Entity parent = parentMap.Value.Target;

                if (main.EditorEnabled)
                {
                    refreshMapTransform();
                    return;
                }

                if (parent != null)
                {
                    if (!parent.Active)
                        parent = null;
                    else
                    {
                        Map staticMap = parent.Get<Map>();

                        map.PhysicsEntity.Position = mapTransform.Position;
                        if (!allowRotation)
                            map.PhysicsEntity.Orientation = mapTransform.Quaternion;

                        if (dir != Direction.None && !main.EditorEnabled)
                        {
                            Vector3 relativeLineAnchor = staticMap.GetRelativePosition(coord) - new Vector3(0.5f) + staticMap.Offset + map.Offset;
                            Vector3 lineAnchor = staticMap.GetAbsolutePosition(relativeLineAnchor);
                            DynamicMap dynamicMap = parent.Get<DynamicMap>();
                            joint = createJoint(map.PhysicsEntity, dynamicMap == null ? null : dynamicMap.PhysicsEntity, map.PhysicsEntity.Position, staticMap.GetAbsoluteVector(dir.Value.GetVector()), lineAnchor);
                            main.Space.Add(joint);
                            map.PhysicsEntity.ActivityInformation.Activate();

                            if (dynamicMap != null)
                            {
                                physicsUpdateBinding = new CommandBinding(dynamicMap.PhysicsUpdated, rebuildJoint);
                                result.Add(physicsUpdateBinding);
                            }

                            jointDeleteBinding = new CommandBinding(parent.Delete, delegate()
                            {
                                parentMap.Value = null;
                            });
                            result.Add(jointDeleteBinding);
                        }
                    }
                }
            };
            result.Add(new NotifyBinding(rebuildJoint, parentMap));
            result.Add(new CommandBinding(result.Delete, delegate()
            {
                if (joint != null && joint.Space != null)
                {
                    main.Space.Remove(joint);
                    joint = null;
                }
            }));
            result.Add(new CommandBinding(map.OnSuspended, delegate()
            {
                if (joint != null && joint.Space != null)
                    main.Space.Remove(joint);
            }));
            result.Add(new CommandBinding(map.OnResumed, delegate()
            {
                if (joint != null && joint.Space == null)
                    main.Space.Add(joint);
            }));
            rebuildJoint();
            Command rebuildJointCommand = new Command();
            result.Add(new CommandBinding(rebuildJointCommand, rebuildJoint));
            result.Add("RebuildJoint", rebuildJointCommand);

            if (main.EditorEnabled)
                JointFactory.attachEditorComponents(result, main);
        }
Exemplo n.º 5
0
        public void testRemoveComponent()
        {
            Entity entity = new Entity();
            Id id = new Id();
            id.Value = 1;
            entity.Add(id);
            Name name = new Name();
            name.Value = "bob";
            entity.Add(name);

            NodeList<TestNode> nodeList = Ash.GetNodeList<TestNode>();

            Ash.AddEntity(entity);
            //Ash.RemoveEntity(entity);
            Assert.AreEqual(nodeList.Count, 1);

            entity.Remove(typeof(Name));
            Assert.AreEqual(nodeList.Count, 0); // entity should no longer be in nodeList
        }
Exemplo n.º 6
0
		public static void Bind(Entity entity, Main main, Func<BEPUphysics.Entities.Entity, BEPUphysics.Entities.Entity, Vector3, Vector3, Vector3, ISpaceObject> createJoint, bool allowRotation, bool creating = false, bool directional = true)
		{
			Transform mapTransform = entity.GetOrCreate<Transform>("MapTransform");
			mapTransform.Selectable.Value = false;

			Transform transform = entity.GetOrCreate<Transform>("Transform");

			Factory.Get<DynamicVoxelFactory>().InternalBind(entity, main, creating, mapTransform);

			DynamicVoxel map = entity.Get<DynamicVoxel>();

			Components.Joint jointData = entity.GetOrCreate<Components.Joint>("Joint");

			Action refreshMapTransform = delegate()
			{
				Entity parent = jointData.Parent.Value.Target;
				if (parent != null && parent.Active)
				{
					Voxel staticMap = parent.Get<Voxel>();
					jointData.Coord.Value = staticMap.GetCoordinate(transform.Matrix.Value.Translation);
					mapTransform.Position.Value = staticMap.GetAbsolutePosition(staticMap.GetRelativePosition(jointData.Coord) - new Vector3(0.5f) + staticMap.Offset + map.Offset.Value);
					if (allowRotation)
					{
						if (main.EditorEnabled)
							mapTransform.Quaternion.Value = transform.Quaternion;
					}
					else
					{
						Matrix parentOrientation = staticMap.Transform;
						parentOrientation.Translation = Vector3.Zero;
						mapTransform.Quaternion.Value = Quaternion.CreateFromRotationMatrix(parentOrientation);
					}
				}
				else
					mapTransform.Matrix.Value = transform.Matrix;
			};

			if (main.EditorEnabled)
				entity.Add(new NotifyBinding(refreshMapTransform, transform.Matrix, transform.Quaternion, map.Offset, jointData.Parent));

			ISpaceObject joint = null;
			CommandBinding jointDeleteBinding = null, parentPhysicsUpdateBinding = null;
			NotifyBinding parentStaticMoveBinding = null;

			Action updateJoint = null;

			Action rebuildJoint = null;
			rebuildJoint = delegate()
			{
				if (jointDeleteBinding != null)
					entity.Remove(jointDeleteBinding);
				jointDeleteBinding = null;

				if (parentPhysicsUpdateBinding != null)
					entity.Remove(parentPhysicsUpdateBinding);
				parentPhysicsUpdateBinding = null;

				updateJoint();
			};

			updateJoint = delegate()
			{
				if (joint != null)
				{
					if (joint.Space != null)
						main.Space.Remove(joint);
					joint = null;
				}
				if (parentStaticMoveBinding != null)
				{
					entity.Remove(parentStaticMoveBinding);
					parentStaticMoveBinding = null;
				}

				Entity parent = jointData.Parent.Value.Target;

				if (main.EditorEnabled)
					refreshMapTransform();
				else if (parent != null && parent.Active)
				{
					Voxel parentStaticMap = parent.Get<Voxel>();

					//map.PhysicsEntity.Position = mapTransform.Position;
					if (!allowRotation)
						map.PhysicsEntity.Orientation = mapTransform.Quaternion;

					if (jointData.Direction != Direction.None)
					{
						Vector3 relativeLineAnchor = parentStaticMap.GetRelativePosition(jointData.Coord) - new Vector3(0.5f) + parentStaticMap.Offset + map.Offset;
						Vector3 lineAnchor = parentStaticMap.GetAbsolutePosition(relativeLineAnchor);
						DynamicVoxel parentDynamicMap = parent.Get<DynamicVoxel>();
						joint = createJoint(map.PhysicsEntity, parentDynamicMap == null ? null : parentDynamicMap.PhysicsEntity, lineAnchor, parentStaticMap.GetAbsoluteVector(jointData.Direction.Value.GetVector()), parentStaticMap.GetAbsolutePosition(jointData.Coord));
						main.Space.Add(joint);
						map.PhysicsEntity.ActivityInformation.Activate();

						if (parentDynamicMap != null && parentPhysicsUpdateBinding == null)
						{
							parentPhysicsUpdateBinding = new CommandBinding(parentDynamicMap.PhysicsUpdated, updateJoint);
							entity.Add(parentPhysicsUpdateBinding);
						}

						if (parentDynamicMap == null && joint is PrismaticJoint)
						{
							parentStaticMoveBinding = new NotifyBinding(delegate()
							{
								PrismaticJoint prismaticJoint = (PrismaticJoint)joint;
								Vector3 a = parentStaticMap.GetAbsolutePosition(relativeLineAnchor);
								prismaticJoint.PointOnLineJoint.LineAnchor = a;
								prismaticJoint.Limit.OffsetA = a;
								prismaticJoint.Motor.OffsetA = a;
							}, parentStaticMap.Transform);
							entity.Add(parentStaticMoveBinding);
						}

						if (jointDeleteBinding == null)
						{
							jointDeleteBinding = new CommandBinding(parent.Delete, delegate()
							{
								jointData.Parent.Value = null;
							});
							entity.Add(jointDeleteBinding);
						}
					}
				}
			};
			entity.Add(new CommandBinding(map.PhysicsUpdated, updateJoint));
			entity.Add(new NotifyBinding(rebuildJoint, jointData.Parent));
			entity.Add(new CommandBinding(entity.Delete, delegate()
			{
				if (joint != null && joint.Space != null)
				{
					main.Space.Remove(joint);
					joint = null;
				}
			}));
			
			entity.Add(new CommandBinding(map.OnSuspended, delegate()
			{
				if (joint != null && joint.Space != null)
					main.Space.Remove(joint);
			}));
			entity.Add(new CommandBinding(map.OnResumed, delegate()
			{
				if (joint != null && joint.Space == null)
					main.Space.Add(joint);
			}));
			
			entity.Add(new PostInitialization(rebuildJoint));

			if (main.EditorEnabled)
				JointFactory.attachEditorComponents(entity, main, directional);
		}