Advertisement
cookchar

FruitHunt Beta 2

Oct 17th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <body>
  2. </body>
  3.  
  4. <script src="http://gamingJS.com/Three.js"></script>
  5. <script src="http://gamingJS.com/Tween.js"></script>
  6. <script src="http://gamingJS.com/ChromeFixes.js"></script>
  7. <script src="http://gamingJS.com/Scoreboard.js"></script>
  8. <script src="http://gamingJS.com/Sounds.js"></script>
  9.  
  10. <script>
  11.     // This is where stuff in our game will happen:
  12.     var scene = new THREE.Scene();
  13.  
  14.     // This is what sees the stuff:
  15.     var aspect_ratio = window.innerWidth / window.innerHeight;
  16.     var camera = new THREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
  17.     camera.position.z = 500;
  18.     //scene.add(camera);
  19.  
  20.     // This will draw what the camera sees onto the screen:
  21.     var renderer = new THREE.WebGLRenderer();
  22.     renderer.setSize(window.innerWidth, window.innerHeight);
  23.     document.body.appendChild(renderer.domElement);
  24.  
  25.     // ******** START CODING ON THE NEXT LINE ********
  26.     var not_allowed = [];
  27.  
  28.     var scoreboard = new Scoreboard();
  29.     scoreboard.countdown(45);
  30.     scoreboard.score();
  31.     scoreboard.help(
  32.         'Arrow keys to move. ' +
  33.         'Space bar to jump for fruit. ' +
  34.         'Watch for shaking trees with fruit.' +
  35.         'Get near the tree and jump before the fruit is gone!'
  36.     );
  37.     var game_over = false;
  38.     scoreboard.onTimeExpired(function() {
  39.         scoreboard.message("Game Over!");
  40.         game_over = true;
  41.     }
  42.     );
  43.  
  44.     var marker = new THREE.Object3D();
  45.     scene.add(marker);
  46.  
  47.     var cover = new THREE.MeshNormalMaterial();
  48.     var body = new THREE.SphereGeometry(100);
  49.     var avatar = new THREE.Mesh(body, cover);
  50.     marker.add(avatar);
  51.  
  52.     var hand = new THREE.SphereGeometry(50);
  53.  
  54.     var right_hand = new THREE.Mesh(hand, cover);
  55.     right_hand.position.set(-150, 0, 0);
  56.     avatar.add(right_hand);
  57.  
  58.     var left_hand = new THREE.Mesh(hand, cover);
  59.     left_hand.position.set(150, 0, 0);
  60.     avatar.add(left_hand);
  61.  
  62.     var foot = new THREE.SphereGeometry(50);
  63.  
  64.     var right_foot = new THREE.Mesh(foot, cover);
  65.     right_foot.position.set(-75, -125, 0);
  66.     avatar.add(right_foot);
  67.  
  68.     var left_foot = new THREE.Mesh(foot, cover);
  69.     left_foot.position.set(75, -125, 0);
  70.     avatar.add(left_foot);
  71.  
  72.     var dome = new THREE.SphereGeometry(65);
  73.  
  74.     var head = new THREE.Mesh(dome, cover);
  75.     head.position.set(0, 165, 0);
  76.     avatar.add(head);
  77.  
  78.     var eye = new THREE.SphereGeometry(25);
  79.  
  80.     var right_eye = new THREE.Mesh(eye, cover);
  81.     right_eye.position.set(-25, 165, 45);
  82.     avatar.add(right_eye);
  83.  
  84.     var left_eye = new THREE.Mesh(eye, cover);
  85.     left_eye.position.set(25, 165, 45);
  86.     avatar.add(left_eye);
  87.  
  88.     avatar.rotation.y = Math.PI;
  89.  
  90.     marker.add(camera);
  91.  
  92.     var tree_with_treasure;
  93.     var trees = [];
  94.     trees.push(makeTreeAt( 500,  0));
  95.     trees.push(makeTreeAt(-500,  0));
  96.     trees.push(makeTreeAt( 750, -1000));
  97.     trees.push(makeTreeAt(-750, -1000));
  98.  
  99.     function makeTreeAt(x, z) {
  100.         // Don't change any code at the start...
  101.         var trunk = new THREE.Mesh(
  102.             new THREE.CylinderGeometry(50, 50, 200),
  103.             new THREE.MeshBasicMaterial({color: 0xA0522D})
  104.         );
  105.  
  106.         var top = new THREE.Mesh(
  107.             new THREE.SphereGeometry(150),
  108.             new THREE.MeshBasicMaterial({color: 0x228B22})
  109.         );
  110.         top.position.y = 225;
  111.         trunk.add(top);
  112.  
  113.         var boundary = new THREE.Mesh(
  114.             new THREE.CircleGeometry(300),
  115.             new THREE.MeshNormalMaterial()
  116.         );
  117.         boundary.position.y = -100;
  118.         boundary.rotation.x = -Math.PI/2;
  119.         trunk.add(boundary);
  120.  
  121.         not_allowed.push(boundary);
  122.  
  123.         trunk.position.set(x, -75, z);
  124.         scene.add(trunk);
  125.         // ... but add the following line to the end:
  126.         return top;
  127.     }
  128.  
  129.     function shakeTree() {
  130.         tree_with_treasure = Math.floor(Math.random() * trees.length);
  131.  
  132.         new TWEEN
  133.             .Tween({x: 0})
  134.             .to({x: 2*Math.PI}, 200)
  135.             .repeat(20)
  136.             .onUpdate(function () {trees[tree_with_treasure].position.x = 75 * Math.sin(this.x);})
  137.             .start();
  138.  
  139.         setTimeout(shakeTree, 12*1000);
  140.     }
  141.  
  142.     shakeTree();
  143.  
  144.     // Now, animate what the camera sees on the screen:
  145.     var clock = new THREE.Clock(true);
  146.     function animate() {
  147.         requestAnimationFrame(animate);
  148.         TWEEN.update();
  149.         walk();
  150.         turn();
  151.         acrobatics();
  152.         renderer.render(scene, camera);
  153.     }
  154.     animate();
  155.  
  156.     var speed = 20;
  157.     var rotSpeed = Math.PI / 60;
  158.  
  159.     var is_cartwheeling = false;
  160.     var is_flipping = false;
  161.  
  162.     var is_turning_left;
  163.     var is_turning_right;
  164.     var is_moving_forward;
  165.     var is_moving_back;
  166.  
  167.     function walk() {
  168.         if (!isWalking()) return;
  169.         var position = Math.sin(clock.getElapsedTime()*speed) * 50;
  170.         right_hand.position.z = position;
  171.         left_hand.position.z = -position;
  172.         right_foot.position.z = -position;
  173.         left_foot.position.z = position;
  174.     }
  175.  
  176.     function turn() {
  177.         var direction = 0;
  178.         if (is_turning_right) {
  179.             avatar.rotation.y += rotSpeed;
  180.             camera.rotation.y += rotSpeed;
  181.             camera.position.x += 50 * Math.sin(avatar.rotation.y);
  182.             camera.position.z += 50 * Math.cos(avatar.rotation.y);
  183.         }
  184.         if (is_turning_left) {
  185.             avatar.rotation.y -= rotSpeed;
  186.             camera.rotation.y -= rotSpeed;
  187.             camera.position.x -= 50 * Math.sin(avatar.rotation.y);
  188.             camera.position.z -= 50 * Math.cos(avatar.rotation.y);
  189.         }
  190.     }
  191.  
  192.     function acrobatics() {
  193.         if (is_cartwheeling) {
  194.             avatar.rotation.z = avatar.rotation.z + 0.05;
  195.         }
  196.         if (is_flipping) {
  197.             avatar.rotation.x = avatar.rotation.x + 0.05;
  198.         }
  199.     }
  200.  
  201.     function isWalking() {
  202.         if (is_moving_forward) return true;
  203.         if (is_moving_back) return true;
  204.         return false;
  205.     }
  206.  
  207.     document.addEventListener('keydown', function(event) {
  208.         var code = event.keyCode;
  209.         if (code == 32) jump(); // space
  210.  
  211.         if (code == 37) {   // left
  212.             is_turning_left = true;
  213.         }
  214.         if (code == 38) {   // up
  215.             marker.position.z += speed * Math.cos(avatar.rotation.y);
  216.             marker.position.x += speed * Math.sin(avatar.rotation.y);
  217.             is_moving_forward = true;
  218.         }
  219.         if (code == 39) {   // right
  220.             is_turning_right = true;
  221.         }
  222.         if (code == 40) {   // down
  223.             marker.position.z -= speed * Math.cos(avatar.rotation.y);
  224.             marker.position.x -= speed * Math.sin(avatar.rotation.y);
  225.             is_moving_back = true;
  226.         }
  227.  
  228.         if (code == 67) is_cartwheeling = true; // C
  229.         if (code == 70) is_flipping = true; // F
  230.  
  231.         if (code == 82) {
  232.             avatar.rotation.x = 0;
  233.             avatar.rotation.z = 0;
  234.             is_cartwheeling = false;
  235.             is_flipping = false;
  236.         }
  237.  
  238.         if (detectCollisions()) {
  239.         //  if (is_moving_left) marker.position.x += speed;
  240.         //  if (is_moving_right) marker.position.x -= speed;
  241.             if (is_moving_forward) {
  242.                 marker.position.z -= speed * Math.cos(avatar.rotation.y);
  243.                 marker.position.x -= speed * Math.sin(avatar.rotation.y);
  244.             }
  245.             if (is_moving_back) {
  246.                 marker.position.z -= speed * Math.cos(avatar.rotation.y);
  247.                 marker.position.x -= speed * Math.sin(avatar.rotation.y);
  248.             }
  249.         }
  250.     }
  251.     );
  252.  
  253.     document.addEventListener('keyup', function(event) {
  254.         var code = event.keyCode;
  255.  
  256.         if (code == 37) is_turning_left = false;
  257.         if (code == 39) is_turning_right = false;
  258.         if (code == 38) is_moving_forward = false;
  259.         if (code == 40) is_moving_back = false;
  260.  
  261.         if (code == 67) is_cartwheeling = false;
  262.         if (code == 70) is_flipping = false;
  263.     }
  264.     );
  265.  
  266.     function detectCollisions() {
  267.         var vector = new THREE.Vector3(0, -1, 0);
  268.         var ray = new THREE.Ray(marker.position, vector);
  269.         var intersects = ray.intersectObjects(not_allowed);
  270.         if (intersects.length > 0) return true;
  271.         return false;
  272.     }
  273.  
  274.     function jump() {
  275.         checkForTreasure();
  276.         animateJump();
  277.     }
  278.  
  279.     function checkForTreasure() {
  280.         if (tree_with_treasure == undefined) return;
  281.  
  282.         var treasure_tree = trees[tree_with_treasure];
  283.         var p1 = treasure_tree.parent.position;
  284.         var p2 = marker.position;
  285.  
  286.         var distance = Math.sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.z - p2.z)*(p1.z - p2.z) );
  287.  
  288.         if (distance < 500) {
  289.             scorePoints();
  290.         }
  291.     }
  292.  
  293.     function scorePoints() {
  294.         if (scoreboard.getTimeRemaining() === 0) return;
  295.         scoreboard.addPoints(10);
  296.     }
  297.  
  298.     function animateJump() {
  299.         new TWEEN
  300.             .Tween({jump: 0})
  301.             .to({jump: Math.PI}, 500)
  302.             .onUpdate(function () {marker.position.y = 200* Math.sin(this.jump);})
  303.             .start();
  304.     }
  305.  
  306. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement