Lantern.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using System;
  2. using System.Collections;
  3. using CIS;
  4. using DG.Tweening;
  5. using UnityEngine;
  6. public class Lantern : Attackable, IBlowable, ILevelItem
  7. {
  8. private void Awake()
  9. {
  10. this.animator = base.GetComponent<Animator>();
  11. if (this.animator != null)
  12. {
  13. this.animator.StopPlayback();
  14. }
  15. this.myTrans = base.transform;
  16. this.integrator = base.GetComponent<Integrator>();
  17. this.sinmove = base.GetComponent<SinMove>();
  18. this.maxSpeedSquare = this.maxSpeedInWind * this.maxSpeedInWind;
  19. this.areaTriggered = !this.needArearTrigger;
  20. }
  21. protected override void Start()
  22. {
  23. base.Start();
  24. }
  25. private void OnTriggerEnter2D(Collider2D coll)
  26. {
  27. if (this.integrator == null)
  28. {
  29. return;
  30. }
  31. if (coll.CompareTag("Wall") || coll.CompareTag("Land"))
  32. {
  33. this.colldeNormal = -this.integrator.vel.normalized;
  34. this.integrator.vel = this.colldeNormal * UnityEngine.Random.Range(0.4f, 0.5f);
  35. this.isCollideWithWall = true;
  36. }
  37. }
  38. private void OnTriggerExit2D(Collider2D coll)
  39. {
  40. if (this.integrator == null)
  41. {
  42. return;
  43. }
  44. if (coll.CompareTag("Wall") || coll.CompareTag("Land"))
  45. {
  46. this.isCollideWithWall = false;
  47. }
  48. }
  49. public override void Update()
  50. {
  51. if (SingletonMonoBehaviourClass<GameLogicMgr>.instance.IsPause())
  52. {
  53. return;
  54. }
  55. if (!this.areaTriggered)
  56. {
  57. return;
  58. }
  59. base.Update();
  60. if (this.beBlowed)
  61. {
  62. Vector2 vector = this.myTrans.position;
  63. if (this.currBlowee != null)
  64. {
  65. Vector2 vector2 = this.currBlowee.GetDir(this, vector) * this.currBlowee.GetPower(this, vector) * this.windAffectFactor + this.dampingInWindAccel;
  66. if (this.isCollideWithWall)
  67. {
  68. vector2 = -vector2 * UnityEngine.Random.Range(0.6f, 0.8f);
  69. if ((this.integrator.vel + vector2 * Time.deltaTime).sqrMagnitude > this.maxSpeedSquare)
  70. {
  71. vector2 *= 0.5f;
  72. }
  73. }
  74. this.integrator.accel = vector2;
  75. if (this.dampingInWindTimer == 0f && this.integrator.vel.sqrMagnitude > this.maxSpeedSquare)
  76. {
  77. this.integrator.accel = Vector2.zero;
  78. }
  79. }
  80. this.myTrans.position = this.integrator.Integrate(vector);
  81. Vector2 normalized = this.integrator.vel.normalized;
  82. if (this.currBlowee == null && Vector2.Dot(normalized, this.lastVel) < 0f)
  83. {
  84. this.beBlowed = false;
  85. this.sinmove.SetInitPos(vector);
  86. UnityEngine.Debug.Log("blowed outsied");
  87. this.myTrans.DOMove(this.sinmove.GetCurrentPos(), 0.5f, false).OnComplete(delegate
  88. {
  89. this.sinmove.enabled = true;
  90. });
  91. }
  92. else if (this.dampingInWindTimer > 0f)
  93. {
  94. this.dampingInWindTimer -= SingletonMonoBehaviourClass<TimeMgr>.instance.deltaTime;
  95. }
  96. else if (this.dampingInWindTimer < 0f)
  97. {
  98. Vector2 a = Vector3.Project(this.integrator.vel, this.intoWindDir);
  99. this.integrator.vel += -a;
  100. this.dampingInWindTimer = 0f;
  101. this.dampingInWindAccel = Vector2.zero;
  102. }
  103. this.lastVel = normalized;
  104. }
  105. }
  106. private IEnumerator _PlayAnimation(string name, float time)
  107. {
  108. yield return new WaitForSeconds(time);
  109. if (this.lightAnimator1 != null)
  110. {
  111. this.animator.Play("hit3");
  112. }
  113. float z = UnityEngine.Random.Range(-40f, 40f);
  114. if (this.lightAnimator1 != null)
  115. {
  116. this.lightAnimator1.transform.rotation = Quaternion.Euler(0f, 0f, z);
  117. this.lightAnimator1.Play("lanternLight");
  118. }
  119. if (this.lightAnimator2 != null)
  120. {
  121. this.lightAnimator2.transform.rotation = Quaternion.Euler(0f, 0f, z + UnityEngine.Random.Range(50f, 110f));
  122. this.lightAnimator2.Play("lanternLightAlter");
  123. }
  124. if (this.lightBackground != null)
  125. {
  126. this.lightBackground.Play("lanternLightBackground");
  127. }
  128. if (this.particleEffectPrefab != null)
  129. {
  130. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.particleEffectPrefab);
  131. gameObject.transform.parent = base.transform;
  132. gameObject.transform.localPosition = Vector2.zero;
  133. gameObject.transform.rotation = Quaternion.Euler(0f, 180f, 0f);
  134. gameObject.transform.position = this.upperFireTarget.position;
  135. GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(this.particleEffectPrefab);
  136. gameObject2.transform.parent = base.transform;
  137. gameObject2.transform.localPosition = Vector2.zero;
  138. gameObject2.transform.rotation = Quaternion.Euler(0f, 180f, -180f);
  139. gameObject2.transform.position = this.bottomFireTarget.position;
  140. }
  141. yield break;
  142. }
  143. public override void PlayAnimationAfterTime(string name, float time)
  144. {
  145. if (this.animator != null)
  146. {
  147. base.StartCoroutine(this._PlayAnimation(name, time));
  148. }
  149. }
  150. public override void React(Vector2 dir, float delay = 0f)
  151. {
  152. if (this.beBlowed)
  153. {
  154. return;
  155. }
  156. base.React(dir, 0f);
  157. SinMove sinMove = base.GetComponent<SinMove>();
  158. bool sinEnabled = sinMove.enabled;
  159. sinMove.enabled = false;
  160. Vector2 vector = this.myTrans.position;
  161. Vector2 v = vector + dir.normalized * (float)UnityEngine.Random.Range(25, 30);
  162. Sequence s = DOTween.Sequence();
  163. this.available = false;
  164. s.AppendInterval(delay);
  165. s.Append(this.myTrans.DOMove(v, 0.2f, false));
  166. s.Append(this.myTrans.DOMove(vector, 0.3f, false));
  167. s.AppendCallback(delegate
  168. {
  169. sinMove.enabled = sinEnabled;
  170. this.available = true;
  171. });
  172. }
  173. public override bool Available()
  174. {
  175. return this.available;
  176. }
  177. public void OnReset()
  178. {
  179. if (this.integrator != null)
  180. {
  181. this.integrator.vel = Vector2.zero;
  182. this.integrator.accel = Vector2.zero;
  183. this.lastVel = Vector2.zero;
  184. this.intoWindDir = Vector2.zero;
  185. this.dampingInWindAccel = Vector2.zero;
  186. this.dampingInWindTimer = 0f;
  187. }
  188. if (this.sinmove != null && !this.sinmove.enabled)
  189. {
  190. this.sinmove.OnReset();
  191. }
  192. this.available = true;
  193. this.areaTriggered = !this.needArearTrigger;
  194. }
  195. public void OnPause(bool isPause)
  196. {
  197. }
  198. public void _OnWindEnter(IBlowee wind)
  199. {
  200. if (this.integrator == null)
  201. {
  202. return;
  203. }
  204. this.sinmove.enabled = false;
  205. this.currBlowee = wind;
  206. this.beBlowed = true;
  207. Vector2 vector = wind.GetGO().transform.position - this.myTrans.position;
  208. Vector2 vel = this.integrator.vel;
  209. float a;
  210. if (vector.x == 0f)
  211. {
  212. this.dampingInWindAccel.x = 0f;
  213. a = 0f;
  214. }
  215. else
  216. {
  217. this.dampingInWindAccel.x = -(vel.x * vel.x / (2f * vector.x));
  218. if (this.dampingInWindAccel.x == 0f)
  219. {
  220. a = 0f;
  221. }
  222. else
  223. {
  224. a = Mathf.Abs(vel.x / this.dampingInWindAccel.x);
  225. }
  226. }
  227. float b;
  228. if (vector.y == 0f)
  229. {
  230. this.dampingInWindAccel.y = 0f;
  231. b = 0f;
  232. }
  233. else
  234. {
  235. this.dampingInWindAccel.y = -(vel.y * vel.y / (2f * vector.y));
  236. if (this.dampingInWindAccel.y == 0f)
  237. {
  238. b = 0f;
  239. }
  240. else
  241. {
  242. b = Mathf.Abs(vel.y / this.dampingInWindAccel.y);
  243. }
  244. }
  245. this.dampingInWindTime = Mathf.Max(a, b);
  246. this.dampingInWindTimer = this.dampingInWindTime;
  247. this.intoWindDir = this.integrator.vel.normalized;
  248. }
  249. public void _OnWindExit(IBlowee wind)
  250. {
  251. if (this.integrator == null)
  252. {
  253. return;
  254. }
  255. if (this.currBlowee == null || wind != this.currBlowee)
  256. {
  257. return;
  258. }
  259. Vector2 vector = this.myTrans.position;
  260. if (this.blowPinTargetTrans == null)
  261. {
  262. this.integrator.accel = -wind.GetDir(this, vector) * wind.GetPower(this, vector) * this.dampingFactor;
  263. Vector2 a = Vector3.Project(this.integrator.vel, this.intoWindDir);
  264. this.integrator.vel += -a;
  265. }
  266. else
  267. {
  268. Vector2 a2 = this.blowPinTargetTrans.position;
  269. Vector2 vector2 = a2 - vector;
  270. Vector2 vel = this.integrator.vel;
  271. if (vector2.x == 0f)
  272. {
  273. this.integrator.accel.x = 0f;
  274. }
  275. else
  276. {
  277. this.integrator.accel.x = -(vel.x * vel.x / (2f * vector2.x));
  278. }
  279. if (vector2.y == 0f)
  280. {
  281. this.integrator.accel.y = 0f;
  282. }
  283. else
  284. {
  285. this.integrator.accel.y = -(vel.y * vel.y / (2f * vector2.y));
  286. }
  287. }
  288. this.currBlowee = null;
  289. }
  290. public Bounds GetBounds()
  291. {
  292. return default(Bounds);
  293. }
  294. public void OnAreaTriggerEnter(Collider2D coll)
  295. {
  296. if (coll.CompareTag("Player"))
  297. {
  298. this.areaTriggered = true;
  299. }
  300. }
  301. public void OnAreaTriggerExit(Collider2D coll)
  302. {
  303. }
  304. public void OnEntertBlock(ILevelBlock block)
  305. {
  306. }
  307. public void OnLeaveBlock(ILevelBlock block)
  308. {
  309. }
  310. public Animator lightAnimator1;
  311. public Animator lightAnimator2;
  312. public Animator lightBackground;
  313. public GameObject particleEffectPrefab;
  314. public Transform upperFireTarget;
  315. public Transform bottomFireTarget;
  316. public float windAffectFactor = 1f;
  317. public float dampingFactor = 1f;
  318. public Transform blowPinTargetTrans;
  319. public float maxSpeedInWind;
  320. public bool needArearTrigger;
  321. private bool areaTriggered = true;
  322. private float maxSpeedSquare;
  323. private Animator animator;
  324. private Transform myTrans;
  325. private Integrator integrator;
  326. private SinMove sinmove;
  327. private IBlowee currBlowee;
  328. private bool beBlowed;
  329. private bool isCollideWithWall;
  330. private Vector2 colldeNormal;
  331. private Vector2 dampingInWindAccel;
  332. private float dampingInWindTime;
  333. private float dampingInWindTimer;
  334. private Vector2 intoWindDir;
  335. private bool available = true;
  336. private Vector2 lastVel = Vector2.zero;
  337. }