Avatar.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // CONSTANTS
  2. var MAX_VELOCITY = 2;
  3. var node = self.node;
  4. var animationSet = cache.getResource("AnimationSet2D", "Spriter.scml");
  5. var sprite = node.createComponent("AnimatedSprite2D");
  6. sprite.setAnimation(animationSet, "Idle");
  7. sprite.setLayer(100);
  8. node.setPosition([8, 14, 0]);
  9. node.scale2D = [.25, .25];
  10. var body = node.createComponent("RigidBody2D");
  11. body.setBodyType(Atomic.BT_DYNAMIC);
  12. body.fixedRotation = true;
  13. var circle = node.createComponent("CollisionCircle2D");
  14. // Set radius
  15. circle.setRadius(2);
  16. // Set density
  17. circle.setDensity(1.0);
  18. // Set friction.
  19. circle.setFriction(.1);
  20. // Set restitution
  21. circle.setRestitution(0.1);
  22. var anim = "Idle";
  23. var flipped = false;
  24. var contactCount = 0;
  25. self.onPhysicsBeginContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
  26. if (nodeB == node)
  27. {
  28. contactCount++;
  29. }
  30. }
  31. self.onPhysicsEndContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
  32. if (nodeB == node)
  33. {
  34. contactCount--;
  35. }
  36. }
  37. function start() {
  38. self.listenToEvent(null, "PhysicsBeginContact2D", self.onPhysicsBeginContact2D);
  39. self.listenToEvent(null, "PhysicsEndContact2D", self.onPhysicsEndContact2D);
  40. }
  41. function handleAnimation() {
  42. var vel = body.linearVelocity;
  43. if (vel[0] < -0.1) {
  44. if (!flipped) {
  45. sprite.flipX = true;
  46. flipped = true;
  47. }
  48. if (anim != "Run") {
  49. sprite.setAnimation(animationSet, "Run");
  50. anim = "Run";
  51. }
  52. } else if (vel[0] > 0.1) {
  53. if (flipped) {
  54. sprite.flipX = false;
  55. flipped = false;
  56. }
  57. if (anim != "Run") {
  58. sprite.setAnimation(animationSet, "Run");
  59. anim = "Run";
  60. }
  61. } else {
  62. if (anim != "Idle") {
  63. sprite.setAnimation(animationSet, "Idle");
  64. anim = "Idle";
  65. }
  66. }
  67. }
  68. function handleInput(timeStep) {
  69. var vel = body.linearVelocity;
  70. var pos = node.position2D;
  71. if (Math.abs(vel[0]) > MAX_VELOCITY) {
  72. vel[0] = (vel[0] ? vel[0] < 0 ? -1 : 1 : 0) * MAX_VELOCITY;
  73. body.setLinearVelocity(vel);
  74. }
  75. var left = input.getKeyDown(Atomic.KEY_A);
  76. var right = input.getKeyDown(Atomic.KEY_D);
  77. var jump = input.getKeyDown(Atomic.KEY_SPACE);
  78. if (left && vel[0] > -MAX_VELOCITY) {
  79. body.applyLinearImpulse([-2, 0], pos, true);
  80. } else if (right && vel[0] < MAX_VELOCITY) {
  81. body.applyLinearImpulse([2, 0], pos, true);
  82. }
  83. if (!left && !right) {
  84. vel[0] *= 0.9;
  85. body.linearVelocity = vel;
  86. }
  87. if (jump && contactCount) {
  88. vel[1] = 0;
  89. body.linearVelocity = vel;
  90. body.applyLinearImpulse([0, 3], pos, true);
  91. }
  92. }
  93. function postUpdate() {
  94. // may have to set this post update
  95. cameraNode.position = node.position;
  96. }
  97. function update(timeStep) {
  98. handleInput();
  99. handleAnimation();
  100. }