Advertisement
yskang

threejs-minecraft-40

Apr 28th, 2020 (edited)
2,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Creeper extends THREE.Group {
  2.   constructor( sizeScale, massScale, scene, world ) {
  3.     // ... other code snippet
  4.  
  5.     this.world = world;
  6.  
  7.     // ... other code snippet
  8.  
  9.     this.sizeScale = sizeScale;
  10.     this.massScale = massScale;
  11.  
  12.     // ... other code snippet
  13.  
  14.     // Move init here
  15.     this.init();
  16.   }
  17.  
  18.   // Find and replace this function's content
  19.   init() {
  20.     const sizeScale = this.sizeScale;
  21.     const massScale = this.massScale;
  22.  
  23.     // Geometries for head, body and foot
  24.     const headGeo = new THREE.BoxGeometry(
  25.       4 * sizeScale,
  26.       4 * sizeScale,
  27.       4 * sizeScale
  28.     );
  29.     const bodyGeo = new THREE.BoxGeometry(
  30.       4 * sizeScale,
  31.       8 * sizeScale,
  32.       2 * sizeScale
  33.     );
  34.     const footGeo = new THREE.BoxGeometry(
  35.       2 * sizeScale,
  36.       3 * sizeScale,
  37.       2 * sizeScale
  38.     );
  39.  
  40.     // Skin Color
  41.     const textureLoader = new THREE.TextureLoader();
  42.     const faceMap = textureLoader.load( 'img/creeper_face.png' );
  43.     const skinMap = textureLoader.load( 'img/creeper_skin.png' );
  44.  
  45.     const skinMat = new THREE.MeshPhongMaterial({
  46.       map: skinMap
  47.     });
  48.    
  49.     const headMaterials = [];
  50.     for( let i = 0; i < 6; i++ ) {
  51.         let map;
  52.  
  53.         if( i === 4 ) map = faceMap;
  54.         else map = skinMap;
  55.  
  56.         headMaterials.push(new THREE.MeshPhongMaterial({ map }));
  57.     }
  58.  
  59.     // head
  60.     this.head = new THREE.Mesh( headGeo, headMaterials );
  61.     this.head.position.set( 0, 12 * sizeScale, 0 );
  62.     // this.head.castShadow = true;
  63.     // this.head.receiveShadow = true;
  64.  
  65.     const headShape = new CANNON.Box(
  66.       new CANNON.Vec3( 2 * sizeScale, 2 * sizeScale, 2 * sizeScale )
  67.     );
  68.  
  69.     // body
  70.     this.body = new THREE.Mesh( bodyGeo, skinMat );
  71.     this.body.position.set( 0, 6 * sizeScale, 0 );
  72.     // this.body.castShadow = true;
  73.     // this.body.receiveShadow = true;
  74.  
  75.     this.headBody = new CANNON.Body({
  76.       mass: 5 * massScale,
  77.       position: new CANNON.Vec3( 0, 12 * sizeScale, 0 )
  78.     });
  79.     this.headBody.addShape( headShape );
  80.     this.headBody.position.copy( this.head.position );
  81.     this.headBody.addEventListener(
  82.       'collide',
  83.       ( event ) => this.onCollide( event )
  84.     );
  85.  
  86.     const bodyShape = new CANNON.Box(
  87.       new CANNON.Vec3( 2 * sizeScale, 4 * sizeScale, 1 * sizeScale )
  88.     );
  89.     this.bodyBody = new CANNON.Body({
  90.       mass: 10 * massScale,
  91.       position: new CANNON.Vec3( 0, 6 * sizeScale, 0 )
  92.     });
  93.     this.bodyBody.addShape( bodyShape );
  94.  
  95.     // 4 feet
  96.     this.leftFrontLeg = new THREE.Mesh( footGeo, skinMat );
  97.     // this.leftFrontLeg.castShadow = true;
  98.     // this.leftFrontLeg.receiveShadow = true;
  99.     this.leftFrontLeg.position.set(
  100.       -1 * sizeScale,
  101.       1.5 * sizeScale,
  102.       2 * sizeScale
  103.     );
  104.     this.leftBackLeg = this.leftFrontLeg.clone();
  105.     this.leftBackLeg.position.set(
  106.       -1 * sizeScale,
  107.       1.5 * sizeScale,
  108.       -2 * sizeScale
  109.     );
  110.     this.rightFrontLeg = this.leftFrontLeg.clone();
  111.     this.rightFrontLeg.position.set(
  112.       1 * sizeScale,
  113.       1.5 * sizeScale,
  114.       2 * sizeScale
  115.     );
  116.     this.rightBackLeg = this.leftFrontLeg.clone();
  117.     this.rightBackLeg.position.set(
  118.       1 * sizeScale,
  119.       1.5 * sizeScale,
  120.       -2 * sizeScale
  121.     );
  122.  
  123.     const footShape = new CANNON.Box(
  124.       new CANNON.Vec3( 1 * sizeScale, 1.5 * sizeScale, 1 * sizeScale )
  125.     );
  126.     this.leftFrontLegBody = new CANNON.Body({
  127.       mass: 10 * massScale,
  128.       position: new CANNON.Vec3( -1 * sizeScale, 1.5 * sizeScale, 2 * sizeScale )
  129.     });
  130.     this.leftFrontLegBody.addShape( footShape );
  131.     this.leftBackLegBody = new CANNON.Body({
  132.       mass: 10 * massScale,
  133.       position: new CANNON.Vec3( -1 * sizeScale, 1.5 * sizeScale, -2 * sizeScale )
  134.     })
  135.     this.leftBackLegBody.addShape( footShape );
  136.     this.rightFrontLegBody = new CANNON.Body({
  137.       mass: 10 * massScale,
  138.       position: new CANNON.Vec3( 1 * sizeScale, 1.5 * sizeScale, 2 * sizeScale )
  139.     });
  140.     this.rightFrontLegBody.addShape( footShape );
  141.     this.rightBackLegBody = new CANNON.Body({
  142.       mass: 10 * massScale,
  143.       position: new CANNON.Vec3( 1 * sizeScale, 1.5 * sizeScale, -2 * sizeScale )
  144.     });
  145.     this.rightBackLegBody.addShape( footShape );
  146.  
  147.     // Neck joint
  148.     this.neckJoint = new CANNON.LockConstraint( this.headBody, this.bodyBody );
  149.  
  150.     // Knee joint
  151.     this.leftFrontKneeJoint = new CANNON.LockConstraint(
  152.       this.bodyBody,
  153.       this.leftFrontLegBody
  154.     );
  155.     this.leftBackKneeJoint = new CANNON.LockConstraint(
  156.       this.bodyBody,
  157.       this.leftBackLegBody
  158.     );
  159.     this.rightFrontKneeJoint = new CANNON.LockConstraint(
  160.       this.bodyBody,
  161.       this.rightFrontLegBody
  162.     );
  163.     this.rightBackKneeJoint = new CANNON.LockConstraint(
  164.       this.bodyBody,
  165.       this.rightBackLegBody
  166.     );
  167.  
  168.     // Combine 4 feet as a group
  169.     this.feet = new THREE.Group();
  170.     this.feet.add(this.leftFrontLeg) // 前腳左
  171.     this.feet.add(this.leftBackLeg) // 後腳左
  172.     this.feet.add(this.rightFrontLeg) // 前腳右
  173.     this.feet.add(this.rightBackLeg) // 後腳右
  174.    
  175.  
  176.     // Put head, body and feet into a single group
  177.     this.add( this.head );
  178.     this.add( this.body );
  179.     this.add( this.feet );
  180.     this.name = 'creeper';
  181.  
  182.     // Here add shadow to creeper
  183.     this.traverse(function( object ) {
  184.       if( object instanceof THREE.Mesh ) {
  185.         object.castShadow = true;
  186.         object.receiveShadow = true;
  187.       }
  188.     });
  189.  
  190.     // this.scene.add(this);
  191.     this.attach();
  192.   }
  193.  
  194.   onCollide( event ) {
  195.     this.trigger();
  196.   }
  197.  
  198.   attach() {
  199.     const world = this.world;
  200.     const scene = this.scene;
  201.     if( !world || !scene ) return;
  202.  
  203.     scene.add( this );
  204.  
  205.     world.addBody( this.headBody );
  206.     world.addBody( this.bodyBody );
  207.     world.addBody( this.leftFrontLegBody );
  208.     world.addBody( this.leftBackLegBody );
  209.     world.addBody( this.rightFrontLegBody );
  210.     world.addBody( this.rightBackLegBody );
  211.     world.addConstraint( this.neckJoint );
  212.     world.addConstraint( this.leftFrontKneeJoint );
  213.     world.addConstraint( this.leftBackKneeJoint );
  214.     world.addConstraint( this.rightFrontKneeJoint );
  215.     world.addConstraint( this.rightBackKneeJoint );
  216.   }
  217.  
  218.   detach() {
  219.     const world = this.world;
  220.     const scene = this.scene;
  221.     if( !world || !scene ) return;
  222.  
  223.     for( let i=0; i < this.explosions.length; i++ ) {
  224.       this.explosions[i].destroy();
  225.       this.scene.remove( this.explosions[i] );
  226.     }
  227.  
  228.     this.explosions.length = 0;
  229.     this.isExposed = false;
  230.  
  231.     scene.remove( this );
  232.  
  233.     world.removeBody( this.headBody );
  234.     world.removeBody( this.bodyBody );
  235.     world.removeBody( this.leftFrontLegBody );
  236.     world.removeBody( this.leftBackLegBody );
  237.     world.removeBody( this.rightFrontLegBody );
  238.     world.removeBody( this.rightBackLegBody );
  239.     world.removeConstraint( this.neckJoint );
  240.     world.removeConstraint( this.leftFrontKneeJoint );
  241.     world.removeConstraint( this.leftBackKneeJoint );
  242.     world.removeConstraint( this.rightFrontKneeJoint );
  243.     world.removeConstraint( this.rightBackKneeJoint );
  244.   }
  245.  
  246.   dispose() {
  247.     this.detach();
  248.  
  249.     this.scene = null;
  250.     this.world = null;
  251.   }
  252.  
  253.   update() {
  254.     this.head.position.copy( this.headBody.position );
  255.     this.head.quaternion.copy( this.headBody.quaternion );
  256.     this.body.position.copy( this.bodyBody.position );
  257.     this.body.quaternion.copy( this.bodyBody.quaternion );
  258.     this.leftFrontLeg.position.copy( this.leftFrontLegBody.position );
  259.     this.leftFrontLeg.quaternion.copy( this.leftFrontLegBody.quaternion );
  260.  
  261.     this.leftBackLeg.position.copy( this.leftBackLegBody.position );
  262.     this.leftBackLeg.quaternion.copy( this.leftBackLegBody.quaternion );
  263.     this.rightFrontLeg.position.copy( this.rightFrontLegBody.position );
  264.     this.rightFrontLeg.quaternion.copy( this.rightFrontLegBody.quaternion );
  265.     this.rightBackLeg.position.copy( this.rightBackLegBody.position );
  266.     this.rightBackLeg.quaternion.copy( this.rightBackLegBody.quaternion );
  267.  
  268.     this.animate();
  269.   }
  270.  
  271.   // ... other code snippet
  272.  
  273.   // Find reset function and comment it out
  274.   // reset() {
  275.   //   if (!this.isExposed) return;
  276.  
  277.   //   for (let i = 0; i < this.explosions.length; i++) {
  278.   //     this.explosions[i].destroy();
  279.   //   }
  280.  
  281.   //   this.explosions.length = 0;
  282.   //   this.isExposed = false;
  283.  
  284.   //   this.scene.add(this);
  285.   //   this.position.set(0, 0, 0);
  286.   // }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement