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.

86 lines
2.0 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerController : MonoBehaviour {
  5. public bool isLocked = false;
  6. public float power;
  7. public float radius;
  8. public float time;
  9. public GameObject waveRing;
  10. public int life = 3;
  11. public float lastDamageTime = 0;
  12. public static PlayerController instance;
  13. public GameObject[] lifeHearts;
  14. void Start() {
  15. NotificationServer.register("show GameUI", showGameUI);
  16. NotificationServer.register("hide GameUI", hideGameUI);
  17. instance = this;
  18. }
  19. // Update is called once per frame
  20. void Update () {
  21. if (Input.GetButtonDown("Fire1") && !isLocked) {
  22. Debug.Log("Click");
  23. RaycastHit hit;
  24. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  25. LayerMask rayMask = (1 << 4);
  26. Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
  27. if (Physics.Raycast(ray, out hit, Mathf.Infinity,rayMask)) {
  28. WaterController waterScript = hit.collider.gameObject.GetComponent<WaterController>();
  29. Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red,1);
  30. Debug.Log("I hit: " + hit.transform.name);
  31. if (waterScript != null)
  32. waterScript.CreateWave(hit.point, radius, power);
  33. }
  34. }
  35. }
  36. public void setLife(int life) {
  37. for (int i = 0; i < lifeHearts.Length; i++) {
  38. lifeHearts[i].SetActive((i < life));
  39. }
  40. this.life = life;
  41. }
  42. public void takeOneDamage() {
  43. if (Time.time > lastDamageTime + 2 && life > 0) {
  44. setLife(life - 1);
  45. lastDamageTime = Time.time;
  46. if (life == 0)
  47. NotificationServer.notify("show GameOverPanel");
  48. }
  49. }
  50. private void showGameUI() {
  51. Debug.Log("UI open");
  52. isLocked = false;
  53. }
  54. private void hideGameUI() {
  55. Debug.Log("UI close");
  56. isLocked = true;
  57. }
  58. }