Esempio n. 1
0
        /// <summary>
        /// Used for deploying object in Editor
        /// </summary>
        public void DeployIfPossible(Ray ray, Deployable deployableObject)
        {
            RaycastHit hitInfo;

            Physics.Raycast(ray, out hitInfo, 100, 1 << gridPlaneLayer);
            if (hitInfo.collider)
            {
                Vector3    loc   = hitInfo.point - planeBottomLeftPosition;
                IntVector2 index = new IntVector2(loc.x * Columns / boundX, loc.y * Rows / boundY);

                if (IsPlaceableWithOffset(deployableObject, index))
                {
                    Vector3 pos     = IndexToWorldPosition(index);
                    Vector2 wOffset = deployableObject.TileMap.GetWorldTransformOffset(GlobalCellWidth);
                    pos.x += wOffset.x;
                    pos.y += wOffset.y;

                    Deployable newCell = (Deployable)Instantiate(deployableObject, pos, Quaternion.identity);
                    newCell.transform.parent = deployableParentDictionary [deployableObject.GetLayer()];
                    MapGridCell[] cells = deployableCellDictionary [deployableObject.GetLayer()];
                    newCell.ParentMapGridCell = cells[CalculateIndex(index)];
                    newCell.gameObject.layer  = (int)deployableObject.GetLayer();
                    newCell.GridIndex         = index;

                    UpdateTilesWithOffset(newCell, index, false);
                }
            }
        }
Esempio n. 2
0
        void OnDeployableButtonClick(Deployable deployable, Button button)
        {
            // check whether layer is active
            Transform layerTransform = GameMapGrid.deployableParentDictionary [deployable.GetLayer()];

            if (!layerTransform.gameObject.activeSelf)
            {
                return;
            }

            // when clicking self
            if (button == lastDeployableButton)
            {
                return;
            }

            objectToDeploy      = deployable;
            currentLayerToErase = deployable.GetLayer();

            if (lastDeployableButton)
            {
                lastDeployableButton.image.color = button.image.color;
            }
            lastDeployableButtonColor = button.image.color;
            button.image.color        = new Color(0.5f, 0.5f, 0.5f, button.image.color.a);
            lastDeployableButton      = button;
        }
Esempio n. 3
0
 public void OnGameObjectLayerToggle(bool isActive)
 {
     GameMapGrid.GameObjectTransform.gameObject.SetActive(isActive);
     if (objectToDeploy && GameMapGrid.deployableParentDictionary [objectToDeploy.GetLayer()] == GameMapGrid.GameObjectTransform)
     {
         ResetObjectToDeploy();
     }
 }
Esempio n. 4
0
        public void UpdateTilesWithOffset(Deployable deployableObject, IntVector2 cellIndex, bool isEmpty)
        {
            for (int i = 0; i < deployableObject.TileMap.TileSize.X; i++)
            {
                for (int j = 0; j < deployableObject.TileMap.TileSize.Y; j++)
                {
                    int posX = cellIndex.X + i - deployableObject.TileMap.TileOffset.X;
                    int posY = cellIndex.Y - j + deployableObject.TileMap.TileOffset.Y;

                    if (posX < Columns && posX >= 0 && posY < Rows && posY >= 0)
                    {
                        int           index = CalculateIndex(posX, posY);
                        MapGridCell[] cells = deployableCellDictionary [deployableObject.GetLayer()];
                        cells [index].IsEmpty = isEmpty;
                        if (isEmpty)
                        {
                            cells [index].InCellObject = null;
                        }
                        else
                        {
                            cells [index].InCellObject = deployableObject;
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 检查物体能否放入该格子
        /// </summary>
        public bool IsPlaceableWithOffset(Deployable deployableObject, IntVector2 cellIndex)
        {
            TileMap tile = deployableObject.TileMap;

            for (int i = 0; i < tile.TileSize.X; i++)
            {
                for (int j = 0; j < tile.TileSize.Y; j++)
                {
                    int posX = cellIndex.X + i - tile.TileOffset.X;
                    int posY = cellIndex.Y - j + tile.TileOffset.Y;
                    if (posX < Columns && posX >= 0 && posY < Rows && posY >= 0)
                    {
                        int           index = CalculateIndex(posX, posY);
                        MapGridCell[] cells = deployableCellDictionary [deployableObject.GetLayer()];
                        if (!cells [index].IsEmpty)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Esempio n. 6
0
        /// <summary>
        /// Used for Loading map file
        /// </summary>
        public Deployable DeployIfPossible(IntVector2 index, Deployable deployableObject)
        {
            if (IsPlaceableWithOffset(deployableObject, index))
            {
                Vector3 pos     = IndexToWorldPosition(index);
                Vector2 wOffset = deployableObject.TileMap.GetWorldTransformOffset(GlobalCellWidth);
                pos.x += wOffset.x;
                pos.y += wOffset.y;

                Deployable newCell = (Deployable)Instantiate(deployableObject, pos, Quaternion.identity);
                newCell.transform.parent = deployableParentDictionary [deployableObject.GetLayer()];
                MapGridCell[] cells = deployableCellDictionary [deployableObject.GetLayer()];
                newCell.ParentMapGridCell = cells[CalculateIndex(index)];
                newCell.gameObject.layer  = (int)deployableObject.GetLayer();
                newCell.GridIndex         = index;

                UpdateTilesWithOffset(newCell, index, false);

                return(newCell);
            }

            return(null);
        }