Пример #1
0
    void Start()
    {
        pauseCanvas.SetActive(false);
        paused = false;

        SceneManager.sceneLoaded += OnSceneLoaded;
        string startScene = "Level1";

        currentScene = startScene;
        LevelMap.GetLevelMapObject().onLoadLevel();


        if (CutsceneMode)
        {
            SetHUDVisibility(false);
        }

        string sceneName = SceneManager.GetActiveScene().name;

        if (sceneName.Equals(currentScene))
        {
            //samescene
            // LevelMap.GetLevelMapObject().onLoadLevel();
            // MyGlobal.GetPlayerObject().transform.position = (Vector3)respawnPosition;
        }
        else
        {
            currentScene = sceneName;
            // respawnPosition = null;
        }
    }
Пример #2
0
 private void OnSceneLoaded(Scene aScene, LoadSceneMode aMode)
 {
     LevelMap.GetLevelMapObject().onLoadLevel();
     // gameOverCanvas.SetActive(false);
     //if (!CutsceneMode && (respawnPosition != null))
     //{
     //    if (!aScene.name.Equals(currentScene))
     //    {
     //        currentScene = aScene.name;
     //        respawnPosition = null;
     //    }
     //    else
     //    {
     //        MyGlobal.GetPlayerObject().transform.position = (Vector3)respawnPosition;
     //    }
     //}
 }
Пример #3
0
    private static void NextTileToCheck(ref int?curPri, ref int?curSec, int startPri, int endPri, int startSec, int endSec)
    {
        LevelMap levelMap = LevelMap.GetLevelMapObject();
        int      dirPri   = ((startPri - endPri) < 0) ? 1 : -1;
        int      dirSec   = -1;

        if (curPri == null)
        {
            curPri = startPri;
            curSec = startSec;
            return;
        }
        curSec += dirSec;
        if (curSec < endSec)
        {
            curPri += dirPri;
            curSec  = startSec;
        }
        if (((dirPri == -1) && (curPri < endPri)) || ((dirPri == 1) && (curPri > endPri)))
        {
            curPri = null;
            curSec = null;
        }
    }
Пример #4
0
    private static float GetNewY(Vector2 curPosm, BoxCollider2D boxCollider, Vector2 velocity, ref bool hitTile, bool wasOnGround = false)
    {
        LevelMap   levelMap          = LevelMap.GetLevelMapObject();
        RectPoints boxColliderPoints = BoxColliderPoints(curPosm, boxCollider);
        bool       onGround          = OnGround(curPosm, boxCollider);
        int        yDir = (velocity.y > 0) ? 1 : -1;

        if (velocity.y == 0)
        {
            yDir = -1;
        }
        float boxLeadingY = (yDir == -1) ? boxColliderPoints.bottom : boxColliderPoints.top;
        float boxDesiredY = boxLeadingY + velocity.y; //pos of leading edge if move full vel

        if (wasOnGround)
        {
            boxDesiredY -= 0.2f;
        }
        float boxRightX = boxColliderPoints.right;
        float boxLeftX = boxColliderPoints.left;
        int   ystartY = RoundFloat(curPosm.y); //used to be leading
        int   yendY = RoundFloat(boxDesiredY);
        int   ystartX = RoundFloat(boxRightX);
        int   yendX = RoundFloat(boxLeftX);
        int?  curXIt, curYIt;

        curYIt = null;
        curXIt = null;
        int   curX, curY;
        float slopeHeightToUse    = 0;
        float nonSlopeHeightToUse = 0;
        float halfPlayerHeight    = (boxCollider.size.y / 2.0f);
        bool  hitBlock            = false;
        float blockBottomY        = 0;

        while (true)
        {
            //   Debug.Log("search tiles for ydir = " + yDir + "  y= " + ystartY + "," + yendY + ", and x" + ystartX + "," + yendX);
            NextTileToCheck(ref curYIt, ref curXIt, ystartY, yendY, ystartX, yendX);
            if (curYIt == null)
            {
                break;
            }
            curX = (int)curXIt;
            curY = (int)curYIt;

            if (levelMap.TileIsSolid(curX, curY))
            {
                if (yDir == 1)
                {
                    //when going up, ignore slopes completely
                    //assume that every slope will have a solid block below it
                    //assume that when moving in -y direction, will only intersect non-slopes of the same y
                    //should be fine unless moving very fast
                    if (levelMap.TileSlopeDir(curX, curY) == 0)
                    {
                        //blockBottomY = (float)curY - (tileSize / 2) - 0.02f;
                        blockBottomY = (float)curY - 0.02f;
                        hitBlock     = true;
                        hitTile      = true;
                        break;
                    }
                }
                else // 0 or -1
                {
                    float playerBotMidY = curPosm.y - (halfPlayerHeight) + velocity.y - 0.01f;  //was .03
                    if (wasOnGround)
                    {
                        playerBotMidY -= 0.2f;
                    }
                    float slopeHeightAtPlayerMid = levelMap.getTileTop(curX, curY, curPosm.x);
                    // Debug.Log("ydir= " + yDir + "  col with " + curX + "," + curY + " slopeHeightAtPlayerMid=" + slopeHeightAtPlayerMid + " playerBotMidY= " + playerBotMidY + " playerX=" + curPosm.x);
                    if (levelMap.TileIsBelowSlope(curX, curY))
                    {
                        //completely ignore tiles that are below slopes
                        continue;
                    }

                    if (playerBotMidY > slopeHeightAtPlayerMid)
                    {
                        //inside a slope tile, but bottom of player box doesn't touch slope height
                        continue;
                    }

                    int slopeDir = levelMap.TileSlopeDir(curX, curY);
                    // Debug.Log("ydir= " + yDir + "  col with " + curX + "," + curY + " slopeHeightAtPlayerMid=" + slopeHeightAtPlayerMid + " playerBotMidY= " + playerBotMidY + " playerX=" + curPosm.x + "  hitblock");
                    if (RoundFloat(curPosm.x) == curX && slopeDir != 0)
                    {
                        //assume there will be only one slope that matches x and can collide with player
                        //should be safe since it wouldn't make sense to stack multiple slopes in same x
                        //assume that every slope will have a solid block below it, otherwise could fall through sometimes
                        hitBlock         = true;
                        hitTile          = true;
                        slopeHeightToUse = slopeHeightAtPlayerMid;
                    }
                    else if (slopeDir == 0)
                    {
                        //assume that when moving in -y direction, will only intersect non-slopes of the same y
                        //should be fine unless moving very fast
                        hitBlock            = true;
                        hitTile             = true;
                        nonSlopeHeightToUse = slopeHeightAtPlayerMid;
                    }
                }
            }
        }
        float realY            = 0;
        float realLeadingEdgeY = 0;
        float extraSpace       = 0.01f;

        //If  hit a level tile, set realLeadingEdgeY
        if (hitBlock)
        {
            if (yDir == 1)
            {
                realLeadingEdgeY = blockBottomY - extraSpace;
            }
            else //yDir 0 or 1
            {
                //if hitting a slope and a non-slope, use slope height
                if (slopeHeightToUse != 0)
                {
                    realLeadingEdgeY = (slopeHeightToUse) + extraSpace;
                }
                else
                {
                    realLeadingEdgeY = (nonSlopeHeightToUse) + extraSpace;
                }
            }
        }

        //If didn't hit a level tile, set realLeadingEdgeY if hit a non-level block
        if (!hitBlock)
        {
            foreach (GameObject block in GetBlocks())
            {
                BoxCollider2D blockCollider       = block.GetComponent <BoxCollider2D>();
                RectPoints    blockPoints         = BoxColliderPoints(block.transform.position, blockCollider);
                bool          aboveBlock          = curPosm.y > block.transform.position.y;
                bool          belowBlock          = curPosm.y < block.transform.position.y;
                bool          horizontalWithBlock = (boxColliderPoints.left < blockPoints.right) && (boxColliderPoints.right > blockPoints.left);
                if (horizontalWithBlock)
                {
                    if (yDir == -1 && aboveBlock)
                    {
                        bool willBePastTopSide = boxDesiredY < blockPoints.top;
                        if (willBePastTopSide)
                        {
                            realLeadingEdgeY = blockPoints.top + (extraSpace);
                            hitBlock         = true;
                            hitTile          = true;
                        }
                    }
                    else if (yDir == 1 && belowBlock)
                    {
                        bool willBePastBotSide = boxDesiredY > blockPoints.bottom;
                        if (willBePastBotSide)
                        {
                            realLeadingEdgeY = blockPoints.bottom - (extraSpace);
                            hitBlock         = true;
                            hitTile          = true;
                        }
                    }
                }
            }
        }

        if (hitBlock)
        {
            realY = realLeadingEdgeY + (yDir * -1) * halfPlayerHeight;
        }
        else
        {
            realY = curPosm.y + velocity.y;
        }
        return(realY);
    }
Пример #5
0
    private static float GetNewX(Vector2 curPosm, BoxCollider2D boxCollider, Vector2 velocity, ref bool hitTile)
    {
        LevelMap   levelMap          = LevelMap.GetLevelMapObject();
        RectPoints boxColliderPoints = BoxColliderPoints(curPosm, boxCollider);
        float      halfPlayerWidth   = boxColliderPoints.width / 2.0f;
        float      realX             = curPosm.x;
        float      realLeadingEdgeX  = 0;
        int        xDir = (velocity.x > 0) ? 1 : -1;
        int?       curY = null, curX = null;
        List <int> yValsToSkip = new List <int>();

        if (velocity.x == 0)
        {
            xDir = 0;
        }
        else
        {
            float boxLeadingX = (xDir == -1) ? boxColliderPoints.topLeft.x : boxColliderPoints.topRight.x;
            float boxDesiredX = boxLeadingX + velocity.x; //pos of leading edge if move full vel
            float boxTopY     = boxColliderPoints.topLeft.y;
            float boxBotY     = boxColliderPoints.botLeft.y;
            // if (xDir == -1) boxDesiredX += 0.5f;
            int     startX  = RoundFloat(curPosm.x);
            int     endX    = RoundFloat(boxDesiredX);
            int     startY  = RoundFloat(boxTopY);
            int     endY    = RoundFloat(boxBotY);
            Vector2?colTile = null;
            while (true)
            {
                NextTileToCheck(ref curX, ref curY, startX, endX, startY, endY);
                if (curX == null)
                {
                    break;
                }
                {
                    if (!yValsToSkip.Contains((int)curY) && levelMap.TileIsSolid(curX, curY))
                    {
                        if (levelMap.TileSlopeDir((int)curX, (int)curY) == 0)
                        {
                            //tile is flat
                            colTile = new Vector2((float)curX, (float)curY);
                            hitTile = true;
                            break;
                        }
                        else
                        {
                            //is slope, ignore this tile and every other tile we find in this row
                            yValsToSkip.Add((int)curY);
                        }
                    }
                }
            }
            float extraSpace = 0.01f;
            if (hitTile)
            {
                if (xDir == 1)
                {
                    realLeadingEdgeX  = colTile.Value.x;
                    realLeadingEdgeX -= extraSpace;
                }
                else if (xDir == -1)
                {
                    realLeadingEdgeX  = colTile.Value.x + 1.0f;
                    realLeadingEdgeX += extraSpace;
                }
            }

            //check with blocks
            if (!hitTile)
            {
                foreach (GameObject block in GetBlocks())
                {
                    BoxCollider2D blockCollider     = block.GetComponent <BoxCollider2D>();
                    RectPoints    blockPoints       = BoxColliderPoints(block.transform.position, blockCollider);
                    bool          rightOfBlock      = curPosm.x > block.transform.position.x;
                    bool          leftOfBlock       = curPosm.x < block.transform.position.x;
                    bool          verticalWithBlock = (boxColliderPoints.top > blockPoints.bottom) && (boxColliderPoints.bottom < blockPoints.top);
                    if (verticalWithBlock)
                    {
                        if (xDir == -1 && rightOfBlock)
                        {
                            bool willBePastRightSide = boxDesiredX < blockPoints.right;
                            if (willBePastRightSide)
                            {
                                realLeadingEdgeX = blockPoints.right + extraSpace;
                                hitTile          = true;
                            }
                        }
                        else if (xDir == 1 && leftOfBlock)
                        {
                            bool willBePastLeftSide = boxDesiredX > blockPoints.left;
                            if (willBePastLeftSide)
                            {
                                realLeadingEdgeX = blockPoints.left - extraSpace;
                                hitTile          = true;
                            }
                        }
                    }
                }
            }
        }
        if (hitTile)
        {
            realX = realLeadingEdgeX + -xDir * halfPlayerWidth;
        }
        else
        {
            realX = curPosm.x + velocity.x;
        }
        return(realX);
    }
Пример #6
0
    private static bool OnGroundCor(Vector2 curPosm, RectPoints boxColliderPoints, ref GameObject touchedPlat)
    {
        int   yDir           = -1;
        float searchDistance = 0.03f;
        float boxLeadingY    = (yDir == -1) ? boxColliderPoints.botLeft.y : boxColliderPoints.topLeft.y;
        float boxDesiredY    = boxLeadingY - searchDistance; //pos of leading edge if move full vel was .03
        float boxRightX      = boxColliderPoints.botRight.x;
        float boxLeftX       = boxColliderPoints.botLeft.x;
        int   startY         = RoundFloat(boxLeadingY);
        int   endY           = RoundFloat(boxDesiredY);
        int   startX         = RoundFloat(boxRightX);
        int   endX           = RoundFloat(boxLeftX);

        Vector2?colTileY = null;
        int?    curX, curY;

        curY = null;
        curX = null;
        LevelMap levelMap = LevelMap.GetLevelMapObject();

        //check level tiles
        while (true)
        {
            NextTileToCheck(ref curY, ref curX, startY, endY, startX, endX);
            if (curY == null)
            {
                break;
            }
            if (levelMap.TileIsSolid(curX, curY))
            {
                int slopeDir = levelMap.TileSlopeDir((int)curX, (int)curY);
                if (RoundFloat(curPosm.x) != curX && slopeDir != 0)
                {
                    //ignore slopes if x is not inside
                    continue;
                }
                colTileY = new Vector2((float)curX, (float)curY);
                float slopeHeightAtPlayerMid = levelMap.getTileTop((int)colTileY.Value.x, (int)colTileY.Value.y, curPosm.x);

                if (boxDesiredY <= slopeHeightAtPlayerMid)
                {
                    return(true);
                }
            }
        }

        //check other blocks

        if (true)
        {
            foreach (GameObject block in GetBlocks())
            {
                BoxCollider2D blockCollider       = block.GetComponent <BoxCollider2D>();
                RectPoints    blockPoints         = BoxColliderPoints(block.transform.position, blockCollider);
                bool          aboveBlock          = curPosm.y > block.transform.position.y;
                bool          belowBlock          = curPosm.y < block.transform.position.y;
                bool          horizontalWithBlock = (boxColliderPoints.left < blockPoints.right) && (boxColliderPoints.right > blockPoints.left);
                if (horizontalWithBlock)
                {
                    bool willBePastTopSide = boxDesiredY < blockPoints.top;
                    if (willBePastTopSide && aboveBlock)
                    {
                        touchedPlat = block;
                        return(true);
                    }
                }
            }
        }

        return(false);
    }