Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3.  
  4. public class ShootController : MonoBehaviour
  5. {
  6.     private LevelController levelController;
  7.     public float bulletMass = 1;
  8.     private LineDrawer lineDrawer;
  9.     private Vector2 shootDir = new Vector2();
  10.     public Transform gun;
  11.     public Transform rayStart;
  12.     public bool isFlip = false;
  13.     private bool wasDown = false;
  14.     private void Awake()
  15.     {
  16.         levelController = FindObjectOfType<LevelController>();
  17.     }
  18.  
  19.     private void Start()
  20.     {
  21.         lineDrawer = new LineDrawer(0.05f);
  22.     }
  23.  
  24.     void Update()
  25.     {
  26.         var fingerId = PointerInputModule.kMouseLeftId;
  27.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
  28.         {
  29.             fingerId = Input.GetTouch(0).fingerId;
  30.         }
  31.  
  32.         if (levelController.canShoot())
  33.         {
  34.             if (EventSystem.current.IsPointerOverGameObject(fingerId))
  35.                 return;
  36.            
  37.             if (Input.GetMouseButtonDown(0))
  38.             {
  39.                 lineDrawer.enable(true);
  40.                 wasDown = true;
  41.             }
  42.            
  43.             if (wasDown)
  44.             {
  45.                 if (Input.GetMouseButton(0))
  46.                     updateAim(Input.mousePosition);
  47.  
  48.                 if (Input.GetMouseButtonUp(0))
  49.                 {
  50.                     GoFabric.createBullet(rayStart.position, shootDir, bulletMass);
  51.                     AudioController.Instance.PlaySound(Sounds.Shot);
  52.                     EventManager.TriggerEvent(new Events.ShotEvent(shootDir.x));
  53.                     wasDown = false;
  54.                 }  
  55.             }
  56.         }
  57.  
  58.         if (Input.GetMouseButtonUp(0))
  59.         {
  60.             lineDrawer.enable(false);
  61.         }
  62.     }
  63.    
  64.     private void updateAim(Vector2 touchPos)
  65.     {
  66.         var mouseWorldPosition = Camera.main.ScreenToWorldPoint(touchPos);
  67.         var pos = rayStart.position;
  68.         var gunDirection = new Vector2(
  69.             mouseWorldPosition.x - gun.position.x,
  70.             mouseWorldPosition.y - gun.position.y
  71.             );
  72.         gun.right = gunDirection;
  73.         shootDir = gunDirection;
  74.         lineDrawer.DrawLineInGameView(pos, pos + (Vector3) (gunDirection * 200), new Color(1, 0, 0, 0.5f));
  75.          gunDirection = new Vector2(
  76.             mouseWorldPosition.x - transform.position.x,
  77.             mouseWorldPosition.y - transform.position.y
  78.         );
  79.        
  80.         FlipHero(gunDirection);
  81.     }
  82.    
  83.  
  84.     private void FlipHero(Vector2 dir)
  85.     {
  86.         var angle = dir.Angle();
  87.         var flip = angle >= 90 && angle <= 270;
  88.         gun.transform.localScale = new Vector3(flip ? -1 : 1, flip ? -1 : 1, 1);
  89.         transform.localScale = new Vector3(flip ? 1 : -1, 1, 1);
  90.     }
  91.     bool IsPointerOverGameObject( int fingerId )
  92.     {
  93.         EventSystem eventSystem = EventSystem.current;
  94.         return ( eventSystem.IsPointerOverGameObject( fingerId )
  95.                  && eventSystem.currentSelectedGameObject != null );
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement