Avatar.js 4.8 KB

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