Player.cs 8.5 KB

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