Player.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public enum PowerLevelLabel
  6. {
  7. Low,
  8. Mid,
  9. High
  10. }
  11. public class Player : MonoBehaviour
  12. {
  13. [SerializeField]
  14. private float powerLevel;
  15. private Color _powerLevelLow = Color.red;
  16. private Color _powerLevelMid = new Color(255, 165, 0);
  17. private Color _powerLevelHigh = Color.green;
  18. private GameObject[] _powerUp = new GameObject[3];
  19. private GameObject[] _powerDown = new GameObject[3];
  20. public static AudioSource AudioSource;
  21. public AudioClip shootSound;
  22. public float PowerLevel
  23. {
  24. get
  25. {
  26. return powerLevel;
  27. }
  28. set
  29. {
  30. float offset = value - powerLevel;
  31. powerLevel = value;
  32. powerLevel = Mathf.Clamp(powerLevel, 0, powerLevelMax);
  33. UpdatePowerBar();
  34. UpdatePowerIndicator(offset);
  35. }
  36. }
  37. public float PowerFactor
  38. {
  39. get
  40. {
  41. return powerLevel / powerLevelMax;
  42. }
  43. }
  44. public PowerLevelLabel PowerLevelName
  45. {
  46. get
  47. {
  48. if (PowerFactor >= 0 && PowerFactor < 1.0f / 3.0f)
  49. {
  50. return PowerLevelLabel.Low;
  51. }
  52. if (PowerFactor >= 1.0f / 3.0f && PowerFactor < 2.0f / 3.0f)
  53. {
  54. return PowerLevelLabel.Mid;
  55. }
  56. if (PowerFactor >= 2.0f / 3.0f && PowerFactor <= 1.0f)
  57. {
  58. return PowerLevelLabel.High;
  59. }
  60. return PowerLevelLabel.Low;
  61. }
  62. }
  63. private Color _powerLevelColor
  64. {
  65. get
  66. {
  67. switch (PowerLevelName)
  68. {
  69. case PowerLevelLabel.Low:
  70. {
  71. return _powerLevelLow;
  72. };
  73. case PowerLevelLabel.Mid:
  74. {
  75. return _powerLevelMid;
  76. };
  77. case PowerLevelLabel.High:
  78. {
  79. return _powerLevelHigh;
  80. };
  81. }
  82. return Color.white;
  83. }
  84. }
  85. private void UpdatePowerBar()
  86. {
  87. gauge.Find("PowerBar").transform.localScale = new Vector3(1, PowerFactor, 1);
  88. gauge.GetComponentInChildren<Renderer>().material.color = _powerLevelColor;
  89. }
  90. float goodIndicatorTimeOut = 0;
  91. float badIndicatorTimeOut = 0;
  92. private void UpdatePowerIndicator(float offset)
  93. {
  94. if (offset < 0)
  95. {
  96. if (Mathf.Abs(offset) > 0)
  97. _powerDown[2].SetActive(true);
  98. if (Mathf.Abs(offset) >= bulletDegenRate)
  99. _powerDown[1].SetActive(true);
  100. if (Mathf.Abs(offset) >= 0.95)
  101. _powerDown[0].SetActive(true);
  102. badIndicatorTimeOut = 1.5f;
  103. }
  104. else
  105. {
  106. if (Mathf.Abs(offset) > 0)
  107. _powerUp[2].SetActive(true);
  108. if (Mathf.Abs(offset) > 0.45)
  109. _powerUp[1].SetActive(true);
  110. if (Mathf.Abs(offset) > 0.95)
  111. _powerUp[0].SetActive(true);
  112. goodIndicatorTimeOut = 1.5f;
  113. }
  114. }
  115. private void UpdatePowerIndicatorTimeout(ref float timeOut, GameObject[] powerIndicators)
  116. {
  117. if (timeOut > 0)
  118. {
  119. timeOut -= Time.deltaTime;
  120. }
  121. if (timeOut < 0)
  122. {
  123. for (int i = 0; i < 3; i++)
  124. {
  125. powerIndicators[i].SetActive(false);
  126. }
  127. timeOut = 0;
  128. }
  129. }
  130. private float walkingDegenTimer = 1.5f;
  131. private void UpdateWalkingDegen(float speed)
  132. {
  133. if (speed > 0.1)
  134. {
  135. if (walkingDegenTimer == 0)
  136. {
  137. PowerLevel -= movementDegenRate;
  138. walkingDegenTimer = 1.5f;
  139. }
  140. if (walkingDegenTimer > 0)
  141. {
  142. walkingDegenTimer -= Time.deltaTime;
  143. }
  144. if (walkingDegenTimer < 0)
  145. {
  146. walkingDegenTimer = 0f;
  147. }
  148. }
  149. if (speed <= 0.1)
  150. {
  151. walkingDegenTimer = 0;
  152. }
  153. //Debug.Log(speed + ", " + walkingDegenTimer);
  154. }
  155. public float movementDegenRate = 0.1f;
  156. public float bulletDegenRate = 0.2f;
  157. public float startSpeed = 5f;
  158. public float lookSmoothingSeconds = 0.05f;
  159. public float cameraSmoothingSeconds = 1;
  160. public float bulletUpBias = 0.1f;
  161. public float bulletForce = 10f;
  162. public float powerLevelMax = 8.0f;
  163. public Transform gunTransform;
  164. public Rigidbody bulletPrefab;
  165. public Light muzzleLight;
  166. public Light headLight;
  167. public Transform gauge;
  168. public DayTimeManager dayTimeManager;
  169. private Vector3 inputDirection;
  170. private float speed;
  171. private Vector3 lookDirection;
  172. private float deadZone = 0.1f;
  173. private Vector3 lookVelocity;
  174. private Vector3 cameraOffset;
  175. private Vector3 cameraVelocity;
  176. private bool shooting = false;
  177. private float lightIntensity;
  178. private bool useMouseLook;
  179. // Use this for initialization
  180. void Start()
  181. {
  182. speed = startSpeed;
  183. cameraOffset = Camera.main.transform.position - transform.position;
  184. lightIntensity = muzzleLight.intensity;
  185. muzzleLight.intensity = 0;
  186. var joystickNames = Input.GetJoystickNames();
  187. useMouseLook = joystickNames.Length == 0 || string.IsNullOrEmpty(joystickNames[0]);
  188. //if (useMouseLook)
  189. //{
  190. // lookSmoothingSeconds = 0;
  191. //}
  192. _powerUp[0] = gauge.Find("PowerUp1").gameObject;
  193. _powerUp[1] = gauge.Find("PowerUp2").gameObject;
  194. _powerUp[2] = gauge.Find("PowerUp3").gameObject;
  195. _powerDown[0] = gauge.Find("PowerDown1").gameObject;
  196. _powerDown[1] = gauge.Find("PowerDown2").gameObject;
  197. _powerDown[2] = gauge.Find("PowerDown3").gameObject;
  198. for (int i = 0; i < 3; i++)
  199. {
  200. _powerUp[i].SetActive(false);
  201. _powerDown[i].SetActive(false);
  202. }
  203. AudioSource = GetComponent<AudioSource>();
  204. }
  205. // Update is called once per frame
  206. void Update()
  207. {
  208. // TODO lerp movement direction
  209. GetInput();
  210. if (lookDirection.magnitude > deadZone)
  211. transform.forward = Vector3.SmoothDamp(transform.forward, lookDirection, ref lookVelocity, lookSmoothingSeconds);
  212. transform.position += inputDirection * speed * Time.deltaTime;
  213. UpdatePowerBar();
  214. UpdatePowerIndicatorTimeout(ref goodIndicatorTimeOut, _powerUp);
  215. UpdatePowerIndicatorTimeout(ref badIndicatorTimeOut, _powerDown);
  216. UpdateWalkingDegen(inputDirection.magnitude);
  217. Camera.main.transform.position = Vector3.SmoothDamp(Camera.main.transform.localPosition, TargetCameraPosition(), ref cameraVelocity, cameraSmoothingSeconds);
  218. if (shooting)
  219. {
  220. Shoot();
  221. StartCoroutine(FlashMuzzle());
  222. }
  223. headLight.intensity =
  224. (dayTimeManager.dayTimeLabel == DayTimeLabel.Night || dayTimeManager.dayTimeLabel == DayTimeLabel.Evening)
  225. ? .5f
  226. : 0;
  227. }
  228. private IEnumerator FlashMuzzle()
  229. {
  230. float start = .1f;
  231. float initial = UnityEngine.Random.Range(.5f, 2f) * lightIntensity;
  232. for (float left = start; left > 0; left -= Time.deltaTime)
  233. {
  234. muzzleLight.intensity = Mathf.Lerp(initial, 0, left / start);
  235. yield return null;
  236. }
  237. muzzleLight.intensity = 0;
  238. }
  239. public float fidgetSpin = 30.0f;
  240. int bulletCounter = 5;
  241. private float chargingStart;
  242. public float maxChargingTime;
  243. public float maxBulletForce;
  244. private void Shoot()
  245. {
  246. var rb = Instantiate<Rigidbody>(bulletPrefab);
  247. rb.transform.position = gunTransform.position;
  248. rb.transform.forward = gunTransform.forward;
  249. rb.maxAngularVelocity = 300;
  250. rb.AddTorque(transform.up * fidgetSpin, ForceMode.VelocityChange);
  251. rb.AddForce(
  252. Mathf.Lerp(bulletForce, maxBulletForce, (Time.time - chargingStart) / maxChargingTime)
  253. * (transform.forward + Vector3.up * bulletUpBias), ForceMode.VelocityChange);
  254. Destroy(rb.gameObject, 2f);
  255. if (bulletCounter == 5)
  256. {
  257. PowerLevel -= bulletDegenRate;
  258. }
  259. if (bulletCounter >= 0)
  260. {
  261. bulletCounter--;
  262. }
  263. if (bulletCounter < 0)
  264. {
  265. bulletCounter = 5;
  266. }
  267. AudioSource.pitch = 1.5f;
  268. AudioSource.clip = shootSound;
  269. AudioSource.Play();
  270. AudioSource.pitch = 1.0f;
  271. }
  272. private Vector3 TargetCameraPosition()
  273. {
  274. return transform.position + cameraOffset;
  275. }
  276. void GetInput()
  277. {
  278. if (Input.GetButtonDown("Fire1"))
  279. {
  280. chargingStart = Time.time;
  281. }
  282. shooting = Input.GetButtonUp("Fire1");
  283. var padLook = new Vector3(
  284. Input.GetAxis("LookX"),
  285. 0,
  286. -Input.GetAxis("LookY")
  287. );
  288. if (useMouseLook)
  289. {
  290. var mouseLook = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
  291. mouseLook.Normalize();
  292. lookDirection = new Vector3(mouseLook.x, 0, mouseLook.y);
  293. }
  294. else if (padLook.magnitude > 0.2f)
  295. {
  296. lookDirection = padLook.normalized;
  297. }
  298. inputDirection = new Vector3(
  299. Input.GetAxis("Horizontal"),
  300. 0,
  301. Input.GetAxis("Vertical")
  302. ).normalized;
  303. }
  304. }