Player.js 4.1 KB

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