kinematic-character-controller.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {THREE} from './three-defs.js';
  2. import {entity} from './entity.js';
  3. export const kinematic_character_controller = (() => {
  4. class KinematicCharacterController extends entity.Component {
  5. static CLASS_NAME = 'KinematicCharacterController';
  6. get NAME() {
  7. return KinematicCharacterController.CLASS_NAME;
  8. }
  9. constructor(params) {
  10. super();
  11. this.params_ = params;
  12. }
  13. Destroy() {
  14. this.FindEntity('physics').GetComponent('AmmoJSController').RemoveRigidBody(this.body_);
  15. }
  16. InitEntity() {
  17. const pos = this.Parent.Position;
  18. const quat = this.Parent.Quaternion;
  19. this.body_ = this.FindEntity('physics').GetComponent('AmmoJSController').CreateKinematicCharacterController(
  20. pos, quat, {name: this.Parent.Name});
  21. this.Parent.Attributes.Physics = {
  22. CharacterController: this.body_,
  23. };
  24. this.Broadcast({topic: 'physics.loaded'});
  25. }
  26. InitComponent() {
  27. this.RegisterHandler_('update.position', (m) => { this.OnPosition_(m); });
  28. }
  29. OnPosition_(m) {
  30. this.OnTransformChanged_();
  31. }
  32. OnTransformChanged_() {
  33. const pos = this.Parent.Position;
  34. const quat = this.Parent.Quaternion;
  35. const t = this.body_.transform_;
  36. this.body_.body_.getWorldTransform(t);
  37. t.getOrigin().setValue(pos.x, pos.y, pos.z);
  38. this.body_.body_.setWorldTransform(t);
  39. }
  40. // Update(timeElapsedS) {
  41. // this.UpdateTranslation_(timeElapsedS);
  42. // }
  43. // UpdateTranslation_(timeElapsedS) {
  44. // const input = this.GetComponent('PlayerInput');
  45. // const forwardVelocity = (input.key(player_input.KEYS.w) ? 1 : 0) + (input.key(player_input.KEYS.s) ? -1 : 0)
  46. // const strafeVelocity = (input.key(player_input.KEYS.a) ? 1 : 0) + (input.key(player_input.KEYS.d) ? -1 : 0)
  47. // }
  48. };
  49. return {
  50. KinematicCharacterController: KinematicCharacterController,
  51. };
  52. })();