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.

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