Avatar.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // CONSTANTS
  2. var MAX_VELOCITY = 3;
  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(PlayerSpawnPoint);
  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.friction = .2;
  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.75) {
  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.75) {
  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. circle.friction = 1000.0;
  87. }
  88. else
  89. {
  90. circle.friction = .2;
  91. }
  92. if (!contactCount)
  93. circle.friction = 0.0;
  94. if (jump && contactCount) {
  95. vel[1] = 0;
  96. body.linearVelocity = vel;
  97. body.applyLinearImpulse([0, 3], pos, true);
  98. }
  99. }
  100. function postUpdate() {
  101. // may have to set this post update
  102. cameraNode.position = node.position;
  103. }
  104. function update(timeStep) {
  105. handleInput();
  106. handleAnimation();
  107. }