Player.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. "atomic component";
  2. var inspectorFields = {
  3. jumpSound: ["Sound"]
  4. };
  5. //A Player
  6. exports.component = function(self) {
  7. var node = self.node;
  8. //get main camera of the current scene
  9. var camera = self.node.scene.getMainCamera();
  10. var cameraNode = camera.node;
  11. //link to Atomic.Input
  12. var input = Atomic.input;
  13. //get SoundSource component
  14. var soundSource = node.getComponent("SoundSource");
  15. var MAX_VELOCITY = 3;
  16. var anim = "";
  17. var flipped = false;
  18. var contactCount = 0;
  19. var jumpDelta = 0;
  20. var control = false;
  21. var lastAnimDelta = 0;
  22. var spawnPosition = node.position2D;
  23. var sprite;
  24. var animationSet;
  25. var jumpSound;
  26. // physics
  27. var body;
  28. var circle;
  29. //function called after the component attached to the node
  30. self.start = function() {
  31. //get components
  32. sprite = node.getComponent("AnimatedSprite2D");
  33. body = node.getComponent("RigidBody2D");
  34. circle = node.getComponent("CollisionCircle2D");
  35. setAnimation("Idle");
  36. //subscribe to PhysicsPostStep2D
  37. self.subscribeToEvent("PhysicsPostStep2D", function(event) {
  38. //set camera position to the current node position
  39. cameraNode.position = node.position;
  40. });
  41. //subscribe to PhysicsBeginContact2D
  42. self.subscribeToEvent("PhysicsBeginContact2D", function(event) {
  43. //if bodyB is our body, so increment contactCount
  44. if (event.bodyB == body)
  45. contactCount++;
  46. });
  47. //subscribe to
  48. self.subscribeToEvent("PhysicsEndContact2D", function(event) {
  49. //if bodyB is our body, so decrement contactCount
  50. if (event.bodyB == body)
  51. contactCount--;
  52. });
  53. //get current dayTime by requiring GlobalVariables object
  54. var dayTime = require("GlobalVariables").dayTime;
  55. if(!dayTime) {
  56. //ok, it's a night, then create a light
  57. var light = node.createComponent("PointLight2D");
  58. light.color = [1, 0.5, 0.7, 0.35];
  59. light.backtrace = true;
  60. light.castShadows = true;
  61. light.numRays = 256;
  62. light.radius = 4;
  63. }
  64. };
  65. self.update = function(timeStep) {
  66. //handle our input and animation stuff
  67. handleInput(timeStep);
  68. handleAnimation(timeStep);
  69. if (node.position[1] < 14) {
  70. //TODO: FIX, I have to set scale to 0 and the back to 1 to force
  71. // setposition catching dirty
  72. node.scale2D = [0, 0];
  73. node.position2D = spawnPosition;
  74. node.scale2D = [1, 1];
  75. }
  76. };
  77. function setAnimation(animName) {
  78. if (anim == animName || lastAnimDelta > 0)
  79. return;
  80. lastAnimDelta = .25;
  81. //sprite is an AnimatedSprite2D object, set its animation to the given animName
  82. sprite.setAnimation(animName);
  83. anim = animName;
  84. }
  85. function handleAnimation(timeStep) {
  86. lastAnimDelta -= timeStep;
  87. var vel = body.linearVelocity;
  88. //if we have a contact with something
  89. if (contactCount) {
  90. //if velocity by X less than zero
  91. if (vel[0] < -0 && control) {
  92. if (!flipped) {
  93. sprite.flipX = true;
  94. flipped = true;
  95. }
  96. //set current animation to Run animation
  97. setAnimation("Run");
  98. //if velocity by X is greater than zero
  99. } else if (vel[0] > 0 && control) {
  100. if (flipped) {
  101. sprite.flipX = false;
  102. flipped = false;
  103. }
  104. //set current animation to Run animation
  105. setAnimation("Run");
  106. } else {
  107. //if our velocity equals to zero, so set current animation to Idle animation
  108. setAnimation("Idle");
  109. }
  110. } else {
  111. //if velocity by Y greater than 1, so we are jumping
  112. if (vel[1] > 1.0) {
  113. setAnimation("Jump");
  114. //if velocity by Y is less than 1, so we are falling
  115. } else if (vel[1] < -1.0) {
  116. setAnimation("Land");
  117. }
  118. }
  119. }
  120. function handleInput(timeStep) {
  121. var vel = body.linearVelocity;
  122. var pos = node.position2D;
  123. jumpDelta -= timeStep;
  124. if (Math.abs(vel[0]) > MAX_VELOCITY) {
  125. vel[0] = (vel[0] ? vel[0] < 0 ? -1 : 1 : 0) * MAX_VELOCITY;
  126. //set body linear velocity to the current velocity
  127. body.setLinearVelocity(vel);
  128. }
  129. //input stuff
  130. var left = input.getKeyDown(Atomic.KEY_LEFT) || input.getKeyDown(Atomic.KEY_A);
  131. var right = input.getKeyDown(Atomic.KEY_RIGHT) || input.getKeyDown(Atomic.KEY_D);
  132. var jump = input.getKeyDown(Atomic.KEY_UP) || input.getKeyDown(Atomic.KEY_SPACE) || input.getKeyDown(Atomic.KEY_W);
  133. control = false;
  134. //if left or right is pressed
  135. if (left || right)
  136. control = true;
  137. //applying linear impulses to the left and right
  138. if (left && vel[0] > -MAX_VELOCITY) {
  139. body.applyLinearImpulse([-2, 0], pos, true);
  140. } else if (right && vel[0] < MAX_VELOCITY) {
  141. body.applyLinearImpulse([2, 0], pos, true);
  142. }
  143. //if we are not pressing any buttons, so enable friction
  144. if (!left && !right) {
  145. vel[0] *= 0.9;
  146. body.linearVelocity = vel;
  147. circle.friction = 1000.0;
  148. } else {
  149. circle.friction = .2;
  150. }
  151. if (!contactCount)
  152. circle.friction = 0.0;
  153. //if we are jumping and colliding with something
  154. if (jump && jumpDelta <= 0 && contactCount) {
  155. jumpDelta = .25;
  156. //is sound exists
  157. if (self.jumpSound) {
  158. soundSource.gain = 0.45;
  159. //playing sound
  160. soundSource.play(self.jumpSound);
  161. }
  162. vel[1] = 0;
  163. body.linearVelocity = vel;
  164. //applying linear impuls to the Y
  165. body.applyLinearImpulse([0, 6], pos, true);
  166. }
  167. }
  168. };