ThirdPersonPlayerObject.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. function ThirdPersonPlayerObject::onAdd(%this)
  2. {
  3. %this.turnRate = 0.3;
  4. %this.phys = %this.getComponent("PlayerControllerComponent");
  5. %this.collision = %this.getComponent("CollisionComponent");
  6. %this.cam = %this.getComponent("CameraComponent");
  7. %this.camArm = %this.getComponent("CameraOrbiterComponent");
  8. %this.animation = %this.getComponent("AnimationComponent");
  9. %this.stateMachine = %this.getComponent("StateMachineComponent");
  10. %this.mesh = %this.getComponent("MeshComponent");
  11. %this.stateMachine.forwardVector = 0;
  12. %this.crouch = false;
  13. %this.firstPerson = false;
  14. %this.crouchSpeedMod = 0.5;
  15. %this.aimOrbitDist = 1.5;
  16. %this.regularOrbitDist = 5;
  17. %this.regularOrbitMaxPitch = 70;
  18. %this.regularOrbitMinPitch = -10;
  19. %this.aimedMaxPitch = 90;
  20. %this.aimedMinPitch = -90;
  21. }
  22. function ThirdPersonPlayerObject::onRemove(%this)
  23. {
  24. }
  25. function ThirdPersonPlayerObject::moveVectorEvent(%this)
  26. {
  27. %moveVector = %this.getMoveVector();
  28. // forward of the camera on the x-z plane
  29. %cameraForward = %this.cam.getForwardVector();
  30. %cameraRight = %this.cam.getRightVector();
  31. %moveVec = VectorAdd(VectorScale(%cameraRight, %moveVector.x), VectorScale(%cameraForward, %moveVector.y));
  32. if(%this.aiming || %this.firstPerson)
  33. {
  34. %forMove = "0 0 0";
  35. if(%moveVector.x != 0)
  36. {
  37. %this.phys.inputVelocity.x = %moveVector.x * 10;
  38. }
  39. else
  40. {
  41. %this.phys.inputVelocity.x = 0;
  42. }
  43. if(%moveVector.y != 0)
  44. {
  45. %this.phys.inputVelocity.y = %moveVector.y * 10;
  46. }
  47. else
  48. {
  49. %this.phys.inputVelocity.y = 0;
  50. }
  51. }
  52. else
  53. {
  54. if(%moveVec.x == 0 && %moveVec.y == 0)
  55. {
  56. %this.phys.inputVelocity = "0 0 0";
  57. %this.stateMachine.forwardVector = 0;
  58. }
  59. else
  60. {
  61. %moveVec.z = 0;
  62. %curForVec = %this.getForwardVector();
  63. %newForVec = VectorLerp(%curForVec, %moveVec, %this.turnRate);
  64. %this.setForwardVector(%newForVec);
  65. %this.phys.inputVelocity.y = 10;
  66. %this.stateMachine.forwardVector = 1;
  67. }
  68. }
  69. if(%this.crouch)
  70. %this.phys.inputVelocity = VectorScale(%this.phys.inputVelocity, %this.crouchSpeedMod);
  71. }
  72. function ThirdPersonPlayerObject::moveYawEvent(%this)
  73. {
  74. %moveRotation = %this.getMoveRotation();
  75. %camOrb = %this.getComponent("CameraOrbiterComponent");
  76. if(%this.aiming || %this.firstPerson)
  77. {
  78. %this.rotation.z += %moveRotation.z * 10;
  79. }
  80. %camOrb.rotation.z += %moveRotation.z * 10;
  81. }
  82. function ThirdPersonPlayerObject::movePitchEvent(%this)
  83. {
  84. %moveRotation = %this.getMoveRotation();
  85. %camOrb = %this.getComponent("CameraOrbiterComponent");
  86. %camOrb.rotation.x += %moveRotation.x * 10;
  87. }
  88. function ThirdPersonPlayerObject::moveRollEvent(%this){}
  89. function ThirdPersonPlayerObject::moveTriggerEvent(%this, %triggerNum, %triggerValue)
  90. {
  91. if(%triggerNum == 3 && %triggerValue)
  92. {
  93. if(%triggerValue)
  94. {
  95. %this.firstPerson = !%this.firstPerson;
  96. if(%this.firstPerson)
  97. {
  98. %this.rotation.z = %this.cam.rotationOffset.z;
  99. %this.camArm.orbitDistance = 0;
  100. %this.camArm.maxPitchAngle = %this.aimedMaxPitch;
  101. %this.camArm.minPitchAngle = %this.aimedMinPitch;
  102. %this.cam.positionOffset = "0 0 0";
  103. %this.cam.rotationOffset = "0 0 0";
  104. }
  105. else if(%this.aiming)
  106. {
  107. %this.camArm.orbitDistance = %this.aimOrbitDist;
  108. %this.camArm.maxPitchAngle = %this.aimedMaxPitch;
  109. %this.camArm.minPitchAngle = %this.aimedMinPitch;
  110. }
  111. else
  112. {
  113. %this.camArm.orbitDistance = %this.regularOrbitDist;
  114. %this.camArm.maxPitchAngle = %this.regularOrbitMaxPitch;
  115. %this.camArm.minPitchAngle = %this.regularOrbitMinPitch;
  116. }
  117. commandToClient(localclientConnection, 'SetClientRenderShapeVisibility',
  118. localclientConnection.getGhostID(%this.getComponent("MeshComponent")), !%this.firstPerson);
  119. }
  120. }
  121. else if(%triggerNum == 2 && %triggerValue == true)
  122. {
  123. //get our best collision assuming up is 0 0 1
  124. %collisionAngle = %this.collision.getBestCollisionAngle("0 0 1");
  125. if(%collisionAngle >= 80)
  126. {
  127. %surfaceNormal = %this.collision.getCollisionNormal(0);
  128. %jumpVector = VectorScale(%surfaceNormal, 200);
  129. echo("Jump surface Angle is at: " @ %surfaceNormal);
  130. %this.phys.applyImpulse(%this.position, %jumpVector);
  131. %this.setForwardVector(%jumpVector);
  132. }
  133. else
  134. %this.phys.applyImpulse(%this.position, "0 0 300");
  135. }
  136. else if(%triggerNum == 4)
  137. {
  138. %this.crouch = %triggerValue;
  139. }
  140. else if(%triggerNum == 1)
  141. {
  142. %this.aiming = %triggerValue;
  143. if(%this.aiming)
  144. {
  145. %this.rotation.z = %this.cam.rotationOffset.z;
  146. %this.camArm.orbitDistance = %this.aimOrbitDist;
  147. %this.camArm.maxPitchAngle = %this.aimedMaxPitch;
  148. %this.camArm.minPitchAngle = %this.aimedMinPitch;
  149. }
  150. else
  151. {
  152. %this.camArm.orbitDistance = %this.regularOrbitDist;
  153. %this.camArm.maxPitchAngle = %this.regularOrbitMaxPitch;
  154. %this.camArm.minPitchAngle = %this.regularOrbitMinPitch;
  155. }
  156. }
  157. }
  158. function ThirdPersonPlayerObject::onCollisionEvent(%this, %colObject, %colNormal, %colPoint, %colMatID, %velocity)
  159. {
  160. if(!%this.phys.isContacted())
  161. echo(%this @ " collided with " @ %colObject);
  162. }
  163. function ThirdPersonPlayerObject::processTick(%this)
  164. {
  165. %moveVec = %this.getMoveVector();
  166. %bestFit = "";
  167. if(%this.crouch)
  168. {
  169. if(%moveVec.x != 0 || %moveVec.y != 0)
  170. %bestFit = "Crouch_Forward";
  171. else
  172. %bestFit = "Crouch_Root";
  173. }
  174. else
  175. {
  176. if(%moveVec.x != 0 || %moveVec.y != 0)
  177. %bestFit = "Run";
  178. else
  179. %bestFit = "Root";
  180. }
  181. if(%this.animation.getThreadAnimation(0) !$= %bestFit)
  182. %this.animation.playThread(0, %bestFit);
  183. }
  184. //Used for first person mode
  185. function clientCmdSetClientRenderShapeVisibility(%id, %visiblilty)
  186. {
  187. %localID = ServerConnection.resolveGhostID(%id);
  188. %localID.enabled = %visiblilty;
  189. }
  190. function serverToClientObject( %serverObject )
  191. {
  192. assert( isObject( LocalClientConnection ), "serverToClientObject() - No local client connection found!" );
  193. assert( isObject( ServerConnection ), "serverToClientObject() - No server connection found!" );
  194. %ghostId = LocalClientConnection.getGhostId( %serverObject );
  195. if ( %ghostId == -1 )
  196. return 0;
  197. return ServerConnection.resolveGhostID( %ghostId );
  198. }