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.

104 lines
2.6 KiB

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