Player.js 4.9 KB

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