Player.cs 8.9 KB

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