Beispiel #1
0
        public static List <gridTile> GetTileNeighbours(gridTile _ThisTile)
        {
            List <gridTile> Neighbours = new List <gridTile>();

            gridTile N = new gridTile();

            Vector2[] Positions = new Vector2[]
            {
                new Vector2(_ThisTile.gPosition.x - 1, _ThisTile.gPosition.y),
                new Vector2(_ThisTile.gPosition.x + 1, _ThisTile.gPosition.y),
                new Vector2(_ThisTile.gPosition.x, _ThisTile.gPosition.y - 1),
                new Vector2(_ThisTile.gPosition.x, _ThisTile.gPosition.y + 1)
            };


            for (int i = 0; i < Positions.Length; i++)
            {
                N = GetGridTileFromPosition(Positions[i]);
                if (N.gContents != mapGrid.Contents.none)
                {
                    Neighbours.Add(N);
                }
            }

            return(Neighbours);
        }
Beispiel #2
0
 public bool CheckTilesAreValid(gridTile[] GridTiles)
 {
     foreach (gridTile G in GridTiles)
     {
         gridTile ThisTile = mapData.GetGridTileFromPosition(G.gPosition);
         Debug.Log("This tile contains " + ThisTile.gContents);
         if (ThisTile.gContents != mapGrid.Contents.none || G.gPosition.x < 0 || G.gPosition.y < 0 || G.gPosition.x > Grid.gridSize.x || G.gPosition.y > Grid.gridSize.y)
         {
             return(false);
         }
     }
     return(true);
 }
Beispiel #3
0
        public static int AddGridTile(gridTile InputPiece)
        {
            if (InputPiece.gContents == mapGrid.Contents.none)
            {
                gridData.Remove(InputPiece);
            }
            else
            {
                gridData.Add(InputPiece);
            }

            return(gridData.Count);
        }
Beispiel #4
0
        public static gridTile GetGridTileFromPosition(Vector2 _GridPosition)
        {
            gridTile ReturnTile = new gridTile();

            for (int i = 0; i < gridData.Count; i++)
            {
                if (gridData[i].gPosition == _GridPosition)
                {
                    ReturnTile = gridData[i];
                }
            }

            return(ReturnTile);
        }
Beispiel #5
0
        private void ReleaseHandler(object sender, EventArgs e)
        {
            var     gesture = sender as ReleaseGesture;
            HitData hit     = gesture.GetScreenPositionHitData();

            _GridReleasePoint  = Grid.transform.InverseTransformPoint(hit.Point);
            _GridReleasePoint += new Vector3(_GridSpacing * 0.5f, _GridSpacing * 0.5f, 0);
            _GridReleasePoint  = new Vector3(
                Mathf.Floor(_GridPressPoint.x * _GridSpacing),
                Mathf.Floor(_GridPressPoint.y * _GridSpacing),
                0);

            if (_GridReleasePoint.x < 0 || _GridReleasePoint.y < 0 || _GridReleasePoint.x >= Grid.gridSize.x || _GridReleasePoint.y >= Grid.gridSize.y)
            {
                return;
            }

            if (MoveDelta < MoveTolerance) //_GridReleasePoint == _GridPressPoint
            {
                _SelectedPiece = mapData.GetGridTileFromPosition(new Vector2(_GridReleasePoint.x, _GridReleasePoint.y));
                if (_SelectedPiece != null)
                {
                    GridContents = _SelectedPiece.gContents.ToString();
                }
                else
                {
                    GridContents = "Nothing";
                }

                Instantiate(SpawnVFX, hit.Point, Quaternion.identity);

                gridTile NewTile = new gridTile
                {
                    gContents = ActiveProp,
                    gPosition = (new Vector2(_GridPressPoint.x, _GridPressPoint.y))
                };
                AddNewTile(NewTile);
                //Debug.Log("Valid click release on grid at: " + _GridReleasePoint + " and contains " + _GridContents);
            }
        }
Beispiel #6
0
        public static List <mapGrid.Contents> GetContentsNeighbours(gridTile _ThisTile)
        {
            List <mapGrid.Contents> Contents = new List <mapGrid.Contents>();

            gridTile N = new gridTile();

            Vector2[] Positions = new Vector2[]
            {
                new Vector2(_ThisTile.gPosition.x - 1, _ThisTile.gPosition.y),
                new Vector2(_ThisTile.gPosition.x + 1, _ThisTile.gPosition.y),
                new Vector2(_ThisTile.gPosition.x, _ThisTile.gPosition.y - 1),
                new Vector2(_ThisTile.gPosition.x, _ThisTile.gPosition.y + 1)
            };

            for (int i = 0; i < Positions.Length; i++)
            {
                N = GetGridTileFromPosition(Positions[i]);
                Contents.Add(N.gContents);
            }

            return(Contents);
        }
Beispiel #7
0
        ///load data from file, returns true if map data found
        public static bool ReadMapData()
        {
            //clear previous data
            gridData.Clear();

            StreamReader reader = new StreamReader(path + mapName + ".json", true);

            if (reader != null)
            {
                while (reader.Peek() >= 0)
                {
                    //get info from json file

                    string line = reader.ReadLine();
                    //Debug.Log("Line:" + line);
                    mapLoadData InputData = mapLoadData.CreateFromJSON(line);
                    //Debug.Log("Read data from file:" + InputData.Content);

                    //add to new gridPiece
                    gridTile InputPiece = new gridTile();
                    InputPiece.gPosition.x = InputData.PositionX;
                    InputPiece.gPosition.y = InputData.PositionY;
                    InputPiece.gSize.x     = InputData.SizeX;
                    InputPiece.gSize.y     = InputData.SizeY;

                    InputPiece.gContents = (mapGrid.Contents)InputData.Content;
                    gridData.Add(InputPiece);
                }
                reader.Close();
                return(true);
            }
            else
            {
                return(false);
            }
        }
        void Start()
        {
            _ThisTile = mapData.GetGridTileFromPosition(propPosition);

            UpdateNeighbours();
        }
Beispiel #9
0
 /// <summary>
 /// Removes the specified tile from the map data and scene
 /// </summary>
 /// <param name="PieceToRemove"></param>
 public void RemoveTile(gridTile PieceToRemove)
 {
     Destroy(PieceToRemove.gObject);
     mapData.RemoveGridTile(PieceToRemove);
 }
Beispiel #10
0
        /// <summary>
        /// Add a new tile to the map data and scene
        /// </summary>
        /// <param name="_NewGridTile"></param>
        public void AddNewTile(gridTile _NewGridTile)
        {
            if (_NewGridTile == null)
            {
                return;
            }

            //Get contents of existing tile
            gridTile ExistingTile = mapData.GetGridTileFromPosition(_NewGridTile.gPosition);

            //If trying to clear an empty tile
            if (_NewGridTile.gContents == mapGrid.Contents.none && ExistingTile.gContents == mapGrid.Contents.none)
            {
                return;
            }

            //Tool is set to clear tile and tile has contents
            if (_NewGridTile.gContents == mapGrid.Contents.none)
            {
                if (ExistingTile.gContents != mapGrid.Contents.none && ExistingTile.gContents != mapGrid.Contents.reserved)
                {
                    //Create list of tiles to remove
                    List <gridTile> TilesToRemove = new List <gridTile>();

                    Debug.Log("Size of this object = " + ExistingTile.gSize);
                    //If our object is bigger than 1x1
                    if (ExistingTile.gSize.x > 1 || ExistingTile.gSize.y > 1)
                    {
                        Debug.Log("removing large object");
                        for (int x = 0; x < ExistingTile.gSize.x; x++)
                        {
                            for (int y = 0; y < ExistingTile.gSize.y; y++)
                            {
                                gridTile ThisTile = mapData.GetGridTileFromPosition(new Vector2(ExistingTile.gPosition.x + x, ExistingTile.gPosition.y + y));
                                Debug.Log("Adding tile top remove: " + ThisTile.gContents);
                                TilesToRemove.Add(ThisTile);
                            }
                        }
                    }
                    else
                    {
                        TilesToRemove.Add(ExistingTile);
                    }

                    //Remove each tile in this list
                    foreach (gridTile T in TilesToRemove)
                    {
                        RemoveTile(T);
                    }
                    return;
                }
                return;
            }

            GameObject ObjectToPlace   = Grid.PlaceableObjects[(int)_NewGridTile.gContents - 1];
            int        xTiles          = (int)ObjectToPlace.GetComponent <mapPropInfo>().propSize.x;
            int        yTiles          = (int)ObjectToPlace.GetComponent <mapPropInfo>().propSize.y;
            int        numTilesToCheck = xTiles * yTiles;

            gridTile[] tilesToCheck = new gridTile[numTilesToCheck];

            int i = 0;

            for (int x = 0; x < xTiles; x++)
            {
                for (int y = 0; y < yTiles; y++)
                {
                    tilesToCheck[i]           = new gridTile();
                    tilesToCheck[i].gPosition = new Vector2(_NewGridTile.gPosition.x + x, _NewGridTile.gPosition.y + y);
                    i++;
                }
            }

            if (!CheckTilesAreValid(tilesToCheck))
            {
                Debug.Log("Invalid tile for this prop.");
                return;
            }

            //same as existing piece
            if (ExistingTile.gContents == _NewGridTile.gContents)
            {
                return;
            }

            //tool is set to different tile than existing tile
            if (ExistingTile.gContents != _NewGridTile.gContents)
            {
                RemoveTile(ExistingTile);
            }

            //if tool is not set to none, place the new tile
            if (_NewGridTile.gContents != mapGrid.Contents.none)
            {
                //instatiate prefab
                GameObject NewObject = null;

                NewObject = Instantiate(Grid.PlaceableObjects[(int)_NewGridTile.gContents - 1], new Vector3(_GridSpacing * _NewGridTile.gPosition.x, _GridSpacing * _NewGridTile.gPosition.y, 0), Quaternion.identity) as GameObject;

                //set properties of object
                if (NewObject != null)
                {
                    //Store size of our new prop in the tile data
                    _NewGridTile.gSize = NewObject.GetComponent <mapPropInfo>().propSize;

                    //Get our unique id
                    NewObject.GetComponent <mapPropInfo>().propID = mapData.AddGridTile(_NewGridTile);

                    //Store our position
                    NewObject.GetComponent <mapPropInfo>().propPosition = _NewGridTile.gPosition;

                    //Add the object to the map data
                    mapData.gridData[mapData.gridData.Count - 1].gObject = NewObject;

                    //If prop size x,y is greater than 1, set other tiles to reserved
                    if (xTiles > 1 || yTiles > 1)
                    {
                        for (int x = 0; x < xTiles; x++)
                        {
                            for (int y = 0; y < yTiles; y++)
                            {
                                if (x == 0 && y == 0)
                                {
                                    //Debug.Log("Row = " + x + " Column = " + y + " < Skipping");
                                }
                                else
                                {
                                    //Debug.Log("Row = " + x + " Column = " + y);
                                    //Create a tile for this reserved position
                                    gridTile ReservedTile = new gridTile();

                                    //Set contents to reserved
                                    ReservedTile.gContents = mapGrid.Contents.reserved;

                                    //Set position
                                    ReservedTile.gPosition = new Vector2(_NewGridTile.gPosition.x + x, _NewGridTile.gPosition.y + y);

                                    //Add tile to map data
                                    int NewPropID = mapData.AddGridTile(ReservedTile);
                                    Debug.Log("New prop id = " + NewPropID);
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #11
0
 public static void RemoveGridTile(gridTile InputPiece)
 {
     gridData.Remove(InputPiece);
 }
Beispiel #12
0
        /// <summary>
        /// Remove an idividual tile from the map data
        /// </summary>
        /// <param name="TargetTile"></param>
        /// <returns></returns>
        public static GameObject GetGridObject(gridTile TargetTile)
        {
            GameObject GridObject = TargetTile.gObject;

            return(GridObject);
        }