/Assets/Scripts/playerHitScript.cs

https://bitbucket.org/Feriluce/puzzlephysics · C# · 155 lines · 98 code · 25 blank · 32 comment · 26 complexity · 90940afb57902fa99c573f3eb24d4064 MD5 · raw file

  1. using UnityEngine;
  2. //using System.Collections;
  3. using System;
  4. //I know this script is code duplication and bad, but it seems appropriate to have a seperate script for this,
  5. //as monsters and objects doesn't have health.
  6. public class playerHitScript : MonoBehaviour {
  7. public int maxHealth;
  8. [HideInInspector]
  9. public int currentHealth;
  10. public string plane = "front";
  11. int platDist = 10; //Current distance between platforms. Change if distance changes.
  12. PlatformerController platcontrol;
  13. CharacterController controller;
  14. public float mass = 3.0f; // define the character mass
  15. Vector3 impact = Vector3.zero;
  16. PlayerAnimator playerAnimator;
  17. public int meleeDamageTakenFromEnemies = 50;
  18. public float howOftenIsTheMeleeDamage = 0.5f;
  19. private float timeSinceLastCollision = 1.0f;
  20. private bool inMenu = false;
  21. // Use this for initialization
  22. void Start () {
  23. currentHealth = maxHealth;
  24. platcontrol = GetComponent<PlatformerController>();
  25. controller = GetComponent<CharacterController>();
  26. playerAnimator = transform.GetComponentInChildren<PlayerAnimator>();
  27. }
  28. // Update is called once per frame
  29. void Update () {
  30. if(currentHealth <= 0){
  31. currentHealth = maxHealth;
  32. platcontrol.OnDeath();
  33. }
  34. // apply the impact effect:
  35. if (impact.magnitude > 0.2f){
  36. controller.Move(impact * Time.deltaTime);
  37. }
  38. // impact energy goes by over time:
  39. impact = Vector3.Lerp(impact, Vector3.zero, 10*Time.deltaTime);
  40. if (Input.GetKeyDown("escape")){
  41. if(!inMenu){
  42. Time.timeScale = 0.0f;
  43. inMenu = true;
  44. } else {
  45. Time.timeScale = 1.0f;
  46. inMenu = false;
  47. }
  48. }
  49. }
  50. void OnCollisionEnter(Collision col){
  51. foreach(ContactPoint contact in col.contacts){
  52. if(contact.otherCollider.CompareTag("teleportProjectile") && contact.otherCollider.gameObject.layer != 12){
  53. if(plane == "front" && !platcontrol.capsuleCheck(10)){
  54. transform.Translate(Vector3.forward*platDist, Space.World);
  55. platcontrol.planeOfExistence = plane = "back";
  56. } else if(!platcontrol.capsuleCheck(-10)){
  57. transform.Translate(Vector3.forward*(-platDist), Space.World);
  58. platcontrol.planeOfExistence = plane = "front";
  59. }
  60. playerAnimator.hitAnimation();
  61. }
  62. else //this is so player doesn't shoot himself.
  63. if(contact.otherCollider.CompareTag("dmgProjectile") && contact.otherCollider.gameObject.layer != 12){
  64. projectileScript pScript = contact.otherCollider.GetComponent<projectileScript>();
  65. currentHealth -= pScript.damage;
  66. AddImpact(transform.position - contact.otherCollider.transform.position);
  67. playerAnimator.hitAnimation();
  68. }
  69. else
  70. if(Time.time - timeSinceLastCollision > howOftenIsTheMeleeDamage
  71. && checkEnemy(contact.otherCollider.transform) && contact.otherCollider.gameObject.layer != 16){
  72. currentHealth -= meleeDamageTakenFromEnemies;
  73. AddImpact(transform.position - contact.otherCollider.transform.position);
  74. playerAnimator.hitAnimation();
  75. }
  76. //Debug.Log ("contact.otherCollider.gameObject.layer: " +contact.otherCollider.gameObject.layer);
  77. }
  78. }
  79. public void dealDamage(int howMuch, Vector3 otherColliderPosition, bool force){
  80. currentHealth -= howMuch;
  81. AddImpact(transform.position - otherColliderPosition);
  82. transform.position = new Vector3(transform.position.x, transform.position.y+0.8f, transform.position.z);
  83. if(force)
  84. playerAnimator.hitAnimationForce();
  85. else
  86. playerAnimator.hitAnimation();
  87. }
  88. void OnGUI(){
  89. //GUI.Box(new Rect(Screen.width - 100, 0, 100, 25), currentHealth.ToString());
  90. GUI.HorizontalScrollbar(new Rect (Screen.width/2-100,Screen.height-80,200,20), 0, currentHealth,0, 100);
  91. if(inMenu){
  92. if(GUI.Button(new Rect(Screen.width/2 - 150, Screen.height/2 - 33, 300, 66), "Quit to Menu")){
  93. Time.timeScale = 1.0f;
  94. Application.LoadLevel(8);
  95. }
  96. }
  97. }
  98. void AddImpact(Vector3 force){ // CharacterController version of AddForce
  99. impact += 50*(force / mass);
  100. }
  101. bool checkEnemy(Transform trans){
  102. if(trans.GetComponent<teleportObjectScriptWithinParent>()) {
  103. timeSinceLastCollision = Time.time;
  104. return true;
  105. } else return false;
  106. }
  107. /*
  108. int n = 0;
  109. bool checkEnemy(Transform trans){
  110. n++;
  111. teleportObjectScriptWithinParent parentScript = null;
  112. Debug.Log ("testing against: " + trans.name);
  113. try{
  114. parentScript = trans.GetComponent<teleportObjectScriptWithinParent>();
  115. }
  116. catch( Exception e ){
  117. }
  118. if(n > 5){
  119. return false;
  120. } else if(parentScript) {
  121. return true;
  122. } else {
  123. return checkEnemy(trans.parent);
  124. }
  125. }
  126. */
  127. }