player.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
  2. import {FBXLoader} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/FBXLoader.js';
  3. export const player = (() => {
  4. class Player {
  5. constructor(params) {
  6. this.position_ = new THREE.Vector3(0, 0, 0);
  7. this.velocity_ = 0.0;
  8. // this.mesh_ = new THREE.Mesh(
  9. // new THREE.BoxBufferGeometry(1, 1, 1),
  10. // new THREE.MeshStandardMaterial({
  11. // color: 0x80FF80,
  12. // }),
  13. // );
  14. // this.mesh_.castShadow = true;
  15. // this.mesh_.receiveShadow = true;
  16. // params.scene.add(this.mesh_);
  17. this.playerBox_ = new THREE.Box3();
  18. this.params_ = params;
  19. this.LoadModel_();
  20. this.InitInput_();
  21. }
  22. LoadModel_() {
  23. const loader = new FBXLoader();
  24. loader.setPath('./resources/Dinosaurs/FBX/');
  25. loader.load('Velociraptor.fbx', (fbx) => {
  26. fbx.scale.setScalar(0.0025);
  27. fbx.quaternion.setFromAxisAngle(
  28. new THREE.Vector3(0, 1, 0), Math.PI / 2);
  29. this.mesh_ = fbx;
  30. this.params_.scene.add(this.mesh_);
  31. fbx.traverse(c => {
  32. let materials = c.material;
  33. if (!(c.material instanceof Array)) {
  34. materials = [c.material];
  35. }
  36. for (let m of materials) {
  37. if (m) {
  38. m.specular = new THREE.Color(0x000000);
  39. m.color.offsetHSL(0, 0, 0.25);
  40. }
  41. }
  42. c.castShadow = true;
  43. c.receiveShadow = true;
  44. });
  45. const m = new THREE.AnimationMixer(fbx);
  46. this.mixer_ = m;
  47. for (let i = 0; i < fbx.animations.length; ++i) {
  48. if (fbx.animations[i].name.includes('Run')) {
  49. const clip = fbx.animations[i];
  50. const action = this.mixer_.clipAction(clip);
  51. action.play();
  52. }
  53. }
  54. });
  55. }
  56. InitInput_() {
  57. this.keys_ = {
  58. spacebar: false,
  59. };
  60. this.oldKeys = {...this.keys_};
  61. document.addEventListener('keydown', (e) => this.OnKeyDown_(e), false);
  62. document.addEventListener('keyup', (e) => this.OnKeyUp_(e), false);
  63. }
  64. OnKeyDown_(event) {
  65. switch(event.keyCode) {
  66. case 32:
  67. this.keys_.space = true;
  68. break;
  69. }
  70. }
  71. OnKeyUp_(event) {
  72. switch(event.keyCode) {
  73. case 32:
  74. this.keys_.space = false;
  75. break;
  76. }
  77. }
  78. CheckCollisions_() {
  79. const colliders = this.params_.world.GetColliders();
  80. this.playerBox_.setFromObject(this.mesh_);
  81. for (let c of colliders) {
  82. const cur = c.collider;
  83. if (cur.intersectsBox(this.playerBox_)) {
  84. this.gameOver = true;
  85. }
  86. }
  87. }
  88. Update(timeElapsed) {
  89. if (this.keys_.space && this.position_.y == 0.0) {
  90. this.velocity_ = 30;
  91. }
  92. const acceleration = -75 * timeElapsed;
  93. this.position_.y += timeElapsed * (
  94. this.velocity_ + acceleration * 0.5);
  95. this.position_.y = Math.max(this.position_.y, 0.0);
  96. this.velocity_ += acceleration;
  97. this.velocity_ = Math.max(this.velocity_, -100);
  98. if (this.mesh_) {
  99. this.mixer_.update(timeElapsed);
  100. this.mesh_.position.copy(this.position_);
  101. this.CheckCollisions_();
  102. }
  103. }
  104. };
  105. return {
  106. Player: Player,
  107. };
  108. })();