Code viewer for World: 3. Making an Avatar
/*  
Proposed port of "building_an_avatar"
Adapted to the Ancient Brain "three" API
Awaiting permission to do this
*/

/*
 ! Excerpted from "3D Game Programming for Kids, Second Edition",
 ! published by The Pragmatic Bookshelf.
 ! Copyrights apply to this code. It may not be used to create training material,
 ! courses, books, articles, and the like. Contact us if you are in doubt.
 ! We make no guarantees that this code is fit for any purpose.
 ! Visit http://www.pragmaticprogrammer.com/titles/csjava2 for more book information.
*/


 
const startRadius       = 500;                 // distance from centre we start the camera at

const maxRadius         = startRadius * 10;     // maximum distance from camera we render things 

const skycolor          = 'LightBlue';          // sky color

const skycolorObject    = new THREE.Color ( skycolor.toLowerCase() );                              

// not using API camera control buttons (see API docs):

ABWorld.drawCameraControls = false;


// change these:
  var isCartwheeling = true;
  var isFlipping = false;
 
 
    var avatar;
    

AB.world.newRun = function()
{
 // customised renderer argument:
 ABWorld.renderer = new THREE.WebGLRenderer ( { antialias: true } );

 // set up a 3D scene:
 ABWorld.init3d ( startRadius, maxRadius, skycolorObject ); 

  AB.msg ( " <p> Drag and zoom the camera (built-in). </p> " ); 

 var flat = {flatShading: true};
 var light = new THREE.AmbientLight('white', 0.8);
 ABWorld.scene.add( light );

  var body = new THREE.SphereGeometry(100);
  var cover = new THREE.MeshNormalMaterial();
    avatar = new THREE.Mesh(body, cover);
  ABWorld.scene.add(avatar);

  var hand = new THREE.SphereGeometry(50);

  var rightHand = new THREE.Mesh(hand, cover);
  rightHand.position.set(-150, 0, 0);
  avatar.add(rightHand);

  var leftHand = new THREE.Mesh(hand, cover);
  leftHand.position.set(150, 0, 0);
  avatar.add(leftHand);

  var foot = new THREE.SphereGeometry(50);

  var rightFoot = new THREE.Mesh(foot, cover);
  rightFoot.position.set(-75, -125, 0);
  avatar.add(rightFoot);

  var leftFoot = new THREE.Mesh(foot, cover);
  leftFoot.position.set(75, -125, 0);
  avatar.add(leftFoot);
};



AB.world.nextStep = function()		 
{
    if (isCartwheeling) 
      avatar.rotation.z = avatar.rotation.z + 0.05;
    
    if (isFlipping) 
      avatar.rotation.x = avatar.rotation.x + 0.05;
};