Esempio n. 1
0
        public Tile GetTile( Cell cell )
        {
            if( cell == null || _TileSet== null || _TileSet.tiles == null || _TileSet.tiles.Length == 0 || _TileSet.tiles.Length <= cell.tileID )
            {
                return null;
            }

            return _TileSet.tiles[cell.tileID];
        }
Esempio n. 2
0
        public bool PutTile( Point2 pos,int tileID,int partsID,Point2 tilePos,bool autoTiling,bool changeParts )
        {
            bool changed = false;

            if( !IsCellContains( pos ) )
            {
                return false;
            }

            bool add = false;

            Point2 chunkPos = pos/32;
            Chunk chunk = GetChunk( chunkPos );
            if( chunk == null )
            {
                chunk = AddChunk( chunkPos );

                changed = true;
            }

            Cell cell = GetCell( pos );
            if( cell == null )
            {
                cell = new Cell( pos,tileID );

                add = true;

                changed = true;
            }

            Tile tile = _TileSet.tiles[tileID];
            if( tile.collider )
            {
                if( cell.collider == null )
                {
                    cell.collider = ComponentUtility.AddComponent<BoxCollider2D>( gameObject );

                    cell.collider.hideFlags = HideFlags.HideInInspector;
                }

                cell.collider.sharedMaterial = tile.physicsMaterial;

                cell.collider.isTrigger = tile.isTrigger;

                float halfSize = 0.5f;

                Vector3 localPos = MapPointToLocalPoint( pos );

            #if UNITY_5
                cell.collider.offset = new Vector2( localPos.x+halfSize,localPos.y+halfSize );
            #else
                cell.collider.center = new Vector2( localPos.x+halfSize,localPos.y+halfSize );
            #endif
                cell.collider.size = new Vector2( 1.0f,1.0f );

                if( _ColliderCellDic != null && !_ColliderCellDic.ContainsKey( cell.collider ) )
                {
                    _ColliderCellDic.Add( cell.collider,cell );
                }
            }
            else
            {
                if( cell.collider != null )
                {
                    if( _ColliderCellDic != null && _ColliderCellDic.ContainsKey( cell.collider ) )
                    {
                        _ColliderCellDic.Remove( cell.collider );
                    }

                    ComponentUtility.Destroy( cell.collider );
                    cell.collider = null;
                }
            }

            Point2 tilePosTmp = ( tile.type == Tile.Type.Normal )?new Point2( Mod(tilePos.x,tile.width),Mod(tilePos.y,tile.height) ) : Point2.zero;

            if( cell.tilePos != tilePosTmp )
            {
                cell.tilePos = tilePosTmp;
                changed = true;
            }

            if( add )
            {
                _Cells.Add( cell );
                if( _CellDic != null )
                {
                    _CellDic.Add( cell.position,cell );
                }
            }
            else
            {
                if( cell.tileID != tileID )
                {
                    cell.tileID = tileID;

                    changed = true;
                }
            }

            if( changeParts )
            {
                if( autoTiling )
                {
                    int cellPartsID = cell.partsID;
                    GenerateIndices( cell );
                    if( cell.partsID != cellPartsID )
                    {
                        changed = true;
                    }

                    for( int x=-1;x<=1;x++ )
                    {
                        for( int y=-1;y<=1;y++ )
                        {
                            if( x==0 && y==0 )
                            {
                                continue;
                            }

                            Cell aroundCell = GetCell( pos + new Point2( x,y ) );
                            if( aroundCell != null )
                            {
                                if( GenerateIndices( aroundCell ) )
                                {
                                    changed = true;
                                }
                            }
                        }
                    }
                }
                else
                {
                    if( cell.partsID != partsID )
                    {
                        cell.partsID = partsID;
                        changed = true;
                    }
                }
            }

            if( changed )
            {
                chunk.dirty = true;
            }

            return changed;
        }
Esempio n. 3
0
        bool GenerateIndices( Cell cell )
        {
            int oldPartsID = cell.partsID;

            cell.partsID = 0;

            Point2 aroundPos = new Point2();

            int id = cell.tileID;

            for( int y=0;y<2;y++ )
            {
                for( int x=0;x<2;x++ )
                {
                    int vx = (x%2==0)?-1:1;
                    int vy = (y%2==0)?1:-1;

                    aroundPos.x = cell.position.x + 0;
                    aroundPos.y = cell.position.y + vy;
                    int longitudinalID = id;
                    if( IsCellContains( aroundPos ) )
                    {
                        longitudinalID = GetTileID( aroundPos );
                    }

                    aroundPos.x = cell.position.x + vx;
                    aroundPos.y = cell.position.y + 0;
                    int transverseID = id;
                    if( IsCellContains( aroundPos ) )
                    {
                        transverseID = GetTileID( aroundPos );
                    }

                    aroundPos.x = cell.position.x + vx;
                    aroundPos.y = cell.position.y + vy;
                    int obliqueID = id;
                    if( IsCellContains( aroundPos ) )
                    {
                        obliqueID = GetTileID( aroundPos );
                    }

                    int index = 4;

                    if (id == transverseID && id == longitudinalID && id == obliqueID) index = 0;
                    else if (id == transverseID && id == longitudinalID) index = 3;
                    else if (id == longitudinalID) index = 2;
                    else if (id == transverseID) index = 1;

                    cell.SetPartsIndex( x,y,index );
                }
            }

            if( cell.partsID != oldPartsID )
            {
                Chunk chunk = GetChunk ( cell.position/32 );
                if( chunk != null )
                {
                    chunk.dirty = true;
                }

                return true;
            }

            return false;
        }
Esempio n. 4
0
        void UpdateTile( Cell cell )
        {
            if( map == null || map.tileSet== null || map.tileSet.tiles == null || map.tileSet.tiles.Length == 0 || map.tileSet.tiles.Length <= cell.tileID )
            {
                return;
            }

            TileSet tileSet = map.tileSet;

            Tile tile = tileSet.tiles[cell.tileID];

            float halfSize = 0.5f;

            Vector3 localPos = map.MapPointToLocalPoint( cell.position );

            Texture texture = tileSet.material.mainTexture;

            float textureWidth = texture.width;
            float textureHeight = texture.height;

            float epsilon = 1.0f;
            Vector2 vEPS = new Vector2( epsilon,epsilon) * 0.5f;

            float tileSize = tile.size-epsilon;

            float uvWidth = tileSize/textureWidth*0.5f;
            float uvHeight = tileSize/textureHeight*0.5f;

            int animationFrame = tile.animation;
            if( animationFrame == 0 )
            {
                animationFrame = 1;
            }

            Vector2 animationUV = new Vector2( animationFrame,tile.GetPartsWidth()/textureHeight );

            for( int y=0;y<2;y++ )
            {
                for( int x=0;x<2;x++ )
                {
                    int index = cell.GetPartsIndex( x,y );

                    int vertCount = _Vertices.Count;

                    Vector3 vertex = localPos + new Vector3( x*halfSize,(1-y)*halfSize,0.0f );
                    _Vertices.Add ( vertex + new Vector3(     0.0f, halfSize,0.0f));
                    _Vertices.Add ( vertex + new Vector3( halfSize, halfSize,0.0f) );
                    _Vertices.Add ( vertex + new Vector3(     0.0f, 0.0f,0.0f) );
                    _Vertices.Add ( vertex + new Vector3( halfSize, 0.0f,0.0f) );

                    Vector2 pos = tile.IndexToPos( index,x,y,cell.tilePos ) + vEPS;

                    Vector2 uv = new Vector2( pos.x/textureWidth,(textureHeight-pos.y)/textureHeight );
                    _UVs.Add ( uv );
                    _UVs.Add ( uv + new Vector2( uvWidth,0.0f ) );
                    _UVs.Add ( uv + new Vector2( 0.0f,-uvHeight ) );
                    _UVs.Add ( uv + new Vector2( uvWidth,-uvHeight ) );

                    _Animations.Add( animationUV );
                    _Animations.Add( animationUV );
                    _Animations.Add( animationUV );
                    _Animations.Add( animationUV );

                    _Colors.Add( cell.GetVertexColor( x,y,0 ) );
                    _Colors.Add( cell.GetVertexColor( x,y,1 ) );
                    _Colors.Add( cell.GetVertexColor( x,y,2 ) );
                    _Colors.Add( cell.GetVertexColor( x,y,3 ) );

                    _Indices.Add( vertCount+0 );
                    _Indices.Add( vertCount+1 );
                    _Indices.Add( vertCount+2 );
                    _Indices.Add( vertCount+2 );
                    _Indices.Add( vertCount+1 );
                    _Indices.Add( vertCount+3 );
                }
            }
        }