Coin.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // FIXME
  2. keepAlive = typeof(keepAlive) == "undefined" ? [] : keepAlive;
  3. keepAlive.push(self);
  4. var glmatrix = require("gl-matrix");
  5. var vec2 = glmatrix.vec2;
  6. var node = self.node;
  7. // Resources
  8. var animationSet = cache.getResource("AnimationSet2D", "Sprites/GoldIcon.scml");
  9. var coinSound = cache.getResource("Sound", "Sounds/Pickup_Coin8.ogg");
  10. var coinBounceSound = cache.getResource("Sound", "Sounds/Coin_Bounce.ogg");
  11. var sprite = node.createComponent("AnimatedSprite2D");
  12. sprite.setAnimation(animationSet, "idle");
  13. sprite.setLayer(100);
  14. var light = node.createComponent("PointLight2D");
  15. if (daytime)
  16. light.color = [1, 1, .56, .4];
  17. else
  18. light.color = [1, 1, .56, .8];
  19. light.radius = .85;
  20. lightGroup.addLight(light);
  21. var activated = false;
  22. var body;
  23. function onPlayerHit() {
  24. //ThePlayer.light.enabled = false;
  25. // sprite enabled is not removing the sprite
  26. light.enabled = false;
  27. node.scale2D = [0, 0];
  28. sprite.enabled = false;
  29. body.enabled = false;
  30. self.soundSource.gain = 1.0;
  31. self.soundSource.play(coinSound);
  32. }
  33. self.onPhysicsBeginContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
  34. if (nodeA == ThePlayer.node && nodeB == node) {
  35. onPlayerHit();
  36. } else if (nodeB == node) {
  37. var vel = body.linearVelocity;
  38. if (vec2.length(vel) > 3) {
  39. self.soundSource.gain = .3;
  40. self.soundSource.play(coinBounceSound);
  41. }
  42. }
  43. }
  44. function start() {
  45. self.soundSource = node.createComponent("SoundSource");
  46. self.soundSource.soundType = Atomic.SOUND_EFFECT;
  47. // would just like to listen to body collisions here
  48. self.listenToEvent(null, "PhysicsBeginContact2D", self.onPhysicsBeginContact2D);
  49. }
  50. function update(timeStep) {
  51. if (activated)
  52. return false;
  53. if (vec2.distance(cameraNode.position2D, node.position2D) < 3.0) {
  54. activated = true;
  55. body = node.createComponent("RigidBody2D");
  56. body.setBodyType(Atomic.BT_DYNAMIC);
  57. body.fixedRotation = true;
  58. body.castShadows = false;
  59. var circle = node.createComponent("CollisionCircle2D");
  60. // Set radius
  61. circle.setRadius(.3);
  62. // Set density
  63. circle.setDensity(1.0);
  64. // Set friction.
  65. circle.friction = .2;
  66. // Set restitution
  67. circle.setRestitution(.8);
  68. }
  69. }