Esempio n. 1
0
        public bool PutTile(Tile tile, Vector3Int loc)
        {
            if (!IsValidLoc(loc))
            {
                return(false);
            }
            if (null == tile)
            {
                return(false);
            }

            if (tile.size.x < 1 || tile.size.y < 1)
            {
                Debug.LogWarningFormat("ERROR. Invalid tile size {0}, {1}", tile.size.x, tile.size.y);
                return(false);
            }

            Rect2Int tileBounds = DestTileBounds(tile, loc);

            if (IsBlocked(tileBounds, loc.z))
            {
                return(false);
            }

            var layer = layers[loc.z];

            // Store all tiles here for easy iteration.
            layer.tiles.Add(tile);

            tile.board    = new WeakReference <SomeGridBoard>(this);
            tile.position = new Vector2Int(loc.x, loc.y);

            // A tile might extend across several cells, fill them all
            for (int x = tileBounds.position.x; x < (tileBounds.position.x + tileBounds.size.x); x++)
            {
                for (int y = tileBounds.position.y; y < (tileBounds.position.y + tileBounds.size.y); y++)
                {
                    CellAt(new Vector3Int(x, y, loc.z)).tile = tile;
                }
            }
            layer.OnCellsBlocked(tileBounds);

            // Don't notify until tile's state is set (or wrong state will be encoded).
            if (!suspendEvents)
            {
                OnAddTile(tile);
            }

            return(true);
        }
Esempio n. 2
0
        public bool IsBlocked(Rect2Int bounds)
        {
            for (int x = bounds.position.x; x < (bounds.position.x + bounds.size.x); x++)
            {
                for (int y = bounds.position.y; y < (bounds.position.y + bounds.size.y); y++)
                {
                    if (IsCellBlocked(new Vector2Int(x, y)))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Esempio n. 3
0
        public virtual void RemoveTile(Tile tile, int layer)
        {
            if (null == tile)
            {
                return;
            }
            if (!tile.board.TryGetTarget(out SomeGridBoard owner))
            {
                return;
            }
            if (owner != this)
            {
                return;
            }

            if (!suspendEvents)
            {
                OnWillRemoveTile(tile);
            }

            Vector3Int loc        = new Vector3Int(tile.position.x, tile.position.y, layer);
            Rect2Int   tileBounds = DestTileBounds(tile, loc);

            tile.board = null;

            int depth = loc.z;

            for (int x = tileBounds.position.x; x < (tileBounds.position.x + tileBounds.size.x); x++)
            {
                for (int y = tileBounds.position.y; y < (tileBounds.position.y + tileBounds.size.y); y++)
                {
                    CellAt(new Vector3Int(x, y, depth)).tile = null;
                }
            }
            layers[depth].tiles.Remove(tile);
            layers[depth].OnCellsUnblocked(tileBounds);

            if (!suspendEvents)
            {
                OnRemoveTile(tile);
            }
        }
Esempio n. 4
0
        public void TestGridBoard_Test()
        {
            TestGridBoard gridBoard = new TestGridBoard();

            gridBoard.Resize(new Vector3Int(20, 20, 2));
            Assert.AreEqual(2, gridBoard.Depth);
            Assert.AreEqual(20, gridBoard.Width);
            Assert.AreEqual(20, gridBoard.Height);
            Assert.IsFalse(gridBoard.IsValidLoc(new Vector3Int(0, 0, 2)));
            Assert.IsFalse(gridBoard.IsValidLoc(new Vector3Int(0, 0, -1)));
            Assert.IsFalse(gridBoard.IsValidLoc(new Vector3Int(20, 20, 0)));
            Assert.IsFalse(gridBoard.IsValidLoc(new Vector3Int(-1, -1, 0)));
            Assert.IsTrue(gridBoard.IsValidLoc(new Vector3Int(19, 19, 1)));
            Assert.IsTrue(gridBoard.IsValidLoc(new Vector3Int(0, 0, 0)));

            GridCell <GridTile> gridCell = gridBoard.CellAt(new Vector3Int(0, 0, 0));

            Assert.AreEqual(null, gridCell.tile);
            Assert.AreEqual(null, gridBoard.TileAt(new Vector3Int(0, 0, 0)));

            int          deconstruct = 0;
            TestGridTile tile        = new TestGridTile(deconstruct);

            Assert.IsFalse(gridBoard.PutTile(tile, new Vector3Int(20, 20, 0)));
            Assert.IsFalse(gridBoard.PutTile(tile, new Vector3Int(0, 0, 2)));
            Assert.IsTrue(gridBoard.PutTile(tile, new Vector3Int(0, 0, 0)));

            gridCell = gridBoard.CellAt(new Vector3Int(0, 0, 0));
            Assert.AreEqual(tile, gridCell.tile);
            Assert.AreEqual(tile, gridBoard.TileAt(new Vector3Int(0, 0, 0)));
            Assert.AreEqual(tile, gridBoard.TileAt(new Vector3Int(0, 1, 0)));
            Assert.AreEqual(tile, gridBoard.TileAt(new Vector3Int(1, 0, 0)));
            Assert.AreEqual(tile, gridBoard.TileAt(new Vector3Int(1, 1, 0)));
            Assert.AreEqual(null, gridBoard.TileAt(new Vector3Int(0, 0, 1)));
            Assert.AreEqual(null, gridBoard.TileAt(new Vector3Int(2, 0, 0)));
            Assert.AreEqual(null, gridBoard.TileAt(new Vector3Int(0, 2, 0)));

            Assert.IsTrue(gridBoard.IsCellBlocked(new Vector3Int(-1, -1, 0)));
            Assert.IsTrue(gridBoard.IsCellBlocked(new Vector3Int(0, 0, 0)));
            Assert.IsTrue(gridBoard.IsCellBlocked(new Vector3Int(0, 1, 0)));
            Assert.IsTrue(gridBoard.IsCellBlocked(new Vector3Int(1, 0, 0)));
            Assert.IsTrue(gridBoard.IsCellBlocked(new Vector3Int(1, 1, 0)));
            Assert.IsFalse(gridBoard.IsCellBlocked(new Vector3Int(0, 0, 1)));
            Assert.IsFalse(gridBoard.IsCellBlocked(new Vector3Int(0, 2, 0)));
            Assert.IsFalse(gridBoard.IsCellBlocked(new Vector3Int(2, 0, 0)));

            Assert.IsTrue(gridBoard.IsBlocked(new Rect2Int(new Vector2Int(0, 0), new Vector2Int(1, 2)), 0));
            Assert.IsTrue(gridBoard.IsBlocked(new Rect2Int(new Vector2Int(1, 0), new Vector2Int(2, 1)), 0));
            Assert.IsTrue(gridBoard.IsBlocked(new Rect2Int(new Vector2Int(0, 1), new Vector2Int(1, 1)), 0));
            Assert.IsFalse(gridBoard.IsBlocked(new Rect2Int(new Vector2Int(2, 0), new Vector2Int(1, 1)), 0));
            Assert.IsFalse(gridBoard.IsBlocked(new Rect2Int(new Vector2Int(0, 2), new Vector2Int(1, 1)), 0));

            Rect2Int destTileBounds = gridBoard.DestTileBounds(tile, new Vector3Int(0, 0, 0));

            Assert.AreEqual(0, destTileBounds.position.x);
            Assert.AreEqual(0, destTileBounds.position.y);
            Assert.AreEqual(1, destTileBounds.position.x + destTileBounds.size.x - 1);
            Assert.AreEqual(1, destTileBounds.position.y + destTileBounds.size.y - 1);

            gridBoard.RemoveTile(tile, 0); tile = null;
            Assert.IsFalse(gridBoard.IsCellBlocked(new Vector3Int(0, 0, 0)));
            Assert.IsFalse(gridBoard.IsCellBlocked(new Vector3Int(0, 1, 0)));
            Assert.IsFalse(gridBoard.IsCellBlocked(new Vector3Int(1, 0, 0)));
            Assert.IsFalse(gridBoard.IsCellBlocked(new Vector3Int(1, 1, 0)));
            Assert.IsFalse(gridBoard.IsBlocked(new Rect2Int(new Vector2Int(0, 0), new Vector2Int(1, 1)), 0));
            Assert.IsFalse(gridBoard.IsBlocked(new Rect2Int(new Vector2Int(1, 0), new Vector2Int(2, 1)), 0));
            Assert.IsFalse(gridBoard.IsBlocked(new Rect2Int(new Vector2Int(0, 1), new Vector2Int(1, 2)), 0));
        }
Esempio n. 5
0
 public override void OnCellsUnblocked(Rect2Int blocked)
 {
     lastCellsUnblocked = blocked;
     base.OnCellsUnblocked(blocked);
 }
Esempio n. 6
0
 public virtual void OnCellsUnblocked(Rect2Int bounds)
 {
 }