Advertisement
Guest User

Untitled

a guest
Dec 29th, 2015
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 30.74 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using GTA;
  5. using GTA.Math;
  6. using GTA.Native;
  7. using NativeUI;
  8. using Control = GTA.Control;
  9.  
  10. namespace BennysMotorworks
  11. {
  12.     public enum CameraPosition
  13.     {
  14.         Car,
  15.         Interior,
  16.         Engine,
  17.         Trunk,
  18.         FrontBumper,
  19.         RearBumper,
  20.         Grille,
  21.         Tank,
  22.         Plaque,
  23.         BackPlate,
  24.         FrontPlate,
  25.         Wheels,
  26.     }
  27.  
  28.     public enum CameraRotationMode
  29.     {
  30.         Around,
  31.         FirstPerson,
  32.     }
  33.  
  34.     public class WorkshopCamera
  35.     {
  36.         private Camera _mainCamera;
  37.         private bool _isDragging;
  38.         private PointF _dragOffset;
  39.         private Entity _target;
  40.         private Vector3 _targetPos;
  41.  
  42.         private CameraPosition _internalCameraPosition;
  43.  
  44.  
  45.         // LERPING
  46.         public bool IsLerping;
  47.         private DateTime startTime;
  48.         private float duration;
  49.  
  50.         private Vector3 startValuePosition;
  51.         private Vector3 endValuePosition;
  52.  
  53.         private Vector3 startValueRotation;
  54.         private Vector3 endValueRotation;
  55.  
  56.         public CameraPosition MainCameraPosition
  57.         {
  58.             get
  59.             {
  60.                 return _internalCameraPosition;
  61.             }
  62.             set
  63.             {
  64.                 OnCameraChange(value);
  65.                 _internalCameraPosition = value;
  66.             }
  67.         }
  68.  
  69.         public CameraRotationMode RotationMode { get; set; }
  70.  
  71.         public float CameraZoom
  72.         {
  73.             get { return _cameraZoom; }
  74.             set
  75.             {
  76.                 if (_mainCamera != null)
  77.                 {
  78.                     var dir = CutsceneManager.RotationToDirection(_mainCamera.Rotation);
  79.                     _mainCamera.Position += dir*(_cameraZoom - value);
  80.                 }
  81.                 _cameraZoom = value;
  82.             }
  83.         }
  84.  
  85.         public CameraClamp CameraClamp { get; set; }
  86.  
  87.         private void OnCameraChange(CameraPosition newPos)
  88.         {
  89.             switch (newPos)
  90.             {
  91.                 case CameraPosition.Car:
  92.                     {
  93.                         Game.Player.Character.Alpha = 255;
  94.                         RotationMode = CameraRotationMode.Around;
  95.                         if ((_internalCameraPosition != CameraPosition.Car && _internalCameraPosition != CameraPosition.Interior))
  96.                         {
  97.                             _mainCamera.PointAt(_target);
  98.                             _targetPos = _target.Position;
  99.                             _cameraZoom = 5f;
  100.                             RotationMode = CameraRotationMode.Around;
  101.                             CameraClamp = new CameraClamp() { MaxVerticalValue = -40f, MinVerticalValue = -3f };
  102.                             _mainCamera.Shake(CameraShake.Hand, 0.5f);
  103.  
  104.                             endValuePosition = startValuePosition;
  105.                             endValueRotation = startValueRotation;
  106.                             duration = 1000f;
  107.                             IsLerping = true;
  108.                             startTime = DateTime.Now;
  109.  
  110.                             startValuePosition = _mainCamera.Position;
  111.                             startValueRotation = _mainCamera.Rotation;
  112.                         }
  113.                         else if(_internalCameraPosition == CameraPosition.Interior)
  114.                         {
  115.                             RepositionFor((Vehicle)_target);
  116.                         }
  117.                     }
  118.                     break;
  119.                 case CameraPosition.Wheels:
  120.                     {
  121.                         Game.Player.Character.Alpha = 255;
  122.                         RotationMode = CameraRotationMode.Around;
  123.                         if (_internalCameraPosition != CameraPosition.Car)
  124.                             RepositionFor((Vehicle)_target);
  125.                         CameraClamp = new CameraClamp()
  126.                         {
  127.                             MaxVerticalValue = -60f,
  128.                             MinVerticalValue = -3f,
  129.                             LeftHorizontalValue = _target.Heading - 510f,
  130.                             RightHorizontalValue = _target.Heading - 380f,
  131.                         };
  132.                         _cameraZoom = 4f;
  133.                         startValueRotation = _mainCamera.Rotation;
  134.                         startValuePosition = _mainCamera.Position;
  135.                         duration = 1000f;
  136.                         IsLerping = true;
  137.                         startTime = DateTime.Now;
  138.  
  139.                         endValuePosition = _target.Position - _target.RightVector * 4f + _target.UpVector;
  140.                         endValueRotation = new Vector3(0, 0, _target.Heading-90f);
  141.                     }
  142.                     break;
  143.                 case CameraPosition.Interior:
  144.                     {
  145.                         IsLerping = false;
  146.                         var headPos = Game.Player.Character.GetBoneCoord(Bone.IK_Head);
  147.                         World.DestroyAllCameras();
  148.                         _mainCamera = World.CreateCamera(headPos + new Vector3(0, 0, 0.1f),new Vector3(0,0,_target.Heading), GameplayCamera.FieldOfView);
  149.                         World.RenderingCamera = _mainCamera;
  150.                         _targetPos = headPos;
  151.                         RotationMode = CameraRotationMode.FirstPerson;
  152.                         Game.Player.Character.Alpha = 0;
  153.                         CameraClamp = new CameraClamp() { MaxVerticalValue = -60f, MinVerticalValue = -3f };
  154.                         _justSwitched = true;
  155.                     }
  156.                     break;
  157.                 case CameraPosition.Engine:
  158.                     {
  159.                         Game.Player.Character.Alpha = 255;
  160.                         RotationMode = CameraRotationMode.Around;
  161.                         _targetPos = GetBonePosition(_target, "engine");
  162.                         _cameraZoom = 3f;
  163.  
  164.                         startValueRotation = _mainCamera.Rotation;
  165.                         startValuePosition = _mainCamera.Position;
  166.                         duration = 1000f;
  167.                         IsLerping = true;
  168.                         startTime = DateTime.Now;
  169.  
  170.                         endValuePosition = _targetPos + _target.ForwardVector*3f + _target.UpVector;
  171.                         endValueRotation = new Vector3(0, 0, -_target.Heading);
  172.                         _mainCamera.StopPointing();
  173.                         _mainCamera.PointAt(_targetPos);
  174.  
  175.  
  176.                         CameraClamp = new CameraClamp()
  177.                         {
  178.                             MaxVerticalValue = -40f,
  179.                             MinVerticalValue = -3f,
  180.                             LeftHorizontalValue = _target.Heading - 250.6141f,
  181.                             RightHorizontalValue = _target.Heading - 470.79f,
  182.                         };
  183.                         _justSwitched = true;
  184.                     }
  185.                     break;
  186.                 case CameraPosition.Trunk:
  187.                     {
  188.                         Game.Player.Character.Alpha = 255;
  189.                         RotationMode = CameraRotationMode.Around;
  190.                         _targetPos = GetBonePosition(_target, "boot");
  191.                         _cameraZoom = 3f;
  192.  
  193.                         startValueRotation = _mainCamera.Rotation;
  194.                         startValuePosition = _mainCamera.Position;
  195.                         duration = 1000f;
  196.                         IsLerping = true;
  197.                         startTime = DateTime.Now;
  198.  
  199.                         endValuePosition = _targetPos + _target.ForwardVector * -3f + _target.UpVector;
  200.                         endValueRotation = new Vector3(0, 0, _target.Heading);
  201.                         _mainCamera.StopPointing();
  202.                         _mainCamera.PointAt(_targetPos);
  203.                         CameraClamp = new CameraClamp()
  204.                         {
  205.                             MaxVerticalValue = -40f,
  206.                             MinVerticalValue = -3f,
  207.                             LeftHorizontalValue = _target.Heading - 410f,
  208.                             RightHorizontalValue = _target.Heading - 300f,
  209.                         };
  210.                         _justSwitched = true;
  211.                     }
  212.                     break;
  213.                 case CameraPosition.FrontBumper:
  214.                     {
  215.                         Game.Player.Character.Alpha = 255;
  216.                         RotationMode = CameraRotationMode.Around;
  217.                         _targetPos = GetBonePosition(_target, "neon_f");
  218.                         _cameraZoom = 2f;
  219.  
  220.                         startValueRotation = _mainCamera.Rotation;
  221.                         startValuePosition = _mainCamera.Position;
  222.                         duration = 1000f;
  223.                         IsLerping = true;
  224.                         startTime = DateTime.Now;
  225.  
  226.                         endValuePosition = _targetPos + _target.ForwardVector * 2f + _target.UpVector;
  227.                         endValueRotation = new Vector3(0, 0, -_target.Heading);
  228.                         _mainCamera.StopPointing();
  229.                         _mainCamera.PointAt(_targetPos);
  230.  
  231.  
  232.                         CameraClamp = new CameraClamp()
  233.                         {
  234.                             MaxVerticalValue = -40f,
  235.                             MinVerticalValue = -3f,
  236.                             LeftHorizontalValue = _target.Heading - 250.6141f,
  237.                             RightHorizontalValue = _target.Heading - 470.79f,
  238.                         };
  239.                         _justSwitched = true;
  240.                     }
  241.                     break;
  242.                 case CameraPosition.Grille:
  243.                     {
  244.                         Game.Player.Character.Alpha = 255;
  245.                         RotationMode = CameraRotationMode.Around;
  246.                         _targetPos = GetBonePosition(_target, "neon_f");
  247.                         _cameraZoom = 2f;
  248.  
  249.                         startValueRotation = _mainCamera.Rotation;
  250.                         startValuePosition = _mainCamera.Position;
  251.                         duration = 1000f;
  252.                         IsLerping = true;
  253.                         startTime = DateTime.Now;
  254.  
  255.                         endValuePosition = _targetPos + _target.ForwardVector * 2f + _target.UpVector*3f;
  256.                         endValueRotation = new Vector3(0, 0, -_target.Heading);
  257.                         _mainCamera.StopPointing();
  258.                         _mainCamera.PointAt(_targetPos);
  259.  
  260.  
  261.                         CameraClamp = new CameraClamp()
  262.                         {
  263.                             MaxVerticalValue = -40f,
  264.                             MinVerticalValue = -3f,
  265.                             LeftHorizontalValue = _target.Heading - 250.6141f,
  266.                             RightHorizontalValue = _target.Heading - 470.79f,
  267.                         };
  268.                         _justSwitched = true;
  269.                     }
  270.                     break;
  271.                 case CameraPosition.RearBumper:
  272.                     {
  273.                         Game.Player.Character.Alpha = 255;
  274.                         RotationMode = CameraRotationMode.Around;
  275.                         _targetPos = GetBonePosition(_target, "neon_b");
  276.                         _cameraZoom = 2f;
  277.  
  278.                         startValueRotation = _mainCamera.Rotation;
  279.                         startValuePosition = _mainCamera.Position;
  280.                         duration = 1000f;
  281.                         IsLerping = true;
  282.                         startTime = DateTime.Now;
  283.  
  284.                         endValuePosition = _targetPos + _target.ForwardVector * -2f + _target.UpVector;
  285.                         endValueRotation = new Vector3(0, 0, _target.Heading);
  286.                         _mainCamera.StopPointing();
  287.                         _mainCamera.PointAt(_targetPos);
  288.                         CameraClamp = new CameraClamp()
  289.                         {
  290.                             MaxVerticalValue = -40f,
  291.                             MinVerticalValue = -3f,
  292.                             LeftHorizontalValue = _target.Heading - 410f,
  293.                             RightHorizontalValue = _target.Heading - 300f,
  294.                         };
  295.                         _justSwitched = true;
  296.                     }
  297.                     break;
  298.                 case CameraPosition.Tank:
  299.                     {
  300.                         Game.Player.Character.Alpha = 255;
  301.                         RotationMode = CameraRotationMode.Around;
  302.                         _targetPos = GetBonePosition(_target, "neon_b");
  303.                         _cameraZoom = 2f;
  304.  
  305.                         startValueRotation = _mainCamera.Rotation;
  306.                         startValuePosition = _mainCamera.Position;
  307.                         duration = 1000f;
  308.                         IsLerping = true;
  309.                         startTime = DateTime.Now;
  310.  
  311.                         endValuePosition = _targetPos + _target.ForwardVector*-2f;
  312.                         endValueRotation = new Vector3(0, 0, _target.Heading);
  313.                         _mainCamera.StopPointing();
  314.                         _mainCamera.PointAt(_targetPos);
  315.                         CameraClamp = new CameraClamp()
  316.                         {
  317.                             MaxVerticalValue = -40f,
  318.                             MinVerticalValue = -3f,
  319.                             LeftHorizontalValue = _target.Heading - 410f,
  320.                             RightHorizontalValue = _target.Heading - 300f,
  321.                         };
  322.                         _justSwitched = true;
  323.                     }
  324.                     break;
  325.                 case CameraPosition.Plaque:
  326.                     {
  327.                         Game.Player.Character.Alpha = 255;
  328.                         RotationMode = CameraRotationMode.Around;
  329.                         switch (_target.Model.Hash)
  330.                         {
  331.                             case -1013450936:
  332.                             case -1361687965:
  333.                                 _targetPos = GetBonePosition(_target, 28);
  334.                                 break;
  335.                             case -1790546981:
  336.                                 _targetPos = GetBonePosition(_target, 30);
  337.                                 break;
  338.                             case -2040426790:
  339.                                 _targetPos = GetBonePosition(_target, 60);
  340.                                 break;
  341.                             case 1896491931:
  342.                                 _targetPos = GetBonePosition(_target, 33);
  343.                                 break;
  344.                             case 2006667053:
  345.                                 _targetPos = GetBonePosition(_target, 41);
  346.                                 break;
  347.                         }
  348.                            
  349.                            
  350.                         _cameraZoom = 0.5f;
  351.  
  352.                         startValueRotation = _mainCamera.Rotation;
  353.                         startValuePosition = _mainCamera.Position;
  354.                         duration = 1000f;
  355.                         IsLerping = true;
  356.                         startTime = DateTime.Now;
  357.                        
  358.                         endValuePosition = _targetPos + _target.ForwardVector * -0.5f + _target.UpVector*0.1f;
  359.                         var tRot = _target.Heading;
  360.                         if (tRot > 180f)
  361.                             tRot -= 360f;
  362.                         endValueRotation = new Vector3(0, 0, tRot);
  363.                         _mainCamera.PointAt(_targetPos);
  364.                         CameraClamp = new CameraClamp()
  365.                         {
  366.                             MaxVerticalValue = -40f,
  367.                             MinVerticalValue = -3f,
  368.                             LeftHorizontalValue = _target.Heading - 410f,
  369.                             RightHorizontalValue = _target.Heading - 300f,
  370.                         };
  371.                         _justSwitched = true;
  372.                     }
  373.                     break;
  374.                 case CameraPosition.BackPlate:
  375.                     {
  376.                         Game.Player.Character.Alpha = 255;
  377.                         RotationMode = CameraRotationMode.Around;
  378.                         _targetPos = GetBonePosition(_target, "platelight");
  379.                         _cameraZoom = 1f;
  380.  
  381.                         startValueRotation = _mainCamera.Rotation;
  382.                         startValuePosition = _mainCamera.Position;
  383.                         duration = 1000f;
  384.                         IsLerping = true;
  385.                         startTime = DateTime.Now;
  386.  
  387.                         endValuePosition = _targetPos + _target.ForwardVector * -1f + _target.UpVector;
  388.                         endValueRotation = new Vector3(0, 0, _target.Heading);
  389.                         _mainCamera.StopPointing();
  390.                         _mainCamera.PointAt(_targetPos);
  391.                         CameraClamp = new CameraClamp()
  392.                         {
  393.                             MaxVerticalValue = -40f,
  394.                             MinVerticalValue = -3f,
  395.                             LeftHorizontalValue = _target.Heading - 410f,
  396.                             RightHorizontalValue = _target.Heading - 300f,
  397.                         };
  398.                         _justSwitched = true;
  399.                     }
  400.                     break;
  401.                 case CameraPosition.FrontPlate:
  402.                     {
  403.                         Game.Player.Character.Alpha = 255;
  404.                         RotationMode = CameraRotationMode.Around;
  405.                         _targetPos = GetBonePosition(_target, "neon_f");
  406.                         _cameraZoom = 1f;
  407.  
  408.                         startValueRotation = _mainCamera.Rotation;
  409.                         startValuePosition = _mainCamera.Position;
  410.                         duration = 1000f;
  411.                         IsLerping = true;
  412.                         startTime = DateTime.Now;
  413.  
  414.                         endValuePosition = _targetPos + _target.ForwardVector * 2f + _target.UpVector*2f;
  415.                         endValueRotation = new Vector3(0, 0, -_target.Heading);
  416.                         _mainCamera.StopPointing();
  417.                         _mainCamera.PointAt(_targetPos);
  418.  
  419.  
  420.                         CameraClamp = new CameraClamp()
  421.                         {
  422.                             MaxVerticalValue = -40f,
  423.                             MinVerticalValue = -3f,
  424.                             LeftHorizontalValue = _target.Heading - 250.6141f,
  425.                             RightHorizontalValue = _target.Heading - 470.79f,
  426.                         };
  427.                         _justSwitched = true;
  428.                     }
  429.                     break;
  430.             }
  431.         }
  432.  
  433.         public WorkshopCamera()
  434.         {
  435.             World.DestroyAllCameras();
  436.         }
  437.  
  438.         public void Stop()
  439.         {
  440.             World.RenderingCamera = null;
  441.             World.DestroyAllCameras();
  442.         }
  443.  
  444.         public void RepositionFor(Vehicle lowrider)
  445.         {
  446.             World.DestroyAllCameras();
  447.             _mainCamera = World.CreateCamera(lowrider.Position - lowrider.ForwardVector*5f + new Vector3(0f, 0f, 1.5f), CutsceneManager.DirectionToRotation(lowrider.ForwardVector*-5f),
  448.                 GameplayCamera.FieldOfView);
  449.             _mainCamera.PointAt(lowrider);
  450.             World.RenderingCamera = _mainCamera;
  451.             _target = lowrider;
  452.             _targetPos = lowrider.Position;
  453.             _cameraZoom = 5f;
  454.             _internalCameraPosition = CameraPosition.Car;
  455.             RotationMode = CameraRotationMode.Around;
  456.             CameraClamp = new CameraClamp() {MaxVerticalValue = -40f, MinVerticalValue = -3f};
  457.             _mainCamera.Shake(CameraShake.Hand, 0.5f);
  458.         }
  459.  
  460.         public bool IsMouseInMenu()
  461.         {
  462.             var safe = UIMenu.GetSafezoneBounds();
  463.             return UIMenu.IsMouseInBounds(new Point(safe.X, safe.Y), new Size(431, 107 + 37 + (38*10) + 18 + 18));
  464.         }
  465.  
  466.        
  467.  
  468.         private bool _justSwitched;
  469.         public void Update()
  470.         {
  471.             if (_mainCamera == null) return;
  472.             Game.DisableControl(0, Control.VehicleMouseControlOverride);
  473.  
  474.             if (IsLerping)
  475.             {
  476.                 var t = DateTime.Now;
  477.                 if (t.Subtract(startTime).TotalMilliseconds > duration)
  478.                 {
  479.                     IsLerping = false;
  480.                     _mainCamera.Position = endValuePosition;
  481.                     _mainCamera.Rotation = endValueRotation;
  482.                     return;
  483.                 }
  484.  
  485.                 _mainCamera.Position = LerpVector((float)t.Subtract(startTime).TotalMilliseconds, duration, startValuePosition, endValuePosition);
  486.                 _mainCamera.Rotation = LerpVector((float)t.Subtract(startTime).TotalMilliseconds, duration, startValueRotation, endValueRotation);
  487.                 return;
  488.             }
  489.  
  490.             if (_justSwitched)
  491.             {
  492.                 _justSwitched = false;
  493.                 return;
  494.             }
  495.  
  496.             if (Game.IsControlJustPressed(0, Control.Attack) && !_isDragging && !IsMouseInMenu())
  497.             {
  498.                 _isDragging = true;
  499.                 var mouseX = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int)Control.CursorX);
  500.                 var mouseY = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int)Control.CursorY);
  501.  
  502.                 Function.Call((Hash)0x8DB8CFFD58B62552, 4);
  503.  
  504.  
  505.                 mouseX = (mouseX * 2) - 1;
  506.                 mouseY = (mouseY * 2) - 1;
  507.  
  508.                 _dragOffset = new PointF(mouseX, mouseY);
  509.             }
  510.             if (Game.IsControlJustReleased(0, Control.Attack) && _isDragging)
  511.             {
  512.                 _isDragging = false;
  513.                 _dragOffset = new Point();
  514.                 Function.Call((Hash)0x8DB8CFFD58B62552, 0);
  515.             }
  516.  
  517.             if (RotationMode == CameraRotationMode.Around)
  518.             {
  519.                 if (_isDragging)
  520.                 {
  521.                     Function.Call(Hash._SHOW_CURSOR_THIS_FRAME);
  522.                     var dir = CutsceneManager.RotationToDirection(_mainCamera.Rotation);
  523.  
  524.                     var len = (_targetPos - _mainCamera.Position).Length();
  525.  
  526.  
  527.                     var rotLeft = _mainCamera.Rotation + new Vector3(0, 0, -10);
  528.                     var rotRight = _mainCamera.Rotation + new Vector3(0, 0, 10);
  529.                     var right = CutsceneManager.RotationToDirection(rotRight) -
  530.                                 CutsceneManager.RotationToDirection(rotLeft);
  531.  
  532.                     var rotUp = _mainCamera.Rotation + new Vector3(-20, 0, 0);
  533.                     var rotDown = _mainCamera.Rotation + new Vector3(20, 0, 0);
  534.                     var up = CutsceneManager.RotationToDirection(rotUp) - CutsceneManager.RotationToDirection(rotDown);
  535.  
  536.  
  537.                     var mouseX = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int) Control.CursorX);
  538.                     var mouseY = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int) Control.CursorY);
  539.  
  540.                     mouseX = (mouseX*2) - 1;
  541.                     mouseY = (mouseY*2) - 1;
  542.  
  543.                     Vector3 rotation = new Vector3();
  544.                    
  545.                     if(!IsCameraClamped(true, mouseX - _dragOffset.X))
  546.                         rotation += right*15*(mouseX - _dragOffset.X);
  547.                     if (!IsCameraClamped(false, mouseY - _dragOffset.Y))
  548.                         rotation += up*-((mouseY - _dragOffset.Y)*15);
  549.                     rotation += dir*(len - CameraZoom);
  550.  
  551.                     _mainCamera.Position += rotation;
  552.  
  553.                     _dragOffset = new PointF(mouseX, mouseY);
  554.                 }
  555.  
  556.                 if (Game.CurrentInputMode == InputMode.GamePad)
  557.                 {
  558.                     var dir = CutsceneManager.RotationToDirection(_mainCamera.Rotation);
  559.  
  560.                     var len = (_targetPos - _mainCamera.Position).Length();
  561.  
  562.                     var rotLeft = _mainCamera.Rotation + new Vector3(0, 0, -10);
  563.                     var rotRight = _mainCamera.Rotation + new Vector3(0, 0, 10);
  564.                     var right = CutsceneManager.RotationToDirection(rotRight) -
  565.                                 CutsceneManager.RotationToDirection(rotLeft);
  566.  
  567.                     var rotUp = _mainCamera.Rotation + new Vector3(-20, 0, 0);
  568.                     var rotDown = _mainCamera.Rotation + new Vector3(20, 0, 0);
  569.                     var up = CutsceneManager.RotationToDirection(rotUp) - CutsceneManager.RotationToDirection(rotDown);
  570.  
  571.  
  572.                     var mouseX = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int) Control.LookLeftRight);
  573.                     var mouseY = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int) Control.LookUpDown);
  574.  
  575.                     Vector3 rotation = new Vector3();
  576.  
  577.                     if(!IsCameraClamped(true, mouseX))
  578.                         rotation += right*mouseX*0.6f;
  579.                     if (!IsCameraClamped(false, mouseY))
  580.                         rotation += up*-mouseY*0.5f;
  581.                     rotation += dir*(len - CameraZoom);
  582.  
  583.                     _mainCamera.Position += rotation;
  584.                 }
  585.             }
  586.             else if(RotationMode == CameraRotationMode.FirstPerson)
  587.             {
  588.                 if (_isDragging)
  589.                 {
  590.                     Function.Call(Hash._SHOW_CURSOR_THIS_FRAME);
  591.  
  592.                     var mouseX = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int)Control.CursorX);
  593.                     var mouseY = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int)Control.CursorY);
  594.  
  595.  
  596.                     mouseX = (mouseX * 2) - 1;
  597.                     mouseY = (mouseY * 2) - 1;
  598.  
  599.                     mouseY *= -1;
  600.  
  601.                     var right = new Vector3(0, 0, 1);
  602.                     var up = new Vector3(1, 0, 0);
  603.  
  604.                     Vector3 rotation = new Vector3();
  605.  
  606.                     if (!IsCameraClamped(true, mouseX - _dragOffset.X))
  607.                         rotation += right * 20 * (mouseX - _dragOffset.X);
  608.                     if (!IsCameraClamped(false, mouseY - _dragOffset.Y))
  609.                         rotation += up * -((mouseY - _dragOffset.Y) * 20);
  610.  
  611.                     _mainCamera.Rotation += rotation;
  612.  
  613.                     _dragOffset = new PointF(mouseX, mouseY);
  614.                 }
  615.                 if (Game.CurrentInputMode == InputMode.GamePad)
  616.                 {
  617.                     var mouseX = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int)Control.LookLeftRight);
  618.                     var mouseY = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int)Control.LookUpDown);
  619.  
  620.                     mouseX *= -1;
  621.  
  622.                     var right = new Vector3(0, 0, 1);
  623.                     var up = new Vector3(1, 0, 0);
  624.  
  625.                     Vector3 rotation = new Vector3();
  626.  
  627.                     if (!IsCameraClamped(true, mouseX))
  628.                         rotation += right * mouseX * 4f;
  629.                     if (!IsCameraClamped(false, mouseY))
  630.                         rotation += up * -mouseY * 4f;
  631.  
  632.                     _mainCamera.Rotation += rotation;
  633.                 }
  634.             }
  635.         }
  636.  
  637.         public bool IsCameraClamped(bool horizontally, float delta)
  638.         {
  639.             if (horizontally)
  640.             {
  641.                 var goingLeft = delta < 0;
  642.                 if (goingLeft && !CameraClamp.LeftHorizontalValue.HasValue)
  643.                     return false;
  644.                 if (!goingLeft && !CameraClamp.RightHorizontalValue.HasValue)
  645.                     return false;
  646.  
  647.                
  648.                 if (CameraClamp.LeftHorizontalValue > 180f)
  649.                     CameraClamp.LeftHorizontalValue = CameraClamp.LeftHorizontalValue - (360*((int)(CameraClamp.LeftHorizontalValue/360))+1);
  650.                    
  651.                 if (CameraClamp.RightHorizontalValue > 180f)
  652.                     CameraClamp.RightHorizontalValue = CameraClamp.RightHorizontalValue - (360 * ((int)(CameraClamp.RightHorizontalValue / 360)) + 1);
  653.  
  654.                 var sameHemisphereLeft = (_mainCamera.Rotation.Z > 0f && CameraClamp.LeftHorizontalValue > 0f) ||
  655.                                      (_mainCamera.Rotation.Z < 0f && CameraClamp.LeftHorizontalValue < 0f);
  656.  
  657.                 var sameHemisphereRight = (_mainCamera.Rotation.Z > 0f && CameraClamp.RightHorizontalValue > 0f) ||
  658.                                      (_mainCamera.Rotation.Z < 0f && CameraClamp.RightHorizontalValue < 0f);
  659.  
  660.                 if (goingLeft && _mainCamera.Rotation.Z > CameraClamp.RightHorizontalValue && sameHemisphereRight)
  661.                     return true;
  662.                 if (!goingLeft && _mainCamera.Rotation.Z < CameraClamp.LeftHorizontalValue && sameHemisphereLeft)
  663.                     return true;
  664.                 return false;
  665.             }
  666.             else
  667.             {
  668.                 var goingDown = delta < 0;
  669.                 if (goingDown && !CameraClamp.MinVerticalValue.HasValue)
  670.                     return false;
  671.                 if (!goingDown && !CameraClamp.MaxVerticalValue.HasValue)
  672.                     return false;
  673.                 if (goingDown && _mainCamera.Rotation.X > CameraClamp.MinVerticalValue)
  674.                     return true;
  675.                 if (!goingDown && _mainCamera.Rotation.X < CameraClamp.MaxVerticalValue)
  676.                     return true;
  677.                 return false;
  678.             }
  679.         }
  680.  
  681.         public static float Clamp(float value, float min, float max)
  682.         {
  683.             if (value > max) return max;
  684.             if (value < min) return min;
  685.             return value;
  686.         }
  687.  
  688.         public static Vector3 GetBonePosition(Entity target, string bone)
  689.         {
  690.             return Function.Call<Vector3>((Hash) 0x44A8FCB8ED227738, target.Handle, Function.Call<int>((Hash) 0xFB71170B7E76ACBA, target.Handle, bone));
  691.         }
  692.  
  693.         public static Vector3 GetBonePosition(Entity target, int bone)
  694.         {
  695.             return Function.Call<Vector3>((Hash)0x44A8FCB8ED227738, target.Handle, bone);
  696.         }
  697.  
  698.         public static float QuadraticEasing(float currentTime, float startValue, float changeInValue, float duration)
  699.         {
  700.             currentTime /= duration/2;
  701.             if (currentTime < 1) return changeInValue/2*currentTime*currentTime + startValue;
  702.             currentTime--;
  703.             return -changeInValue/2*(currentTime*(currentTime - 2) - 1) + startValue;
  704.         }
  705.  
  706.         public static Vector3 LerpVector(float currentTime, float duration, Vector3 startValue, Vector3 destination)
  707.         {
  708.             var output = new Vector3();
  709.             output.X = QuadraticEasing(currentTime, startValue.X, destination.X - startValue.X, duration);
  710.             output.Y = QuadraticEasing(currentTime, startValue.Y, destination.Y - startValue.Y, duration);
  711.             output.Z = QuadraticEasing(currentTime, startValue.Z, destination.Z - startValue.Z, duration);
  712.             return output;
  713.         }
  714.        
  715.         private float _cameraZoom;
  716.     }
  717. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement