pipe.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. export const pipe = (function() {
  2. class _PipePairObject {
  3. constructor(config) {
  4. this._config = config;
  5. const height = config.config_height * (0.25 + 0.5 * Math.random());
  6. this._sprite1 = config.scene.add.sprite(config.x, height + config.spacing * 0.5, 'pipe');
  7. this._sprite1.displayOriginX = 0;
  8. this._sprite1.displayOriginY = 0;
  9. this._sprite2 = config.scene.add.sprite(config.x, height - config.spacing * 0.5, 'pipe');
  10. this._sprite2.displayOriginX = 0;
  11. this._sprite2.displayOriginY = 0;
  12. this._sprite2.displayHeight = -1 * this._sprite2.height;
  13. }
  14. Destroy() {
  15. this._sprite1.destroy();
  16. this._sprite2.destroy();
  17. }
  18. Update(timeElapsed) {
  19. this._sprite1.x += timeElapsed * this._config.speed;
  20. this._sprite2.x += timeElapsed * this._config.speed;
  21. }
  22. Intersects(aabb) {
  23. const b1 = this._sprite1.getBounds();
  24. const b2 = this._sprite2.getBounds();
  25. b2.y -= this._sprite2.height;
  26. return (
  27. Phaser.Geom.Intersects.RectangleToRectangle(b1, aabb) ||
  28. Phaser.Geom.Intersects.RectangleToRectangle(b2, aabb));
  29. }
  30. Reset(x) {
  31. const height = this._config.config_height * (0.25 + 0.5 * Math.random());
  32. this._sprite1.x = x;
  33. this._sprite1.y = height + this._config.spacing * 0.5;
  34. this._sprite2.x = x;
  35. this._sprite2.y = height - this._config.spacing * 0.5;
  36. }
  37. get X() {
  38. return this._sprite1.x;
  39. }
  40. get Width() {
  41. return this._sprite1.width;
  42. }
  43. }
  44. return {
  45. PipePairObject: _PipePairObject
  46. };
  47. })();