예제 #1
0
    void PrepareDataToSave(string path)
    {
        _levelToSave = new SerializedMap();

        _levelToSave.Path     = path;
        _levelToSave.MapSizeX = MapSizeX;
        _levelToSave.MapSizeY = MapSizeY;

        _levelToSave.MapTiles.Clear();

        foreach (Transform t in MapHolder)
        {
            TileBase to = t.GetComponent <TileBase>();

            SerializedTile st = new SerializedTile();

            var layer1 = FillSerializedTileData(to.TileObjectLayer1);
            st.TileLayer1 = layer1;

            if (to.TileObjectLayer2 != null)
            {
                var layer2 = FillSerializedTileData(to.TileObjectLayer2);
                st.TileLayer2 = layer2;
            }

            _levelToSave.MapTiles.Add(st);
        }
    }
예제 #2
0
    public void DeleteGameObjectFromScene(SerializedTile currTile, Map currentMap)
    {
        Debug.LogWarning("Deleting GO From Scene");
        GameObject tileToDelete = GameObject.Find(NetworkPlayerMapUpdater.GetNameFromTileUID(currTile));

        currentMap.RemoveTile(currTile);
        GameObject.Destroy(tileToDelete);
    }
예제 #3
0
    private void Start()
    {
        //Ensure obj has collider
        if (!gameObject.GetComponent <Collider>())
        {
            gameObject.AddComponent <BoxCollider>();
        }

        tile = new SerializedTile(gameObject.name, gameObject.GetHashCode());
    }
예제 #4
0
 private void Awake()
 {
     gm          = GameObject.FindGameObjectWithTag("Game Manager").GetComponent <GameManager>();
     child_tiles = new List <GameObject>();
     all_tiles   = new List <SerializedTile>();
     base_tile   = new SerializedTile();
     all_tiles.Add(base_tile);
     next_move_wait  = next_move_wait_max;
     next_move_ready = true;
 }
예제 #5
0
    private void CompareMaps()
    {
        //omg so clunky

        //Determine if new GameObject required:
        for (int i = 0; i < newMap.tiles.Count; i++)
        {
            bool contains = false;

            foreach (SerializedTile currTile in currentMap.tiles)
            {
                if (CompareTiles(currTile, newMap.tiles[i]))
                {
                    //Determine if location must be updated:
                    UpdateTileLocation(currTile, newMap.tiles[i]);
                    contains = true;
                    break;
                }
            }

            if (contains)
            {
                continue;           // Do nothing if correct tile exists
            }
            else
            {
                mapHandler.CreateNewGameobjectFromTile(newMap.tiles[i], currentMap);
            }
        }

        //Determine if old GameObject must be removed:
        for (int i = currentMap.tiles.Count - 1; i >= 0; i--)
        {
            SerializedTile currTile = currentMap.tiles[i];
            bool           contains = false;
            foreach (SerializedTile newTile in newMap.tiles)
            {
                if (CompareTiles(currTile, newTile))
                {
                    contains = true;
                    break;
                }
            }

            if (contains)
            {
                continue;          // Do nothing if tile is supposed to be here
            }
            else
            {
                mapHandler.DeleteGameObjectFromScene(currTile, currentMap);
            }
        }
    }
예제 #6
0
    public void CreateTileGroup(int l)
    {
        group_length = l;
        for (int i = 1; i < group_length; i++)
        {
            SerializedTile tempTile = ChooseRandomTile();
            SerializedTile newTile  = tempTile.SetRandomAdjacent(all_tiles);

            if (newTile != null)
            {
                all_tiles.Add(newTile);
            }
            else
            {
                Debug.LogWarning("null tile @: " + i);
            }
        }

        foreach (SerializedTile s in all_tiles)
        {
            GameObject g = Instantiate(temp_tile_to_place);
            g.AddComponent(typeof(FakeTile));
            //g.AddComponent(typeof(BoxCollider2D));
            g.GetComponent <FakeTile>().x = s.x;
            g.GetComponent <FakeTile>().y = s.y;
            float xPos = 0;
            g.name = "x: " + s.x + " y: " + s.y;
            xPos   = s.x * .98f;
            if (Mathf.Abs(s.y % 2) == 1)
            {
                Debug.Log("yoyo");
                xPos += xOffset;
            }


            g.transform.position = new Vector3(this.transform.position.x + xPos, this.transform.position.y + s.y * .85f, 0);
            g.transform.parent   = this.gameObject.transform;
            //Debug.Log("x: " + s.x + " y: " + s.y);
            string h = "";
            foreach (SerializedTile m in s.adjacent_tiles)
            {
                if (m == null)
                {
                    h += "null, ";
                }
                else
                {
                    h += m.ToString() + ", ";
                }
            }
            child_tiles.Add(g);
            //Debug.Log(h);
        }
    }
예제 #7
0
    private void Start()
    {
        //Ensure obj has collider
        if (!gameObject.GetComponent <Collider>())
        {
            gameObject.AddComponent <BoxCollider>();
        }

        tile = new SerializedTile(gameObject.name, gameObject.GetHashCode());

        myMaterial = GetComponentInChildren <Renderer>().material;
    }
예제 #8
0
    bool CompareTilePSR(SerializedTile t1, SerializedTile t2)
    {
        bool loc = (Vector3.Distance(t1.location, t2.location) < 0.1f);

        bool rot = Vector3.Angle(t1.rotation, t2.rotation) < 0.05f;

        if (loc && rot)
        {
            return(true);
        }
        return(false);
    }
예제 #9
0
    private void UpdateTileLocation(SerializedTile currTile, SerializedTile newTile)
    {
        if (CompareTilePSR(currTile, newTile))
        {
            return;
        }
        else
        {
            GameObject.Find(GetNameFromTileUID(currTile)).transform.position = newTile.location;

            GameObject.Find(GetNameFromTileUID(currTile)).transform.rotation = Quaternion.Euler(newTile.rotation);
        }
    }
예제 #10
0
    public SerializedChunk(Chunk chunk)
    {
        chunkSize      = chunk.chunkSize;
        chunkPositionX = chunk.chunkPosition.x;
        chunkPositionY = chunk.chunkPosition.y;
        tiles          = new SerializedTile[chunkSize, chunkSize];

        for (int x = 0; x < chunk.chunkSize; x++)
        {
            for (int y = 0; y < chunk.chunkSize; y++)
            {
                if (chunk.tiles[x, y] != null)
                {
                    tiles[x, y]      = new SerializedTile();
                    tiles[x, y].sand = chunk.tiles[x, y].sand;
                    tiles[x, y].silt = chunk.tiles[x, y].silt;
                    tiles[x, y].clay = chunk.tiles[x, y].clay;
                }
            }
        }
    }
예제 #11
0
    public void CreateNewGameobjectFromTile(SerializedTile newTile, Map currentMap)
    {
        //Debug.LogWarning("Creating New GO From Tile");

        string newTileName = newTile.prefabName;

        foreach (GameObject prefab in creatableObjects)
        {
            if (prefab.name == newTileName)
            {
                GameObject clone = Instantiate(prefab, newTile.location, Quaternion.Euler(newTile.rotation));
                clone.name = NetworkPlayerMapUpdater.GetNameFromTileUID(newTile);
                currentMap.AddTile(newTile);
                return;
            }
            else
            {
                Debug.LogWarning("New tile name not in list of creatable objects");
            }
        }
        Debug.LogWarning("No tile/object name match :(");
    }
예제 #12
0
    private SerializedTile[] convertMapToSerialize(Dictionary <MapLayer, Tile[, ]> input)
    {
        List <SerializedTile> res = new List <SerializedTile>();

        foreach (MapLayer l in input.Keys)
        {
            Tile[,] tiles;
            input.TryGetValue(l, out tiles);
            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    Tile t = this.getTile(l, x, y);
                    if (t != null)
                    {
                        SerializedTile st = new SerializedTile(t, l, x, y);
                        res.Add(st);
                    }
                }
            }
        }
        return(res.ToArray());
    }
예제 #13
0
 public static string GetNameFromTileUID(SerializedTile tile)
 {
     return(tile.uid.ToString());
 }
예제 #14
0
        public SerializedTile SetRandomAdjacent(List <SerializedTile> allTiles)
        {
            SerializedTile rand        = new SerializedTile();
            bool           debugInloop = false;
            int            removeCount = 0;

            while (rand != null)
            {
                int randIndex = Random.Range(0, indexValues.Count);
                rand = adjacent_tiles[randIndex];
                if (rand == null)
                {
                    debugInloop = true;
                    adjacent_tiles[randIndex] = new SerializedTile();
                    int indexToPlacePrevious = 0;
                    int newX = 0;
                    int newY = 0;
                    switch (randIndex)
                    {
                    case 0:
                        indexToPlacePrevious = 3;

                        newY = y;
                        newX = x - 1;
                        break;

                    case 1:
                        indexToPlacePrevious = 4;
                        newX = x - 1;
                        if (Mathf.Abs(y % 2) == 1)
                        {
                            newX = x;
                        }
                        newY = y - 1;
                        break;

                    case 2:
                        indexToPlacePrevious = 5;
                        newX = x;
                        if (Mathf.Abs(y % 2) == 1)
                        {
                            newX = x + 1;
                        }
                        newY = y - 1;
                        break;

                    case 3:
                        indexToPlacePrevious = 0;
                        newX = x + 1;

                        newY = y;
                        break;

                    case 4:
                        indexToPlacePrevious = 1;
                        newX = x;
                        if (Mathf.Abs(y % 2) == 1)
                        {
                            newX = x + 1;
                        }
                        newY = y + 1;
                        break;

                    case 5:
                        indexToPlacePrevious = 2;
                        newX = x - 1;
                        if (Mathf.Abs(y % 2) == 1)
                        {
                            newX = x;
                        }
                        newY = y + 1;
                        break;
                    }

                    bool checkAnother = false;

                    foreach (SerializedTile j in allTiles)
                    {
                        if (j.x == newX && j.y == newY)
                        {
                            checkAnother = true;
                        }
                    }

                    if (!checkAnother)
                    {
                        adjacent_tiles[randIndex].y = newY;
                        adjacent_tiles[randIndex].x = newX;
                        adjacent_tiles[randIndex].adjacent_tiles[indexToPlacePrevious] = this;
                        return(adjacent_tiles[randIndex]);
                    }
                    else
                    {
                        List <int> tmpList = new List <int>();
                        removeCount++;
                        for (int p = 0; p < indexValues.Count; p++)
                        {
                            if (p != randIndex)
                            {
                                tmpList.Add(p);
                            }
                        }


                        indexValues = tmpList;
                        rand        = new SerializedTile();
                        Debug.Log(indexValues.Count);

                        if (tmpList.Count == 0)
                        {
                            return(null);
                        }
                    }
                }
            }
            Debug.LogWarning("NO SIRRR: " + rand + " debug in loop: " + debugInloop);

            return(rand);
        }
예제 #15
0
 public void AddTile(SerializedTile tile)
 {
     tiles.Add(tile);
 }
예제 #16
0
 public void RemoveTile(SerializedTile tile)
 {
     tiles.Remove(tile);
 }
예제 #17
0
 bool CompareTiles(SerializedTile t1, SerializedTile t2)
 {
     return(t1.uid == t2.uid);
 }