Coin.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "atomic component";
  2. //requiring gl-matrix module
  3. var glmatrix = require("gl-matrix");
  4. var vec2 = glmatrix.vec2;
  5. //settings some fields to make them visible from editor
  6. //the first parameter is a type, the second is a default value
  7. var inspectorFields = {
  8. pickupSound: ["Sound", "Sounds/Pickup_Coin8.ogg"],
  9. bounceSound: ["Sound"]
  10. };
  11. //A Coin
  12. var component = function(self) {
  13. var node = self.node;
  14. //getting main camera from our current scene
  15. var camera = self.node.scene.getMainCamera();
  16. var cameraNode = camera.node;
  17. // Resources
  18. //getting AnimatedSprite2D component
  19. var sprite = node.getComponent("AnimatedSprite2D");
  20. sprite.setAnimation("idle");
  21. //getting SoundSource component
  22. var soundSource = node.getComponent("SoundSource");
  23. var activated = false;
  24. var body;
  25. self.start = function() {
  26. //get current dayTime by requiring GlobalVariables object
  27. var dayTime = require("GlobalVariables").dayTime;
  28. if(!dayTime) {
  29. //ok, it's a night, then create a light
  30. var light = node.createComponent("PointLight2D");
  31. light.color = [1, 1, .56, .6];
  32. light.radius = .85;
  33. node.createJSComponent("Components/LightFlicker.js");
  34. }
  35. };
  36. self.update = function(timeStep) {
  37. if (activated)
  38. return false;
  39. if (vec2.distance(cameraNode.position2D, node.position2D) < 3.0) {
  40. activated = true;
  41. //setting rigid body
  42. body = node.createComponent("RigidBody2D");
  43. //our body is Dynamic
  44. body.setBodyType(Atomic.BodyType2D.BT_DYNAMIC);
  45. //fix rotation
  46. body.fixedRotation = true;
  47. //don't make our body to cast shadows
  48. body.castShadows = false;
  49. //subscribing to PhysicsBeginContact2D event, and specifying a callback
  50. self.subscribeToEvent(Atomic.PhysicsBeginContact2DEvent(function(ev) {
  51. //checking if nodeB is our current node
  52. if (ev.nodeB == node) {
  53. //checking if nodeA(another node) is a Player
  54. if (ev.nodeA && ev.nodeA.name == "Player") {
  55. //picking up a coin
  56. if (self.pickupSound) {
  57. soundSource.gain = 1.0;
  58. //playing pickupSound
  59. soundSource.play(self.pickupSound);
  60. //ok, remove itself from the parent node
  61. node.remove();
  62. }
  63. //if it's not a player, and we have bounceSound, then play it
  64. } else if (self.bounceSound) {
  65. var vel = body.linearVelocity;
  66. if (vec2.length(vel) > 3) {
  67. soundSource.gain = .3;
  68. soundSource.play(self.bounceSound);
  69. }
  70. }
  71. }
  72. }));
  73. //adding circle colision shape
  74. var circle = node.createComponent("CollisionCircle2D");
  75. // Set radius
  76. circle.setRadius(.3);
  77. // Set density
  78. circle.setDensity(1.0);
  79. // Set friction.
  80. circle.friction = .2;
  81. // Set restitution
  82. circle.setRestitution(.8);
  83. }
  84. };
  85. };
  86. exports.component = component;