Exemplo n.º 1
0
    private Vector3Int[] GetCollisionCellPositions()
    {
        Bounds bns   = collision.bounds;
        int    cmaxX = (int)Mathf.Ceil(bns.max.x / CELL_SIZE);
        int    cmaxY = (int)Mathf.Ceil(bns.max.y / CELL_SIZE);
        int    cminX = (int)Mathf.Floor(bns.min.x / CELL_SIZE);
        int    cminY = (int)Mathf.Floor(bns.min.y / CELL_SIZE);

        Vector3Int max = new Vector3Int(cmaxX + 1, cmaxY + 1, 0);
        Vector3Int min = new Vector3Int(cminX - 1, cminY - 1, 0);

        List <Vector3Int> list = new List <Vector3Int>();

        for (int cy = min.y; cy <= max.y; cy++)
        {
            for (int cx = min.x; cx <= max.x; cx++)
            {
                Vector3Int        pos  = new Vector3Int(cx, cy, 0);
                Tile.ColliderType type = tilemap.GetColliderType(pos);
                if (type == Tile.ColliderType.Sprite)
                {
                    list.Add(pos);
                }
            }
        }

        return(list.ToArray());
    }
Exemplo n.º 2
0
    private bool OnMove(int horizontalMovment, int verticalMovement)
    {
        Vector2    nextPlayerLocation      = PlayerGridLocation + new Vector2(horizontalMovment, verticalMovement);
        Explodable explodableInDestination = cells[(int)nextPlayerLocation.y, (int)nextPlayerLocation.x];

        TileBase tilebase = tilemap.GetTile(new
                                            Vector3Int((int)nextPlayerLocation.x - gridSize.x / 2, (int)nextPlayerLocation.y - gridSize.y / 2, 0));

        if (tilebase == tileBaseCompare)
        {
            Debug.Log("load new level");
            SceneManager.LoadScene(levelbase);
        }

        Tile.ColliderType colliderType = tilemap.GetColliderType(new
                                                                 Vector3Int((int)nextPlayerLocation.x - gridSize.x / 2, (int)nextPlayerLocation.y - gridSize.y / 2, 0));

        if ((explodableInDestination != null && !explodableInDestination.Destroyed) || colliderType == Tile.ColliderType.Sprite)
        {
            player.nextMove = Vector3.zero;
            return(false);
        }

        float deltaX = horizontalMovment * cellSize;
        float deltaY = verticalMovement * cellSize;

        player.nextMove = new Vector3(deltaX, deltaY, 0);

        return(true);
    }
Exemplo n.º 3
0
 /// <summary>
 /// Constructor for Tiling Rule. This defaults to a Single Output.
 /// </summary>
 public TilingRule()
 {
     m_Output         = OutputSprite.Single;
     m_Sprites        = new Sprite[1];
     m_GameObject     = null;
     m_AnimationSpeed = 1f;
     m_PerlinScale    = 0.5f;
     m_ColliderType   = Tile.ColliderType.Sprite;
 }
Exemplo n.º 4
0
			public TilingRule()
			{
				m_Output = OutputSprite.Single;
				m_Neighbors = new Neighbor[8];
				m_Sprites = new Sprite[1];
				m_AnimationSpeed = 1f;
				m_PerlinScale = 0.5f;
				m_ColliderType = Tile.ColliderType.Sprite;

				for(int i=0; i<m_Neighbors.Length; i++)
					m_Neighbors[i] = Neighbor.DontCare;
			}
Exemplo n.º 5
0
            /// <summary>
            /// Constructor for Tiling Rule. This defaults to a Single Output.
            /// </summary>
            public TilingRule()
            {
                m_Output         = OutputSprite.Single;
                m_Neighbors      = new int[NeighborCount];
                m_Sprites        = new Sprite[1];
                m_AnimationSpeed = 1f;
                m_ColliderType   = Tile.ColliderType.Sprite;

                for (var i = 0; i < m_Neighbors.Length; i++)
                {
                    m_Neighbors[i] = Neighbor.DontCare;
                }
            }
Exemplo n.º 6
0
        public TilingRule(Tile.ColliderType defaultColliderType)
        {
            output       = OutputSprite.Single;
            neighbours   = new Neighbour[8];    // top left, top mid, top right, mid left, etc.
            sprites      = new Sprite[1];
            perlinScale  = 0.5f;
            colliderType = defaultColliderType;

            for (int i = 0; i < neighbours.Length; i++)
            {
                neighbours[i] = Neighbour.DontCare;
            }
        }
Exemplo n.º 7
0
    public TilingRule()
    {
        Output         = OutputSpriteEnum.Single;
        Neighbors      = new NeighborEnum[8];
        Sprites        = new Sprite[1];
        AnimationSpeed = 1f;
        PerlinScale    = 0.5f;
        ColliderType   = Tile.ColliderType.None;

        for (int i = 0; i < Neighbors.Length; i++)
        {
            Neighbors[i] = NeighborEnum.DontCare;
        }
    }
Exemplo n.º 8
0
            public TilingRule()
            {
                output         = OutputSprite.Single;
                neighbors      = new Neighbor[8];
                sprites        = new Sprite[1];
                animationSpeed = 1f;
                perlinScale    = 0.5f;
                colliderType   = Tile.ColliderType.None;

                for (int i = 0; i < neighbors.Length; i++)
                {
                    neighbors[i] = Neighbor.DontCare;
                }
            }
Exemplo n.º 9
0
            public TilingRule()
            {
                m_Output         = OutputSprite.Single;
                m_Neighbors      = new int[NeighborCount];
                m_Sprites        = new Sprite[1];                               //change this -> enable animated tile
                m_GameObject     = null;
                m_AnimationSpeed = 1f;
                m_PerlinScale    = 0.5f;
                m_ColliderType   = Tile.ColliderType.Sprite;

                for (int i = 0; i < m_Neighbors.Length; i++)
                {
                    m_Neighbors[i] = Neighbor.DontCare;
                }
            }
Exemplo n.º 10
0
        private static void SetCollision(Tilemap tilemap, Vector3Int position, Tile.ColliderType colliderType)
        {
            TileBase tile = tilemap.GetTile(position);

            if (tile != null)
            {
                var t = tilemap.GetTile <Tile> (position);
                t.colliderType = colliderType;
                tilemap.RefreshTile(position);

                EditorUtility.SetDirty(t);

                if (!PaletteOnlyMode)
                {
                    tilemap.RefreshAllTiles();
                }
            }
        }
Exemplo n.º 11
0
        static ImportedTile CreateTileAsset(Sprite tileSprite, string tileAssetPath, Tile.ColliderType colliderType)
        {
            Tile tile = AssetDatabase.LoadAssetAtPath <Tile>(tileAssetPath);

            if (tile == null)
            {
                tile              = Tile.CreateInstance <Tile>();
                tile.sprite       = tileSprite;
                tile.colliderType = colliderType;

                AssetDatabase.CreateAsset(tile, tileAssetPath);
            }
            else if (tile.sprite != tileSprite || tile.colliderType != colliderType)
            {
                tile.sprite       = tileSprite;
                tile.colliderType = colliderType;
                EditorUtility.SetDirty(tile);
            }

            return(new ImportedTile(tile, tileAssetPath));
        }
Exemplo n.º 12
0
            public TilingRule()
            {
                m_Output         = OutputSprite.Single;
                m_Neighbors      = new int[NeighborCount];
                m_Sprites        = new Sprite[1];
                m_GameObject     = null;
                m_AnimationSpeed = 1f;
                m_PerlinScale    = 0.5f;
                m_ColliderType   = Tile.ColliderType.Sprite;

                for (int i = 0; i < m_Neighbors.Length; i++)
                {
                    m_Neighbors[i] = Neighbor.DontCare;
                }

                m_CheckSiblings    = false;
                m_SiblingTile      = null;
                m_SiblingNeighbors = new int[NeighborCount];

                for (int i = 0; i < m_SiblingNeighbors.Length; i++)
                {
                    m_SiblingNeighbors[i] = Neighbor.DontCare;
                }
            }
Exemplo n.º 13
0
 private extern void SetColliderType_Injected(ref Vector3Int position, Tile.ColliderType colliderType);
Exemplo n.º 14
0
 public void SetColliderType(Vector3Int position, Tile.ColliderType colliderType)
 {
     this.SetColliderType_Injected(ref position, colliderType);
 }
 public extern void SetColliderType(Vector3Int position, Tile.ColliderType colliderType);
Exemplo n.º 16
0
        static bool CreateTileAssets(TSX.Tile[] tiles, bool singleImageTileset, string tilesetName, int pixelsPerUnit, Sprite[] tileSprites, string[] tileTargetPaths, out ImportedTile[] outputTiles)
        {
            AssetDatabase.StartAssetEditing();
            Debug.Assert(tileSprites != null && tileTargetPaths != null);
            outputTiles = new ImportedTile[tileTargetPaths.Length];
            if (tileSprites.Length != tileTargetPaths.Length)
            {
                Debug.Log("We have " + tileSprites.Length + " sprites but trying to create " + tileTargetPaths.Length + " tiles");
                AssetDatabase.StopAssetEditing();
                return(false);
            }

            bool success = true;

            for (int i = 0; i < tileTargetPaths.Length; i++)
            {
                EditorUtility.DisplayProgressBar("Importing " + tilesetName + " tileset tiles", null, (float)i / (float)tileTargetPaths.Length);
                if (tileSprites[i] != null)
                {
                    Tile.ColliderType colliderType = Tile.ColliderType.None;

                    // Tile Collision
                    if (!singleImageTileset && tiles != null)
                    {
                        TSX.Tile tile = tiles[i];
                        if (tile.HasCollisionData())
                        {
                            colliderType = Tile.ColliderType.Sprite;
                        }
                    }

                    // Tile Animation
                    TSX.Tile animationTile = null;
                    if (tiles != null)
                    {
                        if (singleImageTileset)
                        {
                            // Have to find relevant tile
                            foreach (TSX.Tile tile in tiles)
                            {
                                if (tile.id == i && tile.animation != null)
                                {
                                    animationTile = tile;
                                    break;
                                }
                            }
                        }
                        else if (tiles[i].animation != null)
                        {
                            animationTile = tiles[i];
                        }
                    }

                    if (animationTile != null)
                    {
                        TSX.Frame[] frames           = animationTile.animation.frames;
                        Sprite[]    animationSprites = new Sprite[frames.Length];
                        for (int f = 0; f < frames.Length; f++)
                        {
                            animationSprites[f] = tileSprites[frames[f].tileid];
                        }
                        //Just have to assume a constant animation speed ATM due to how Unity's Tilemap animation works :(
                        float        animationSpeed = 1.0f / (frames[0].duration / 1000.0f);
                        ImportedTile newTile        = CreateAnimatedTileAsset(tileSprites[i], animationSprites, animationSpeed, tileTargetPaths[i], colliderType);
                        success = newTile != null;
                        if (!success)
                        {
                            break;
                        }
                        outputTiles[i] = newTile;
                    }
                    else
                    {
                        ImportedTile newTile = CreateTileAsset(tileSprites[i], tileTargetPaths[i], colliderType);
                        success = newTile != null;
                        if (!success)
                        {
                            break;
                        }
                        outputTiles[i] = newTile;
                    }
                }
                else
                {
                    Debug.LogError("Sprite for tile " + tileTargetPaths[i] + " is null when creating Tile");
                }
            }
            AssetDatabase.StopAssetEditing();
            return(success);
        }
Exemplo n.º 17
0
        // If you're getting "error CS0246: The type of namespace name 'AnimatedTile' could not be found", then you need the prerequisite AnimatedTile class from
        // https://github.com/Unity-Technologies/2d-techdemos/blob/master/Assets/Tilemap/Tiles/Animated%20Tile/Scripts/AnimatedTile.cs
        // and put it somewhere in your project.
        static ImportedTile CreateAnimatedTileAsset(Sprite tileSprite, Sprite[] animatedSprites, float animationSpeed, string tileAssetPath, Tile.ColliderType colliderType)
        {
            AnimatedTile tile = AssetDatabase.LoadAssetAtPath <AnimatedTile>(tileAssetPath);

            if (tile == null)
            {
                tile = AnimatedTile.CreateInstance <AnimatedTile>();
                tile.m_AnimatedSprites    = animatedSprites;
                tile.m_AnimationStartTime = 0.0f;
                tile.m_MinSpeed           = animationSpeed;
                tile.m_MaxSpeed           = animationSpeed;
                tile.sprite       = tileSprite;
                tile.colliderType = colliderType;

                AssetDatabase.CreateAsset(tile, tileAssetPath);
            }
            else if ((tile.m_AnimatedSprites == null) == (animatedSprites == null) ||
                     tile.m_AnimatedSprites == null ||
                     animatedSprites == null ||
                     !tile.m_AnimatedSprites.SequenceEqual <Sprite>(animatedSprites) ||
                     tile.m_MinSpeed != animationSpeed ||
                     tile.m_MaxSpeed != animationSpeed ||
                     tile.sprite != tileSprite ||
                     tile.colliderType != colliderType)
            {
                tile.m_AnimatedSprites = animatedSprites;
                tile.m_MinSpeed        = animationSpeed;
                tile.m_MaxSpeed        = animationSpeed;
                tile.sprite            = tileSprite;
                tile.colliderType      = colliderType;
                EditorUtility.SetDirty(tile);
            }

            return(new ImportedTile(tile, tileAssetPath));
        }