Avatar.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // CONSTANTS
  2. var MAX_VELOCITY = 3;
  3. ThePlayer = self;
  4. var node = self.node;
  5. // Resources
  6. var animationSet = cache.getResource("AnimationSet2D", "Sprites/Hero/Hero.scml");
  7. var jumpSound = cache.getResource("Sound", "Sounds/Jump13.ogg");
  8. var sprite = node.createComponent("AnimatedSprite2D");
  9. sprite.setAnimation(animationSet, "Idle");
  10. sprite.setLayer(100);
  11. var light = node.createComponent("PointLight2D");
  12. light.color = [1, 1, 1, .75];
  13. light.softShadowLength = 20;
  14. light.radius = 20;
  15. light.castShadows = true;
  16. light.softShadows = true;
  17. light.numRays = 256;
  18. light.backtrace = true;
  19. self.light = light;
  20. if (!daytime)
  21. lightGroup.addLight(light);
  22. node.createJSComponent("LightFlicker");
  23. camera.zoom = .9;
  24. node.setPosition(PlayerSpawnPoint);
  25. //node.scale2D = [.25, .25];
  26. var body = node.createComponent("RigidBody2D");
  27. body.setBodyType(Atomic.BT_DYNAMIC);
  28. body.fixedRotation = true;
  29. body.bullet = true;
  30. body.castShadows = false;
  31. var circle = node.createComponent("CollisionCircle2D");
  32. // Set radius
  33. circle.setRadius(.5);
  34. // Set density
  35. circle.setDensity(1.0);
  36. // Set friction.
  37. circle.friction = .2;
  38. // Set restitution
  39. circle.setRestitution(0.1);
  40. var anim = "Idle";
  41. var flipped = false;
  42. var contactCount = 0;
  43. var jumpDelta = 0;
  44. var control = false;
  45. self.onPhysicsBeginContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
  46. if (nodeB == node) {
  47. contactCount++;
  48. }
  49. }
  50. self.onPhysicsEndContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
  51. if (nodeB == node) {
  52. contactCount--;
  53. }
  54. }
  55. function start() {
  56. self.soundSource = node.createComponent("SoundSource");
  57. self.soundSource.soundType = Atomic.SOUND_EFFECT;
  58. self.listenToEvent(null, "PhysicsBeginContact2D", self.onPhysicsBeginContact2D);
  59. self.listenToEvent(null, "PhysicsEndContact2D", self.onPhysicsEndContact2D);
  60. }
  61. var lastAnimDelta = 0;
  62. function setAnimation(animName) {
  63. if (anim == animName || lastAnimDelta > 0)
  64. return;
  65. lastAnimDelta = .25;
  66. sprite.setAnimation(animationSet, animName);
  67. anim = animName;
  68. }
  69. function handleAnimation(timeStep) {
  70. lastAnimDelta -= timeStep;
  71. var vel = body.linearVelocity;
  72. if (contactCount) {
  73. if (vel[0] < -0 && control) {
  74. if (!flipped) {
  75. sprite.flipX = true;
  76. flipped = true;
  77. }
  78. setAnimation("Run");
  79. } else if (vel[0] > 0 && control) {
  80. if (flipped) {
  81. sprite.flipX = false;
  82. flipped = false;
  83. }
  84. setAnimation("Run");
  85. } else {
  86. setAnimation("Idle");
  87. }
  88. } else {
  89. if (vel[1] > 1.0) {
  90. setAnimation("Jump");
  91. } else if (vel[1] < -1.0) {
  92. setAnimation("Land");
  93. }
  94. }
  95. }
  96. function handleInput(timeStep) {
  97. var vel = body.linearVelocity;
  98. var pos = node.position2D;
  99. jumpDelta -= timeStep;
  100. if (Math.abs(vel[0]) > MAX_VELOCITY) {
  101. vel[0] = (vel[0] ? vel[0] < 0 ? -1 : 1 : 0) * MAX_VELOCITY;
  102. body.setLinearVelocity(vel);
  103. }
  104. var left = input.getKeyDown(Atomic.KEY_A);
  105. var right = input.getKeyDown(Atomic.KEY_D);
  106. var jump = input.getKeyDown(Atomic.KEY_SPACE);
  107. var zoomIn = input.getKeyDown(Atomic.KEY_W);
  108. var zoomOut = input.getKeyDown(Atomic.KEY_S);
  109. if (daytime)
  110. {
  111. var sunleft = input.getKeyDown(Atomic.KEY_Q);
  112. var sunright = input.getKeyDown(Atomic.KEY_E);
  113. if (sunleft)
  114. TheSun.direction += timeStep * 100;
  115. if (sunright)
  116. TheSun.direction -= timeStep * 100;
  117. }
  118. if (input.getNumJoysticks()) {
  119. var state = GetGamepadState(0);
  120. if (state.axis0 < -0.5)
  121. left = true;
  122. if (state.axis0 > 0.5)
  123. right = true;
  124. if (state.button0)
  125. jump = true;
  126. if (state.button1)
  127. zoomIn = true;
  128. if (state.button2)
  129. zoomOut = true;
  130. }
  131. control = false;
  132. if (left || right)
  133. control = true;
  134. if (zoomIn)
  135. camera.zoom += timeStep;
  136. if (zoomOut)
  137. camera.zoom -= timeStep;
  138. if (camera.zoom > 1.5)
  139. camera.zoom = 1.5;
  140. if (camera.zoom < .45)
  141. camera.zoom = .45;
  142. if (left && vel[0] > -MAX_VELOCITY) {
  143. body.applyLinearImpulse([-2, 0], pos, true);
  144. } else if (right && vel[0] < MAX_VELOCITY) {
  145. body.applyLinearImpulse([2, 0], pos, true);
  146. }
  147. if (!left && !right) {
  148. vel[0] *= 0.9;
  149. body.linearVelocity = vel;
  150. circle.friction = 1000.0;
  151. } else {
  152. circle.friction = .2;
  153. }
  154. if (!contactCount)
  155. circle.friction = 0.0;
  156. if (jump && jumpDelta <= 0 && contactCount) {
  157. jumpDelta = .25;
  158. self.soundSource.gain = 0.45;
  159. self.soundSource.play(jumpSound);
  160. vel[1] = 0;
  161. body.linearVelocity = vel;
  162. body.applyLinearImpulse([0, 6], pos, true);
  163. }
  164. }
  165. function postUpdate() {
  166. // may have to set this post update
  167. cameraNode.position = node.position;
  168. }
  169. function update(timeStep) {
  170. handleInput(timeStep);
  171. handleAnimation(timeStep);
  172. if (node.position[1] < 14) {
  173. //TODO: FIX, I have to set scale to 0 and the back to 1 to force
  174. // setposition catching dirty
  175. node.scale2D = [0, 0];
  176. node.setPosition(PlayerSpawnPoint);
  177. node.scale2D = [1, 1];
  178. }
  179. }