public void TestMove()
        {
            var itemsAdded = new List <MySpatialMapItem>();

            var sm = new LayeredSpatialMap <MySpatialMapItem>(5);

            for (int i = 0; i < 5; i++)
            {
                var item = new MySpatialMapItem(i);
                itemsAdded.Add(item);
                sm.Add(item, (1, 2));
            }

            var lastItem = new MySpatialMapItem(3);

            itemsAdded.Add(lastItem);
            sm.Add(lastItem, (5, 6));

            bool result = sm.Move(lastItem, 1, 2);

            Assert.AreEqual(false, result);             // Blocked by first 5 adds and they no layers support multiple items

            result = sm.Move(lastItem, (2, 3));         // At 2, 3 we now have item on layer 3
            Assert.AreEqual(true, result);

            List <MySpatialMapItem> itemsMoved = sm.Move((1, 2), (2, 3)).ToList();

            Assert.AreEqual(4, itemsMoved.Count);             // All original items minus 1 because that one was blocked

            itemsMoved = sm.Move((2, 3), (1, 2), ~sm.LayerMasker.Mask(2)).ToList();
            Assert.AreEqual(3, itemsMoved.Count);             // lastItem from above masked out by layer

            sm = new LayeredSpatialMap <MySpatialMapItem>(5, 1);
            for (int i = 0; i < itemsAdded.Count - 1; i++)             // All but that last item so we add at diff pos
            {
                sm.Add(itemsAdded[i], (1, 2));
            }

            sm.Add(lastItem, (2, 3));
            // We should have added all but the one that was layer 0
            itemsMoved = sm.Move(1, 2, 2, 3).ToList();
            Assert.AreEqual(3, itemsMoved.Count);

            itemsMoved = sm.Move(2, 3, 1, 2, ~sm.LayerMasker.Mask(2)).ToList();
            Assert.AreEqual(2, itemsMoved.Count);
            List <MySpatialMapItem> itemsLeft = sm.GetItems((2, 3)).ToList();

            Assert.AreEqual(2, itemsLeft.Count);
            bool found = false;

            foreach (var item in itemsLeft)
            {
                found = found || item == itemsAdded[2];
            }
            Assert.AreEqual(true, found);
        }
        public void ManualTestPrint()
        {
            var map = new LayeredSpatialMap <MySpatialMapItem>(3, 1, LayerMasker.DEFAULT.Mask(2));

            map.Add(new MySpatialMapItem(1), (1, 2));
            map.Add(new MySpatialMapItem(1), (3, 4));
            map.Add(new MySpatialMapItem(2), (1, 1));
            map.Add(new MySpatialMapItem(3), (0, 0));

            Console.WriteLine("SpatialMap: ");
            Console.WriteLine(map);

            Console.WriteLine("No need to test stringifier, as used by impl of regular ToString()");
        }
Esempio n. 3
0
        /// <summary>
        /// Adds the given entity (non-terrain object) to its recorded location, removing it from the map it is currently a part of.  Returns true if the
        /// entity was added, and false otherwise (eg., collision detection would not allow it, etc.)
        /// </summary>
        /// <param name="entity">Entity to add.</param>
        /// <returns>True if the entity was successfully added to the map, false otherwise.</returns>
        public bool AddEntity(IGameObject entity)
        {
            if (entity.CurrentMap == this)
            {
                return(false);
            }

            if (entity.Layer < 1)
            {
                return(false);
            }

            if (!entity.IsWalkable && (!LayerMasker.HasLayer(LayersBlockingWalkability, entity.Layer) || !WalkabilityView[entity.Position]))
            {
                return(false);
            }

            if (!entity.IsTransparent && !LayerMasker.HasLayer(LayersBlockingTransparency, entity.Layer))
            {
                return(false);
            }

            if (!_entities.Add(entity, entity.Position))
            {
                return(false);
            }

            entity.CurrentMap?.RemoveEntity(entity);
            entity.OnMapChanged(this);
            return(true);
        }
        public void TestAdd()
        {
            var  sm = new LayeredSpatialMap <MySpatialMapItem>(5);
            bool result;

            for (int i = 0; i < 5; i++)
            {
                var item = new MySpatialMapItem(i);
                result = sm.Add(item, (1, 2));
                Assert.AreEqual(true, result);
            }

            result = sm.Add(new MySpatialMapItem(5), (2, 3));             // Out of range layer
            Assert.AreEqual(false, result);

            sm     = new LayeredSpatialMap <MySpatialMapItem>(5, 1);
            result = sm.Add(new MySpatialMapItem(0), (5, 6));
            Assert.AreEqual(false, result);
        }
        public void TestRemove()
        {
            var itemsAdded = new List <MySpatialMapItem>();
            var sm         = new LayeredSpatialMap <MySpatialMapItem>(5);

            for (int i = 0; i < 5; i++)
            {
                var item = new MySpatialMapItem(i);
                itemsAdded.Add(item);
                sm.Add(item, (1, 2));
            }

            bool result;

            var nonAddedItem = new MySpatialMapItem(2);

            result = sm.Remove(nonAddedItem);
            Assert.AreEqual(false, result);

            foreach (var i in itemsAdded)
            {
                result = sm.Remove(i);
                Assert.AreEqual(true, result);
            }


            foreach (var i in itemsAdded)
            {
                sm.Add(i, (1, 2));
            }

            List <MySpatialMapItem> itemsRemoved = sm.Remove((5, 6)).ToList();

            Assert.AreEqual(0, itemsRemoved.Count);

            itemsRemoved = sm.Remove((1, 2)).ToList();
            Assert.AreEqual(itemsAdded.Count, itemsRemoved.Count);
        }
Esempio n. 6
0
 public void Add(Entity e, Coord pos)
 {
     entities.Add(e, pos);
 }