Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <body>
- </body>
- <script src="http://gamingJS.com/Three.js"></script>
- <script src="http://gamingJS.com/Tween.js"></script>
- <script src="http://gamingJS.com/ChromeFixes.js"></script>
- <script src="http://gamingJS.com/Scoreboard.js"></script>
- <script src="http://gamingJS.com/Sounds.js"></script>
- <script>
- /*
- Fruit Hunt
- Created by Natasha K., modified by Charlie C.
- Last updated 10/19/17
- */
- // This is where stuff in our game will happen:
- var scene = new THREE.Scene();
- // This is what sees the stuff:
- var aspect_ratio = window.innerWidth / window.innerHeight;
- var camera = new THREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
- camera.position.z = 900;
- camera.position.y = 750;
- camera.rotation.x = Math.PI / - 4;
- //scene.add(camera);
- // This will draw what the camera sees onto the screen:
- var renderer = new THREE.WebGLRenderer();
- renderer.setSize(window.innerWidth, window.innerHeight);
- document.body.appendChild(renderer.domElement);
- // ******** START CODING ON THE NEXT LINE ********
- //Sets up the scoreboard with controls
- var scoreboard = new Scoreboard();
- scoreboard.countdown(59);
- scoreboard.score();
- scoreboard.help(
- 'Up & Down Arrows: Move || ' +
- 'Shift: Run || ' +
- 'Left & Right Arrows: Turn || ' +
- 'C: Cartwheel || ' +
- 'F: Flip || ' +
- 'R: Reset Orientation'
- );
- //Sets the game to game-over on time expiration
- var game_over = false;
- scoreboard.onTimeExpired(function() {
- scoreboard.message("Game Over! R to Restart!");
- game_over = true;
- }
- );
- //Base container of the player model
- var marker = new THREE.Object3D();
- scene.add(marker);
- //Create the body
- var body = new THREE.SphereGeometry(100);
- var avatar = new THREE.Mesh(body, new THREE.MeshBasicMaterial({color: 0x77d19b}));
- marker.add(avatar);
- var hand = new THREE.SphereGeometry(50);
- //Create the hands
- var right_hand = new THREE.Mesh(hand, new THREE.MeshBasicMaterial({color: 0xd1c6ab}));
- right_hand.position.set(-150, 0, 0);
- avatar.add(right_hand);
- var left_hand = new THREE.Mesh(hand, new THREE.MeshBasicMaterial({color: 0xd1c6ab}));
- left_hand.position.set(150, 0, 0);
- avatar.add(left_hand);
- var foot = new THREE.SphereGeometry(50);
- //Create the feet
- var right_foot = new THREE.Mesh(foot, new THREE.MeshBasicMaterial({color: 0x5e5642}));
- right_foot.position.set(-75, -125, 0);
- avatar.add(right_foot);
- var left_foot = new THREE.Mesh(foot, new THREE.MeshBasicMaterial({color: 0x5e5642}));
- left_foot.position.set(75, -125, 0);
- avatar.add(left_foot);
- var dome = new THREE.SphereGeometry(65);
- //Create the head
- var head = new THREE.Mesh(dome, new THREE.MeshBasicMaterial({color: 0xd1c6ab}));
- head.position.set(0, 165, 0);
- avatar.add(head);
- var eye = new THREE.SphereGeometry(25);
- //Create the eyes
- var right_eye = new THREE.Mesh(eye, new THREE.MeshBasicMaterial({color: 0x888888}));
- right_eye.position.set(-25, 165, 45);
- avatar.add(right_eye);
- var left_eye = new THREE.Mesh(eye, new THREE.MeshBasicMaterial({color: 0x888888}));
- left_eye.position.set(25, 165, 45);
- avatar.add(left_eye);
- avatar.rotation.y = Math.PI;
- marker.position.z = 500;
- marker.add(camera);
- var tree_with_treasure;
- var trees = [];
- var not_allowed = [];
- //Create trees
- trees.push(makeTreeAt(900, 900));
- trees.push(makeTreeAt(900, -900));
- trees.push(makeTreeAt(1200, 300));
- trees.push(makeTreeAt(1200, -300));
- trees.push(makeTreeAt(300, 1200));
- trees.push(makeTreeAt(300, -1200));
- trees.push(makeTreeAt(-900, 900));
- trees.push(makeTreeAt(-900, -900));
- trees.push(makeTreeAt(-1200, 300));
- trees.push(makeTreeAt(-1200, -300));
- trees.push(makeTreeAt(-300, 1200));
- trees.push(makeTreeAt(-300, -1200));
- //Returns the top leaves of a tree to be added to the trees list
- function makeTreeAt(x, z) {
- // Don't change any code at the start...
- var trunk = new THREE.Mesh(
- new THREE.CylinderGeometry(50, 50, 200),
- new THREE.MeshBasicMaterial({color: 0xA0522D})
- );
- var top = new THREE.Mesh(
- new THREE.SphereGeometry(150),
- new THREE.MeshBasicMaterial({color: 0x228B22})
- );
- top.position.y = 225;
- trunk.add(top);
- var fruit = new THREE.Mesh(
- new THREE.SphereGeometry(75),
- new THREE.MeshBasicMaterial({color: 0xff0000})
- );
- fruit.position.y = 275;
- trunk.add(fruit);
- var boundary = new THREE.Mesh(
- new THREE.CircleGeometry(300),
- new THREE.MeshNormalMaterial()
- );
- boundary.position.y = -100;
- boundary.rotation.x = -Math.PI/2;
- trunk.add(boundary);
- not_allowed.push(boundary);
- trunk.position.set(x, -75, z);
- scene.add(trunk);
- // ... but add the following line to the end:
- return top;
- }
- var claimed;
- //Chooses a random tree to start shaking and animates it.
- function shakeTree() {
- if (game_over) return;
- if (tree_with_treasure != undefined) trees[tree_with_treasure].position.x = 0;
- tree_with_treasure = Math.floor(Math.random() * trees.length);
- claimed = false;
- new TWEEN
- .Tween({x: 0})
- .to({x: 2*Math.PI}, 300)
- .repeat(20)
- .onUpdate(function () { if (!claimed) trees[tree_with_treasure].position.x = 75 * Math.sin(this.x);
- })
- .start();
- setTimeout(shakeTree, 6*1000);
- }
- shakeTree();
- // Now, animate what the camera sees on the screen:
- var clock = new THREE.Clock(true);
- function animate() {
- requestAnimationFrame(animate);
- TWEEN.update();
- walk();
- run();
- turn();
- acrobatics();
- renderer.render(scene, camera);
- }
- animate();
- var speed = 10;
- var rotSpeed = Math.PI / 60;
- //Default movement speed
- var is_cartwheeling = false;
- var is_flipping = false;
- var is_running = false;
- //Booleans for movement
- var is_turning_left;
- var is_turning_right;
- var is_moving_forward;
- var is_moving_back;
- function walk() {
- if (!isWalking()) { //Returns the hands and feet to their default position when not walking
- right_hand.position.z = 0;
- left_hand.position.z = 0;
- right_foot.position.z = 0;
- left_foot.position.z = 0;
- return;
- }
- //Uses trig and the clock to animate walking
- var position = Math.sin(clock.getElapsedTime()*speed) * 50;
- right_hand.position.z = position;
- left_hand.position.z = -position;
- right_foot.position.z = -position;
- left_foot.position.z = position;
- }
- //Checks the is_running flag to change the movement speeds
- function run() {
- if (is_running) {
- speed = 20;
- rotSpeed = Math.PI / 45;
- } else {
- speed = 10;
- rotSpeed = Math.PI / 60;
- }
- }
- //Turns the player according to the turning flags
- function turn() { //The comments here are for trying to get the camera to pan around the player, still working that out
- var direction = avatar.rotation.y;
- if (is_turning_right) {
- avatar.rotation.y -= rotSpeed;
- // camera.rotation.y += rotSpeed;
- // var newdirection = avatar.rotation.y - direction;
- // camera.position.x += 50 * Math.sin(newdirection) * Math.cos(newdirection);
- // camera.position.z += 50 * Math.sin(newdirection) * Math.sin(newdirection);
- }
- if (is_turning_left) {
- avatar.rotation.y += rotSpeed;
- // camera.rotation.y -= rotSpeed;
- // var newdirection = avatar.rotation.y - direction;
- // camera.position.x += 50 * Math.sin(newdirection) * Math.cos(newdirection);
- // camera.position.z += 50 * Math.sin(newdirection) * Math.sin(newdirection);
- }
- }
- //Playfully rotate the player
- function acrobatics() {
- if (is_cartwheeling) {
- avatar.rotation.z += 0.05;
- }
- if (is_flipping) {
- avatar.rotation.x += 0.05;
- }
- }
- //Sets the movement flags
- function isWalking() {
- if (detectCollisions()) { //Pushes the player back from a tree
- // if (is_moving_left) marker.position.x += speed;
- // if (is_moving_right) marker.position.x -= speed;
- if (is_moving_forward) {
- marker.position.z -= 2 * speed * Math.cos(avatar.rotation.y);
- marker.position.x -= 2 * speed * Math.sin(avatar.rotation.y);
- return true;
- }
- if (is_moving_back) {
- marker.position.z += 2 * speed * Math.cos(avatar.rotation.y);
- marker.position.x += 2 * speed * Math.sin(avatar.rotation.y);
- return true;
- }
- } else {
- if (is_moving_forward) {
- marker.position.z += speed * Math.cos(avatar.rotation.y);
- marker.position.x += speed * Math.sin(avatar.rotation.y);
- return true;
- }
- if (is_moving_back) {
- marker.position.z -= speed * Math.cos(avatar.rotation.y);
- marker.position.x -= speed * Math.sin(avatar.rotation.y);
- return true;
- }
- }
- return false;
- }
- //Checks for key presses
- document.addEventListener('keydown', function(event) {
- var code = event.keyCode;
- if (code == 32) jump(); // space
- if (code == 37) { // left
- is_turning_left = true;
- }
- if (code == 38) { // up
- is_moving_forward = true;
- }
- if (code == 39) { // right
- is_turning_right = true;
- }
- if (code == 40) { // down
- is_moving_back = true;
- }
- if (code == 16) { //Shift
- is_running = true;
- }
- if (code == 67) is_cartwheeling = true; // C
- if (code == 70) is_flipping = true; // F
- if (code == 82) { //R
- avatar.rotation.x = 0;
- avatar.rotation.y = Math.PI;
- avatar.rotation.z = 0;
- is_cartwheeling = false;
- is_flipping = false;
- if (game_over) { //Resets the game
- scoreboard.score(0);
- scoreboard.message("");
- game_over = false;
- scoreboard.countdown(59);
- shakeTree();
- }
- }
- }
- );
- //Resets flags on key press ends
- document.addEventListener('keyup', function(event) {
- var code = event.keyCode;
- if (code == 37) is_turning_left = false;
- if (code == 39) is_turning_right = false;
- if (code == 38) is_moving_forward = false;
- if (code == 40) is_moving_back = false;
- if (code == 16) is_running = false;
- if (code == 67) is_cartwheeling = false;
- if (code == 70) is_flipping = false;
- }
- );
- //Checks for if the player is too close to a tree
- function detectCollisions() {
- var vector = new THREE.Vector3(0, -1, 0);
- var ray = new THREE.Ray(marker.position, vector);
- var intersects = ray.intersectObjects(not_allowed);
- if (intersects.length > 0) return true;
- return false;
- }
- //Makes the player jump to check for apples
- function jump() {
- animateJump();
- checkForTreasure();
- }
- //Checks to see if the treasure tree
- function checkForTreasure() {
- if (tree_with_treasure == undefined || claimed) return;
- var treasure_tree = trees[tree_with_treasure];
- var p1 = treasure_tree.parent.position;
- var p2 = marker.position;
- var distance = Math.sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.z - p2.z)*(p1.z - p2.z) );
- if (distance < 500) {
- scorePoints();
- claimed = true;
- treasureFound();
- }
- }
- //Makes the tree jump if it's the treasure tree and the player is near enough
- function treasureFound() {
- trees[tree_with_treasure].position.x = 0;
- new TWEEN
- .Tween({raise: 0})
- .to({raise: Math.PI}, 400)
- .onUpdate(function () {
- trees[tree_with_treasure].position.y = 150 * Math.sin(this.raise) + 225;
- })
- .start();
- }
- //Adds points to the scoreboard
- function scorePoints() {
- if (scoreboard.getTimeRemaining() == 0) return;
- scoreboard.addPoints(10);
- }
- //Animates the player jumping
- function animateJump() {
- new TWEEN
- .Tween({jump: 0})
- .to({jump: Math.PI}, 500)
- .onUpdate(function () {marker.position.y = 200* Math.sin(this.jump);})
- .start();
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement