Player.cs 9.2 KB

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