Avatar.js 4.6 KB

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