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.

104 lines
3.0 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. yield return StartCoroutine(LerpToPosition(lillyPad.transform, VisualPosition, 1));
  59. trappedCharacter.stuck = false;
  60. isLillyPadUp = true;
  61. trappedCharacter = null;
  62. }
  63. isFinished = true;
  64. }
  65. private IEnumerator LerpToPosition(Transform target, Vector3 endPos, float time)
  66. {
  67. Vector3 _startPos = target.position;
  68. float elapsedTime = 0;
  69. while (elapsedTime / time < 1)
  70. {
  71. target.position = Vector3.Lerp(_startPos, endPos, (elapsedTime / time));
  72. yield return new WaitForEndOfFrame();
  73. elapsedTime += Time.deltaTime;
  74. }
  75. target.position = endPos;
  76. }
  77. }