VirbationManager.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Receive {
  5. public class VirbationManager : MonoBehaviour
  6. {
  7. public Transform TargetPostion;
  8. Transform RotatedTargetPostion;
  9. public Transform pointerPosition;
  10. public bool initialized = false;
  11. public bool gameEnded = false;
  12. public Receive messenger;
  13. public float rotationAngle;
  14. float minimumTreshhold = 0.8f;
  15. bool rightXPosition;
  16. bool rightYPosition;
  17. public DirectionIndicator[] arrows;
  18. // Start is called before the first frame update
  19. void Start()
  20. {
  21. GameObject go = new GameObject();
  22. RotatedTargetPostion = go.transform;
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. RotatedTargetPostion.position = TargetPostion.position;
  28. RotatedTargetPostion.RotateAround(pointerPosition.position, Vector3.forward, rotationAngle);
  29. rightXPosition = (Mathf.Abs((RotatedTargetPostion.position.x - pointerPosition.position.x)) <= minimumTreshhold);
  30. rightYPosition = (Mathf.Abs((RotatedTargetPostion.position.y - pointerPosition.position.y)) <= minimumTreshhold);
  31. if (initialized && TargetPostion != null && pointerPosition != null) {
  32. //Up
  33. handleMotor(arrows[0], "Up", checkDirectionPositive(RotatedTargetPostion.position.y, pointerPosition.position.y));
  34. //Down
  35. handleMotor(arrows[1], "Down", checkDirectionNegative(RotatedTargetPostion.position.y, pointerPosition.position.y));
  36. //Right
  37. handleMotor(arrows[3], "Right", checkDirectionPositive(RotatedTargetPostion.position.x, pointerPosition.position.x));
  38. //Left
  39. handleMotor(arrows[2], "Left", checkDirectionNegative(RotatedTargetPostion.position.x, pointerPosition.position.x));
  40. }
  41. }
  42. bool checkDirectionNegative(float targetCoordinate, float pointerCoordinate) {
  43. return (targetCoordinate - pointerCoordinate <= -minimumTreshhold || (rightXPosition && rightYPosition)) && !gameEnded ;
  44. }
  45. bool checkDirectionPositive(float targetCoordinate, float pointerCoordinate)
  46. {
  47. return (targetCoordinate - pointerCoordinate >= minimumTreshhold || (rightXPosition && rightYPosition)) && !gameEnded;
  48. }
  49. void handleMotor(DirectionIndicator arrow, string direction, bool active) {
  50. arrow.setFlash(active);
  51. messenger.setMotor(direction, active);
  52. }
  53. }
  54. }