Advertisement
metalx1000

ThreeJS Basic 2 Player Game Controls

Jan 4th, 2020
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*-- ------------------------------------------------------------
  2. ######################################################################
  3. #Copyright (C) 2020  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation, either version 3 of the License, or
  9. #(at your option) any later version.
  10.  
  11. #This program is distributed in the hope that it will be useful,
  12. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #GNU General Public License for more details.
  15.  
  16. #You should have received a copy of the GNU General Public License
  17. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. ######################################################################
  19.  
  20. */
  21.  
  22. var camera, scene, renderer, controls, raycaster;
  23. var keyboard = new KeyboardState();
  24. var clock = new THREE.Clock();
  25. var clockD;
  26. var player_configs = [];
  27. var players = [];
  28.  
  29. init();
  30. animate();
  31.  
  32. function init() {
  33.   scene = new THREE.Scene();
  34.  
  35.   createRenderer();
  36.   createCamera();
  37.  
  38.   player_configs.push({
  39.     speed : 5,
  40.     keyleft : "A",
  41.     keyright : "D",
  42.     keyback : "S",
  43.     keyforward : "W"
  44.   });
  45.  
  46.   player_configs.push({
  47.     speed : 5,
  48.     keyright : "J",
  49.     keyleft : "G",
  50.     keyback : "H",
  51.     keyforward : "Y"
  52.   });
  53.  
  54.   players.push(createCube(1));
  55.   players.push(createCube(1));
  56.   players[1].position.x = 4;
  57.   for(var i in players){
  58.     for ( var p in player_configs[i] ){
  59.       players[i][p] = player_configs[i][p];
  60.       players[i].update = player_update;
  61.     };
  62.  
  63.   };
  64.  
  65.   floor = createPlane();
  66.   floor.rotation.x = Math.PI /2;//rotate 90 degrees
  67.   floor.position.y = -.5;
  68.   light = createLights();
  69.   window.addEventListener( 'resize', onWindowResize, false );
  70. }
  71.  
  72. function onWindowResize() {
  73.   camera.aspect = window.innerWidth / window.innerHeight;
  74.   camera.updateProjectionMatrix();
  75.  
  76.   renderer.setSize( window.innerWidth, window.innerHeight );
  77. }
  78.  
  79. function animate() {
  80.   requestAnimationFrame( animate );
  81.   renderer.render( scene, camera );
  82.  
  83.   clockD = clock.getDelta();
  84.   //uncoment to have camera look at player
  85.   //controls.target = player.position;
  86.   controls.update();
  87.   for(var i in players){
  88.     players[i].update();
  89.   }
  90.   //TWEEN.update();
  91. }
  92.  
  93. function player_update(){
  94.   keyboard.update();
  95.  
  96.   var moveDistance = this.speed * clockD;
  97.   if ( keyboard.pressed(this.keyleft) )
  98.     this.rotateY( moveDistance );
  99.  
  100.   if ( keyboard.pressed(this.keyright) )
  101.     this.rotateY( -moveDistance );
  102.  
  103.   if ( keyboard.pressed(this.keyforward) )
  104.     this.translateZ( -moveDistance );
  105.  
  106.   if ( keyboard.pressed(this.keyback) )
  107.     this.translateZ( moveDistance );
  108. }
  109.  
  110. function createCamera(){
  111.   camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 5000 );
  112.   camera.position.z = 5;
  113.   camera.position.y = 4;
  114.   camera.lookAt(scene.position);
  115.  
  116.   //addcontrols
  117.   controls = new THREE.OrbitControls( camera, renderer.domElement );
  118. }
  119.  
  120. function createRenderer(){
  121.   renderer = new THREE.WebGLRenderer( { antialias: true,alpha: true } );
  122.   renderer.setPixelRatio( window.devicePixelRatio );
  123.   renderer.setSize( window.innerWidth, window.innerHeight );
  124.   document.body.appendChild( renderer.domElement );
  125.  
  126. }
  127.  
  128. function createCube(size){
  129.   var geometry = new THREE.BoxGeometry( size, size, size );
  130.   var material = new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } );
  131.   var cube = new THREE.Mesh( geometry, material );
  132.   cube.position.set( 0, 0, 0 );
  133.   scene.add(cube);
  134.   cube.update = player_update;
  135.   return cube;
  136. }
  137.  
  138.  
  139. function createLights(){
  140.   //set to true to view light positions
  141.   var helpers = false;
  142.  
  143.   light1 = new THREE.DirectionalLight( 0xffffff, 1 );
  144.   light1.position.set( 5, 5, 10 ).normalize();
  145.   scene.add( light1 );
  146.  
  147.   //Add helper to view light position
  148.   if(helpers){
  149.     light1.helper = new THREE.DirectionalLightHelper( light1, 50 );
  150.     scene.add( light1.helper);
  151.   }
  152.  
  153.   light2 = new THREE.DirectionalLight( 0xffffff, 1 );
  154.   light2.position.set(-1,-1,-5).normalize();
  155.   scene.add( light2 );
  156.  
  157.   //Add helper to view light position
  158.   if(helpers){
  159.     light2.helper = new THREE.DirectionalLightHelper( light2, 50 );
  160.     scene.add( light2.helper);
  161.   }
  162.  
  163.   return true;
  164. }
  165.  
  166. function createPlane(){
  167.   //PlaneGeometry(width : Float, height : Float, widthSegments : Integer, heightSegments : Integer)
  168.   var geometry = new THREE.PlaneGeometry( 20, 20, 32 );
  169.   var material = new THREE.MeshBasicMaterial( {color: 0xffff00, side: THREE.DoubleSide} );
  170.   var plane = new THREE.Mesh( geometry, material );
  171.   scene.add( plane );
  172.  
  173.   return plane;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement