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.

103 lines
2.9 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 != 0)
  46. {
  47. trappedCharacter.transform.Rotate(Vector3.up, -trappedCharacter.lastRotation);
  48. //if they are still on an angle fix to the closest rotation
  49. if (trappedCharacter.transform.eulerAngles.y % 90 != 0)
  50. {
  51. Vector3 newRot = trappedCharacter.transform.eulerAngles;
  52. newRot.y = Mathf.Round(newRot.y / 90) * 90;
  53. trappedCharacter.transform.eulerAngles = newRot;
  54. }
  55. }
  56. StartCoroutine(LerpToPosition(trappedCharacter.transform, VisualPosition, 1));
  57. yield return StartCoroutine(LerpToPosition(lillyPad.transform, VisualPosition, 1));
  58. trappedCharacter.stuck = false;
  59. isLillyPadUp = true;
  60. trappedCharacter = null;
  61. }
  62. isFinished = true;
  63. }
  64. private IEnumerator LerpToPosition(Transform target, Vector3 endPos, float time)
  65. {
  66. Vector3 _startPos = target.position;
  67. float elapsedTime = 0;
  68. while (elapsedTime / time < 1)
  69. {
  70. target.position = Vector3.Lerp(_startPos, endPos, (elapsedTime / time));
  71. yield return new WaitForEndOfFrame();
  72. elapsedTime += Time.deltaTime;
  73. }
  74. target.position = endPos;
  75. }
  76. }