Avatar.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. print("contact: ", contactCount);
  29. contactCount++;
  30. }
  31. }
  32. self.onPhysicsEndContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
  33. if (nodeB == node)
  34. {
  35. print("end contact: ", contactCount);
  36. contactCount--;
  37. }
  38. }
  39. function start() {
  40. self.listenToEvent(null, "PhysicsBeginContact2D", self.onPhysicsBeginContact2D);
  41. self.listenToEvent(null, "PhysicsEndContact2D", self.onPhysicsEndContact2D);
  42. }
  43. function handleAnimation() {
  44. var vel = body.linearVelocity;
  45. if (vel[0] < -0.1) {
  46. if (!flipped) {
  47. sprite.flipX = true;
  48. flipped = true;
  49. }
  50. if (anim != "Run") {
  51. sprite.setAnimation(animationSet, "Run");
  52. anim = "Run";
  53. }
  54. } else if (vel[0] > 0.1) {
  55. if (flipped) {
  56. sprite.flipX = false;
  57. flipped = false;
  58. }
  59. if (anim != "Run") {
  60. sprite.setAnimation(animationSet, "Run");
  61. anim = "Run";
  62. }
  63. } else {
  64. if (anim != "Idle") {
  65. sprite.setAnimation(animationSet, "Idle");
  66. anim = "Idle";
  67. }
  68. }
  69. }
  70. function handleInput(timeStep) {
  71. var vel = body.linearVelocity;
  72. var pos = node.position2D;
  73. if (Math.abs(vel[0]) > MAX_VELOCITY) {
  74. vel[0] = (vel[0] ? vel[0] < 0 ? -1 : 1 : 0) * MAX_VELOCITY;
  75. body.setLinearVelocity(vel);
  76. }
  77. var left = input.getKeyDown(Atomic.KEY_A);
  78. var right = input.getKeyDown(Atomic.KEY_D);
  79. var jump = input.getKeyDown(Atomic.KEY_SPACE);
  80. if (left && vel[0] > -MAX_VELOCITY) {
  81. body.applyLinearImpulse([-2, 0], pos, true);
  82. } else if (right && vel[0] < MAX_VELOCITY) {
  83. body.applyLinearImpulse([2, 0], pos, true);
  84. }
  85. if (!left && !right) {
  86. vel[0] *= 0.9;
  87. body.linearVelocity = vel;
  88. }
  89. if (jump && contactCount) {
  90. vel[1] = 0;
  91. body.linearVelocity = vel;
  92. body.applyLinearImpulse([0, 3], pos, true);
  93. }
  94. }
  95. function postUpdate() {
  96. // may have to set this post update
  97. cameraNode.position = node.position;
  98. }
  99. function update(timeStep) {
  100. handleInput();
  101. handleAnimation();
  102. }