You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.7 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. public class PlayerControler : MonoBehaviour {
  4. public Animator animator;
  5. // Use this for initialization
  6. void Start () {
  7. }
  8. public float speed;
  9. public Vector2 jumpVector;
  10. public int health = 0;
  11. private int displayedHealth = 0;
  12. private bool updateHealth = false;
  13. private float curDirection = -1;
  14. public GameObject bullet;
  15. public GameObject heart;
  16. bool canJump = true;
  17. public GameObject newConfetti;
  18. void Update() {
  19. Vector2 velo = rigidbody2D.velocity;
  20. velo.x = Input.GetAxis ("Horizontal_P1") * speed;
  21. rigidbody2D.velocity = velo;
  22. animator.SetFloat("Velocity",velo.magnitude);
  23. animator.SetBool ("Fall", !canJump);
  24. if (curDirection != Input.GetAxisRaw ("Horizontal_P1"))
  25. applyPlayerDirection (Input.GetAxisRaw ("Horizontal_P1"));
  26. if (displayedHealth != health) {
  27. updateHealth = true;
  28. }
  29. if (updateHealth)
  30. displayHealth (health);
  31. if (health == 0)
  32. death();
  33. if (Input.GetKey (KeyCode.UpArrow)){
  34. if (canJump == true){
  35. rigidbody2D.velocity = jumpVector;
  36. canJump = false;
  37. }
  38. }
  39. //if (Input.GetKey (KeyCode.Space)){
  40. //shoot
  41. //GameObject bulletGO = Instantiate (bullet) as GameObject;
  42. //bulletGO.transform.position = transform.position;
  43. //}
  44. }
  45. private void displayHealth(int health){
  46. GameObject[] hearts = GameObject.FindGameObjectsWithTag("heartUI2");
  47. foreach (GameObject desHeart in hearts)
  48. GameObject.Destroy (desHeart);
  49. for (int i=1; i<= health; i++) {
  50. GameObject heartCanister = Instantiate (heart) as GameObject;
  51. Vector3 heartPos = new Vector3 (1-(i * 0.033f), 0.95f, 0);
  52. heartCanister.transform.position = heartPos;
  53. }
  54. displayedHealth = health;
  55. updateHealth = false;
  56. }
  57. private void applyPlayerDirection(float moveHorizontal)
  58. {
  59. if (moveHorizontal != 0)
  60. {
  61. transform.Rotate(0,180,0);
  62. curDirection = moveHorizontal;
  63. }
  64. }
  65. private void Confettishoot(Vector3 target,float size){
  66. Debug.Log ("Creating Confetti");
  67. target.y += 1.5f;
  68. for (int i = 0; i <size; i++) {
  69. Instantiate (newConfetti, target, transform.rotation);
  70. }
  71. }
  72. private void death(){
  73. if (health == 0) {
  74. Vector3 spawnPos = new Vector3 (Random.Range (-20.0f, 20.0f), 15.0f, 0);
  75. health = 3;
  76. transform.position = spawnPos;
  77. rigidbody2D.velocity = Vector2.zero;
  78. }
  79. }
  80. void OnCollisionEnter2D(Collision2D col){
  81. if (col.collider.tag == "ground")
  82. canJump = true;
  83. else if (col.collider.tag == "Player") {
  84. canJump = true;
  85. if(transform.position.y>col.transform.position.y){
  86. col.gameObject.GetComponent<player2_controls>().health --;
  87. Confettishoot(col.gameObject.transform.position,20.0f);
  88. //health --;
  89. rigidbody2D.velocity = jumpVector;
  90. canJump = false;
  91. }
  92. }
  93. }
  94. }