예제 #1
0
        public Tile GetTile(TeleporterTileData t)
        {
            if (t.Teleport == null)
            {
                throw new InvalidOperationException("Invalid map format. Teleport tile has to have teleport");
            }

            SetMinimapTile(Color.Blue);

            var initializer = new TeleprotInitializer
            {
                Direction         = t.Teleport.Rotation.ToMapDirection(),
                AbsoluteDirection = t.Teleport.RotationType == RotationType.Absolute,
                Open               = t.IsOpen,
                ScopeConstrain     = GetTeleportScopeType(t.Teleport.Scope),
                TargetTilePosition = t.Teleport.DestinationPosition.ToAbsolutePosition(builder.Data.Maps[t.Teleport.MapIndex]),
                NextLevelIndex     = t.Teleport.MapIndex,
                Visible            = t.IsVisible,
            };


            var res = new TeleportTile(initializer);

            //setup target tile for in level teleports, cross lever teleportation doesn't work for creatures unless player is near by.
            GetTeleportTarget(initializer);
            SetupTeleportSides(initializer, currentGridPosition, true, res);
            res.Renderer     = builder.Factories.RenderersSource.GetTileRenderer(res);
            this.initializer = initializer;
            return(res);
        }
예제 #2
0
 private bool notAlreadyTeleporting()
 {
     foreach (var tile in otherTeleports)
     {
         TeleportTile t = tile.GetComponent <TeleportTile>();
         if (t.teleporting)
         {
             return(false);
         }
     }
     return(true);
 }
예제 #3
0
 public void OnTriggerEnter(Collider other)
 {
     try{
         if (other.GetComponent <TeleportTile>() != null)
         {
             tp = other.GetComponent <TeleportTile>();
             agent.Warp(tp.Teleport.position);
             Debug.Log("The teleport from [" + other.name + "] to [" + tp.Teleport.name + "] was successfull!!");
         }
     }catch {
         Debug.Log("Something went terribly wrong!! " + other.name + " is [" + tp.Teleport.name + "] <- if this says it is null, you need to set the teleport in inspector!");
     }
 }
예제 #4
0
    public override void Break()
    {
        base.Break();

        if (receiver)
        {
            TeleportTile r = receiver.transform.GetComponent <TeleportTile>();
            if (r.active && r.senders.Count == 1)
            {
                r.Break();
            }
        }
        foreach (GameObject sender in senders)
        {
            TeleportTile s = sender.transform.GetComponent <TeleportTile>();
            if (s.active)
            {
                s.Break();
            }
        }

        currentColor = Color.black;
        transform.GetComponent <Renderer>().material.color = currentColor;
    }
예제 #5
0
    private void FixedUpdate()
    {
        if (!isDead)
        {
            #region Movement
            // Determine accel and decel values based on if the player is grounded or not
            float acceleration = grounded ? walkAcceleration : airAcceleration;
            float deceleration = grounded ? groundDeceleration : 0;

            if (canMove || movingToTargetPos || forcedInput)
            {
                if (movingToTargetPos || forcedInput)
                {
                    moveInput = forcedMoveInput;
                }

                if (moveInput != 0)
                {
                    // Accelerate when moving
                    velocity.x = Mathf.MoveTowards(velocity.x, speed * moveInput, acceleration * Time.deltaTime);
                }
                else
                {
                    // Decelerate when not moving
                    velocity.x = Mathf.MoveTowards(velocity.x, 0, deceleration * Time.deltaTime);
                }
            }

            if (movingToTargetPos)
            {
                Debug.Log("Moving target");
                float targetDistance = Mathf.Abs(targetPosX - transform.position.x);

                if (targetDistance <= .2f)
                {
                    Debug.Log(movingToTargetPos);
                    velocity.x        = 0f;
                    movingToTargetPos = false;
                }
            }

            velocity.y += currentGravity * Time.deltaTime; // Vertical movement
            if (velocity.y < maxFallSpeed)
            {
                velocity.y = maxFallSpeed;                  // Terminal velocity
            }
            transform.Translate(velocity * Time.deltaTime); // Horizontal movement
            grounded = false;                               // Resets grounded state
            #endregion

            #region Collision
            // Get overlapping colliders after velocity is applied
            Collider2D[] hits = Physics2D.OverlapBoxAll(transform.position, col.size, 0);

            // Wall cling and ceiling checking
            leftCheck  = Physics2D.Raycast(transform.position, Vector2.left, .5f, whatIsCling);  // Left side
            rightCheck = Physics2D.Raycast(transform.position, Vector2.right, .5f, whatIsCling); // Right side
            //Debug.DrawRay(transform.position, new Vector2(-.5f, 0f), Color.green);
            //Debug.DrawRay(transform.position, new Vector2(.5f, 0f), Color.red);

            RaycastHit2D ceilingCheck = Physics2D.BoxCast(transform.position, new Vector2(.75f, .1f), 0f, Vector2.up, .35f, whatIsCeiling); // Ceiling check

            // If the cling buffer time doesn't apply anymore
            if (clingBufferTimeCurrent <= 0)
            {
                // Determines if the player is clinged to a wall. If both are true, the player is inside the ground and isn't clinging.
                isClinged = leftCheck ^ rightCheck;
            }
            // Otherwise, continue counting down the cling buffer time
            else
            {
                clingBufferTimeCurrent -= Time.deltaTime;
            }

            // Fix for ensuring landed is false when player is in the air
            if (hits.Length == 1 && !isClinged)
            {
                landed = false;
            }

            // Reset teleportating bool
            isTeleporting = false;

            // Ground checking
            foreach (Collider2D hit in hits)
            {
                // Ignore own collider
                if (hit == col)
                {
                    continue;
                }

                ColliderDistance2D colliderDistance = hit.Distance(col);

                // Are we still overlapping this collider?
                if (colliderDistance.isOverlapped)
                {
                    // Deathbox collision
                    if (hit.gameObject.CompareTag("Deathbox"))
                    {
                        AudioManager.am.Play("Death");
                        Invoke("DelayTransition", .5f);
                        Instantiate(deathParticle, transform.position, Quaternion.identity);
                        groundParticle.gameObject.SetActive(false);
                        eyes.gameObject.SetActive(false);
                        body.gameObject.SetActive(false);
                        isDead = true;
                        return;
                    }

                    // Endpoint collision
                    if (hit.gameObject.CompareTag("Endpoint"))
                    {
                        hit.gameObject.GetComponent <LevelEndpoint>().EndLevel(this);
                        LevelController.levelController.DisableSaturation();
                        return;
                    }

                    // Teleport collision
                    if (hit.gameObject.CompareTag("Teleport") && !isTeleporting)
                    {
                        TeleportTile teleport = hit.GetComponentInParent <TeleportTile>().GetOtherTeleporter();
                        Vector3      newPos   = teleport.GetTeleportPos();
                        transform.position = newPos;
                        bufferedVelocity   = velocity;

                        landed        = false;
                        grounded      = false;
                        isTeleporting = true;
                        isClinged     = false;
                    }

                    overlap = true;
                    transform.Translate(colliderDistance.pointA - colliderDistance.pointB);

                    // If the object beneath us intersects, grounded is true
                    if (Vector2.Angle(colliderDistance.normal, Vector2.up) < 90 && velocity.y < 0 && !isTeleporting)
                    {
                        isJumping      = false;
                        touchingGround = true;
                        isWallJumping  = false;
                        grounded       = true;
                        canMove        = true;
                        velocity.y     = 0f;

                        // Ensures particle only spawns once
                        if (!landed)
                        {
                            AudioManager.am.Play("Land");
                            eyes.localScale = new Vector2(eyes.localScale.x, 0f);
                            Instantiate(jumpParticle, new Vector2(transform.position.x, transform.position.y - .5f), Quaternion.identity);
                            landed   = true;
                            grounded = true;
                        }

                        // Bounce
                        if (hit.gameObject.CompareTag("Bounce"))
                        {
                            BounceTile bounceTile = hit.GetComponentInParent <BounceTile>();

                            // Assuming the bounce tile will boost the Y-axis
                            if (!bounceTile.GetAxis())
                            {
                                AudioManager.am.Play("Bounce");

                                // Somehow fixed by having tile colliders having IsTrigger set to false
                                // What if we stopped making games like all of them :)

                                /*
                                 * RaycastHit2D castToGround = Physics2D.Raycast(transform.position, Vector2.down, .5f, whatIsCling); // Right side
                                 *
                                 * // Adjust player position to ensure player does not end up inside tile
                                 * if (castToGround.collider != null && castToGround.collider.gameObject.CompareTag("Bounce"))
                                 * {
                                 *  Vector2 gridPos = TilemapUtils.GetGridPosition(tilemap, castToGround.point);
                                 *  Vector2 tilePos = TilemapUtils.GetGridWorldPos(tilemap, (int)gridPos.x, (int)gridPos.y);
                                 *
                                 *  transform.position = new Vector2(transform.position.x, tilePos.y + .25f);
                                 *
                                 * }
                                 */

                                velocity.y = Mathf.Sqrt(bounceForceY * jumpHeight * Mathf.Abs(currentGravity));
                                isClinged  = false;
                                landed     = false;
                                grounded   = false;
                                if (moveInput == 0)
                                {
                                    velocity.x = 0f;
                                }
                            }
                        }
                    }
                    else
                    {
                        touchingGround = false;
                    }
                }
                else
                {
                    touchingGround = false;
                    overlap        = false;

                    // If dialogueActive is true
                    // Call WriteText to disable text
                }
            }

            RaycastHit2D groundCheck = Physics2D.Raycast(transform.position, Vector2.down, .5f, whatIsCling);
            if (isClinged && groundCheck)
            {
                velocity.x = 0f;
                isClinged  = false;
            }

            if (((leftCheck && moveInput == 1) || (rightCheck && moveInput == -1)) && isClinged)
            {
                if (currentDetachWaitTime > 0)
                {
                    detachActive           = false;
                    currentDetachWaitTime -= Time.deltaTime;
                }
                else
                {
                    detachActive = true;
                }
            }
            else
            {
                detachActive          = false;
                currentDetachWaitTime = detachWaitTime;
            }

            // If player is clinged, stop and lock movement
            if (!detachActive && isClinged)
            {
                currentGravity = 0f;
                velocity       = Vector2.zero;
                moveInput      = 0f;
                canMove        = false;
                isWallJumping  = false;

                // Ensures particle only spawns once
                if (!landed)
                {
                    AudioManager.am.Play("Land");
                    eyes.localScale = new Vector2(eyes.localScale.x, 0f);
                    Collider2D tileCol = leftCheck ? leftCheck.collider : rightCheck.collider;

                    // TILE INTERACTIONS

                    // Switch
                    if (tileCol.CompareTag("Switch"))
                    {
                        SwitchTile switchTile = tileCol.GetComponentInParent <SwitchTile>();
                        switchTile.ChangeLockState();
                    }

                    // Pressure
                    if (tileCol.CompareTag("Pressure"))
                    {
                        PressureTile pressureTile = tileCol.GetComponentInParent <PressureTile>();
                        pressureTile.ChangeMoveState(true);
                    }

                    // Bounce
                    if (tileCol.CompareTag("Bounce"))
                    {
                        AudioManager.am.Play("Bounce");

                        BounceTile bounceTile = tileCol.GetComponentInParent <BounceTile>();
                        // Assuming the bounce tile will boost the X-axis
                        if (bounceTile.GetAxis())
                        {
                            isWallJumping  = true;
                            currentGravity = gravity;
                            speedX         = leftCheck ? speed : -speed; // Are we clinged on the left or right?
                            velocity       = new Vector2(bounceForceX * speedX, Mathf.Sqrt(2f * jumpHeight * Mathf.Abs(currentGravity)));

                            isClinged             = false;
                            landed                = false;
                            grounded              = false;
                            detachActive          = false;
                            currentDetachWaitTime = detachWaitTime;
                            StartCoroutine(SetCanMove(.66f));
                        }
                    }

                    if (tileCol.CompareTag("Teleport") && !isTeleporting)
                    {
                        TeleportTile teleport = tileCol.GetComponentInParent <TeleportTile>().GetOtherTeleporter();
                        Vector3      newPos   = teleport.GetTeleportPos();
                        transform.position = newPos;
                        bufferedVelocity   = velocity;

                        landed        = false;
                        grounded      = false;
                        isTeleporting = true;
                        isClinged     = false;
                    }

                    // Spawn particles when colliding with wall
                    if (leftCheck)
                    {
                        Instantiate(jumpParticle, new Vector2(transform.position.x - .5f, transform.position.y), Quaternion.Euler(0f, 0f, -90f));
                    }
                    else
                    {
                        Instantiate(jumpParticle, new Vector2(transform.position.x + .5f, transform.position.y), Quaternion.Euler(0f, 0f, 90f));
                    }

                    landed = true;
                }

                // Wall jump
                if (jumpActive && allowJumping)
                {
                    Collider2D tileCol = leftCheck ? leftCheck.collider : rightCheck.collider;
                    if (tileCol.CompareTag("Pressure"))
                    {
                        PressureTile pressureTile = tileCol.GetComponentInParent <PressureTile>();
                        pressureTile.ChangeMoveState(false);
                    }

                    float pitch = 1 + Random.Range(-.25f, .25f);
                    AudioManager.am.Play("Jump", pitch);
                    currentGravity         = gravity;
                    speedX                 = leftCheck ? speed : -speed; // Are we clinged on the left or right?
                    velocity               = new Vector2(speedX, Mathf.Sqrt(2f * jumpHeight * Mathf.Abs(currentGravity)));
                    clingBufferTimeCurrent = clingBufferTime;

                    isJumping             = true;
                    isWallJumping         = true;
                    isClinged             = false;
                    landed                = false;
                    grounded              = false;
                    detachActive          = false;
                    currentDetachWaitTime = detachWaitTime;
                    StartCoroutine(SetCanMove(true));
                }
            }
            else if (detachActive && isClinged)
            {
                Collider2D tileCol = leftCheck ? leftCheck.collider : rightCheck.collider;
                if (tileCol.CompareTag("Pressure"))
                {
                    PressureTile pressureTile = tileCol.GetComponentInParent <PressureTile>();
                    pressureTile.ChangeMoveState(false);
                }

                currentGravity         = gravity;
                speedX                 = leftCheck ? speed : -speed; // Are we clinged on the left or right?
                velocity               = new Vector2(speedX / 4, 0f);
                clingBufferTimeCurrent = clingBufferTime;

                isClinged             = false;
                isJumping             = true;
                isWallJumping         = true;
                landed                = false;
                grounded              = false;
                detachActive          = false;
                currentDetachWaitTime = detachWaitTime;
                canMove               = true;
            }
            else
            {
                currentGravity = gravity;
            }

            // Stops upward velocity if player hits ceiling
            if (ceilingCheck)
            {
                bool         hittingCeiling = true;
                Collider2D[] ceilingHits    = Physics2D.OverlapBoxAll(transform.position, col.size, 0);

                // If colliding with a Bounce tile, the player is not hitting the ceiling
                foreach (Collider2D hit in ceilingHits)
                {
                    if (hit.transform.tag == "Bounce")
                    {
                        hittingCeiling = false;
                    }
                }

                if (hittingCeiling)
                {
                    velocity.y = 0f;
                }
            }

            #endregion

            #region Animations

            // Changes position of eyes based on velocity of player
            if (allowInput || movingToTargetPos || forcedInput)
            {
                eyesTargetPos = new Vector2(Mathf.Lerp(eyes.localPosition.x, velocity.x / 80, .25f), Mathf.Lerp(eyes.localPosition.y, velocity.y / 100, .25f));
            }
            else
            {
                eyesTargetPos = new Vector2(Mathf.Lerp(eyes.localPosition.x, eyesDialoguePosX, .25f), 0f);
            }

            eyes.localPosition = eyesTargetPos;

            // Eyes blinking
            if (!lockBlink)
            {
                if (isBlinking)
                {
                    eyes.localScale = new Vector2(eyes.localScale.x, Mathf.Lerp(eyes.localScale.y, 0f, .33f));

                    if (eyes.localScale.y <= .025f)
                    {
                        isBlinking = false;
                    }
                }
                else
                {
                    eyes.localScale = new Vector2(eyes.localScale.x, Mathf.Lerp(eyes.localScale.y, 1f, .33f));
                }

                // Hotfix for alleviating lerp issues relative to scale
                if (eyes.localScale.y >= .995f)
                {
                    eyes.localScale = new Vector2(eyes.localScale.x, 1f);
                }

                // Hotfix for alleviating lerp issues relative to position
                if (eyes.localPosition.y < .01f && eyes.localPosition.y > -.01f && velocity.y > -1 && velocity.y < 0)
                {
                    eyes.localPosition = new Vector2(eyes.localPosition.x, 0f);
                }

                // Plays particles if moving, stops particles if not moving
                if ((velocity.y > -1.5f && velocity.y < .25f && velocity.x > -.25f && velocity.x < .25f) || isClinged)
                {
                    playOnce = true;
                    groundParticle.Stop();
                }
                else if (playOnce)
                {
                    playOnce = false;
                    groundParticle.Play();
                }
            }

            #endregion

            #region Debug

            // Debug text
            if (debugText != null)
            {
                debugText.text = "Velocity X: " + velocity.x + "\n" +
                                 "Velocity Y: " + velocity.y + "\n" +
                                 "Grounded: " + grounded + "\n" +
                                 "Landed: " + landed + "\n" +
                                 "Can Move: " + canMove + "\n" +
                                 "Clinging: " + isClinged + "\n" +
                                 "Colliding with Object: " + overlap + "\n" +
                                 "Colliding with Ground?: " + touchingGround;
            }
            #endregion
        }
    }
예제 #6
0
    public void SpawnMap(int mapID, int playerCount)
    {
        Texture2D bitmap = MapLibrary.bitmaps[mapID];

        width  = bitmap.width;
        height = bitmap.height;
        int playerSpawn = 0;


        List <GameObject> tpTiles     = new List <GameObject>();
        List <GameObject> movingTiles = new List <GameObject>();

        for (int y = 0; y < bitmap.height; y++)
        {
            for (int x = 0; x < bitmap.width; x++)
            {
                int k;


                Color32 c = bitmap.GetPixel(x, y);// *255;
                k = MapLibrary.ColorToTile(c);

                if (k % 2 == 0 && k > 0)
                {
                    if (playerSpawn < 4)
                    {
                        playerSpawnPoints[playerSpawn].transform.position = transform.position + new Vector3(x * scaling, 0, -y * scaling);
                        playerSpawn++;
                    }
                    k /= 2;
                }
                else if (k > 2)
                {
                    k = (k + 1) / 2;
                }
                if (k > 0)
                {
                    GameObject tile = Instantiate(tilePrefabs[k]);
                    tile.transform.GetComponent <Tile>().tileID = (y * width) + x;
                    tile.transform.SetParent(tilesHolder.transform);
                    tile.transform.position = transform.position + new Vector3(x * scaling, 0, -y * scaling);

                    if (k == 5)
                    {
                        tpTiles.Add(tile);
                    }
                    else if (k == 6)
                    {
                        movingTiles.Add(tile);
                        tile.transform.position += new Vector3(0, 1.5f, 0);
                    }
                }
            }
        }
        //print(bitmap.GetPixel(j, i) * 255);

        /*
         * for (int i = 0; i < dim; i++)
         * {
         *  for (int j = 0; j < dim; j++)
         *  {
         *      int k;
         *      if (i > dim / 2)
         *          k = 0;
         *      else
         *          k = 1;
         *      k = 0;
         *      k = MapLibrary.maps25x25[mapID, i, j];
         *      if (k % 2 == 0 && k > 0)
         *      {
         *          if (playerSpawn < 4)
         *          {
         *              playerSpawnPoints[playerSpawn].transform.position = transform.position + new Vector3(j * 1.5f, 0, i * 1.5f);
         *              playerSpawn++;
         *          }
         *          k /= 2;
         *      }
         *      else if (k > 2)
         *      {
         *          k = (k + 1) / 2;
         *      }
         *      GameObject tile = Instantiate(tilePrefabs[k]);
         *      tile.transform.GetComponent<Tile>().tileID = (i * dim) + j;
         *      tile.transform.SetParent(tilesHolder.transform);
         *      tile.transform.position = transform.position + new Vector3(j * 1.5f, 0, i * 1.5f);
         *      if (k == 5)
         *      {
         *          tpTiles.Add(tile);
         *      }
         *  }
         * }*/
        List <int> tpTileMap = (List <int>)MapLibrary.teleportTiles[mapID];

        if (tpTileMap != null)
        {
            for (int i = 0; i < tpTiles.Count; i++)
            {
                TeleportTile t     = tpTiles[i].transform.GetComponent <TeleportTile>();
                int          rcver = tpTileMap[i];
                if (rcver >= 0)
                {
                    t.receiver = tpTiles[rcver];
                    tpTiles[rcver].transform.GetComponent <TeleportTile>().senders.Add(t.gameObject);
                }
            }
        }

        List <List <float> > destinations = (List <List <float> >)MapLibrary.movingTiles[mapID];
        List <Vector3>       sizes        = (List <Vector3>)MapLibrary.movingTilesSizes[mapID];

        if (destinations != null)
        {
            for (int i = 0; i < destinations.Count; i++)
            {
                MovingTile m = movingTiles[i].transform.GetComponent <MovingTile>();
                m.destinations = new List <Vector3>();

                m.transform.localScale = new Vector3(scaling * sizes[i].x, sizes[i].y, scaling * sizes[i].z);

                m.originalPosition = m.transform.position;
                List <float> coords = destinations[i];
                for (int j = 0; j < coords.Count; j += 3)
                {
                    float xScale = sizes[i].x, zScale = sizes[i].z, yScale = sizes[i].y;
                    xScale--;
                    yScale--;
                    zScale--;
                    Vector3 dest = m.originalPosition +
                                   new Vector3(coords[j], coords[j + 1], coords[j + 2]) * scaling +
                                   new Vector3(xScale, yScale, -zScale) * scaling / 2;;
                    m.destinations.Add(dest);
                }
            }
        }
        transform.name = "Map " + (mapID + 1);
        for (int i = 0; i < 4; i++)
        {
            GameObject.Find("Player " + (i + 1)).transform.position =
                new Vector3(playerSpawnPoints [i].transform.position.x,
                            2,
                            playerSpawnPoints [i].transform.position.z);
        }
    }