Player.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. Debug.Log("Power Offset: " + offset);
  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 Transform gauge;
  165. private Vector3 inputDirection;
  166. private float speed;
  167. private Vector3 lookDirection;
  168. private float deadZone = 0.1f;
  169. private Vector3 lookVelocity;
  170. private Vector3 cameraOffset;
  171. private Vector3 cameraVelocity;
  172. private bool shooting = false;
  173. private float lightIntensity;
  174. private bool useMouseLook;
  175. // Use this for initialization
  176. void Start()
  177. {
  178. speed = startSpeed;
  179. cameraOffset = Camera.main.transform.position - transform.position;
  180. lightIntensity = muzzleLight.intensity;
  181. var joystickNames = Input.GetJoystickNames();
  182. useMouseLook = joystickNames.Length == 0 || string.IsNullOrEmpty(joystickNames[0]);
  183. if (useMouseLook)
  184. {
  185. lookSmoothingSeconds = 0;
  186. }
  187. _powerUp[0] = gauge.Find("PowerUp1").gameObject;
  188. _powerUp[1] = gauge.Find("PowerUp2").gameObject;
  189. _powerUp[2] = gauge.Find("PowerUp3").gameObject;
  190. _powerDown[0] = gauge.Find("PowerDown1").gameObject;
  191. _powerDown[1] = gauge.Find("PowerDown2").gameObject;
  192. _powerDown[2] = gauge.Find("PowerDown3").gameObject;
  193. for (int i = 0; i < 3; i++)
  194. {
  195. _powerUp[i].SetActive(false);
  196. _powerDown[i].SetActive(false);
  197. }
  198. }
  199. // Update is called once per frame
  200. void Update()
  201. {
  202. // TODO lerp movement direction
  203. GetInput();
  204. if (lookDirection.magnitude > deadZone)
  205. transform.forward = Vector3.SmoothDamp(transform.forward, lookDirection, ref lookVelocity, lookSmoothingSeconds);
  206. transform.position += inputDirection * speed * Time.deltaTime;
  207. UpdatePowerBar();
  208. UpdatePowerIndicatorTimeout(ref goodIndicatorTimeOut, _powerUp);
  209. UpdatePowerIndicatorTimeout(ref badIndicatorTimeOut, _powerDown);
  210. UpdateWalkingDegen(inputDirection.magnitude);
  211. Camera.main.transform.position = Vector3.SmoothDamp(Camera.main.transform.localPosition, TargetCameraPosition(), ref cameraVelocity, cameraSmoothingSeconds);
  212. if (shooting)
  213. {
  214. if (Time.frameCount % 2 == 0)
  215. {
  216. muzzleLight.intensity = 0;
  217. }
  218. else
  219. {
  220. muzzleLight.intensity = Random.Range(.5f, 2f) * lightIntensity;
  221. }
  222. Shoot();
  223. }
  224. else
  225. {
  226. muzzleLight.intensity = 0;
  227. }
  228. }
  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.AddForce(bulletForce * (transform.forward + Vector3.up * bulletUpBias), ForceMode.VelocityChange);
  236. Destroy(rb.gameObject, 2f);
  237. if (bulletCounter == 5)
  238. {
  239. PowerLevel -= bulletDegenRate;
  240. }
  241. if (bulletCounter >= 0)
  242. {
  243. bulletCounter--;
  244. }
  245. if (bulletCounter < 0)
  246. {
  247. bulletCounter = 5;
  248. }
  249. }
  250. private Vector3 TargetCameraPosition()
  251. {
  252. return transform.position + cameraOffset;
  253. }
  254. void GetInput()
  255. {
  256. shooting = Input.GetButton("Fire1");
  257. var padLook = new Vector3(
  258. Input.GetAxis("LookX"),
  259. 0,
  260. -Input.GetAxis("LookY")
  261. );
  262. if (useMouseLook)
  263. {
  264. var mouseLook = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
  265. mouseLook.Normalize();
  266. lookDirection = new Vector3(mouseLook.x, 0, mouseLook.y);
  267. }
  268. else if (padLook.magnitude > 0.2f)
  269. {
  270. lookDirection = padLook.normalized;
  271. }
  272. inputDirection = new Vector3(
  273. Input.GetAxis("Horizontal"),
  274. 0,
  275. Input.GetAxis("Vertical")
  276. ).normalized;
  277. }
  278. }