Avatar.js 4.4 KB

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