Avatar.js 4.8 KB

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