Avatar.js 4.0 KB

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