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>
- // 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 = 500;
- //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 ********
- var not_allowed = [];
- var scoreboard = new Scoreboard();
- scoreboard.countdown(45);
- scoreboard.score();
- scoreboard.help(
- 'Arrow keys to move. ' +
- 'Space bar to jump for fruit. ' +
- 'Watch for shaking trees with fruit.' +
- 'Get near the tree and jump before the fruit is gone!'
- );
- var game_over = false;
- scoreboard.onTimeExpired(function() {
- scoreboard.message("Game Over!");
- game_over = true;
- }
- );
- var marker = new THREE.Object3D();
- scene.add(marker);
- var cover = new THREE.MeshNormalMaterial();
- var body = new THREE.SphereGeometry(100);
- var avatar = new THREE.Mesh(body, cover);
- marker.add(avatar);
- var hand = new THREE.SphereGeometry(50);
- var right_hand = new THREE.Mesh(hand, cover);
- right_hand.position.set(-150, 0, 0);
- avatar.add(right_hand);
- var left_hand = new THREE.Mesh(hand, cover);
- left_hand.position.set(150, 0, 0);
- avatar.add(left_hand);
- var foot = new THREE.SphereGeometry(50);
- var right_foot = new THREE.Mesh(foot, cover);
- right_foot.position.set(-75, -125, 0);
- avatar.add(right_foot);
- var left_foot = new THREE.Mesh(foot, cover);
- left_foot.position.set(75, -125, 0);
- avatar.add(left_foot);
- var dome = new THREE.SphereGeometry(65);
- var head = new THREE.Mesh(dome, cover);
- head.position.set(0, 165, 0);
- avatar.add(head);
- var eye = new THREE.SphereGeometry(25);
- var right_eye = new THREE.Mesh(eye, cover);
- right_eye.position.set(-25, 165, 45);
- avatar.add(right_eye);
- var left_eye = new THREE.Mesh(eye, cover);
- left_eye.position.set(25, 165, 45);
- avatar.add(left_eye);
- avatar.rotation.y = Math.PI;
- marker.add(camera);
- var tree_with_treasure;
- var trees = [];
- trees.push(makeTreeAt( 500, 0));
- trees.push(makeTreeAt(-500, 0));
- trees.push(makeTreeAt( 750, -1000));
- trees.push(makeTreeAt(-750, -1000));
- 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 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;
- }
- function shakeTree() {
- tree_with_treasure = Math.floor(Math.random() * trees.length);
- new TWEEN
- .Tween({x: 0})
- .to({x: 2*Math.PI}, 200)
- .repeat(20)
- .onUpdate(function () {trees[tree_with_treasure].position.x = 75 * Math.sin(this.x);})
- .start();
- setTimeout(shakeTree, 12*1000);
- }
- shakeTree();
- // Now, animate what the camera sees on the screen:
- var clock = new THREE.Clock(true);
- function animate() {
- requestAnimationFrame(animate);
- TWEEN.update();
- walk();
- turn();
- acrobatics();
- renderer.render(scene, camera);
- }
- animate();
- var speed = 20;
- var rotSpeed = Math.PI / 60;
- var is_cartwheeling = false;
- var is_flipping = false;
- var is_turning_left;
- var is_turning_right;
- var is_moving_forward;
- var is_moving_back;
- function walk() {
- if (!isWalking()) return;
- 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;
- }
- function turn() {
- var direction = 0;
- if (is_turning_right) {
- avatar.rotation.y += rotSpeed;
- camera.rotation.y += rotSpeed;
- camera.position.x += 50 * Math.sin(avatar.rotation.y);
- camera.position.z += 50 * Math.cos(avatar.rotation.y);
- }
- if (is_turning_left) {
- avatar.rotation.y -= rotSpeed;
- camera.rotation.y -= rotSpeed;
- camera.position.x -= 50 * Math.sin(avatar.rotation.y);
- camera.position.z -= 50 * Math.cos(avatar.rotation.y);
- }
- }
- function acrobatics() {
- if (is_cartwheeling) {
- avatar.rotation.z = avatar.rotation.z + 0.05;
- }
- if (is_flipping) {
- avatar.rotation.x = avatar.rotation.x + 0.05;
- }
- }
- function isWalking() {
- if (is_moving_forward) return true;
- if (is_moving_back) return true;
- return false;
- }
- 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
- marker.position.z += speed * Math.cos(avatar.rotation.y);
- marker.position.x += speed * Math.sin(avatar.rotation.y);
- is_moving_forward = true;
- }
- if (code == 39) { // right
- is_turning_right = true;
- }
- if (code == 40) { // down
- marker.position.z -= speed * Math.cos(avatar.rotation.y);
- marker.position.x -= speed * Math.sin(avatar.rotation.y);
- is_moving_back = true;
- }
- if (code == 67) is_cartwheeling = true; // C
- if (code == 70) is_flipping = true; // F
- if (code == 82) {
- avatar.rotation.x = 0;
- avatar.rotation.z = 0;
- is_cartwheeling = false;
- is_flipping = false;
- }
- if (detectCollisions()) {
- // if (is_moving_left) marker.position.x += speed;
- // if (is_moving_right) marker.position.x -= speed;
- if (is_moving_forward) {
- marker.position.z -= speed * Math.cos(avatar.rotation.y);
- marker.position.x -= speed * Math.sin(avatar.rotation.y);
- }
- if (is_moving_back) {
- marker.position.z -= speed * Math.cos(avatar.rotation.y);
- marker.position.x -= speed * Math.sin(avatar.rotation.y);
- }
- }
- }
- );
- 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 == 67) is_cartwheeling = false;
- if (code == 70) is_flipping = false;
- }
- );
- 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;
- }
- function jump() {
- checkForTreasure();
- animateJump();
- }
- function checkForTreasure() {
- if (tree_with_treasure == undefined) 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();
- }
- }
- function scorePoints() {
- if (scoreboard.getTimeRemaining() === 0) return;
- scoreboard.addPoints(10);
- }
- 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