Coin.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. light.color = [1, 1, .56, .8];
  16. light.radius = .85;
  17. lightGroup.addLight(light);
  18. var activated = false;
  19. var body;
  20. function onPlayerHit() {
  21. //ThePlayer.light.enabled = false;
  22. // sprite enabled is not removing the sprite
  23. light.enabled = false;
  24. node.scale2D = [0, 0];
  25. sprite.enabled = false;
  26. body.enabled = false;
  27. self.soundSource.gain = 1.0;
  28. self.soundSource.play(coinSound );
  29. }
  30. self.onPhysicsBeginContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
  31. if (nodeA == ThePlayer.node && nodeB == node) {
  32. onPlayerHit();
  33. }
  34. else if (nodeB == node)
  35. {
  36. var vel = body.linearVelocity;
  37. if (vec2.length(vel) > 3)
  38. {
  39. self.soundSource.gain = .4;
  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. }