예제 #1
0
        public void Add(Placeable p, bool dontRefreshCoordinates = false)
        {
            if (!dontRefreshCoordinates)
            {
                if (p.IsMapPlaced)
                {
                    Debug.LogError("Map Already Placed " + p.name);
                    return;
                }
                p.RefreshCoordinates();
            }
            p.Tag = 0;

            #region Coords Prep CopyPaste
            var pos = p.PlacedPosition - mapOffset;
            pos.Scale(CellSize2dInv);
            var pos2 = new Vector2(p.Size.x * CellSize2dInv.x + pos.x, p.Size.y * CellSize2dInv.y + pos.y);

            var posFl  = Vector2Int.FloorToInt(pos);
            var pos2Cl = Vector2Int.CeilToInt(pos2);
            if (posFl.x == pos2Cl.x)
            {
                pos2Cl.x += 1;
            }
            if (posFl.y == pos2Cl.y)
            {
                pos2Cl.y += 1;
            }
            posFl  = Vector2Int.Max(Vector2Int.zero, posFl);
            pos2Cl = Vector2Int.Min(mapSize, pos2Cl);

            int cellPosY = posFl.y * sizex;
            #endregion
            for (int y = posFl.y; y < pos2Cl.y; y++, cellPosY += sizex)
            {
                for (int x = posFl.x; x < pos2Cl.x; x++)
                {
                    cells[cellPosY + x].Add(p, ksids);
                }
            }
        }
예제 #2
0
        public void Move(Placeable p)
        {
            if (!p.IsMapPlaced)
            {
                throw new InvalidOperationException("Nemuzu pohybovat neco co neni v mape " + p.name);
            }

            var blockingOld       = p.CellBlocking;
            var placedPositionOld = p.PlacedPosition;
            var posOld            = p.PlacedPosition - mapOffset;

            posOld.Scale(CellSize2dInv);
            var pos2Old = new Vector2(p.Size.x * CellSize2dInv.x + posOld.x, p.Size.y * CellSize2dInv.y + posOld.y);

            p.RefreshCoordinates();

            if (blockingOld != p.CellBlocking)
            {
                Move2(p, posOld, pos2Old);
                return;
            }

            var posNew = p.PlacedPosition - mapOffset;

            posNew.Scale(CellSize2dInv);
            var pos2New = new Vector2(p.Size.x * CellSize2dInv.x + posNew.x, p.Size.y * CellSize2dInv.y + posNew.y);

            if (posOld == posNew && pos2Old == pos2New)
            {
                p.PlacedPosition = placedPositionOld;
                return;
            }

            LeavingCheck(p, posOld, pos2Old);

            var posFlOld  = Vector2Int.FloorToInt(posOld);
            var pos2ClOld = Vector2Int.CeilToInt(pos2Old);

            if (posFlOld.x == pos2ClOld.x)
            {
                pos2ClOld.x += 1;
            }
            if (posFlOld.y == pos2ClOld.y)
            {
                pos2ClOld.y += 1;
            }
            posFlOld  = Vector2Int.Max(Vector2Int.zero, posFlOld);
            pos2ClOld = Vector2Int.Min(mapSize, pos2ClOld);

            var posFlNew  = Vector2Int.FloorToInt(posNew);
            var pos2ClNew = Vector2Int.CeilToInt(pos2New);

            if (posFlNew.x == pos2ClNew.x)
            {
                pos2ClNew.x += 1;
            }
            if (posFlNew.y == pos2ClNew.y)
            {
                pos2ClNew.y += 1;
            }
            posFlNew  = Vector2Int.Max(Vector2Int.zero, posFlNew);
            pos2ClNew = Vector2Int.Min(mapSize, pos2ClNew);

            var posFlX  = Vector2Int.Max(posFlOld, posFlNew);
            var pos2ClX = Vector2Int.Min(pos2ClOld, pos2ClNew);

            int cellPosY = posFlOld.y * sizex;

            for (int y = posFlOld.y; y < pos2ClOld.y; y++, cellPosY += sizex)
            {
                if (y >= posFlX.y && y < pos2ClX.y)
                {
                    for (int x = posFlOld.x; x < pos2ClOld.x; x++)
                    {
                        if (x < posFlX.x || x >= pos2ClX.x)
                        {
                            cells[cellPosY + x].Remove(p, ksids);
                        }
                    }
                }
                else
                {
                    for (int x = posFlOld.x; x < pos2ClOld.x; x++)
                    {
                        cells[cellPosY + x].Remove(p, ksids);
                    }
                }
            }

            cellPosY = posFlNew.y * sizex;
            for (int y = posFlNew.y; y < pos2ClNew.y; y++, cellPosY += sizex)
            {
                if (y >= posFlX.y && y < pos2ClX.y)
                {
                    for (int x = posFlNew.x; x < pos2ClNew.x; x++)
                    {
                        if (x < posFlX.x || x >= pos2ClX.x)
                        {
                            cells[cellPosY + x].Add(p, ksids);
                        }
                    }
                }
                else
                {
                    for (int x = posFlNew.x; x < pos2ClNew.x; x++)
                    {
                        cells[cellPosY + x].Add(p, ksids);
                    }
                }
            }
        }