Laser.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Collections;
  3. using GameWorld;
  4. using LitJson;
  5. using UnityEngine;
  6. [RequireComponent(typeof(LineRenderer))]
  7. public class Laser : BaseBehaviour
  8. {
  9. private float deltaTime
  10. {
  11. get
  12. {
  13. return Time.time - this.startTime;
  14. }
  15. }
  16. public GameObject orgin
  17. {
  18. get
  19. {
  20. return this._orgin;
  21. }
  22. set
  23. {
  24. this._orgin = value;
  25. this._enemyAttribute = this._orgin.GetComponent<EnemyAttribute>();
  26. }
  27. }
  28. private void Awake()
  29. {
  30. this.line = base.GetComponent<LineRenderer>();
  31. this.line.positionCount = 2;
  32. Laser._effectiveLayer = (LayerManager.GroundMask | LayerManager.WallMask | LayerManager.CeilingMask);
  33. }
  34. private void OnEnable()
  35. {
  36. this._interval = 0f;
  37. this._hit = false;
  38. this.Init();
  39. }
  40. private void Update()
  41. {
  42. if (this.orgin == null || !this.orgin.activeSelf || this._enemyAttribute.isDead)
  43. {
  44. UnityEngine.Object.Destroy(base.gameObject);
  45. }
  46. this.UpdatePoint();
  47. this._interval -= Time.deltaTime;
  48. RaycastHit2D raycastHit2D = Physics2D.Raycast(this.data.point1, this.data.point2 - this.data.point1, this.data.laserLength, LayerManager.PlayerBodyMask | Laser._effectiveLayer);
  49. if (raycastHit2D.collider != null && raycastHit2D.collider.name == "PlayerHurtBox" && this._interval <= 0f)
  50. {
  51. this._interval = 0.2f;
  52. if (!this._hit)
  53. {
  54. this._hit = true;
  55. this.EventTrigger();
  56. }
  57. else if (this.data.type == 1)
  58. {
  59. this.EventTrigger();
  60. }
  61. }
  62. }
  63. private void EventTrigger()
  64. {
  65. PlayerHurtAtkEventArgs args = new PlayerHurtAtkEventArgs(R.Player.GameObject, base.gameObject, this.orgin, this.damage, Incrementor.GetNextId(), this.atkData, false);
  66. EventManager.PostEvent<Transform, PlayerHurtAtkEventArgs>("PlayerHurtAtk", base.transform, args);
  67. }
  68. private void UpdatePoint()
  69. {
  70. Vector3 position = base.transform.position;
  71. position.z = LayerManager.ZNum.Fx;
  72. this.data.point1 = position;
  73. }
  74. private void Init()
  75. {
  76. this.startTime = Time.time;
  77. this.widthTemp = 0f;
  78. this.angleTemp = 0f;
  79. this.line.startWidth = this.data.laserWidth;
  80. this.line.endWidth = this.data.laserWidth;
  81. }
  82. public void ShowLine()
  83. {
  84. this.line.SetPosition(0, this.data.point1);
  85. this.line.SetPosition(1, this.data.point2);
  86. }
  87. public void SetStartPoint(Vector3 point1, Vector3 point2)
  88. {
  89. this.data.point1 = point1;
  90. this.data.point2 = point2;
  91. }
  92. public void Rotate()
  93. {
  94. this.angleTemp += Time.deltaTime * this.data.rotateSpeed;
  95. Vector3 vector = MathfX.RotateVector(Vector3.up, this.data.startAngle + this.angleTemp);
  96. this.hit = Physics2D.Raycast(this.data.point1, vector, this.data.laserLength, Laser._effectiveLayer);
  97. this.data.point2 = this.hit.point;
  98. if (!this.hit)
  99. {
  100. this.data.point2 = this.data.point1 + vector * this.data.laserLength;
  101. }
  102. if (this.data.canHitGround)
  103. {
  104. base.transform.GetChild(0).position = this.data.point2;
  105. }
  106. }
  107. public void Rotate(Vector3 dir)
  108. {
  109. this.hit = Physics2D.Raycast(this.data.point1, dir, this.data.laserLength, Laser._effectiveLayer);
  110. this.data.point2 = this.hit.point;
  111. if (!this.hit)
  112. {
  113. this.data.point2 = this.data.point1 + dir * this.data.laserLength;
  114. }
  115. if (this.data.canHitGround)
  116. {
  117. base.transform.GetChild(0).position = this.data.point2;
  118. }
  119. this.ShowLine();
  120. }
  121. private IEnumerator Run()
  122. {
  123. yield return base.StartCoroutine(this.LaserFadeIn());
  124. this.canRun = true;
  125. while (this.canRun)
  126. {
  127. if (this.data.canRotation && this.deltaTime > this.data.waitTime)
  128. {
  129. if (Mathf.Abs(this.data.rotateAngle) >= Mathf.Abs(this.angleTemp) && Vector2.Distance(this.data.point2, this.startPoint2) < Mathf.Abs(this.data.maxDistance))
  130. {
  131. this.Rotate();
  132. }
  133. else
  134. {
  135. this.canRun = false;
  136. yield return base.StartCoroutine(this.LaserFadeOut());
  137. }
  138. }
  139. this.ShowLine();
  140. yield return null;
  141. }
  142. UnityEngine.Object.Destroy(base.gameObject);
  143. yield break;
  144. }
  145. public void RotateByPoint(float rotateAngle, Vector3 startPoint1, float startAngle, float rotateTime, GameObject orgin)
  146. {
  147. EventManager.RegisterEvent<EventArgs>("DestroyEffect", new EventManager.FBEventHandler<EventArgs>(this.DestroyEffect), EventManager.ListenerQueue.Game);
  148. this.orgin = orgin;
  149. this.data.waitTime = rotateTime - this.data.appearTime * 2f;
  150. this.data.startAngle = startAngle;
  151. this.data.rotateSpeed = rotateAngle / rotateTime;
  152. Vector3 vector = MathfX.RotateVector(Vector3.up, this.data.startAngle);
  153. RaycastHit2D raycastHit2D = Physics2D.Raycast(startPoint1, vector, this.data.laserLength, Laser._effectiveLayer);
  154. this.data.point1 = startPoint1;
  155. this.data.point2 = raycastHit2D.point;
  156. if (!raycastHit2D)
  157. {
  158. this.data.point2 = this.data.point1 + vector * this.data.laserLength;
  159. }
  160. this.startPoint2 = this.data.point2;
  161. if (this.data.canHitGround)
  162. {
  163. base.transform.GetChild(0).position = this.data.point2;
  164. base.transform.GetChild(0).gameObject.SetActive(true);
  165. }
  166. base.StartCoroutine(this.Run());
  167. }
  168. public IEnumerator LaserFadeIn()
  169. {
  170. this.line.startWidth = 0f;
  171. this.line.endWidth = 0f;
  172. this.ShowLine();
  173. for (float i = 0f; i < this.data.appearTime; i += Time.deltaTime)
  174. {
  175. this.widthTemp = Mathf.Lerp(0f, this.data.laserWidth, i / this.data.appearTime);
  176. this.widthTemp = ((this.widthTemp >= 0.05f) ? this.widthTemp : 0f);
  177. this.line.startWidth = this.widthTemp;
  178. this.line.endWidth = this.widthTemp;
  179. yield return null;
  180. }
  181. this.line.startWidth = this.data.laserWidth;
  182. this.line.endWidth = this.data.laserWidth;
  183. yield break;
  184. }
  185. public IEnumerator LaserFadeOut()
  186. {
  187. for (float i = 0f; i < this.data.appearTime; i += Time.deltaTime)
  188. {
  189. this.widthTemp = Mathf.Lerp(this.data.laserWidth, 0f, i / this.data.appearTime);
  190. this.widthTemp = ((this.widthTemp >= 0.05f) ? this.widthTemp : 0f);
  191. this.line.startWidth = this.widthTemp;
  192. this.line.endWidth = this.widthTemp;
  193. yield return null;
  194. }
  195. base.transform.GetChild(0).gameObject.SetActive(false);
  196. this.line.startWidth = 0f;
  197. this.line.endWidth = 0f;
  198. yield break;
  199. }
  200. public void AppearLaser(Vector3 startPoint1, float startAngle, GameObject orgin)
  201. {
  202. EventManager.RegisterEvent<EventArgs>("DestroyEffect", new EventManager.FBEventHandler<EventArgs>(this.DestroyEffect), EventManager.ListenerQueue.Game);
  203. this.orgin = orgin;
  204. this.data.startAngle = startAngle;
  205. Vector3 vector = MathfX.RotateVector(Vector3.up, this.data.startAngle);
  206. RaycastHit2D raycastHit2D = Physics2D.Raycast(startPoint1, vector, this.data.laserLength, Laser._effectiveLayer);
  207. this.data.point1 = startPoint1;
  208. this.data.point2 = raycastHit2D.point;
  209. if (!raycastHit2D)
  210. {
  211. this.data.point2 = this.data.point1 + vector * this.data.laserLength;
  212. }
  213. if (this.data.canHitGround)
  214. {
  215. base.transform.GetChild(0).position = this.data.point2;
  216. base.transform.GetChild(0).gameObject.SetActive(true);
  217. }
  218. base.StartCoroutine(this.UseWithoutRotation());
  219. }
  220. public void AppearLaser(Vector3 dir, GameObject orgin, bool oneShot = false)
  221. {
  222. EventManager.RegisterEvent<EventArgs>("DestroyEffect", new EventManager.FBEventHandler<EventArgs>(this.DestroyEffect), EventManager.ListenerQueue.Game);
  223. this.orgin = orgin;
  224. this.data.point1 = base.transform.position;
  225. this.hit = Physics2D.Raycast(this.data.point1, dir, this.data.laserLength, Laser._effectiveLayer);
  226. this.data.point2 = this.hit.point;
  227. if (!this.hit)
  228. {
  229. this.data.point2 = this.data.point1 + dir * this.data.laserLength;
  230. }
  231. if (this.data.canHitGround)
  232. {
  233. base.transform.GetChild(0).position = this.data.point2;
  234. base.transform.GetChild(0).gameObject.SetActive(true);
  235. }
  236. if (oneShot)
  237. {
  238. base.StartCoroutine(this.UseWithoutRotation());
  239. }
  240. }
  241. public IEnumerator UseWithoutRotation()
  242. {
  243. this.line.startWidth = 0f;
  244. this.line.endWidth = 0f;
  245. this.ShowLine();
  246. for (float i = 0f; i < this.data.appearTime; i += Time.deltaTime)
  247. {
  248. this.widthTemp = Mathf.Lerp(0f, this.data.laserWidth, i / this.data.appearTime);
  249. this.widthTemp = ((this.widthTemp >= 0.05f) ? this.widthTemp : 0f);
  250. this.line.startWidth = this.widthTemp;
  251. this.line.endWidth = this.widthTemp;
  252. yield return null;
  253. }
  254. this.line.startWidth = this.data.laserWidth;
  255. this.line.endWidth = this.data.laserWidth;
  256. yield return new WaitForSeconds(0.1f);
  257. for (float j = 0f; j < this.data.appearTime; j += Time.deltaTime)
  258. {
  259. this.widthTemp = Mathf.Lerp(this.data.laserWidth, 0f, j / this.data.appearTime);
  260. this.widthTemp = ((this.widthTemp >= 0.05f) ? this.widthTemp : 0f);
  261. this.line.startWidth = this.widthTemp;
  262. this.line.endWidth = this.widthTemp;
  263. yield return null;
  264. }
  265. base.transform.GetChild(0).gameObject.SetActive(false);
  266. this.line.startWidth = 0f;
  267. this.line.endWidth = 0f;
  268. UnityEngine.Object.Destroy(base.gameObject);
  269. yield break;
  270. }
  271. public void SetAtkData(JsonData data, int damage)
  272. {
  273. this.atkData = data;
  274. this.damage = damage;
  275. }
  276. public bool DestroyEffect(string eventName, object sender, EventArgs msg)
  277. {
  278. if ((GameObject)sender == this.orgin)
  279. {
  280. UnityEngine.Object.Destroy(base.gameObject);
  281. }
  282. return true;
  283. }
  284. private void OnDestroy()
  285. {
  286. EventManager.UnregisterEvent<EventArgs>("DestroyEffect", new EventManager.FBEventHandler<EventArgs>(this.DestroyEffect), EventManager.ListenerQueue.Game);
  287. }
  288. private int currentShaderIndex;
  289. private bool canRun;
  290. private float startTime;
  291. private float widthTemp;
  292. private float angleTemp;
  293. private float firstHitTime;
  294. private Vector3 startPoint2;
  295. private RaycastHit2D hit;
  296. public LineRenderer line;
  297. [SerializeField]
  298. public LaserData data;
  299. private GameObject _orgin;
  300. private JsonData atkData;
  301. private int damage;
  302. private static int _effectiveLayer;
  303. private float _interval;
  304. private bool _hit;
  305. private EnemyAttribute _enemyAttribute;
  306. }