Avatar.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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, .55];
  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 (input.getNumJoysticks()) {
  110. var state = GetGamepadState(0);
  111. if (state.axis0 < -0.5)
  112. left = true;
  113. if (state.axis0 > 0.5)
  114. right = true;
  115. if (state.button0)
  116. jump = true;
  117. if (state.button1)
  118. zoomIn = true;
  119. if (state.button2)
  120. zoomOut = true;
  121. }
  122. control = false;
  123. if (left || right)
  124. control = true;
  125. if (zoomIn)
  126. camera.zoom += timeStep;
  127. if (zoomOut)
  128. camera.zoom -= timeStep;
  129. if (camera.zoom > 1.5)
  130. camera.zoom = 1.5;
  131. if (camera.zoom < .45)
  132. camera.zoom = .45;
  133. if (left && vel[0] > -MAX_VELOCITY) {
  134. body.applyLinearImpulse([-2, 0], pos, true);
  135. } else if (right && vel[0] < MAX_VELOCITY) {
  136. body.applyLinearImpulse([2, 0], pos, true);
  137. }
  138. if (!left && !right) {
  139. vel[0] *= 0.9;
  140. body.linearVelocity = vel;
  141. circle.friction = 1000.0;
  142. } else {
  143. circle.friction = .2;
  144. }
  145. if (!contactCount)
  146. circle.friction = 0.0;
  147. if (jump && jumpDelta <= 0 && contactCount) {
  148. jumpDelta = .25;
  149. self.soundSource.gain = 0.75;
  150. self.soundSource.play(jumpSound);
  151. vel[1] = 0;
  152. body.linearVelocity = vel;
  153. body.applyLinearImpulse([0, 6], pos, true);
  154. }
  155. }
  156. function postUpdate() {
  157. // may have to set this post update
  158. cameraNode.position = node.position;
  159. }
  160. function update(timeStep) {
  161. handleInput(timeStep);
  162. handleAnimation(timeStep);
  163. if (node.position[1] < 14) {
  164. //TODO: FIX, I have to set scale to 0 and the back to 1 to force
  165. // setposition catching dirty
  166. node.scale2D = [0, 0];
  167. node.setPosition(PlayerSpawnPoint);
  168. node.scale2D = [1, 1];
  169. }
  170. }