Advertisement
Feynom

Untitled

Mar 8th, 2023
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using ObjectPooler;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using Zenject;
  6.  
  7. namespace Gameplay
  8. {
  9.     public class DriftPointSpawner : MonoBehaviour
  10.     {
  11.         [Header("References")]
  12.         [SerializeField] private OnPointerDownEvent _pointerDownEvent;
  13.  
  14.         [Header("Preferences")]
  15.         [SerializeField] private Pool _pool;
  16.         [SerializeField] private Vector3 _spawnOffset;
  17.         [SerializeField] private LayerMask _layerMask;
  18.         [SerializeField] private DriftDirection _startDirection = DriftDirection.Left;
  19.  
  20.         private DriftDirection _previousDriftDirection;
  21.  
  22.         private ObjectPooler.ObjectPooler _objectPooler;
  23.  
  24.         private Camera _camera;
  25.  
  26.         [Inject]
  27.         private void Construct(ObjectPooler.ObjectPooler objectPooler)
  28.         {
  29.             _objectPooler = objectPooler;
  30.         }
  31.  
  32.         public event Action<DriftPoint> onSpawned;
  33.  
  34.         #region MonoBehaviour
  35.  
  36.         private void OnValidate()
  37.         {
  38.             _pointerDownEvent ??= FindObjectOfType<OnPointerDownEvent>();
  39.         }
  40.  
  41.         private void Awake()
  42.         {
  43.             _camera = Camera.main;
  44.  
  45.             _previousDriftDirection = _startDirection == DriftDirection.Left ? DriftDirection.Right : DriftDirection.Left;
  46.         }
  47.  
  48.         private void OnEnable()
  49.         {
  50.             _pointerDownEvent.onPointerDown += SpawnDriftPoint;
  51.         }
  52.  
  53.         private void OnDisable()
  54.         {
  55.             _pointerDownEvent.onPointerDown -= SpawnDriftPoint;
  56.         }
  57.  
  58.         #endregion
  59.  
  60.         private void SpawnDriftPoint(PointerEventData eventData)
  61.         {
  62.             Ray ray = _camera.ScreenPointToRay(eventData.position);
  63.  
  64.             if (Physics.Raycast(ray, out RaycastHit hitInfo, Mathf.Infinity, _layerMask))
  65.             {
  66.                 SpawnDriftPoint(hitInfo.point + _spawnOffset, Quaternion.LookRotation(hitInfo.normal));
  67.             }
  68.         }
  69.  
  70.         private void SpawnDriftPoint(Vector3 position, Quaternion rotation)
  71.         {
  72.             GameObject driftPointObject = _objectPooler.Spawn(_pool, position, rotation);
  73.  
  74.             if (!driftPointObject.TryGetComponent(out DriftPoint driftPoint)) return;
  75.  
  76.             driftPoint.DriftDirection = _previousDriftDirection == DriftDirection.Left ? DriftDirection.Right : DriftDirection.Left;
  77.             _previousDriftDirection = driftPoint.DriftDirection;
  78.  
  79.             onSpawned?.Invoke(driftPoint);
  80.         }
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement