Exemplo n.º 1
0
    public void refreshMap()
    {
        logInfo("view center : " + mCurViewPos, LOG_LEVEL.LL_FORCE);
        // 先判断是否有窗口已经从可见变为不可见
        int        viewWidth    = stringToInt(mViewWidthEdit.getText());
        int        viewHeight   = stringToInt(mViewHeightEdit.getText());
        List <int> removedIndex = new List <int>();

        foreach (var item in mVisibleTiles)
        {
            int x = tileIndexToTileX(item.Key);
            int y = tileIndexToTileY(item.Key);
            // 判断是否已经超出视野范围
            if (!isInRange(x, mCurViewPos.x - viewWidth / 2, mCurViewPos.x + viewWidth / 2) ||
                !isInRange(y, mCurViewPos.y - viewHeight / 2, mCurViewPos.y + viewHeight / 2))
            {
                mTileArray[item.Key].setActive(false);
                removedIndex.Add(item.Key);
            }
        }
        int removedCount = removedIndex.Count;

        for (int i = 0; i < removedCount; ++i)
        {
            mVisibleTiles.Remove(removedIndex[i]);
        }
        // 查找从不可见变为可见的窗口
        for (int i = 0; i < viewWidth; ++i)
        {
            for (int j = 0; j < viewHeight; ++j)
            {
                int curTilePosX = i + (int)mCurViewPos.x - viewWidth / 2;
                int curTilePosY = j + (int)mCurViewPos.y - viewHeight / 2;
                int tileIndex   = tilePosToTileIndex(curTilePosX, curTilePosY);
                if (isInRange(curTilePosX, (int)mCurViewPos.x - viewWidth / 2, (int)mCurViewPos.x + viewWidth / 2) &&
                    isInRange(curTilePosY, (int)mCurViewPos.y - viewHeight / 2, (int)mCurViewPos.y + viewHeight / 2) &&
                    isInRange(curTilePosX, 0, mSceneMap.mHeader.mWidth - 1) &&
                    isInRange(curTilePosY, 0, mSceneMap.mHeader.mHeight - 1) &&
                    !mVisibleTiles.ContainsKey(tileIndex))
                {
                    // 变为可见的窗口如果不存在,则需要创建
                    if (mTileArray[tileIndex] == null)
                    {
                        mTileArray[tileIndex] = new TileTextureWindow();
                        createTile(mTileArray[tileIndex], mSceneMap.mTileList[tileIndex]);
                    }
                    mTileArray[tileIndex].setActive(true);
                    mVisibleTiles.Add(tileIndex, mSceneMap.mTileList[tileIndex]);
                }
            }
        }
    }
Exemplo n.º 2
0
    public void createTile(TileTextureWindow tileWindow, MapTile tile)
    {
        // 只有真正要使用该地砖时才解析
        if (!tile.mParsed)
        {
            tile.parseTile();
        }
        int    x          = tileIndexToTileX(tile.mIndex);
        int    y          = tileIndexToTileY(tile.mIndex);
        string tileSuffix = x + "_" + y;

        // 偶数行偶数列才渲染大地砖
        if (tileWindow.mBackTile == null && x % 2 == 0 && y % 2 == 0)
        {
            // 大地砖图片窗口
            string  backTexName = mBackTilePath + (tile.mBngImgIdx - 1);
            Texture backTex     = mResourceManager.loadResource <Texture>(backTexName, false);
            if (backTex != null)
            {
                // 一个panel满时,就应该放入新的panel中
                int panelIndex = tile.mIndex / MAX_WINDOW_PER_PANEL;
                if (!mBackPanelList.ContainsKey(panelIndex))
                {
                    mBackPanelList.Add(panelIndex, createObject <txNGUIPanel>(mBackRoot, "BackPanel" + panelIndex));
                }
                tileWindow.mBackTile = mWindowPool.createWindow <txNGUITexture>("back_" + tileSuffix, mBackPanelList[panelIndex]);
                tileWindow.mBackTile.setTexture(backTex, true);
                tileWindow.mBackTile.setMaterial("NGUIDefault", false);
                Vector2 posOffset = tileWindow.mBackTile.getTextureSize() / 2.0f;
                posOffset += new Vector2(48 * x - mHalfMap.x, mHalfMap.y - 32 * y - 32);
                tileWindow.mBackTile.setLocalPosition(posOffset);
                tileWindow.mBackTile.setDepth(1);
                if (tile.mHasBng)
                {
                    tileWindow.mBackTile.setColor(new Color(0.5f, 0.0f, 0.0f));
                }
                else
                {
                    tileWindow.mBackTile.setColor(Color.white);
                }
                tileWindow.mBackTile.getUITexture().mUserData = charArrayToHexString(tile.mTileBuffer, tile.mTileBuffer.Length);
            }
        }
        if (tileWindow.mMidTile == null)
        {
            // 小地砖图片窗口
            string  midTexName = mMidTilePath + (tile.mMidImgIdx - 1);
            Texture midTex     = mResourceManager.loadResource <Texture>(midTexName, false);
            if (midTex != null)
            {
                tileWindow.mMidTile = mWindowPool.createWindow <txNGUITexture>("mid_" + tileSuffix, mMiddleRoot);
                tileWindow.mMidTile.setTexture(midTex, true);
                tileWindow.mMidTile.setMaterial("NGUIDefault", false);
                Vector2 posOffset = tileWindow.mMidTile.getTextureSize() / 2.0f;
                posOffset += new Vector2(48 * x - mHalfMap.x, mHalfMap.y - 32 * y);
                tileWindow.mMidTile.setLocalPosition(posOffset);
                tileWindow.mMidTile.setColor(Color.white);
            }
        }
        if (tileWindow.mObjectTile == null)
        {
            // 普通物体
            if (!tile.mHasAni)
            {
                string  objTexName = mObjectImagePreString + (tile.mObjFileIdx + 1) + "/" + (tile.mObjImgIdx - 1);
                Texture objTex     = mResourceManager.loadResource <Texture>(objTexName, false);
                if (objTex != null)
                {
                    tileWindow.mObjectTile = mWindowPool.createWindow <txNGUITexture>("obj_" + tileSuffix, mObjRoot);
                    tileWindow.mObjectTile.setTexture(objTex, true);
                    tileWindow.mObjectTile.setMaterial("NGUIDefault", false);
                    Vector2 posOffset = tileWindow.mObjectTile.getTextureSize() / 2.0f;
                    posOffset += new Vector2(48 * x - mHalfMap.x, mHalfMap.y - 32 * y);
                    tileWindow.mObjectTile.setLocalPosition(posOffset);
                    tileWindow.mObjectTile.setDepth(3);
                    if (tile.mHasObj)
                    {
                        tileWindow.mObjectTile.setColor(new Color(0.5f, 0.0f, 0.0f));
                    }
                    else
                    {
                        tileWindow.mObjectTile.setColor(Color.white);
                    }
                    tileWindow.mObjectTile.getUITexture().mUserData = charArrayToHexString(tile.mTileBuffer, tile.mTileBuffer.Length);
                }
            }
            // 带序列帧的物体
            else
            {
                string  objTexName = mObjectImagePreString + (tile.mObjFileIdx + 1) + "/" + (tile.mObjImgIdx - 1);
                Texture objTex     = mResourceManager.loadResource <Texture>(objTexName, false);
                if (objTex != null)
                {
                    tileWindow.mObjectTile = mWindowPool.createWindow <txNGUITexture>("obj_" + tileSuffix, mObjRoot);
                    tileWindow.mObjectTile.setTexture(objTex, true);
                    tileWindow.mObjectTile.setMaterial("Multiple", false);
                    Vector2 posOffset = new Vector2(0.0f, tileWindow.mObjectTile.getTextureSize().y * 1.5f);
                    posOffset += new Vector2(48 * x - mHalfMap.x, mHalfMap.y - 32 * y);
                    tileWindow.mObjectTile.setLocalPosition(posOffset);
                    tileWindow.mObjectTile.setDepth(3);
                }
            }
        }
    }