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.

115 lines
2.9 KiB

  1.  using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. public class ShootingCannon : ActiveBlock
  6. {
  7. [Header("Cannon Settings")]
  8. public bool shootingRight;
  9. public GameObject shootingObject;
  10. public Transform spawnLocation;
  11. public TextMeshPro counter;
  12. public TextMeshPro counter2;
  13. int countdowntimer = 3;
  14. int countdown;
  15. public override int GetInitative()
  16. {
  17. //order
  18. return 4;
  19. }
  20. private void Start()
  21. {
  22. countdown = countdowntimer;
  23. }
  24. private void Update()
  25. {
  26. counter.text = countdown.ToString();
  27. counter2.text = countdown.ToString();
  28. }
  29. public override IEnumerator OnEnvironmentTurn(PlayerData[] allPlayers)
  30. {
  31. if (countdown > 0)
  32. {
  33. countdown--;
  34. isFinished = true;
  35. yield break;
  36. }
  37. countdown = countdowntimer;
  38. Vector3 bulletPosition;
  39. Character[] hitPlayers = getHitPlayers(out bulletPosition);
  40. foreach (Character player in hitPlayers)
  41. {
  42. Vector3 moveToPosition = player.CurrentBlock.position + transform.forward;
  43. Block moveToBlock = Block.GetOrCreateBlockAtPosition(moveToPosition, 1, ~0);
  44. StartCoroutine(player.MoveToBlock(moveToBlock, Character.Animation.Hit, 1));
  45. }
  46. StartCoroutine(lerpShot(bulletPosition, Vector3.Distance(spawnLocation.position,bulletPosition)/15));
  47. if (hitPlayers.Length > 0)
  48. yield return new WaitForSeconds(1.5f);
  49. isFinished = true;
  50. yield break;
  51. }
  52. private Character[] getHitPlayers(out Vector3 endPosition)
  53. {
  54. Vector3 curPos;
  55. List<Character> retVal = new List<Character>();
  56. Block hit;
  57. endPosition = position + Vector3.up;
  58. for (int i = 1; i < 50; i++)
  59. {
  60. curPos = position + (transform.forward * i);
  61. endPosition = curPos + Vector3.one; //position which bullet will stop at
  62. if (Block.isBlockAtPosition(curPos, 1, ~0, out hit))
  63. {
  64. if (hit.CurrentPlayer != null)
  65. retVal.Add(hit.CurrentPlayer);
  66. }
  67. //this will stop the bullet
  68. if (Block.isBlockAtPosition(curPos + Vector3.up, 1, ~0))
  69. break;
  70. }//end forloop
  71. return retVal.ToArray();
  72. }
  73. private IEnumerator lerpShot(Vector3 endPosition, float time)
  74. {
  75. GameObject shot = Instantiate(shootingObject, spawnLocation.position, spawnLocation.rotation);
  76. Vector3 startPosition = spawnLocation.position;
  77. float elapsedTime = 0;
  78. while(elapsedTime < time)
  79. {
  80. shot.transform.position = Vector3.Lerp(startPosition, endPosition, elapsedTime / time);
  81. yield return new WaitForEndOfFrame();
  82. elapsedTime += Time.deltaTime;
  83. }
  84. //reset
  85. Destroy(shot);
  86. }
  87. }