fpsControls.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //registerComponent("FPSControls", "Component", "FPS Controls", "Input", false, "First Person Shooter-type controls");
  23. function FPSControls::onAdd(%this)
  24. {
  25. //
  26. %this.beginGroup("Keys");
  27. %this.addComponentField(forwardKey, "Key to bind to vertical thrust", keybind, "keyboard w");
  28. %this.addComponentField(backKey, "Key to bind to vertical thrust", keybind, "keyboard s");
  29. %this.addComponentField(leftKey, "Key to bind to horizontal thrust", keybind, "keyboard a");
  30. %this.addComponentField(rightKey, "Key to bind to horizontal thrust", keybind, "keyboard d");
  31. %this.addComponentField(jump, "Key to bind to horizontal thrust", keybind, "keyboard space");
  32. %this.endGroup();
  33. %this.beginGroup("Mouse");
  34. %this.addComponentField(pitchAxis, "Key to bind to horizontal thrust", keybind, "mouse yaxis");
  35. %this.addComponentField(yawAxis, "Key to bind to horizontal thrust", keybind, "mouse xaxis");
  36. %this.endGroup();
  37. %this.addComponentField(moveSpeed, "Horizontal thrust force", float, 300.0);
  38. %this.addComponentField(jumpStrength, "Vertical thrust force", float, 3.0);
  39. //
  40. %control = %this.owner.getComponent( ControlObjectComponent );
  41. if(!%control)
  42. return echo("SPECTATOR CONTROLS: No Control Object behavior!");
  43. //%this.Physics = %this.owner.getComponent( PlayerPhysicsComponent );
  44. //%this.Animation = %this.owner.getComponent( AnimationComponent );
  45. //%this.Camera = %this.owner.getComponent( MountedCameraComponent );
  46. //%this.Animation.playThread(0, "look");
  47. %this.setupControls(%control.getClientID());
  48. }
  49. function FPSControls::onRemove(%this)
  50. {
  51. Parent::onBehaviorRemove(%this);
  52. commandToClient(%control.clientOwnerID, 'removeInput', %this.forwardKey);
  53. commandToClient(%control.clientOwnerID, 'removeInput', %this.backKey);
  54. commandToClient(%control.clientOwnerID, 'removeInput', %this.leftKey);
  55. commandToClient(%control.clientOwnerID, 'removeInput', %this.rightKey);
  56. commandToClient(%control.clientOwnerID, 'removeInput', %this.pitchAxis);
  57. commandToClient(%control.clientOwnerID, 'removeInput', %this.yawAxis);
  58. }
  59. function FPSControls::onBehaviorFieldUpdate(%this, %field)
  60. {
  61. %controller = %this.owner.getBehavior( ControlObjectBehavior );
  62. commandToClient(%controller.clientOwnerID, 'updateInput', %this.getFieldValue(%field), %field);
  63. }
  64. function FPSControls::onClientConnect(%this, %client)
  65. {
  66. %this.setupControls(%client);
  67. }
  68. function FPSControls::setupControls(%this, %client)
  69. {
  70. %control = %this.owner.getComponent( ControlObjectComponent );
  71. if(!%control.isControlClient(%client))
  72. {
  73. echo("FPS CONTROLS: Client Did Not Match");
  74. return;
  75. }
  76. %inputCommand = "FPSControls";
  77. %test = %this.forwardKey;
  78. /*SetInput(%client, %this.forwardKey.x, %this.forwardKey.y, %inputCommand@"_forwardKey");
  79. SetInput(%client, %this.backKey.x, %this.backKey.y, %inputCommand@"_backKey");
  80. SetInput(%client, %this.leftKey.x, %this.leftKey.y, %inputCommand@"_leftKey");
  81. SetInput(%client, %this.rightKey.x, %this.rightKey.y, %inputCommand@"_rightKey");
  82. SetInput(%client, %this.jump.x, %this.jump.y, %inputCommand@"_jump");
  83. SetInput(%client, %this.pitchAxis.x, %this.pitchAxis.y, %inputCommand@"_pitchAxis");
  84. SetInput(%client, %this.yawAxis.x, %this.yawAxis.y, %inputCommand@"_yawAxis");*/
  85. SetInput(%client, "keyboard", "w", %inputCommand@"_forwardKey");
  86. SetInput(%client, "keyboard", "s", %inputCommand@"_backKey");
  87. SetInput(%client, "keyboard", "a", %inputCommand@"_leftKey");
  88. SetInput(%client, "keyboard", "d", %inputCommand@"_rightKey");
  89. SetInput(%client, "keyboard", "space", %inputCommand@"_jump");
  90. SetInput(%client, "mouse", "yaxis", %inputCommand@"_pitchAxis");
  91. SetInput(%client, "mouse", "xaxis", %inputCommand@"_yawAxis");
  92. SetInput(%client, "keyboard", "f", %inputCommand@"_flashlight");
  93. }
  94. function FPSControls::onMoveTrigger(%this, %triggerID)
  95. {
  96. //check if our jump trigger was pressed!
  97. if(%triggerID == 2)
  98. {
  99. %this.owner.applyImpulse("0 0 0", "0 0 " @ %this.jumpStrength);
  100. }
  101. }
  102. function FPSControls::Update(%this)
  103. {
  104. return;
  105. %moveVector = %this.owner.getMoveVector();
  106. %moveRotation = %this.owner.getMoveRotation();
  107. %this.Physics.moveVector = "0 0 0";
  108. if(%moveVector.x != 0)
  109. {
  110. %fv = VectorNormalize(%this.owner.getRightVector());
  111. %forMove = VectorScale(%fv, (%moveVector.x));// * (%this.moveSpeed * 0.032)));
  112. //%this.Physics.velocity = VectorAdd(%this.Physics.velocity, %forMove);
  113. %this.Physics.moveVector = VectorAdd(%this.Physics.moveVector, %forMove);
  114. //if(%forMove > 0)
  115. // %this.Animation.playThread(1, "run");
  116. }
  117. /*else
  118. {
  119. %fv = VectorNormalize(%this.owner.getRightVector());
  120. %forMove = VectorScale(%fv, (%moveVector.x * (%this.moveSpeed * 0.032)));
  121. if(%forMove <= 0)
  122. %this.Animation.stopThread(1);
  123. }*/
  124. if(%moveVector.y != 0)
  125. {
  126. %fv = VectorNormalize(%this.owner.getForwardVector());
  127. %forMove = VectorScale(%fv, (%moveVector.y));// * (%this.moveSpeed * 0.032)));
  128. //%this.Physics.velocity = VectorAdd(%this.Physics.velocity, %forMove);
  129. %this.Physics.moveVector = VectorAdd(%this.Physics.moveVector, %forMove);
  130. //if(VectorLen(%this.Physics.velocity) < 2)
  131. // %this.Physics.velocity = VectorAdd(%this.Physics.velocity, %forMove);
  132. }
  133. /*if(%moveVector.z)
  134. {
  135. %fv = VectorNormalize(%this.owner.getUpVector());
  136. %forMove = VectorScale(%fv, (%moveVector.z * (%this.moveSpeed * 0.032)));
  137. %this.Physics.velocity = VectorAdd(%this.Physics.velocity, %forMove);
  138. }*/
  139. if(%moveRotation.x != 0)
  140. {
  141. %look = mRadToDeg(%moveRotation.x) / 180;
  142. //%this.Animation.setThreadPos(0, %look);
  143. %this.owner.getComponent( MountedCameraComponent ).rotationOffset.x += mRadToDeg(%moveRotation.x);
  144. //%this.Camera.rotationOffset.x += mRadToDeg(%moveRotation.x);
  145. }
  146. // %this.owner.rotation.x += mRadToDeg(%moveRotation.x);
  147. if(%moveRotation.z != 0)
  148. {
  149. %zrot = mRadToDeg(%moveRotation.z);
  150. %this.owner.getComponent( MountedCameraComponent ).rotationOffset.z += %zrot;
  151. //%this.owner.rotation.z += %zrot;
  152. }
  153. }
  154. //
  155. function FPSControls_forwardKey(%val)
  156. {
  157. $mvForwardAction = %val;
  158. }
  159. function FPSControls_backKey(%val)
  160. {
  161. $mvBackwardAction = %val;
  162. }
  163. function FPSControls_leftKey(%val)
  164. {
  165. $mvLeftAction = %val;
  166. }
  167. function FPSControls_rightKey(%val)
  168. {
  169. $mvRightAction = %val;
  170. }
  171. function FPSControls_yawAxis(%val)
  172. {
  173. $mvYaw += getMouseAdjustAmount(%val);
  174. }
  175. function FPSControls_pitchAxis(%val)
  176. {
  177. $mvPitch += getMouseAdjustAmount(%val);
  178. }
  179. function FPSControls_jump(%val)
  180. {
  181. $mvTriggerCount2++;
  182. }
  183. function FPSControls_flashLight(%val)
  184. {
  185. $mvTriggerCount3++;
  186. }