Coin.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "atomic component";
  2. var inspectorFields = {
  3. pickupSound: ["Sound", "Sounds/Pickup_Coin8.ogg"],
  4. bounceSound: ["Sound"]
  5. }
  6. var glmatrix = require("gl-matrix");
  7. var vec2 = glmatrix.vec2;
  8. var component = function(self) {
  9. var node = self.node;
  10. var camera = self.node.scene.getMainCamera();
  11. var cameraNode = camera.node;
  12. // Resources
  13. var sprite = node.getComponent("AnimatedSprite2D")
  14. sprite.setAnimation("idle");
  15. var soundSource = node.getComponent("SoundSource");
  16. var activated = false;
  17. var body;
  18. self.start = function() {
  19. }
  20. self.update = function(timeStep) {
  21. if (activated)
  22. return false;
  23. if (vec2.distance(cameraNode.position2D, node.position2D) < 3.0) {
  24. activated = true;
  25. body = node.createComponent("RigidBody2D");
  26. body.setBodyType(Atomic.BT_DYNAMIC);
  27. body.fixedRotation = true;
  28. body.castShadows = false;
  29. self.subscribeToEvent("PhysicsBeginContact2D", function(ev) {
  30. if (ev.nodeB == node) {
  31. if (ev.nodeA && ev.nodeA.name == "Player") {
  32. node.scale2D = [0, 0];
  33. sprite.enabled = false;
  34. body.enabled = false;
  35. if (self.pickupSound) {
  36. soundSource.gain = 1.0;
  37. soundSource.play(self.pickupSound);
  38. }
  39. } else if (self.bounceSound) {
  40. var vel = body.linearVelocity;
  41. if (vec2.length(vel) > 3) {
  42. soundSource.gain = .3;
  43. soundSource.play(self.bounceSound);
  44. }
  45. }
  46. }
  47. });
  48. var circle = node.createComponent("CollisionCircle2D");
  49. // Set radius
  50. circle.setRadius(.3);
  51. // Set density
  52. circle.setDensity(1.0);
  53. // Set friction.
  54. circle.friction = .2;
  55. // Set restitution
  56. circle.setRestitution(.8);
  57. }
  58. }
  59. }
  60. exports.component = component;