Avatar.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // CONSTANTS
  2. var MAX_VELOCITY = 3;
  3. ThePlayer = self;
  4. var node = self.node;
  5. var animationSet = cache.getResource("AnimationSet2D", "Spriter.scml");
  6. var sprite = node.createComponent("AnimatedSprite2D");
  7. sprite.setAnimation(animationSet, "Idle");
  8. sprite.setLayer(100);
  9. node.setPosition(PlayerSpawnPoint);
  10. node.scale2D = [.25, .25];
  11. var body = node.createComponent("RigidBody2D");
  12. body.setBodyType(Atomic.BT_DYNAMIC);
  13. body.fixedRotation = true;
  14. var circle = node.createComponent("CollisionCircle2D");
  15. // Set radius
  16. circle.setRadius(2);
  17. // Set density
  18. circle.setDensity(1.0);
  19. // Set friction.
  20. circle.friction = .2;
  21. // Set restitution
  22. circle.setRestitution(0.1);
  23. var anim = "Idle";
  24. var flipped = false;
  25. var contactCount = 0;
  26. self.onPhysicsBeginContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
  27. if (nodeB == node)
  28. {
  29. contactCount++;
  30. }
  31. }
  32. self.onPhysicsEndContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
  33. if (nodeB == node)
  34. {
  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. if (left && vel[0] > -MAX_VELOCITY) {
  80. body.applyLinearImpulse([-2, 0], pos, true);
  81. } else if (right && vel[0] < MAX_VELOCITY) {
  82. body.applyLinearImpulse([2, 0], pos, true);
  83. }
  84. if (!left && !right) {
  85. vel[0] *= 0.9;
  86. body.linearVelocity = vel;
  87. circle.friction = 1000.0;
  88. }
  89. else
  90. {
  91. circle.friction = .2;
  92. }
  93. if (!contactCount)
  94. circle.friction = 0.0;
  95. if (jump && contactCount) {
  96. vel[1] = 0;
  97. body.linearVelocity = vel;
  98. body.applyLinearImpulse([0, 3], pos, true);
  99. }
  100. }
  101. function postUpdate() {
  102. // may have to set this post update
  103. cameraNode.position = node.position;
  104. }
  105. function update(timeStep) {
  106. handleInput();
  107. handleAnimation();
  108. }