Exemplo n.º 1
0
 public void Remove(SplitTile ren)
 {
     mSortIndices.Remove(ren);
     for (int i = 0; i < mSortIndices.Count; ++i)
     {
         mSortIndices[i].SetRenderOrder(i + 1);
     }
 }
Exemplo n.º 2
0
 void OnSetCorrectPosition(SplitTile tile)
 {
     mTotalTilesInCorrectPosition += 1;
     tile.enabled = false;
     sTilesSorting.Remove(tile);
     tile.SetRenderOrder(1);
     tile.mSpriteRenderer.sortingLayerName = "TilesInPlace";
     if (mTotalTilesInCorrectPosition == mSplitImage.mGameObjects.Length)
     {
         mFsm.SetCurrentState((int)GameStates.WIN);
     }
     mTextInPlaceTiles.text = mTotalTilesInCorrectPosition.ToString();
 }
Exemplo n.º 3
0
    bool LoadLevel()
    {
        // Load data asscociated with the game.
        if (!mSplitImage.LoadGame())
        {
            mSplitImage.CreateJigsawTiles();

            for (int i = 0; i < mSplitImage.mTilesX; i++)
            {
                for (int j = 0; j < mSplitImage.mTilesY; ++j)
                {
                    SplitTile tile = mSplitImage.mGameObjects[i, j].GetComponent<SplitTile>();
                    tile.mOnSetCorrectPosition += OnSetCorrectPosition;
                    sTilesSorting.Add(tile);
                }
            }
        }
        else
        {
            // directly go to PLAY mode.
            mPlayButton.gameObject.SetActive(false);
            mFsm.SetCurrentState((int)GameStates.PLAYING);

            for (int i = 0; i < mSplitImage.mTilesX; i++)
            {
                for (int j = 0; j < mSplitImage.mTilesY; ++j)
                {
                    SplitTile tile = mSplitImage.mGameObjects[i, j].GetComponent<SplitTile>();
                    tile.mOnSetCorrectPosition += OnSetCorrectPosition;
                    sTilesSorting.Add(tile);
                    if (tile.IsInCorrectPosition())
                    {
                        OnSetCorrectPosition(tile);
                    }
                }
            }
        }

        mTextTotalTiles.text = (mSplitImage.mTilesX * mSplitImage.mTilesY).ToString();

        StartCoroutine(Coroutime_Timer());
        return false;
    }
Exemplo n.º 4
0
    public bool LoadGame()
    {
        string filename = Application.persistentDataPath + "/" + mImageFilename;

        if (File.Exists(filename))
        {
            using (BinaryReader Reader = new BinaryReader(File.Open(filename, FileMode.Open)))
            {
                mSecondsSinceStart = Reader.ReadDouble();

                mImageFilename = Reader.ReadString();
                mTilesX        = Reader.ReadInt32();
                mTilesY        = Reader.ReadInt32();

                mGameObjects = new GameObject[mTilesX, mTilesY];
                for (int i = 0; i < mTilesX; ++i)
                {
                    for (int j = 0; j < mTilesY; ++j)
                    {
                        GameObject obj = new GameObject();
                        mGameObjects[i, j] = obj;
                        if (TilesParent)
                        {
                            obj.transform.SetParent(TilesParent);
                        }

                        mGameObjects[i, j].name = Reader.ReadString();
                        SplitTile tile = obj.AddComponent <SplitTile>();
                        tile.ShadowMaterial = mShadowMaterial;
                        int tx = Reader.ReadInt32();
                        int ty = Reader.ReadInt32();

                        tile.mIndex = new Vector2Int(tx, ty);

                        float x = Reader.ReadSingle();
                        float y = Reader.ReadSingle();
                        float z = Reader.ReadSingle();
                        obj.transform.position = new Vector3(x, y, z);

                        float rx = Reader.ReadSingle();
                        float ry = Reader.ReadSingle();
                        float rw = Reader.ReadSingle();
                        float rh = Reader.ReadSingle();

                        int length = Reader.ReadInt32();

                        byte[] bytes = new byte[length];
                        Reader.Read(bytes, 0, bytes.Length);

                        SpriteRenderer spriteRenderer = obj.AddComponent <SpriteRenderer>();

                        int w = 140;
                        int h = 140;

                        Texture2D tex = new Texture2D(w, h, TextureFormat.ARGB32, 1, true);
                        tex.LoadImage(bytes);
                        tex.Apply();

                        tile.mSpriteRenderer            = spriteRenderer;
                        spriteRenderer.sortingLayerName = "Tiles";
                        spriteRenderer.sprite           = SpriteUtils.CreateSpriteFromTexture2D(tex, (int)rx, (int)ry, (int)rw, (int)rh);
                        obj.AddComponent <BoxCollider2D>();
                    }
                }

                int base_w = Reader.ReadInt32();
                int base_h = Reader.ReadInt32();

                int base_length = Reader.ReadInt32();

                byte[] base_bytes = new byte[base_length];
                Reader.Read(base_bytes, 0, base_bytes.Length);

                mBaseTexture = new Texture2D(base_w, base_h, TextureFormat.ARGB32, 1, true);
                mBaseTexture.LoadImage(base_bytes);
                mBaseTexture.Apply();

                Texture2D opaque_tex = new Texture2D(mBaseTexture.width, mBaseTexture.height, TextureFormat.ARGB32, 1, true);
                for (int i = 0; i < mBaseTexture.width; ++i)
                {
                    for (int j = 0; j < mBaseTexture.height; ++j)
                    {
                        Color col = mBaseTexture.GetPixel(i, j);
                        col.a = 1.0f;
                        opaque_tex.SetPixel(i, j, col);
                    }
                }
                opaque_tex.Apply();
                mBaseTextureNonTransparent = opaque_tex;

                mSpriteTransparent = SpriteUtils.CreateSpriteFromTexture2D(
                    mBaseTexture,
                    0,
                    0,
                    mBaseTexture.width,
                    mBaseTexture.height
                    );

                mBaseSpriteNonTransparent = SpriteUtils.CreateSpriteFromTexture2D(
                    mBaseTextureNonTransparent,
                    0,
                    0,
                    mBaseTextureNonTransparent.width,
                    mBaseTextureNonTransparent.height
                    );
                mSpriteRenderer.sprite = mSpriteTransparent;
            }
            return(true);
        }
        return(false);
    }
Exemplo n.º 5
0
    public void SaveGame()
    {
        BinaryWriter Writer   = null;
        string       filename = Application.persistentDataPath + "/" + mImageFilename;

        try
        {
            // Create a new stream to write to the file
            Writer = new BinaryWriter(File.OpenWrite(filename));

            // get the time and save it.
            //long dateTime = System.DateTime.Now.ToBinary();
            Writer.Write(mSecondsSinceStart);

            // Writer raw data
            Writer.Write(mImageFilename);
            Writer.Write(mTilesX);
            Writer.Write(mTilesY);

            for (int i = 0; i < mTilesX; ++i)
            {
                for (int j = 0; j < mTilesY; ++j)
                {
                    GameObject obj = mGameObjects[i, j];

                    Writer.Write(mGameObjects[i, j].name);
                    SplitTile tile = obj.GetComponent <SplitTile>();
                    Writer.Write(tile.mIndex.x);
                    Writer.Write(tile.mIndex.y);

                    Writer.Write(obj.transform.position.x);
                    Writer.Write(obj.transform.position.y);
                    Writer.Write(obj.transform.position.z);

                    SpriteRenderer spriteRenderer = obj.GetComponent <SpriteRenderer>();
                    Writer.Write(spriteRenderer.sprite.rect.x);
                    Writer.Write(spriteRenderer.sprite.rect.y);
                    Writer.Write(spriteRenderer.sprite.rect.width);
                    Writer.Write(spriteRenderer.sprite.rect.height);

                    Texture2D tex = spriteRenderer.sprite.texture;

                    byte[] bytes = tex.EncodeToPNG();

                    Writer.Write(bytes.Length);
                    Writer.Write(bytes, 0, bytes.Length);
                }
            }
            Texture2D base_tex = mSpriteRenderer.sprite.texture;

            byte[] base_bytes = base_tex.EncodeToPNG();

            Writer.Write(base_tex.width);
            Writer.Write(base_tex.height);
            Writer.Write(base_bytes.Length);
            Writer.Write(base_bytes, 0, base_bytes.Length);

            Writer.Close();
        }
        catch (SerializationException e)
        {
            Debug.Log("Failed to save jigsaw game. Reason: " + e.Message);
            throw;
        }
    }
Exemplo n.º 6
0
    void CreateSpriteGameObject(int i, int j)
    {
        GameObject obj = new GameObject();

        obj.name = "Tile_" + i.ToString() + "_" + j.ToString();
        SplitTile tile = obj.AddComponent <SplitTile>();

        tile.ShadowMaterial = mShadowMaterial;

        tile.mIndex        = new Vector2Int(i, j);
        mGameObjects[i, j] = obj;

        if (TilesParent != null)
        {
            obj.transform.SetParent(TilesParent);
        }

        SpriteRenderer spren = obj.AddComponent <SpriteRenderer>();

        tile.mSpriteRenderer = spren;

        obj.transform.position = new Vector3(i * 100, j * 100, 0.0f);

        // create a new tile texture.
        Texture2D mTileTexture = CreateTileTexture(i, j);

        tile.mDirections[0] = GetRandomDirection(0);
        tile.mDirections[1] = GetRandomDirection(1);
        tile.mDirections[2] = GetRandomDirection(2);
        tile.mDirections[3] = GetRandomDirection(3);

        // check for bottom and left tile.
        if (j > 0)
        {
            SplitTile downTile = mGameObjects[i, j - 1].GetComponent <SplitTile>();
            if (downTile.mDirections[0] == Direction.UP)
            {
                tile.mDirections[2] = Direction.DOWN_REVERSE;
            }
            else
            {
                tile.mDirections[2] = Direction.DOWN;
            }
        }

        // check for bottom and left tile.
        if (i > 0)
        {
            SplitTile downTile = mGameObjects[i - 1, j].GetComponent <SplitTile>();
            if (downTile.mDirections[1] == Direction.RIGHT)
            {
                tile.mDirections[3] = Direction.LEFT_REVERSE;
            }
            else
            {
                tile.mDirections[3] = Direction.LEFT;
            }
        }

        if (i == 0)
        {
            tile.mDirections[3] = Direction.NONE;
        }
        if (i == mTilesX - 1)
        {
            tile.mDirections[1] = Direction.NONE;
        }
        if (j == 0)
        {
            tile.mDirections[2] = Direction.NONE;
        }
        if (j == mTilesY - 1)
        {
            tile.mDirections[0] = Direction.NONE;
        }
        for (int d = 0; d < tile.mDirections.Length; ++d)
        {
            if (tile.mDirections[d] != Direction.NONE)
            {
                ApplyBezierMask(mTileTexture, tile.mDirections[d]);
            }
        }

        mTileTexture.Apply();

        // Set the tile texture to the sprite.
        Sprite sprite = SpriteUtils.CreateSpriteFromTexture2D(mTileTexture, 0, 0, 140, 140);

        spren.sprite           = sprite;
        spren.sortingLayerName = "Tiles";
        spren.sortingLayerID   = SortingLayer.NameToID("Tiles");

        obj.AddComponent <BoxCollider2D>();
    }
Exemplo n.º 7
0
 public void BringToTop(SplitTile ren)
 {
     // Find the index of ren.
     Remove(ren);
     Add(ren);
 }
Exemplo n.º 8
0
 public void Add(SplitTile ren)
 {
     mSortIndices.Add(ren);
     ren.SetRenderOrder(mSortIndices.Count);
 }