Advertisement
cookchar

FruitHunt Beta 3

Oct 19th, 2017
66
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.     /*
  12.         Fruit Hunt
  13.         Created by Natasha K., modified by Charlie C.
  14.         Last updated 10/19/17
  15.     */
  16.  
  17.     // This is where stuff in our game will happen:
  18.     var scene = new THREE.Scene();
  19.  
  20.     // This is what sees the stuff:
  21.     var aspect_ratio = window.innerWidth / window.innerHeight;
  22.     var camera = new THREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
  23.     camera.position.z = 900;
  24.     camera.position.y = 750;
  25.     camera.rotation.x = Math.PI / - 4;
  26.     //scene.add(camera);
  27.  
  28.     // This will draw what the camera sees onto the screen:
  29.     var renderer = new THREE.WebGLRenderer();
  30.     renderer.setSize(window.innerWidth, window.innerHeight);
  31.     document.body.appendChild(renderer.domElement);
  32.  
  33.     // ******** START CODING ON THE NEXT LINE ********
  34.     //Sets up the scoreboard with controls
  35.     var scoreboard = new Scoreboard();
  36.     scoreboard.countdown(59);
  37.     scoreboard.score();
  38.     scoreboard.help(
  39.         'Up & Down Arrows: Move || ' +
  40.         'Shift: Run || ' +
  41.         'Left & Right Arrows: Turn || ' +
  42.         'C: Cartwheel || ' +
  43.         'F: Flip || ' +
  44.         'R: Reset Orientation'
  45.     );
  46.     //Sets the game to game-over on time expiration
  47.     var game_over = false;
  48.     scoreboard.onTimeExpired(function() {
  49.         scoreboard.message("Game Over! R to Restart!");
  50.         game_over = true;
  51.     }
  52.     );
  53.     //Base container of the player model
  54.     var marker = new THREE.Object3D();
  55.     scene.add(marker);
  56.     //Create the body
  57.     var body = new THREE.SphereGeometry(100);
  58.     var avatar = new THREE.Mesh(body, new THREE.MeshBasicMaterial({color: 0x77d19b}));
  59.     marker.add(avatar);
  60.  
  61.     var hand = new THREE.SphereGeometry(50);
  62.     //Create the hands
  63.     var right_hand = new THREE.Mesh(hand, new THREE.MeshBasicMaterial({color: 0xd1c6ab}));
  64.     right_hand.position.set(-150, 0, 0);
  65.     avatar.add(right_hand);
  66.  
  67.     var left_hand = new THREE.Mesh(hand, new THREE.MeshBasicMaterial({color: 0xd1c6ab}));
  68.     left_hand.position.set(150, 0, 0);
  69.     avatar.add(left_hand);
  70.  
  71.     var foot = new THREE.SphereGeometry(50);
  72.     //Create the feet
  73.     var right_foot = new THREE.Mesh(foot, new THREE.MeshBasicMaterial({color: 0x5e5642}));
  74.     right_foot.position.set(-75, -125, 0);
  75.     avatar.add(right_foot);
  76.  
  77.     var left_foot = new THREE.Mesh(foot, new THREE.MeshBasicMaterial({color: 0x5e5642}));
  78.     left_foot.position.set(75, -125, 0);
  79.     avatar.add(left_foot);
  80.  
  81.     var dome = new THREE.SphereGeometry(65);
  82.     //Create the head
  83.     var head = new THREE.Mesh(dome, new THREE.MeshBasicMaterial({color: 0xd1c6ab}));
  84.     head.position.set(0, 165, 0);
  85.     avatar.add(head);
  86.  
  87.     var eye = new THREE.SphereGeometry(25);
  88.     //Create the eyes
  89.     var right_eye = new THREE.Mesh(eye, new THREE.MeshBasicMaterial({color: 0x888888}));
  90.     right_eye.position.set(-25, 165, 45);
  91.     avatar.add(right_eye);
  92.  
  93.     var left_eye = new THREE.Mesh(eye, new THREE.MeshBasicMaterial({color: 0x888888}));
  94.     left_eye.position.set(25, 165, 45);
  95.     avatar.add(left_eye);
  96.  
  97.     avatar.rotation.y = Math.PI;
  98.  
  99.     marker.position.z = 500;
  100.     marker.add(camera);
  101.  
  102.     var tree_with_treasure;
  103.     var trees = [];
  104.     var not_allowed = [];
  105.     //Create trees
  106.     trees.push(makeTreeAt(900, 900));
  107.     trees.push(makeTreeAt(900, -900));
  108.     trees.push(makeTreeAt(1200, 300));
  109.     trees.push(makeTreeAt(1200, -300));
  110.     trees.push(makeTreeAt(300, 1200));
  111.     trees.push(makeTreeAt(300, -1200));
  112.  
  113.     trees.push(makeTreeAt(-900, 900));
  114.     trees.push(makeTreeAt(-900, -900));
  115.     trees.push(makeTreeAt(-1200, 300));
  116.     trees.push(makeTreeAt(-1200, -300));
  117.     trees.push(makeTreeAt(-300, 1200));
  118.     trees.push(makeTreeAt(-300, -1200));
  119.     //Returns the top leaves of a tree to be added to the trees list
  120.     function makeTreeAt(x, z) {
  121.         // Don't change any code at the start...
  122.         var trunk = new THREE.Mesh(
  123.             new THREE.CylinderGeometry(50, 50, 200),
  124.             new THREE.MeshBasicMaterial({color: 0xA0522D})
  125.         );
  126.  
  127.         var top = new THREE.Mesh(
  128.             new THREE.SphereGeometry(150),
  129.             new THREE.MeshBasicMaterial({color: 0x228B22})
  130.         );
  131.         top.position.y = 225;
  132.         trunk.add(top);
  133.  
  134.         var fruit = new THREE.Mesh(
  135.             new THREE.SphereGeometry(75),
  136.             new THREE.MeshBasicMaterial({color: 0xff0000})
  137.         );
  138.         fruit.position.y = 275;
  139.         trunk.add(fruit);
  140.  
  141.         var boundary = new THREE.Mesh(
  142.             new THREE.CircleGeometry(300),
  143.             new THREE.MeshNormalMaterial()
  144.         );
  145.         boundary.position.y = -100;
  146.         boundary.rotation.x = -Math.PI/2;
  147.         trunk.add(boundary);
  148.  
  149.         not_allowed.push(boundary);
  150.  
  151.         trunk.position.set(x, -75, z);
  152.         scene.add(trunk);
  153.         // ... but add the following line to the end:
  154.         return top;
  155.     }
  156.  
  157.     var claimed;
  158.     //Chooses a random tree to start shaking and animates it.
  159.     function shakeTree() {
  160.         if (game_over) return;
  161.         if (tree_with_treasure != undefined) trees[tree_with_treasure].position.x = 0;
  162.         tree_with_treasure = Math.floor(Math.random() * trees.length);
  163.         claimed = false;
  164.  
  165.         new TWEEN
  166.             .Tween({x: 0})
  167.             .to({x: 2*Math.PI}, 300)
  168.             .repeat(20)
  169.             .onUpdate(function () { if (!claimed) trees[tree_with_treasure].position.x = 75 * Math.sin(this.x);
  170.             })
  171.             .start();
  172.  
  173.         setTimeout(shakeTree, 6*1000);
  174.     }
  175.  
  176.     shakeTree();
  177.  
  178.     // Now, animate what the camera sees on the screen:
  179.     var clock = new THREE.Clock(true);
  180.     function animate() {
  181.         requestAnimationFrame(animate);
  182.         TWEEN.update();
  183.         walk();
  184.         run();
  185.         turn();
  186.         acrobatics();
  187.         renderer.render(scene, camera);
  188.     }
  189.     animate();
  190.  
  191.     var speed = 10;
  192.     var rotSpeed = Math.PI / 60;
  193.     //Default movement speed
  194.     var is_cartwheeling = false;
  195.     var is_flipping = false;
  196.     var is_running = false;
  197.     //Booleans for movement
  198.     var is_turning_left;
  199.     var is_turning_right;
  200.     var is_moving_forward;
  201.     var is_moving_back;
  202.  
  203.     function walk() {
  204.         if (!isWalking()) { //Returns the hands and feet to their default position when not walking
  205.             right_hand.position.z = 0;
  206.             left_hand.position.z = 0;
  207.             right_foot.position.z = 0;
  208.             left_foot.position.z = 0;
  209.             return;
  210.         }
  211.         //Uses trig and the clock to animate walking
  212.         var position = Math.sin(clock.getElapsedTime()*speed) * 50;
  213.         right_hand.position.z = position;
  214.         left_hand.position.z = -position;
  215.         right_foot.position.z = -position;
  216.         left_foot.position.z = position;
  217.     }
  218.     //Checks the is_running flag to change the movement speeds
  219.     function run() {
  220.         if (is_running) {
  221.             speed = 20;
  222.             rotSpeed = Math.PI / 45;
  223.         } else {
  224.             speed = 10;
  225.             rotSpeed = Math.PI / 60;
  226.         }
  227.     }
  228.     //Turns the player according to the turning flags
  229.     function turn() {   //The comments here are for trying to get the camera to pan around the player, still working that out
  230.         var direction = avatar.rotation.y;
  231.  
  232.         if (is_turning_right) {
  233.             avatar.rotation.y -= rotSpeed;
  234.  
  235.         //  camera.rotation.y += rotSpeed;
  236.         //  var newdirection = avatar.rotation.y - direction;
  237.         //  camera.position.x += 50 * Math.sin(newdirection) * Math.cos(newdirection);
  238.         //  camera.position.z += 50 * Math.sin(newdirection) * Math.sin(newdirection);
  239.         }
  240.         if (is_turning_left) {
  241.             avatar.rotation.y += rotSpeed;
  242.  
  243.         //  camera.rotation.y -= rotSpeed;
  244.         //  var newdirection = avatar.rotation.y - direction;
  245.         //  camera.position.x += 50 * Math.sin(newdirection) * Math.cos(newdirection);
  246.         //  camera.position.z += 50 * Math.sin(newdirection) * Math.sin(newdirection);
  247.         }
  248.     }
  249.     //Playfully rotate the player
  250.     function acrobatics() {
  251.         if (is_cartwheeling) {
  252.             avatar.rotation.z += 0.05;
  253.         }
  254.         if (is_flipping) {
  255.             avatar.rotation.x += 0.05;
  256.         }
  257.     }
  258.     //Sets the movement flags
  259.     function isWalking() {
  260.  
  261.         if (detectCollisions()) {   //Pushes the player back from a tree
  262.         //  if (is_moving_left) marker.position.x += speed;
  263.         //  if (is_moving_right) marker.position.x -= speed;
  264.             if (is_moving_forward) {
  265.                 marker.position.z -= 2 * speed * Math.cos(avatar.rotation.y);
  266.                 marker.position.x -= 2 * speed * Math.sin(avatar.rotation.y);
  267.                 return true;
  268.             }
  269.             if (is_moving_back) {
  270.                 marker.position.z += 2 * speed * Math.cos(avatar.rotation.y);
  271.                 marker.position.x += 2 * speed * Math.sin(avatar.rotation.y);
  272.                 return true;
  273.             }
  274.         } else {
  275.             if (is_moving_forward) {
  276.                 marker.position.z += speed * Math.cos(avatar.rotation.y);
  277.                 marker.position.x += speed * Math.sin(avatar.rotation.y);
  278.                 return true;
  279.             }
  280.             if (is_moving_back) {
  281.                 marker.position.z -= speed * Math.cos(avatar.rotation.y);
  282.                 marker.position.x -= speed * Math.sin(avatar.rotation.y);
  283.                 return true;
  284.             }
  285.         }
  286.         return false;
  287.     }
  288.     //Checks for key presses
  289.     document.addEventListener('keydown', function(event) {
  290.         var code = event.keyCode;
  291.         if (code == 32) jump(); // space
  292.  
  293.         if (code == 37) {   // left
  294.             is_turning_left = true;
  295.         }
  296.         if (code == 38) {   // up
  297.             is_moving_forward = true;
  298.         }
  299.         if (code == 39) {   // right
  300.             is_turning_right = true;
  301.         }
  302.         if (code == 40) {   // down
  303.             is_moving_back = true;
  304.         }
  305.         if (code == 16) {   //Shift
  306.             is_running = true;
  307.         }
  308.  
  309.         if (code == 67) is_cartwheeling = true; // C
  310.         if (code == 70) is_flipping = true; // F
  311.  
  312.         if (code == 82) {   //R
  313.             avatar.rotation.x = 0;
  314.             avatar.rotation.y = Math.PI;
  315.             avatar.rotation.z = 0;
  316.             is_cartwheeling = false;
  317.             is_flipping = false;
  318.  
  319.             if (game_over) {    //Resets the game
  320.                 scoreboard.score(0);
  321.                 scoreboard.message("");
  322.                 game_over = false;
  323.                 scoreboard.countdown(59);
  324.                 shakeTree();
  325.             }
  326.         }
  327.     }
  328.     );
  329.     //Resets flags on key press ends
  330.     document.addEventListener('keyup', function(event) {
  331.         var code = event.keyCode;
  332.  
  333.         if (code == 37) is_turning_left = false;
  334.         if (code == 39) is_turning_right = false;
  335.         if (code == 38) is_moving_forward = false;
  336.         if (code == 40) is_moving_back = false;
  337.         if (code == 16) is_running = false;
  338.  
  339.         if (code == 67) is_cartwheeling = false;
  340.         if (code == 70) is_flipping = false;
  341.     }
  342.     );
  343.     //Checks for if the player is too close to a tree
  344.     function detectCollisions() {
  345.         var vector = new THREE.Vector3(0, -1, 0);
  346.         var ray = new THREE.Ray(marker.position, vector);
  347.         var intersects = ray.intersectObjects(not_allowed);
  348.         if (intersects.length > 0) return true;
  349.         return false;
  350.     }
  351.     //Makes the player jump to check for apples
  352.     function jump() {
  353.         animateJump();
  354.         checkForTreasure();
  355.     }
  356.     //Checks to see if the treasure tree
  357.     function checkForTreasure() {
  358.         if (tree_with_treasure == undefined || claimed) return;
  359.  
  360.         var treasure_tree = trees[tree_with_treasure];
  361.         var p1 = treasure_tree.parent.position;
  362.         var p2 = marker.position;
  363.  
  364.         var distance = Math.sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.z - p2.z)*(p1.z - p2.z) );
  365.  
  366.         if (distance < 500) {
  367.             scorePoints();
  368.             claimed = true;
  369.             treasureFound();
  370.         }
  371.     }
  372.     //Makes the tree jump if it's the treasure tree and the player is near enough
  373.     function treasureFound() {
  374.         trees[tree_with_treasure].position.x = 0;
  375.         new TWEEN
  376.             .Tween({raise: 0})
  377.             .to({raise: Math.PI}, 400)
  378.             .onUpdate(function () {
  379.                 trees[tree_with_treasure].position.y = 150 * Math.sin(this.raise) + 225;
  380.             })
  381.             .start();
  382.     }
  383.     //Adds points to the scoreboard
  384.     function scorePoints() {
  385.         if (scoreboard.getTimeRemaining() == 0) return;
  386.         scoreboard.addPoints(10);
  387.     }
  388.     //Animates the player jumping
  389.     function animateJump() {
  390.         new TWEEN
  391.             .Tween({jump: 0})
  392.             .to({jump: Math.PI}, 500)
  393.             .onUpdate(function () {marker.position.y = 200* Math.sin(this.jump);})
  394.             .start();
  395.     }
  396.  
  397. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement