You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.8 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Water : ActiveBlock
  5. {
  6. [SerializeField]
  7. [Tooltip("GameObject which appears when character respawns")]
  8. private GameObject lillyPad;
  9. [SerializeField]
  10. [Tooltip("Distance character will be submerged into the water")]
  11. private float FallDistance = 0.5f;
  12. private bool isLillyPadUp = false;
  13. private Character trappedCharacter;
  14. public override int GetInitative()
  15. {
  16. return 0;
  17. }
  18. public override IEnumerator OnWalkedOnByPlayer(Character player, Vector3 moveDirection)
  19. {
  20. yield return StartCoroutine(base.OnWalkedOnByPlayer(player, moveDirection));
  21. if (trappedCharacter == null)
  22. {
  23. CurrentPlayer = null;
  24. trappedCharacter = player;
  25. trappedCharacter.stuck = true;
  26. trappedCharacter.StartAnimation(Character.Animation.Hit);
  27. yield return StartCoroutine(LerpToPosition(trappedCharacter.transform, VisualPosition + Vector3.down * FallDistance, 1));
  28. }
  29. }
  30. public override void OnLeftByPlayer(Character player)
  31. {
  32. base.OnLeftByPlayer(player);
  33. if (isLillyPadUp)
  34. {
  35. StartCoroutine(LerpToPosition(lillyPad.transform, VisualPosition + Vector3.down * FallDistance, 1));
  36. isLillyPadUp = false;
  37. }
  38. }
  39. public override IEnumerator OnRoundEnd(PlayerData[] allPlayers)
  40. {
  41. //Debug.Log("reseting water");
  42. if (trappedCharacter != null)
  43. {
  44. //if character is on an angle undo their last rotation
  45. if (trappedCharacter.transform.eulerAngles.y % 90 > 10 && trappedCharacter.transform.eulerAngles.y % 90 < 80)
  46. {
  47. trappedCharacter.transform.Rotate(Vector3.up, -trappedCharacter.lastRotation);
  48. trappedCharacter.lastRotation = 0;
  49. //if they are still on an angle fix to the closest rotation
  50. if (trappedCharacter.transform.eulerAngles.y % 90 != 0)
  51. {
  52. Vector3 newRot = trappedCharacter.transform.eulerAngles;
  53. newRot.y = Mathf.Round(newRot.y / 90) * 90;
  54. trappedCharacter.transform.eulerAngles = newRot;
  55. }
  56. }
  57. StartCoroutine(LerpToPosition(trappedCharacter.transform, VisualPosition, 1));
  58. StartCoroutine(LerpToPosition(lillyPad.transform, VisualPosition, 1));
  59. if (CurrentPlayer != null)
  60. {
  61. yield return new WaitForSeconds(0.2f);
  62. yield return StartCoroutine(PushPlayOnTop());
  63. }
  64. else
  65. {
  66. yield return new WaitForSeconds(1);
  67. CurrentPlayer = trappedCharacter;
  68. }
  69. trappedCharacter.stuck = false;
  70. isLillyPadUp = true;
  71. trappedCharacter = null;
  72. }
  73. isFinished = true;
  74. }
  75. private IEnumerator PushPlayOnTop()
  76. {
  77. Direction[] possibleDirections = DirectionExtras.RandomOrder();
  78. for (int i = 0; i < 4; i++)
  79. {
  80. if (Block.isBlockAtPosition(position + possibleDirections[i].ToVector() + Vector3.up, 1, ~0))
  81. continue;
  82. yield return StartCoroutine(DoPush(trappedCharacter, possibleDirections[i].ToVector()));
  83. break;
  84. }
  85. }
  86. private IEnumerator LerpToPosition(Transform target, Vector3 endPos, float time)
  87. {
  88. Vector3 _startPos = target.position;
  89. float elapsedTime = 0;
  90. while (elapsedTime / time < 1)
  91. {
  92. target.position = Vector3.Lerp(_startPos, endPos, (elapsedTime / time));
  93. yield return new WaitForEndOfFrame();
  94. elapsedTime += Time.deltaTime;
  95. }
  96. target.position = endPos;
  97. }
  98. }