Player.js 3.9 KB

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