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.

97 lines
2.3 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. private Vector3 startPoint;
  15. public GameObject boat;
  16. void Start() {
  17. NotificationServer.register("show GameUI", showGameUI);
  18. NotificationServer.register("hide GameUI", hideGameUI);
  19. instance = this;
  20. startPoint = boat.transform.position;
  21. }
  22. // Update is called once per frame
  23. void Update () {
  24. if (Input.GetButtonDown("Fire1") && !isLocked) {
  25. Debug.Log("Click");
  26. RaycastHit hit;
  27. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  28. LayerMask rayMask = (1 << 4);
  29. Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
  30. if (Physics.Raycast(ray, out hit, Mathf.Infinity,rayMask)) {
  31. WaterController waterScript = hit.collider.gameObject.GetComponent<WaterController>();
  32. Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red,1);
  33. Debug.Log("I hit: " + hit.transform.name);
  34. if (waterScript != null)
  35. waterScript.CreateWave(hit.point, radius, power);
  36. }
  37. }
  38. }
  39. public void setLife(int life) {
  40. for (int i = 0; i < lifeHearts.Length; i++) {
  41. lifeHearts[i].SetActive((i < life));
  42. }
  43. this.life = life;
  44. }
  45. public void takeOneDamage() {
  46. if (Time.time > lastDamageTime + 2 && life > 0) {
  47. setLife(life - 1);
  48. lastDamageTime = Time.time;
  49. if (life == 0)
  50. NotificationServer.notify("show GameOverPanel");
  51. }
  52. }
  53. private void showGameUI() {
  54. Debug.Log("UI open");
  55. isLocked = false;
  56. }
  57. private void hideGameUI() {
  58. Debug.Log("UI close");
  59. isLocked = true;
  60. }
  61. public void restartScene() {
  62. setLife(3);
  63. boat.transform.position = startPoint;
  64. boat.GetComponent<Rigidbody>().velocity = Vector3.zero;
  65. }
  66. }