Avatar.js 4.5 KB

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