// Logic for the player. private void UpdatePlayer() { // if the player is dead (replaced with ragdoll) then exit since none of this code should fire. if (m_player == null) { return; } // AUTO PLAYER FOR TESTING // Allows the AI to control the player. Used for debugging mainly. // m_playerNextDirection = m_playerCell.CellDirection; // if (m_playerCell.CellType == enCellType.DuckObstacle && m_playerSlide <=0.0f && m_playerTimer <0.25f) // { // m_playerSlide = 1.0f; // } // else if (m_playerCell.CellType != enCellType.Run && m_playerCell.CellType != enCellType.DuckObstacle && m_playerJump <=-1.0f && m_playerTimer >=0.2f && m_playerSlide <=0.0f) // { // m_playerJump = 1.0f; // } // Gradually increase the players' running speed, and update the animation to match. m_playerRunSpeed += Time.deltaTime * 0.005f; m_playerRunSpeed = Mathf.Clamp(m_playerRunSpeed, 0.5f, 3.0f); m_player.animation["run"].speed = m_playerRunSpeed * 2.0f; // **************************************************************************************** // INPUT // Player can only turn if they are not already sliding / jumping. // Equally, sliding / jumping are mutually exclusive. if (Input.GetKeyDown(KeyCode.LeftArrow) && m_playerJump <= -1.0f && m_playerSlide <=0.0f) { if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.West; if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.North; if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.East; if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.South; } if (Input.GetKeyDown(KeyCode.RightArrow) && m_playerJump <= -1.0f && m_playerSlide <=0.0f) { if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.East; if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.South; if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.West; if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.North; } if (Input.GetKeyDown(KeyCode.DownArrow) && m_playerJump <= -1.0f && m_playerSlide <=0.0f) { m_playerSlide = 1.0f; m_player.animation.Play("slide_fake"); } if ((Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space)) && m_playerJump <= -1.0f && m_playerSlide <=0.0f) { AudioSource.PlayClipAtPoint(m_jumpAudio, m_player.transform.position); m_playerJump = 1.0f; m_playerYvel = 0.0f; m_player.animation.Play("jump"); } // **************************************************************************************** // Reset animation if not jumping, sliding, or stumbling. if (m_playerJump <=-1.0f && m_playerSlide <=0.0f) { if (!m_player.animation.IsPlaying("flip")) m_player.animation.Play("run"); } // If we are stumbling, make the camera shake a little. if (m_stumble > 0.0f) { m_stumble -= Time.deltaTime * 2.0f; Camera.mainCamera.transform.Rotate (Vector3.up, Random.Range(m_stumble * -5.0f, m_stumble * 5.0f)); } // Increase the players progress across the current cell, taking into accound current run speed and stumbling m_playerTimer += Time.deltaTime * (m_stumble > 0.0f ? m_playerRunSpeed / 4.0f : m_playerRunSpeed); // If we reach the end of a cell, we need to move to the next one (or possibly die!) if (m_playerTimer >= 1.0f) { m_distanceRun += 10.0f; m_playerTimer = 0.0f; m_previousCellDirection = m_playerCell.CellDirection; // Determine which cell to move the player to. switch (m_playerCell.CellDirection) { case enCellDir.North: m_playerCell = m_playerCell.NeighbourN; break; case enCellDir.South: m_playerCell = m_playerCell.NeighbourS; break; case enCellDir.East: m_playerCell = m_playerCell.NeighbourE; break; case enCellDir.West: m_playerCell = m_playerCell.NeighbourW; break; } } // Tell the current cell it's been visited, so it can be removed later. m_playerCell.Visited = true; // If current cell is unspecified (usually on first run) then reset some stuff. if (m_playerCell == null) { m_playerCell = m_cells[0]; m_playerDirection = enCellDir.North; m_playerNextDirection = enCellDir.North; m_previousCellDirection = enCellDir.North; } #region Obstacles // here, we check for the various ways in which the player can screw up, and die. // Is the current cell an obstacle that should be ducked under? if (m_playerCell.CellType == enCellType.DuckObstacle) { if (m_playerTimer >= 0.4f && m_playerTimer <= 0.6f) { if (m_playerJump >-1.0f || m_playerSlide <=0.0f) { if (m_playerSlide <=0.0f && m_playerJump <=-1.0f) { m_player.transform.Translate(Vector3.up * 0.1f); } DoRagDoll(true, false, m_splatAudio); m_dieReason = "Ouch! Migraine. Duck next time!"; } } } // Is the current cell an obstacle that should be jumped (boxes in this case)? if (m_playerCell.CellType == enCellType.JumpObstacle && m_playerJump <= -1.0f && m_playerTimer >= 0.45f && m_playerTimer <= 0.55f && m_stumble <=0.0f) { DoRagDoll(true, false, m_splatAudio); m_dieReason = "Don't forget to jump. Boxes hurt."; } // Are we on a narrow ledge, and not leaning towards the opposite side? if (m_playerCell.CellType == enCellType.LedgeLeft) { if (m_tilt >=-0.05f && m_playerTimer >=0.3f && m_playerTimer <=0.6f && m_playerJump <=-1.0f) { DoRagDoll(false, false, m_playerCell.CellTheme == enCellTheme.Stone ? m_splatAudio : m_fallAudio); m_dieReason = "Watch your footing! Use the mouse to slide to the side."; } } // Are we on a narrow ledge, and not leaning towards the opposite side? if (m_playerCell.CellType == enCellType.LedgeRight) { if (m_tilt <=0.05f && m_playerTimer >=0.3f && m_playerTimer <=0.6f && m_playerJump <=-1.0f) { DoRagDoll(false, false, m_playerCell.CellTheme == enCellTheme.Stone ? m_splatAudio : m_fallAudio); m_dieReason = "Watch your footing! Use the mouse to slide to the side."; } } // Should we be jumping over a gap? if (m_playerCell.CellType == enCellType.JumpGap && m_playerJump <=-1.0f) { if (m_playerTimer > 0.3f && m_playerTimer < 0.7f) { if (m_playerTimer < 0.4f) DoRagDoll(false, false, m_fallAudio); if (m_playerTimer >= 0.4f) DoRagDoll(false, false, m_splatAudio); m_dieReason = "Swan dive with triple bone-crunch! (don't forget to jump at the right time...)"; } } //If we are on a straight, and the player has changed direction, we should "stumble" if ( (m_previousCellDirection == m_playerCell.CellDirection) && (m_playerCell.CellDirection != m_playerNextDirection) && m_stumble <=0.0f) { m_stumble = 1.0f; m_playerNextDirection = m_playerDirection; AudioSource.PlayClipAtPoint(m_stumbleAudio, m_player.transform.position); m_player.animation.Blend("flip"); } // Change Player Direction if we are on a turn cell if (m_playerTimer >= 0.5f && m_previousCellDirection != m_playerCell.CellDirection) { // If we haven't already if (m_playerDirection != m_playerNextDirection) { // Set player direction to player next direction (from input) m_playerDirection = m_playerNextDirection; AudioSource.PlayClipAtPoint(m_turnAudio, m_player.transform.position); } // If player is not travelling in the correct direction, then we destroy player and create ragdoll if (m_playerDirection != m_playerCell.CellDirection) { if (m_playerJump > -1.0f) { DoRagDoll(true, false, m_fallAudio); m_dieReason = "Wheeeeeeeee!"; } else { DoRagDoll(false, false, m_splatAudio); m_dieReason = "Good job your face was there to take the brunt of the impact."; } } } #endregion // This is a cheap trick as we didn't have a slide animation. Simply put the character on his back and play a mini-jump animation. // Hey, if it works... float xRot = m_playerSlide >0.0f ? -70.0f : 0.0f; // Change Player Rotation if (m_playerDirection == enCellDir.North) m_player.transform.rotation = Quaternion.Euler(xRot,000,0); if (m_playerDirection == enCellDir.South) m_player.transform.rotation = Quaternion.Euler(xRot,180,0); if (m_playerDirection == enCellDir.East ) m_player.transform.rotation = Quaternion.Euler(xRot,090,0); if (m_playerDirection == enCellDir.West ) m_player.transform.rotation = Quaternion.Euler(xRot,270,0); // Update player position Vector3 pos = m_playerCell.CellPosition; float offset = (m_playerTimer * m_cellSpacing) - (m_cellSpacing / 2.0f); switch (m_playerDirection) { case enCellDir.North: pos.z += offset; break; case enCellDir.South: pos.z -= offset; break; case enCellDir.East: pos.x += offset; break; case enCellDir.West: pos.x -= offset; break; } pos.y = m_playerBaseHeight; // Do Jumping if (m_playerJump > -1.0f) { // This controls how fast the jump happens // Tweak at your peril. Too long, jumps are easy, too short, you'll never make it. m_playerJump -= Time.deltaTime * (m_playerRunSpeed * 3.5f); m_playerYvel += m_playerJump; // This controls how high the player jumps. Has no effect on gameplay. pos.y += m_playerYvel * 0.05f; } // Do Sliding if (m_playerSlide >0.0f) { m_playerSlide -= Time.deltaTime* (m_playerRunSpeed * 1.5f); } // Set the player's position taking everything above into account. m_player.transform.position = pos; // Strafing, based on mouse input (to simulate tilting the phone, ala Temple Run). m_tilt += Input.GetAxis("Mouse X") * 0.035f; m_tilt = Mathf.Clamp(m_tilt, -0.35f, 0.35f); m_tilt = Mathf.Lerp(m_tilt, 0.0f, Time.deltaTime * 2.0f); m_player.transform.Translate(Vector3.right * m_tilt); }
// CTOR public stCell(Vector3 cellPosition, enCellDir cellDirection, enCellType cellType) { this.CellPosition = cellPosition; this.CellDirection = cellDirection; this.CellType = cellType; this.CellTheme = csTempleRun.enCellTheme.Stone; this.NeighbourN = null; this.NeighbourS = null; this.NeighbourE = null; this.NeighbourW = null; }
// Set / reset the player at the start of each run. private void CreatePlayer() { m_player = (GameObject)Instantiate(m_playerPrefab); m_playerRunSpeed = m_playerRunSpeedStart; m_player.animation["run" ].speed = m_playerRunSpeed * 2.0f; m_player.animation["jump"].speed = 1.5f; m_playerCell = m_cells[0]; m_playerDirection = enCellDir.North; m_playerNextDirection = enCellDir.North; m_previousCellDirection = enCellDir.North; m_playerJump = -1.0f; m_playerYvel = 0.0f; m_playerSlide = 0.0f; if (m_distanceRun > m_distanceRunBest) m_distanceRunBest = m_distanceRun; m_distanceRun = 0.0f; if (m_coinsCollected > m_coinsCollectedBest) m_coinsCollectedBest = m_coinsCollected; m_coinsCollected = 0; if (m_playerRagDoll != null) Destroy(m_playerRagDoll); }