scriptcamera.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #ifdef GAME_TEMPLATE
  2. [EditorAttribute("box", "GameLib/Scripted", "Script camera", "-0.25 -0.25 -0.25", "0.25 0.25 0.25", "255 0 0 255")]
  3. class ScriptCameraClass
  4. {
  5. }
  6. ScriptCameraClass ScriptCameraSource;
  7. class ScriptCamera: GenericEntity
  8. {
  9. [Attribute("60", "slider", "Field of view", "0 180 1")]
  10. float FOV;
  11. [Attribute("1", "editbox", "Near plane clip")]
  12. float NearPlane;
  13. [Attribute("4000", "editbox", "Far plane clip")]
  14. float FarPlane;
  15. [Attribute("1", "combobox", "Projection type", "", ParamEnumArray.FromEnum(CameraType) )]
  16. int Type;
  17. [Attribute("5", "slider", "Camera speed", "0 20 1")]
  18. float Speed;
  19. [Attribute("1", "combobox", "Free Fly", "", ParamEnumArray.FromEnum(EBool) )]
  20. bool FreeFly;
  21. [Attribute("0", "combobox", "Invert vertical", "", ParamEnumArray.FromEnum(EBool) )]
  22. bool Inverted;
  23. [Attribute("0", "slider", "Camera index", "0 31 1")]
  24. int Index;
  25. float m_MouseSensitivity = 0.001; // should be somewhere else.
  26. float m_GamepadSensitivity = 0.2; // should be somewhere else.
  27. int m_GamepadFreeFly;
  28. // debug variables
  29. int m_DbgListSelection = 0;
  30. ref array<string> m_DbgOptions = {"Perspective", "Orthographic"};
  31. void ScriptCamera(IEntitySource src, IEntity parent)
  32. {
  33. SetFlags(EntityFlags.ACTIVE, false);
  34. SetEventMask(EntityEvent.FRAME);
  35. SetCameraVerticalFOV(Index, FOV);
  36. SetCameraFarPlane(Index, FarPlane);
  37. SetCameraNearPlane(Index, NearPlane);
  38. SetCameraType(Index, Type);
  39. m_DbgListSelection = Type - 1;
  40. SetCamera(Index, GetOrigin(), GetYawPitchRoll());
  41. vector camMat[4];
  42. GetTransform(camMat);
  43. SetCameraEx(Index, camMat);
  44. m_GamepadFreeFly = FreeFly;
  45. }
  46. override protected void EOnFrame(IEntity other, float timeSlice) //EntityEvent.FRAME
  47. {
  48. GetGame().GetInputManager().ActivateContext("ScriptCameraContext");
  49. if (GetGame().GetInputManager().GetActionTriggered("CamFreeFly"))
  50. {
  51. FreeFly = !FreeFly;
  52. }
  53. if (FreeFly)
  54. {
  55. FreeFly(timeSlice);
  56. }
  57. else
  58. {
  59. vector camMat[4]; // matrix can be set outside the class
  60. GetTransform(camMat);
  61. SetCameraEx(Index, camMat);
  62. }
  63. if (GameSettings.Debug)
  64. {
  65. DebugInfo();
  66. }
  67. }
  68. protected void FreeFly(float timeSlice)
  69. {
  70. vector camPosition = GetOrigin();
  71. vector angles = GetYawPitchRoll();
  72. vector camMat[4];
  73. GetTransform(camMat);
  74. InputManager imanager = GetGame().GetInputManager();
  75. imanager.ActivateContext("ScriptCameraFreeFlyContext");
  76. // get input
  77. float turnX = imanager.LocalValue("CamTurnRight") * 20.0 * timeSlice;
  78. float turnY = imanager.LocalValue("CamTurnUp") * 20.0 * timeSlice;
  79. float turnZ = imanager.LocalValue("CamRotate") * 20.0 * timeSlice;
  80. float moveForward = imanager.LocalValue("CamForward");
  81. float moveRight = imanager.LocalValue("CamRight");
  82. float moveAscend = imanager.LocalValue("CamAscend");
  83. float speedDelta = imanager.LocalValue("CamSpeedDelta") * timeSlice;
  84. bool speedBoostHigh = imanager.GetActionTriggered("CamSpeedBoostHigh");
  85. bool speedBoostLow = imanager.GetActionTriggered("CamSpeedBoostLow");
  86. Speed = Math.Clamp(Speed + speedDelta * Speed * 0.25, 0.1, 1000.0);
  87. float finalSpeed = Speed;
  88. if (speedBoostLow)
  89. finalSpeed *= 25;
  90. else if (speedBoostHigh)
  91. finalSpeed *= 5;
  92. // rotation
  93. angles[0] = turnX + angles[0];
  94. if (Inverted)
  95. angles[1] = turnY + angles[1];
  96. else
  97. angles[1] = -turnY + angles[1];
  98. angles[2] = turnZ + angles[2];
  99. // movement
  100. vector move = vector.Zero;
  101. vector forward = camMat[2];
  102. vector up = camMat[1];
  103. vector side = camMat[0];
  104. move += forward * moveForward;
  105. move += side * moveRight;
  106. move += up * moveAscend;
  107. // ------------
  108. camPosition = (move * timeSlice * finalSpeed) + camPosition;
  109. Math3D.YawPitchRollMatrix(angles, camMat);
  110. camMat[3] = camPosition;
  111. SetTransform(camMat);
  112. SetCameraEx(Index, camMat);
  113. }
  114. protected void DebugInfo()
  115. {
  116. InputManager imanager = GetGame().GetInputManager();
  117. DbgUI.Begin(String("Camera #" + Index.ToString()), 0, Index * 300);
  118. DbgUI.Text(String("Position : " + GetOrigin().ToString()));
  119. DbgUI.Text(String("Orientation (Y, P, R): " + GetYawPitchRoll().ToString()));
  120. DbgUI.Text(String("Speed : " + Speed.ToString()));
  121. DbgUI.Text(String("Mouse sensitivity : " + (2000 - (1 / m_MouseSensitivity)).ToString()));
  122. DbgUI.Check("Select Free fly", FreeFly);
  123. DbgUI.List("Camera type", m_DbgListSelection, m_DbgOptions);
  124. if (m_DbgListSelection + 1 != Type)
  125. {
  126. Type = m_DbgListSelection + 1;
  127. SetCameraType(Index, Type);
  128. }
  129. float sensitivity = 2000 - (1 / m_MouseSensitivity);
  130. DbgUI.SliderFloat("Mouse sensitivity", sensitivity, 1, 1999);
  131. m_MouseSensitivity = 1 / (2000 - sensitivity);
  132. DbgUI.Text("CamTurnRight: " + imanager.LocalValue("CamTurnRight"));
  133. DbgUI.Text("CamTurnUp: " + imanager.LocalValue("CamTurnUp"));
  134. DbgUI.Text("CamSpeedDelta: " + imanager.LocalValue("CamSpeedDelta"));
  135. DbgUI.Text("CamForward: " + imanager.LocalValue("CamForward"));
  136. DbgUI.Text("CamRight: " +imanager.LocalValue("CamRight"));
  137. DbgUI.Text("CamAscend: " + imanager.LocalValue("CamAscend"));
  138. DbgUI.Text("CamSpeedBoostHigh: " + imanager.GetActionTriggered("CamSpeedBoostHigh"));
  139. DbgUI.Text("CamSpeedBoostLow:" + imanager.GetActionTriggered("CamSpeedBoostLow"));
  140. DbgUI.End();
  141. }
  142. }
  143. #endif