Пример #1
0
        public void DeployIfPossible(Ray ray, Deployable deployableObject)
        {
            RaycastHit hitInfo;

            Physics.Raycast(ray, out hitInfo, 100, 1 << 10);
            if (hitInfo.collider)
            {
                Vector3 loc   = hitInfo.point - _planeBottomLeftPosition;
                var     index = new IntVector2(loc.x * Columns / _boundX, loc.y * Rows / _boundY);

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

                    var newCell = (Deployable)Instantiate(deployableObject, pos, Quaternion.identity);
                    newCell.transform.parent      = ChildTransform;
                    newCell.gameObject.layer      = 9;
                    newCell.ParentAdvanceGridCell = Cells[CalculateIndex(index)];
                    newCell.GridIndex             = index;

                    UpdateTilesStateWithOffset(newCell, index, CellState.Full);
                }
            }
        }
Пример #2
0
        public bool DropDeployableIfPossible(Ray ray, Deployable deployableObject, IntVector2 deltaOffset)
        {
            RaycastHit hitInfo;

            Physics.Raycast(ray, out hitInfo, 100, 1 << 10);
            if (hitInfo.collider)
            {
                Vector3 loc   = hitInfo.point - _planeBottomLeftPosition;
                var     index = new IntVector2(loc.x * Columns / _boundX, loc.y * Rows / _boundY);
                index = index - deltaOffset;

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

                    deployableObject.transform.position    = pos;
                    deployableObject.ParentAdvanceGridCell = Cells[CalculateIndex(index)];
                    deployableObject.GridIndex             = index;

                    UpdateTilesStateWithOffset(deployableObject, index, CellState.Full);
                    return(true);
                }
            }

            return(false);
        }
Пример #3
0
        public void UpdateTilesStateWithOffset(Deployable deployableObject, IntVector2 cellIndex, CellState newState)
        {
            for (int i = 0; i < deployableObject.TileMap.TileSize.X; i++)
            {
                for (int j = 0; j < deployableObject.TileMap.TileSize.Y; j++)
                {
                    int posX = i + cellIndex.X - deployableObject.TileMap.TileOffset.X;
                    int posY = -j + cellIndex.Y + deployableObject.TileMap.TileOffset.Y;

                    if (posX < Rows && 0 <= posX && posY < Columns && 0 <= posY)
                    {
                        int index = CalculateIndex(posX, posY);

                        Cells[index].IsEmpty = newState.ToBool();

                        if (newState == CellState.Empty)
                        {
                            Cells[index].InCellObject = null;
                        }
                        else if (newState == CellState.Full)
                        {
                            Cells[index].InCellObject = deployableObject;
                        }
                    }
                }
            }
        }
Пример #4
0
        public void DrawGhostTiles(Ray ray, Deployable deployableObject, IntVector2 deltaOffset)
        {
            RaycastHit hitInfo;

            Physics.Raycast(ray, out hitInfo, 100, 1 << 10);
            if (hitInfo.collider)
            {
                Vector3 loc   = hitInfo.point - _planeBottomLeftPosition;
                var     index = new IntVector2(loc.x * Columns / _boundX, loc.y * Rows / _boundY);

                if (_lastIndex != index)
                {
                    IntVector2 ghostIndex = index - deltaOffset - new IntVector2(0, deployableObject.TileMap.TileSize.Y - 1) - new IntVector2(deployableObject.TileMap.TileOffset.X, -deployableObject.TileMap.TileOffset.Y);

                    Vector3 firstPoint  = IndexToWorldPositionWithNoOffset(ghostIndex);
                    Vector3 secondPoint = firstPoint + new Vector3(deployableObject.TileMap.TileSize.X, deployableObject.TileMap.TileSize.Y, 0);

                    GridLinesMaterial.SetColor("_CellMaskColour", IsPlaceableWithOffset(deployableObject.TileMap, index, deltaOffset) ? new Color(0, 1.0f, 0, 0.4f) : new Color(1.0f, 0, 0, 0.4f));
                    GridLinesMaterial.SetVector("_StartPosition", firstPoint);
                    GridLinesMaterial.SetVector("_EndPosition", secondPoint);

                    _lastIndex = index;
                }
            }
        }
Пример #5
0
        private void EditUpdate()
        {
            if (Input.GetMouseButtonDown(0))
            {
                _isDown = true;
                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitInfo;
                Physics.Raycast(ray, out hitInfo, 100, 1 << 9);
                if (hitInfo.collider)
                {
                    if (_selectedDeployable)
                    {
                        _selectedDeployable.AllowToDrawGUI = false;
                    }

                    _selectedDeployable = hitInfo.collider.gameObject.GetComponent <Deployable>();

                    AllowToMove = true;

                    _deltaIndexOffset = GameGrid.PointOnPlaneToIndex(hitInfo.point) - _selectedDeployable.GridIndex;

                    GameGrid.UpdateTilesStateWithOffset(_selectedDeployable, _selectedDeployable.GridIndex, CellState.Empty);
                    _selectedObjectDeltaPosition   = _selectedDeployable.transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    _selectedObjectDeltaPosition.z = 0;

                    _selectedDeployable.AllowToDrawGUI = true;
                }
                else
                {
                    AllowToMove = false;
                }
            }

            if (AllowToMove)
            {
                Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                pos.z = _selectedDeployable.transform.position.z;
                _selectedDeployable.transform.position = pos + _selectedObjectDeltaPosition;

                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                GameGrid.DrawGhostTiles(ray, _selectedDeployable, _deltaIndexOffset);
            }

            if (Input.GetMouseButtonUp(0))
            {
                if (AllowToMove)
                {
                    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                    if (!GameGrid.DropDeployableIfPossible(ray, _selectedDeployable, _deltaIndexOffset))
                    {
                        ResetSelectedObjectPosition();
                    }
                }

                _isDown           = false;
                AllowToMove       = false;
                _deltaIndexOffset = null;
            }
        }
Пример #6
0
        private void DrawDeployables()
        {
            Event e = Event.current;

            for (int i = 0, n = DeployableList.Count; i < n; i++)
            {
                var buttonRect = new Rect(180, 110 + i * 60, 150, 50);

                if (e.isMouse && buttonRect.Contains(e.mousePosition))
                {
                    if (e.type == EventType.mouseDown)
                    {
                        _onGui          = true;
                        _objectToDeploy = DeployableList[i];

                        if (_objectToDeploy.DeploymentMethod == DeploymentMethod.Drag)
                        {
                            GameGrid.IsDrawGhostTilesEnable = true;
                            _isDown = true;
                        }
                    }
                    else
                    {
                        _onGui = false;
                    }
                }

                GUI.Button(buttonRect, DeployableList[i].GetDisplayName());
            }
        }
Пример #7
0
        public Deployable[] GetAllChildren()
        {
            var result = new Deployable[ChildTransform.childCount];

            for (int i = 0, n = ChildTransform.childCount; i < n; i++)
            {
                result[i] = ChildTransform.GetChild(i).gameObject.GetComponent <Deployable>();
            }

            return(result);
        }
Пример #8
0
        public void EraseTiles(Ray ray)
        {
            RaycastHit hitInfo;

            Physics.Raycast(ray, out hitInfo, 100, 1 << 10);
            if (hitInfo.collider)
            {
                Vector3 loc   = hitInfo.point - _planeBottomLeftPosition;
                var     index = new IntVector2(loc.x * Columns / _boundX, loc.y * Rows / _boundY);

                Deployable toDeleteTile = Cells[CalculateIndex(index)].InCellObject;
                if (toDeleteTile)
                {
                    IntVector2 gIdx = toDeleteTile.GridIndex;
                    UpdateTilesStateWithOffset(toDeleteTile, gIdx, CellState.Empty);
                    Destroy(toDeleteTile.gameObject);
                }
            }
        }
Пример #9
0
        public Deployable DeployIfPossible(IntVector2 index, Deployable deployableObject)
        {
            if (IsPlaceableWithOffset(deployableObject.TileMap, index, new IntVector2()))
            {
                Vector3 pos     = IndexToWorldPosition(index);
                Vector2 wOffset = deployableObject.TileMap.GetWorldTransformOffset(GlobalCellWidth);
                pos.x += wOffset.x;
                pos.y += wOffset.y;

                var newCell = (Deployable)Instantiate(deployableObject, pos, Quaternion.identity);
                newCell.transform.parent      = ChildTransform;
                newCell.gameObject.layer      = 9;
                newCell.ParentAdvanceGridCell = Cells[CalculateIndex(index)];
                newCell.GridIndex             = index;

                UpdateTilesStateWithOffset(newCell, index, CellState.Full);
                return(newCell);
            }

            return(null);
        }
Пример #10
0
        public static void LoadDataFromXML(this AdvanceGrid gameGrid, string mapName, Dictionary <string, Deployable> deployableDictionary)
        {
            var reader = new XmlDocument();

            reader.Load(Application.persistentDataPath + "/" + mapName + ".dm");

            if (reader.DocumentElement != null)
            {
                int row    = Convert.ToInt32(reader.DocumentElement.Attributes["Row"].InnerText);
                int column = Convert.ToInt32(reader.DocumentElement.Attributes["Column"].InnerText);

                if (gameGrid.Rows == row && gameGrid.Columns == column)
                {
                    foreach (XmlNode node in reader.DocumentElement.ChildNodes)
                    {
                        if (node.Attributes != null)
                        {
                            var    index      = new IntVector2(node.Attributes["GridIndex"].InnerText);
                            string objectName = node.Attributes["Name"].InnerText;


                            Deployable newTile = gameGrid.DeployIfPossible(index, deployableDictionary[objectName]);
                            if (newTile)
                            {
                                //Applying Properties!
                                XmlNode proeprties = node.ChildNodes[0];
                                if (proeprties != null)
                                {
                                    foreach (XmlNode proeprty in proeprties.ChildNodes)
                                    {
                                        if (proeprty.Attributes != null)
                                        {
                                            string typeOfProperty = proeprty.Attributes["Type"].InnerText;
                                            Type   t = null;
                                            switch (typeOfProperty)
                                            {
                                            case "bool":
                                                t = typeof(bool);
                                                break;

                                            case "string":
                                                t = typeof(string);
                                                break;

                                            case "int":
                                                t = typeof(int);
                                                break;

                                            case "float":
                                                t = typeof(float);
                                                break;
                                            }
                                            if (t != null)
                                            {
                                                newTile.SetProperty(t, proeprty.Attributes["Name"].InnerText, proeprty.Attributes["Value"].InnerText);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    UpdateGridSize(gameGrid);
                }
            }
        }