コード例 #1
0
    // ----------------------------------------------------------------------------------------------------------------

    /// <summary> Copies this tile's data into target. </summary>
    public void CopyTo(GameMapTile t)
    {
        t.id     = id;
        t.sprite = sprite;
        t.type   = type;
        t.opt1   = opt1;
        t.color  = color;
        t.strVal = strVal;
    }
コード例 #2
0
    // ----------------------------------------------------------------------------------------------------------------

    /// <summary> Add tile definition. Mainly for editor use. </summary>
    public void AddTile()
    {
        GameMapTile t = new GameMapTile()
        {
            id = nextTileId++
        };

        tiles.Add(t);
        _cache = null;
    }
コード例 #3
0
    private Dictionary <int, GameMapTile> _cache = null;    // <Tile ID, Tile Definition>

    // ----------------------------------------------------------------------------------------------------------------

    /// <summary> Get the Tile by its unique ID. Remember that the ID is the value stored in
    /// GameMap.grid, not the tile's index in the GameMapTilesAsset.tiles list. </summary>
    public GameMapTile GetTile(int id)
    {
        if (id < 0)
        {
            return(null);
        }

        if (_cache == null)
        {
            _cache = new Dictionary <int, GameMapTile>();
            foreach (GameMapTile t in tiles)
            {
                _cache.Add(t.id, t);
            }

            foreach (AutoTile at in autoTiles)
            {
                _cache.Add(at.tiles[0].id, at.tiles[0]);

                // copy the mainTile info into all auto-tile pieces, except for the sprite
                // note; skip [0] on purpose since it is the main tile
                for (int i = 1; i < at.tiles.Length; i++)
                {
                    Sprite _sp = at.tiles[i].sprite;
                    int    _id = at.tiles[i].id;
                    at.tiles[0].CopyTo(at.tiles[i]);
                    at.tiles[i].id     = _id;
                    at.tiles[i].sprite = _sp;
                    _cache.Add(at.tiles[i].id, at.tiles[i]);
                }
            }
        }

        GameMapTile tile = null;

        if (_cache.TryGetValue(id, out tile))
        {
            return(tile);
        }
        return(null);
    }
コード例 #4
0
ファイル: Main.cs プロジェクト: plyoung/GameMap2D
    // an example of how map data could be loaded
    private void LoadMap(int mapIdx)
    {
        GameMap map = mapsAsset.maps[mapIdx];

        float sz    = tileSize / ppu;
        float offsX = -((map.width / 2f * sz) - (sz / 2f));
        float offsY = -((map.height / 2f * sz) - (sz / 2f));

        // create containers for the various map objects
        Transform floorContainer = new GameObject("Tiles").transform;
        Transform npcContainer   = new GameObject("NPCs").transform;
        Transform trapContainer  = new GameObject("Traps").transform;

        // place tiles and objects. GameMap supports laters but if you choose not to use them then you only need to read from map.grid[]
        // if you do use layers then you should also read data from map.layers[].grid[] while also still using map.grid[] as layer-0
        // to make it easier to read from these two sources of data you can simply use map.LayerCount and map.GetLayerData

        for (int i = 0; i < map.LayerCount; i++)
        {
            int[] grid = map.GetlayerData(i);
            int   idx  = 0;
            for (int y = 0; y < map.height; y++)
            {
                for (int x = 0; x < map.width; x++)
                {
                    GameMapTile t = mapsAsset.tileAsset.GetTile(grid[idx++]);
                    if (t == null)
                    {
                        continue;
                    }

                    if (t.type == GameMapTile.Type.Floor)
                    {                       // place a floor tile
                        if (t.sprite == null)
                        {
                            continue;
                        }
                        SpriteRenderer ren = Instantiate(floorFab).GetComponent <SpriteRenderer>();
                        ren.sprite = t.sprite;
                        ren.transform.SetParent(floorContainer, false);
                        ren.transform.localScale                = new Vector3(tileSize / ren.sprite.rect.width, tileSize / ren.sprite.rect.height, 1f);
                        ren.transform.localPosition             = new Vector3(x * sz + offsX, y * sz + offsY, 0f);
                        ren.GetComponent <BoxCollider2D>().size = new Vector2(ren.sprite.rect.width / ren.sprite.pixelsPerUnit, ren.sprite.rect.height / ren.sprite.pixelsPerUnit);
                    }

                    else if (t.type == GameMapTile.Type.NPC)
                    {                       // place an NPC
                        SpriteRenderer ren = Instantiate(npcFabs[t.opt1]).GetComponent <SpriteRenderer>();
                        ren.transform.SetParent(npcContainer, false);
                        ren.transform.localScale    = new Vector3(tileSize / ren.sprite.rect.width, tileSize / ren.sprite.rect.height, 1f);
                        ren.transform.localPosition = new Vector3(x * sz + offsX, y * sz + offsY, 0f);
                    }

                    else if (t.type == GameMapTile.Type.Trap)
                    {                       // place a Trap
                        SpriteRenderer ren = Instantiate(trapFabs[t.opt1]).GetComponent <SpriteRenderer>();
                        ren.transform.SetParent(trapContainer, false);
                        ren.transform.localScale    = new Vector3(tileSize / ren.sprite.rect.width, tileSize / ren.sprite.rect.height, 1f);
                        ren.transform.localPosition = new Vector3(x * sz + offsX, y * sz + offsY, 0f);
                    }

                    else if (t.type == GameMapTile.Type.Start)
                    {                       // place player object
                        SpriteRenderer ren = Instantiate(playerFab).GetComponent <SpriteRenderer>();
                        ren.transform.localScale    = new Vector3(tileSize / ren.sprite.rect.width, tileSize / ren.sprite.rect.height, 1f);
                        ren.transform.localPosition = new Vector3(x * sz + offsX, y * sz + offsY, 0f);
                    }
                }
            }
        }
    }