Player.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "atomic component";
  2. module.exports = function(self) {
  3. var node = self.node;
  4. var camera = self.node.scene.getMainCamera();
  5. var cameraNode = camera.node;
  6. var input = Atomic.input;
  7. var MAX_VELOCITY = 3;
  8. var anim = "";
  9. var flipped = false;
  10. var contactCount = 0;
  11. var jumpDelta = 0;
  12. var control = false;
  13. var lastAnimDelta = 0;
  14. var spawnPosition = node.position2D;
  15. var sprite;
  16. var animationSet;
  17. var jumpSound;
  18. // physics
  19. var body;
  20. var circle;
  21. self.start = function() {
  22. sprite = node.getComponent("AnimatedSprite2D");
  23. body = node.getComponent("RigidBody2D");
  24. circle = node.getComponent("CollisionCircle2D");
  25. setAnimation("Idle");
  26. self.subscribeToEvent("PhysicsBeginContact2D", function(event) {
  27. if (event.bodyB == body)
  28. contactCount++;
  29. });
  30. self.subscribeToEvent("PhysicsEndContact2D", function(event) {
  31. if (event.bodyB == body)
  32. contactCount--;
  33. });
  34. }
  35. self.update = function(timeStep) {
  36. handleInput(timeStep);
  37. handleAnimation(timeStep);
  38. if (node.position[1] < 14) {
  39. //TODO: FIX, I have to set scale to 0 and the back to 1 to force
  40. // setposition catching dirty
  41. node.scale2D = [0, 0];
  42. node.position2D = spawnPosition;
  43. node.scale2D = [1, 1];
  44. }
  45. }
  46. self.postUpdate = function() {
  47. cameraNode.position = node.position;
  48. }
  49. function setAnimation(animName) {
  50. if (anim == animName || lastAnimDelta > 0)
  51. return;
  52. lastAnimDelta = .25;
  53. sprite.setAnimation(animName);
  54. anim = animName;
  55. }
  56. function handleAnimation(timeStep) {
  57. lastAnimDelta -= timeStep;
  58. var vel = body.linearVelocity;
  59. if (contactCount) {
  60. if (vel[0] < -0 && control) {
  61. if (!flipped) {
  62. sprite.flipX = true;
  63. flipped = true;
  64. }
  65. setAnimation("Run");
  66. } else if (vel[0] > 0 && control) {
  67. if (flipped) {
  68. sprite.flipX = false;
  69. flipped = false;
  70. }
  71. setAnimation("Run");
  72. } else {
  73. setAnimation("Idle");
  74. }
  75. } else {
  76. if (vel[1] > 1.0) {
  77. setAnimation("Jump");
  78. } else if (vel[1] < -1.0) {
  79. setAnimation("Land");
  80. }
  81. }
  82. }
  83. function handleInput(timeStep) {
  84. var vel = body.linearVelocity;
  85. var pos = node.position2D;
  86. jumpDelta -= timeStep;
  87. if (Math.abs(vel[0]) > MAX_VELOCITY) {
  88. vel[0] = (vel[0] ? vel[0] < 0 ? -1 : 1 : 0) * MAX_VELOCITY;
  89. body.setLinearVelocity(vel);
  90. }
  91. var left = input.getKeyDown(Atomic.KEY_LEFT) || input.getKeyDown(Atomic.KEY_A);
  92. var right = input.getKeyDown(Atomic.KEY_RIGHT) || input.getKeyDown(Atomic.KEY_D);
  93. var jump = input.getKeyDown(Atomic.KEY_UP) || input.getKeyDown(Atomic.KEY_SPACE) || input.getKeyDown(Atomic.KEY_W);
  94. control = false;
  95. if (left || right)
  96. control = true;
  97. if (left && vel[0] > -MAX_VELOCITY) {
  98. body.applyLinearImpulse([-2, 0], pos, true);
  99. } else if (right && vel[0] < MAX_VELOCITY) {
  100. body.applyLinearImpulse([2, 0], pos, true);
  101. }
  102. if (!left && !right) {
  103. vel[0] *= 0.9;
  104. body.linearVelocity = vel;
  105. circle.friction = 1000.0;
  106. } else {
  107. circle.friction = .2;
  108. }
  109. if (!contactCount)
  110. circle.friction = 0.0;
  111. if (jump && jumpDelta <= 0 && contactCount) {
  112. jumpDelta = .25;
  113. //self.soundSource.gain = 0.45;
  114. //self.soundSource.play(jumpSound);
  115. vel[1] = 0;
  116. body.linearVelocity = vel;
  117. body.applyLinearImpulse([0, 6], pos, true);
  118. }
  119. }
  120. }