using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour {
|
|
|
|
public bool isLocked = false;
|
|
|
|
public float power;
|
|
public float radius;
|
|
public float time;
|
|
|
|
public GameObject waveRing;
|
|
|
|
public int life = 3;
|
|
public float lastDamageTime = 0;
|
|
|
|
public static PlayerController instance;
|
|
|
|
public GameObject[] lifeHearts;
|
|
|
|
private Vector3 startPoint;
|
|
public GameObject boat;
|
|
|
|
|
|
void Start() {
|
|
|
|
NotificationServer.register("show GameUI", showGameUI);
|
|
NotificationServer.register("hide GameUI", hideGameUI);
|
|
|
|
instance = this;
|
|
startPoint = boat.transform.position;
|
|
isLocked = true;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update () {
|
|
|
|
if (Input.GetButtonDown("Fire1") && !isLocked) {
|
|
// Debug.Log("Click");
|
|
RaycastHit hit;
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
|
|
LayerMask rayMask = (1 << 4);
|
|
|
|
Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
|
|
|
|
if (Physics.Raycast(ray, out hit, Mathf.Infinity,rayMask)) {
|
|
|
|
WaterController waterScript = hit.collider.gameObject.GetComponent<WaterController>();
|
|
Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red,1);
|
|
|
|
// Debug.Log("I hit: " + hit.transform.name);
|
|
if (waterScript != null)
|
|
waterScript.CreateWave(hit.point, radius, power);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void setLife(int life) {
|
|
for (int i = 0; i < lifeHearts.Length; i++) {
|
|
lifeHearts[i].SetActive((i < life));
|
|
}
|
|
this.life = life;
|
|
}
|
|
|
|
public void takeOneDamage() {
|
|
if (Time.time > lastDamageTime + 2 && life > 0) {
|
|
setLife(life - 1);
|
|
lastDamageTime = Time.time;
|
|
if (life == 0)
|
|
{
|
|
NotificationServer.notify("fade bgm");
|
|
NotificationServer.notify("play sfx", "shipSpotted:0.5");
|
|
NotificationServer.notify("play sfx", "sinkingShip:0.75");
|
|
NotificationServer.notify("show GameOverPanel");
|
|
NotificationServer.notify("hide GameUI");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private void showGameUI() {
|
|
// Debug.Log("UI open");
|
|
isLocked = false;
|
|
}
|
|
|
|
private void hideGameUI() {
|
|
// Debug.Log("UI close");
|
|
isLocked = true;
|
|
}
|
|
|
|
public void restartScene() {
|
|
setLife(3);
|
|
boat.transform.position = startPoint;
|
|
boat.GetComponent<Rigidbody>().velocity = Vector3.zero;
|
|
NotificationServer.notify("restart scene");
|
|
}
|
|
|
|
}
|