Advertisement
Guest User

penguin

a guest
Sep 29th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <body></body>
  2. <script src="http://gamingJS.com/Three.js"></script>
  3. <script src="http://gamingJS.com/Tween.js"></script>
  4. <script src="http://gamingJS.com/ChromeFixes.js"></script>
  5. <body></body><script>
  6. // This is where stuff in our game will happen:
  7. var scene = new THREE.Scene();
  8. // This is what sees the stuff:
  9. var aspect_ratio = window.innerWidth / window.innerHeight;
  10. var camera = new THREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
  11. camera.position.z = 500;
  12. //scene.add(camera);
  13. // This will draw what the camera sees onto the screen:
  14. var renderer = new THREE.CanvasRenderer();
  15. renderer.setSize(window.innerWidth, window.innerHeight);
  16. document.body.appendChild(renderer.domElement);
  17. // ******** START CODING ON THE NEXT LINE ********
  18. var marker = new THREE.Object3D();
  19. scene.add(marker);
  20. var cover = new THREE.MeshNormalMaterial();
  21. var body = new THREE.SphereGeometry(100);
  22. var avatar = new THREE.Mesh(body, cover);
  23.  
  24.  
  25. //color the background light blue
  26. document.bgColor='#56a5ec';
  27.  
  28. //define objects
  29.  
  30. //marker to refer to avatar position by
  31. var marker = new THREE.Object3D();
  32. scene.add(marker);
  33.  
  34. //creates mesh covers for shapes
  35. var cover = new THREE.MeshNormalMaterial();
  36. var blackCover=new THREE.MeshBasicMaterial({color:0x000000});
  37. var whiteCover=new THREE.MeshBasicMaterial({color:0xFFFFFF});
  38. var orangeCover=new THREE.MeshBasicMaterial({color:0xFFA500});
  39.  
  40. //creates the body
  41. var body = new THREE.CylinderGeometry(10,90,200);
  42. var avatar = new THREE.Mesh(body, blackCover);
  43. //associates the body to avatar
  44. marker.add(avatar);
  45.  
  46. //defines the extremeties
  47. var hand = new THREE.CylinderGeometry(60,80,15);
  48. var head=new THREE.SphereGeometry(75);
  49. var mouth=new THREE.CylinderGeometry(0,27,85);
  50. var foot = new THREE.CylinderGeometry(20,55,20);
  51. var belly = new THREE.CylinderGeometry(10,90,170);
  52. var eyeball = new THREE.SphereGeometry(8);
  53.  
  54.  
  55. //spawn and position body parts
  56.  
  57. //hands
  58. var right_hand = new THREE.Mesh(hand, blackCover);
  59. right_hand.position.set(-78,-10,0);
  60. right_hand.rotation.set(0,0,1);
  61. avatar.add(right_hand);
  62.  
  63. var left_hand = new THREE.Mesh(hand, blackCover);
  64. left_hand.position.set(78,-10,0);
  65. left_hand.rotation.set(0,0,-1);
  66. avatar.add(left_hand);
  67.  
  68. //feet
  69. var right_foot = new THREE.Mesh(foot, orangeCover);
  70. right_foot.position.set(-75,-125,0,20);
  71. avatar.add(right_foot);
  72.  
  73. var left_foot = new THREE.Mesh(foot, orangeCover);
  74. left_foot.position.set(75,-125,0,20);
  75. avatar.add(left_foot);
  76.  
  77. //head
  78. var head_box=new THREE.Mesh(head,blackCover);
  79. head_box.position.set(0,130,0,0);
  80. avatar.add(head_box);
  81.  
  82. var beak=new THREE.Mesh(mouth,orangeCover);
  83. beak.position.set(0,90,100);
  84. beak.rotation.set(90,0,0);
  85. avatar.add(beak);
  86.  
  87. //belly
  88. var tummy = new THREE.Mesh(belly,whiteCover);
  89. tummy.position.set(0,-5,1);
  90. avatar.add(tummy);
  91. var back = new THREE.Mesh(belly,blackCover);
  92. back.position.set(0,-5,-5);
  93. avatar.add(back);
  94.  
  95. //eyes
  96. var right_eye = new THREE.Mesh(eyeball, whiteCover);
  97. right_eye.position.set(25,150,57);
  98. avatar.add(right_eye);
  99.  
  100. var left_eye = new THREE.Mesh(eyeball, whiteCover);
  101. left_eye.position.set(-25,150,57);
  102. avatar.add(left_eye);
  103.  
  104.  
  105.  
  106. marker.add(camera);
  107. // Trees
  108. makeTreeAt( 500, 0);
  109. makeTreeAt(-500, 0);
  110. makeTreeAt( 750, -1000);
  111. makeTreeAt(-750, -1000);
  112. function makeTreeAt(x, z) {
  113. var trunk = new THREE.Mesh(
  114. new THREE.CylinderGeometry(50, 50, 200),
  115. new THREE.MeshBasicMaterial({color: 0xA0522D})
  116. );
  117. var top = new THREE.Mesh(
  118. new THREE.SphereGeometry(150),
  119. new THREE.MeshBasicMaterial({color: 0x228B22})
  120. );
  121. top.position.y = 175;
  122. trunk.add(top);
  123. trunk.position.set(x, -75, z);
  124. scene.add(trunk);
  125. }
  126. // Now, animate what the camera sees on the screen:
  127. var clock = new THREE.Clock(true);
  128. function animate() {
  129.  
  130. requestAnimationFrame(animate);
  131. TWEEN.update();
  132. walk();
  133. turn();
  134. acrobatics();
  135. renderer.render(scene, camera);
  136. }
  137. animate();
  138. function walk() {
  139. if (!isWalking()) return;
  140. var position = Math.sin(clock.getElapsedTime()*5) * 50;
  141. right_hand.position.z = position;
  142. left_hand.position.z = -position;
  143. right_foot.position.z = -position;
  144. left_foot.position.z = position;
  145. }
  146. function turn() {
  147. var direction = 0;
  148. if (is_moving_forward) direction = Math.PI;
  149. if (is_moving_back) direction = 0;
  150. if (is_moving_right) direction = Math.PI/2;
  151. if (is_moving_left) direction = -Math.PI/2;
  152. spinAvatar(direction);
  153. }
  154. function spinAvatar(direction) {
  155. new TWEEN.
  156. Tween({y: avatar.rotation.y}).
  157. to({y: direction}, 100).
  158. onUpdate(function () {
  159. avatar.rotation.y = this.y;
  160. }).
  161. start();
  162. }
  163. var is_cartwheeling = false;
  164. var is_flipping = false;
  165. function acrobatics() {
  166. if (is_cartwheeling) {
  167. avatar.rotation.z = avatar.rotation.z + 0.05;
  168. }
  169. if (is_flipping) {
  170. avatar.rotation.x = avatar.rotation.x + 0.05;
  171. }
  172. }
  173. var is_moving_left, is_moving_right, is_moving_forward, is_moving_back;
  174.  
  175.  
  176. function isWalking() {
  177. if (is_moving_right) return true;
  178. if (is_moving_left) return true;
  179. if (is_moving_forward) return true;
  180. if (is_moving_back) return true;
  181. return false;
  182. }
  183. document.addEventListener('keydown', function(event) {
  184. var code = event.keyCode;
  185. if (code == 37) { // left
  186. marker.position.x = marker.position.x-5;
  187. is_moving_left = true;
  188. }
  189. if (code == 38) { // up
  190. marker.position.z = marker.position.z-5;
  191. is_moving_forward = true;
  192. }
  193. if (code == 39) { // right
  194. marker.position.x = marker.position.x+5;
  195. is_moving_right = true;
  196. }
  197. if (code == 40) { // down
  198. marker.position.z = marker.position.z+5;
  199. is_moving_back = true;
  200. }
  201. if (code == 67) is_cartwheeling = !is_cartwheeling; // C
  202. if (code == 70) is_flipping = !is_flipping; // F
  203. });
  204. document.addEventListener('keyup', function(event) {
  205. var code = event.keyCode;
  206. if (code == 37) {
  207. console.log("not left anymore");
  208. is_moving_left = false;
  209. }
  210. if (code == 38) is_moving_forward = false;
  211. if (code == 39) is_moving_right = false;
  212. if (code == 40) is_moving_back = false;
  213. });
  214. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement