Advertisement
yskang

threejs-minecraft-39

Apr 28th, 2020 (edited)
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class PhysicsEngine {
  2.   constructor( scene, camera ) {
  3.     // ... other code snippet
  4.     this.scene = scene;
  5.     this.camera = camera;
  6.  
  7.     // ... other code snippet
  8.  
  9.     this.bullets = [];
  10.     this.bulletMeshes = [];
  11.  
  12.     this.shotHandler = ( event ) => this.shot( event );
  13.   }
  14.  
  15.   initialize() {
  16.     // Find this lien, comment it out and then add the below line
  17.     // sphereBody.position.set( 0, 5, 0 );
  18.     sphereBody.position.set( 10, 0, 10 );
  19.    
  20.     // ... other code snippet
  21.  
  22.     this.bindEvents();
  23.   }
  24.  
  25.   uninitialize() {
  26.     this.clearEvents();
  27.   }
  28.  
  29.   bindEvents() {
  30.     window.addEventListener(
  31.       'click',
  32.       this.shotHandler
  33.     );
  34.   }
  35.  
  36.   clearEvents() {
  37.     window.removeEventListener(
  38.       'click',
  39.       this.shotHandler
  40.     );
  41.   }
  42.  
  43.   mock( count ) {
  44.     // Find this lien, comment it out and then add the below line
  45.     // scene.add( boxMesh );
  46.     thia.scene.add( boxMesh );
  47.   }
  48.  
  49.   shot( event ) {
  50.     if( !event ) return;
  51.  
  52.     if( this.bullets.length >= 10 ) {
  53.       for( let i = 0; i < this.bullets.length; i++ ) {
  54.         this.bulletMeshes[i].geometry.dispose();
  55.         this.scene.remove( this.bulletMeshes[i] );
  56.         this.world.remove( this.bullets[i] );
  57.       }
  58.       this.bullets.length = 0;
  59.       this.bulletMeshes.length = 0;
  60.     }
  61.  
  62.     const ballShape = new CANNON.Sphere( 0.2 );
  63.     const ballGeometry = new THREE.SphereGeometry( ballShape.radius, 32, 32 );
  64.     const shootVelocity = 15;
  65.     const ballMass = 20;
  66.     const ballColor = 0x93882f;
  67.  
  68.     // 取得目前玩家位置
  69.     const playerBody = this. sphereBody;
  70.     let x = playerBody.position.x;
  71.     let y = playerBody.position.y;
  72.     let z = playerBody.position.z;
  73.  
  74.     // 子彈剛體與網格
  75.     const ammoBody = new CANNON.Body({ mass: ballMass })
  76.     ammoBody.addShape( ballShape );
  77.     const ammoMaterial = new THREE.MeshStandardMaterial({ color: ballColor });
  78.     const ammoMesh = new THREE.Mesh( ballGeometry, ammoMaterial );
  79.     this.world.addBody( ammoBody );
  80.     this.scene.add( ammoMesh );
  81.     ammoMesh.castShadow = true;
  82.     ammoMesh.receiveShadow = true;
  83.     this.bullets.push( ammoBody );
  84.     this.bulletMeshes.push( ammoMesh );
  85.  
  86.     // 取得滑鼠在網頁上 (x, y) 位置
  87.     const raycaster = new THREE.Raycaster();
  88.     let shootDirection = new THREE.Vector3();
  89.     let mouse = new THREE.Vector2();
  90.  
  91.     mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  92.     mouse.y = -( event.clientY / window.innerHeight ) * 2 + 1;
  93.  
  94.     // 透過 raycaster 取得目前玩家朝向方向
  95.     raycaster.setFromCamera( mouse, this.camera );
  96.     // 取得 raycaster 方向並決定發射方向
  97.     shootDirection.copy( raycaster.ray.direction );
  98.  
  99.     ammoBody.velocity.set(
  100.       shootDirection.x * shootVelocity,
  101.       shootDirection.y * shootVelocity,
  102.       shootDirection.z * shootVelocity
  103.     );
  104.  
  105.     // Move the ball outside the player sphere
  106.     x += shootDirection.x * ( this.sphereShape.radius * 1.02 + ballShape.radius );
  107.     y += shootDirection.y * ( this.sphereShape.radius * 1.02 + ballShape.radius );
  108.     z += shootDirection.z * ( this.sphereShape.radius * 1.02 + ballShape.radius );
  109.     ammoBody.position.set( x, y, z );
  110.     ammoMesh.position.set( x, y, z );
  111.   }
  112.  
  113.   update( navigation ) {
  114.     // ... other code snippet
  115.  
  116.     // Update bullet mesh positions
  117.     const bullets = this.bullets;
  118.     const bulletMeshes = this.bulletMeshes;
  119.     for( let i = 0; i < bullets.length; i++ ) {
  120.       bulletMeshes[i].position.copy( bullets[i].position );
  121.       bulletMeshes[i].quaternion.copy( bullets[i].quaternion );
  122.     }
  123.   }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement