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.

114 lines
2.9 KiB

7 years ago
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 float cooldownWaveCreate = 0.5f;
  6. private float timeOfLastWave;
  7. public bool isLocked = false;
  8. public float power;
  9. public float radius;
  10. public float time;
  11. public GameObject waveRing;
  12. public ParticleSystem boatHitParticle;
  13. public int life = 3;
  14. public float lastDamageTime = 0;
  15. public static PlayerController instance;
  16. public GameObject[] lifeHearts;
  17. private Vector3 startPoint;
  18. public GameObject boat;
  19. void Start() {
  20. NotificationServer.register("show GameUI", showGameUI);
  21. NotificationServer.register("hide GameUI", hideGameUI);
  22. instance = this;
  23. startPoint = boat.transform.position;
  24. isLocked = true;
  25. }
  26. // Update is called once per frame
  27. void Update () {
  28. if (Input.GetButtonDown("Fire1") && !isLocked) {
  29. if (cooldownWaveCreate > Time.timeSinceLevelLoad - timeOfLastWave)
  30. return;
  31. timeOfLastWave = Time.timeSinceLevelLoad;
  32. // Debug.Log("Click");
  33. RaycastHit hit;
  34. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  35. LayerMask rayMask = (1 << 4);
  36. Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
  37. if (Physics.Raycast(ray, out hit, Mathf.Infinity,rayMask)) {
  38. WaterController waterScript = hit.collider.gameObject.GetComponent<WaterController>();
  39. Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red,1);
  40. // Debug.Log("I hit: " + hit.transform.name);
  41. if (waterScript != null)
  42. waterScript.CreateWave(hit.point, radius, power);
  43. }
  44. }
  45. }
  46. public void setLife(int life) {
  47. for (int i = 0; i < lifeHearts.Length; i++) {
  48. lifeHearts[i].SetActive((i < life));
  49. }
  50. this.life = life;
  51. }
  52. public void takeOneDamage() {
  53. // NotificationServer.notify("shake camera");
  54. NotificationServer.notify("flash damage");
  55. boatHitParticle.Play();
  56. if (Time.time > lastDamageTime + 2 && life > 0) {
  57. setLife(life - 1);
  58. lastDamageTime = Time.time;
  59. if (life == 0)
  60. {
  61. NotificationServer.notify("fade bgm");
  62. NotificationServer.notify("play sfx", "shipSpotted:0.5");
  63. NotificationServer.notify("play sfx", "sinkingShip_2:0.75");
  64. NotificationServer.notify("show GameOverPanel");
  65. NotificationServer.notify("hide GameUI");
  66. }
  67. }
  68. }
  69. private void showGameUI() {
  70. // Debug.Log("UI open");
  71. isLocked = false;
  72. }
  73. private void hideGameUI() {
  74. // Debug.Log("UI close");
  75. isLocked = true;
  76. }
  77. public void restartScene() {
  78. setLife(3);
  79. boat.transform.position = startPoint;
  80. boat.GetComponent<Rigidbody>().velocity = Vector3.zero;
  81. NotificationServer.notify("restart scene");
  82. }
  83. }