Exemplo n.º 1
0
        public void PlaceTile(Vector3Int pos, Vector3 worldPos, GameObject tile, bool UpdateOnPlace = true, bool onlyUpdateNeighbours = false)
        {
            if (Application.isPlaying || !Application.isEditor)
            {
                return;
            }
            GameObject TestTile = PrefabUtility.InstantiatePrefab(tile, transform) as GameObject;

            if (!useTileMaterial)
            {
                Renderer r = TestTile.GetComponent <Renderer>();
                r.sharedMaterial = materials[selectedMaterial];
            }
            //Get the world position of the cell from it's grid coordinates make sure we place at the center of the cell

            //Add tile to tile dictionary with the key being the grid location of the cell it was placed on.
            tiles.Add(pos, new TileData(TestTile, pos.x, pos.y));
            Tile t = TestTile.GetComponent <Tile>();

            t.enabled            = false;
            t.tilemap            = this;
            t.transform.parent   = transform;
            t.transform.position = worldPos;
            t.gridPosition       = pos;
            t.enabled            = true;
        }
Exemplo n.º 2
0
 public void ResetMap()
 {
     TileDictionary = new TileDictionary();
     for (var x = 0; x < BoardWidth; x++)
     {
         for (var y = 0; y < BoardHeight; y++)
         {
             TileDictionary.Add(new Coordinate(x, y), new Tile());
         }
     }
 }
Exemplo n.º 3
0
        public TileInfo AddTile(Vector2Int position)
        {
            var tile = new TileInfo
            {
                Position = position
            };

            _tiles.Add(position, tile);

            return(tile);
        }
Exemplo n.º 4
0
        public Ship(Coordinate[] location, string name)
        {
            Name     = name;
            Location = location;
            Hull     = new TileDictionary();

            foreach (var coord in location)
            {
                Hull.Add(coord, new Tile {
                    OnHit = WhenHit, Status = TileStatus.Ship
                });
            }
        }
    public void PlaceTile(EditorGridPosition info, int tileIndex)
    {
        container = GameObject.Find(stageName).transform;
        if (container == null)
        {
            container = new GameObject(stageName).transform;
        }
        EditorTile editorTile = new GameObject(
            string.Format("[{0}, {1}] {2}", info.gridPosition.x, info.gridPosition.y, tileInfo[tileIndex].sprite.name)
            ).AddComponent <EditorTile>();

        editorTile.gameObject.AddComponent <SpriteRenderer>().sprite = tileInfo[tileIndex].sprite;
        editorTile.gridPosition   = info.gridPosition;
        editorTile.placementLayer = tileInfo[tileIndex].placementLayer;
        editorTile.numLayers      = tileInfo[tileIndex].numLayers;

        Vector3 offset = new Vector3();

        if (tileInfo[tileIndex].numLayers == 1)
        {
            offset.y = tileInfo[tileIndex].placementLayer == 1 ? scaledHeight * .25f : -scaledHeight * .25f;
        }
        info.offsetY += offset;
        editorTile.transform.position = info.worldPosition + offset;

        editorTile.transform.SetParent(container);
        editorTile.tileType      = tileInfo[tileIndex].tileType;
        editorTile.savedPosition = info.worldPosition;
        editorTile.scaleX        = editorTile.transform.localScale.x;
        editorTile.scaleY        = editorTile.transform.localScale.y;
        info.tile = editorTile;
        GridTile tile = editorTile.gameObject.AddComponent <GridTile>();

        tile.gridPosition = info.gridPosition;
        tile.tileType     = editorTile.tileType;

        if (tileInfo[tileIndex].numLayers == 1)
        {
            info.worldUnitHeight += tileInfo[tileIndex].placementLayer == 2 ? tileHeightPerLayer : tileHeightPerLayer * 2;
        }
        else
        {
            info.worldUnitHeight += tileHeightPerLayer * 2;
        }
        info.savedWorldUnitHeight = info.worldUnitHeight;
        tile.worldHeight          = info.worldUnitHeight;

        tileDictionary.Add(editorTile.gridPosition, tile);
        GetAdjacentTiles(info);
    }
    //takes a string (our tiles) and the x,y position it needs to be placed with the camera's position as reference to place 1 tile.
    //placeTile is used in generateMap()
    private void placeTile(string tileType, int x, int y, Vector3 camPos)
    {
        int         tileIndex = int.Parse(tileType);
        TileManager nextTile  = Instantiate(tiles[tileIndex]).GetComponent <TileManager>();

        TileDictionary.Add(new Point(x, y), nextTile);
        nextTile.Setup(new Point(x, y), new Vector3(camPos.x + (tileSize * x), camPos.y - (tileSize * y), 0), map);
        if (tileType == "0")
        {
            nextTile.ableToWalk = false;
        }
        if (tileType == "1")
        {
            nextTile.IsEmpty = false;
        }
    }
Exemplo n.º 7
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var dict = new TileDictionary();
            var cr   = new Coordinate();

            var started = true;

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                default:
                    break;

                case JsonToken.StartObject:
                    started = true;
                    break;

                case JsonToken.EndObject:
                    if (!started)
                    {
                        return(dict);
                    }
                    started = false;
                    break;

                case JsonToken.PropertyName:
                    var readerValue = reader.Value.ToString().Split(',');
                    if (readerValue.Length == 2)
                    {
                        cr = new Coordinate(int.Parse(readerValue[0]), int.Parse(readerValue[1]));
                    }
                    break;

                case JsonToken.Integer:
                    dict.Add(cr, new Tile {
                        Status = (TileStatus)Enum.Parse(typeof(TileStatus), reader.Value.ToString())
                    });
                    break;
                }
            }

            return(dict);
        }