Skip to content
Permalink
d864e80fc3
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
410 lines (322 sloc) 13.5 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(Controller2D))]
public class Player : MonoBehaviour
{
//animation
public Animator animator; //here to better instantiate jumping
public Transform ninja; //ninja
public Transform teleporthere; //go here
//Checkpoints
public Transform ActiveCheckpoint;
public Transform Checkpoint1;
public Transform Checkpoint2;
public Transform Checkpoint3;
public Transform Checkpoint4;
float moveSpeed = 10; //How Fast He moves
float gravity; //Gravity (No Rigid Body)
float maxJumpVelocity; //maxjump velocity
float minJumpVelocity; //min jump velocity
Vector3 velocity; //Velocity (Again since using raycasting need to create alll this)
float velocityXSmoothing;
//Wall Jumping
public float wallSlideSpeedMax = 4;//speed of sliding
public float wallStickTime = .15f;
float timeToWallUnstick;
public Vector2 wallJumpClimb; //Keep going up
public Vector2 wallJumpOff;//Jump off
public Vector2 wallLeap; //leap to other side
//Jumping
public float maxJumpHeight = 4; //height
public float minJumpHeight = 1;
public float timeToJumpApex = .4f; //time to get there
float accelerationTimeAirborne = .2f; //acceleration time
float accelerationTimeGrounded = .15f; //time to
Controller2D controller; //controller
Vector2 directionalInput;
public int currentHealth; //health
private int maxHealth = 100; //max health
public HealthBar healthbar; //Healthbar
bool crouch = false;
public GameObject door; //Public so I could drag door in
public GameObject door2; //WallJump Door
public GameObject door3; //AfterKnight Wall
public GameObject door4; //Trapped in
public int lives = 3; //amount of lives
public Text LifeAmount; //text reference
public Text Score;
public int score = 0;
bool wallSliding;//need access to these in OnJump methods
int wallDirX;
void Start()
{
LifeAmount.text = "X: " + lives;
controller = GetComponent<Controller2D>(); //Get Components controller2d
ActiveCheckpoint.position = new Vector3(ActiveCheckpoint.position.x, ActiveCheckpoint.position.y, ActiveCheckpoint.position.z); //set active checkpoint
gravity = -(2 * maxJumpHeight) / Mathf.Pow(timeToJumpApex, 2); //Derived from equation
maxJumpVelocity = Mathf.Abs(gravity) * timeToJumpApex; //Derived from equation
minJumpVelocity = Mathf.Sqrt(2 * Mathf.Abs(gravity) * minJumpHeight);
currentHealth = maxHealth; //Reset to max health
healthbar.SetMaxHealth(maxHealth); //Health bar to max
}
void Update()
{
///Call the methods here for neater code
CalculateVelocity();
HandleWallSliding();
//move
controller.Move(velocity * Time.deltaTime, directionalInput);
//if there is something above or below stop moving
if (controller.collisions.above || controller.collisions.below)
{
velocity.y = 0;
animator.SetBool("IsJumping", false);
}
}
//directional input method
public void SetDirectionalInput(Vector2 input)
{
directionalInput = input;
}
public void OnJumpInputDown()
{
if (wallSliding) //if you are sliding on a wall
{
if (wallDirX == directionalInput.x) //which direction
{
velocity.x = -wallDirX * wallJumpClimb.x;
velocity.y = wallJumpClimb.y; //jumping up the wall
animator.SetBool("IsJumping", true);
}
else if (directionalInput.x == 0)
{
velocity.x = -wallDirX * wallJumpOff.x;
velocity.y = wallJumpOff.y; //jumping off the wall
animator.SetBool("IsJumping", true);
}
else
{
velocity.x = -wallDirX * wallLeap.x;
velocity.y = wallLeap.y; //leaping from one side to other
animator.SetBool("IsJumping", true);
}
}
if (controller.collisions.below) //standing
{
velocity.y = maxJumpVelocity; //jump
animator.SetBool("IsJumping", true);
}
}
public void OnJumpInputUp() //jump up method
{
if (velocity.y > minJumpVelocity)
{
velocity.y = minJumpVelocity;
animator.SetBool("IsJumping", true);
}
}
void HandleWallSliding() //wall sliding method
{
wallDirX = (controller.collisions.left) ? -1 : 1; //left or right direction
wallSliding = false; //not on a wall
if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) //if you are on a wall
{
wallSliding = true; //bool to true
if (velocity.y < -wallSlideSpeedMax) //if velocity y is negative
{
velocity.y = -wallSlideSpeedMax; //set velocity y to wall slide speed, negative cuz you slide down
}
if (timeToWallUnstick > 0)
{
velocityXSmoothing = 0;//reset x velocity smoothing
velocity.x = 0;//reset x velocity smoothing
if (directionalInput.x != wallDirX && directionalInput.x != 0) //subtract time to unstick time cuz on wall
{
timeToWallUnstick -= Time.deltaTime;
}
else //thats how long to stick on wall
{
timeToWallUnstick = wallStickTime;
}
}
else
{
timeToWallUnstick = wallStickTime;
}
}
}
void CalculateVelocity() //calculate velocity method
{
float targetVelocityX = directionalInput.x * moveSpeed; //how fast we want to do
velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne); //smoothing
velocity.y += gravity * Time.deltaTime; //velocity y
}
public GameObject dialogBox;//reference
public Text dialogText; //text
//Ontrigger Enter (AKA TAGS)
void OnTriggerEnter2D(Collider2D other) //Pickups
{
if (other.gameObject.CompareTag("Key")) //If you got the key
{
SoundManagerScript.PlaySound("Key");
other.gameObject.SetActive(false); //key is not actice
door.SetActive(false); // Door is accessible
}
if (other.gameObject.CompareTag("Key2")) //If you got the key
{
SoundManagerScript.PlaySound("Key");
other.gameObject.SetActive(false); //key is not actice
door2.SetActive(false); // Door is accessible
}
if (other.gameObject.CompareTag("Key3")) //If you got the key
{
SoundManagerScript.PlaySound("Key");
other.gameObject.SetActive(false); //key is not actice
door3.SetActive(false); // Door is accessible
}
if (other.gameObject.CompareTag("AntiKey")) //If you got the key
{
SoundManagerScript.PlaySound("Laugh");
other.gameObject.SetActive(false); //key is not actice
door4.SetActive(true); // Door is blocking
}
if (other.gameObject.CompareTag("teleport")) //If you got the key
{
SoundManagerScript.PlaySound("teleport");
ninja.position = new Vector3(ninja.position.x, ninja.position.y, ninja.position.z);
teleporthere.position = new Vector3(teleporthere.position.x, teleporthere.position.y, teleporthere.position.z);
ninja.position = new Vector3(teleporthere.position.x, teleporthere.position.y, teleporthere.position.z);
}
if (other.gameObject.CompareTag("Heart"))
{
SoundManagerScript.PlaySound("Heart");
other.gameObject.SetActive(false); // heart dissapears
if (currentHealth == 100)
{
lives = lives + 1;
LifeAmount.text = "X: " + lives;
}
if (currentHealth < maxHealth)
{
currentHealth = maxHealth; // Full Health
healthbar.SetHealth(currentHealth); //Full HealthBar
}
}
if (other.gameObject.CompareTag("Enemy")) //take damage
{
TakeDamage(20);
score = score - 10;
}
if (other.gameObject.CompareTag("Bee")) //take damage
{
TakeDamage(10);
score = score - 10;
}
if (other.gameObject.CompareTag("Spikes")) //take damage
{
TakeDamage(10);
score = score - 10;
other.gameObject.SetActive(false);
}
if (other.gameObject.CompareTag("Fall")) //Fell Off Map
{
TakeDamage(100);
}
if (other.gameObject.CompareTag("Score"))
{
score = score + 10;
SoundManagerScript.PlaySound("Collecting");
other.gameObject.SetActive(false);
Score.text = "X: " + score;
if (score == 250 || score == 500 || score == 750 || score == 1000 || score == 1250 || score == 1500 || score == 1750 || score == 2000)
{
SoundManagerScript.PlaySound("Heart");
lives = lives + 1;
LifeAmount.text = "X: " + lives;
}
}
///Sign tags, they turn off after a certain distance
if (other.gameObject.CompareTag("Sign0"))
{
dialogBox.SetActive(true);
dialogText.text = "Space to Jump\n" + "Arrows or ASD to move\n" + "J to attack\n" + "Esc to pause";
}
if (other.gameObject.CompareTag("Sign1"))
{
dialogBox.SetActive(true);
dialogText.text = "K to throw Shurikens\n" + "Some Objects break..\n" + "Mysterious looking box..";
}
if (other.gameObject.CompareTag("Sign2"))
{
dialogBox.SetActive(true);
dialogText.text = "Every 250 score you get another life\n" + "Enemies drop score points\n" + "Lose points if you get hurt";
}
if (other.gameObject.CompareTag("Sign3"))
{
dialogBox.SetActive(true);
dialogText.text = "The Katana is very powerful\n" +"The Shurikens are very fast\n"+"Be wise with what you use";
}
if (other.gameObject.CompareTag("Sign4"))
{
dialogBox.SetActive(true);
dialogText.text = "These forests are infested\n" + "Clear it to keep your village safe\n" + "Almost Home..";
}
if (other.gameObject.CompareTag("SignOff")) //Turn off dialogue box
{
dialogBox.SetActive(false);
}
if (other.gameObject.CompareTag("Checkpoint 1")) //Set active checkpoint
{
SoundManagerScript.PlaySound("Collecting");
Checkpoint1.position = new Vector3(Checkpoint1.position.x, Checkpoint1.position.y, Checkpoint1.position.z);
ActiveCheckpoint.position = new Vector3(Checkpoint1.position.x, Checkpoint1.position.y, Checkpoint1.position.z);
}
if (other.gameObject.CompareTag("Checkpoint 2")) //set active checkpoint
{
SoundManagerScript.PlaySound("Collecting");
Checkpoint2.position = new Vector3(Checkpoint2.position.x, Checkpoint2.position.y, Checkpoint2.position.z);
ActiveCheckpoint.position = new Vector3(Checkpoint2.position.x, Checkpoint2.position.y, Checkpoint2.position.z);
}
if (other.gameObject.CompareTag("Checkpoint 3")) //set active checkpoint
{
SoundManagerScript.PlaySound("Collecting");
Checkpoint3.position = new Vector3(Checkpoint3.position.x, Checkpoint3.position.y, Checkpoint3.position.z);
ActiveCheckpoint.position = new Vector3(Checkpoint3.position.x, Checkpoint3.position.y, Checkpoint3.position.z);
}
if (other.gameObject.CompareTag("Checkpoint 4")) //set active checkpoint
{
SoundManagerScript.PlaySound("Collecting");
Checkpoint4.position = new Vector3(Checkpoint4.position.x, Checkpoint4.position.y, Checkpoint4.position.z);
ActiveCheckpoint.position = new Vector3(Checkpoint4.position.x, Checkpoint4.position.y, Checkpoint4.position.z);
}
//Ninja star is in the weapon script (easier that way for some reason )
}
//Damage Function
void TakeDamage(int damage)
{
animator.SetTrigger("Hurt");
SoundManagerScript.PlaySound("Hurt");
currentHealth -= damage; //take damage
healthbar.SetHealth(currentHealth); //modify health bar
if (currentHealth < 1)
{
SoundManagerScript.PlaySound("Laugh");
if (lives > 0) //more lifes to live
{
//change for checkpoints
lives = lives - 1; //1 less life
LifeAmount.text = "X: " + lives;
ninja.position = new Vector3(ActiveCheckpoint.position.x, ActiveCheckpoint.position.y, ActiveCheckpoint.position.z);
currentHealth = 100;
healthbar.SetHealth(currentHealth);
}
if (lives == 0)
{
SceneManager.LoadScene("GameOver");
}
}
}
}