1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| interface IHoleConfig { bounds: Phaser.Geom.Rectangle; padding?: number; radius?: number; shape?: 'rect' | 'circle'; } interface IShowConfig { alpha?: number; touchClose?: boolean; }
export class GuideMask extends Phaser.GameObjects.Container { private hitArea: Phaser.Geom.Rectangle; private guideMask: Phaser.GameObjects.Graphics; private maskGraphics: Phaser.GameObjects.Graphics;
private handSprite: Phaser.GameObjects.Sprite; constructor(scene: Phaser.Scene, x: number = 0, y: number = 0) { super(scene, x, y); this.name = 'GuideMaskContainer'; this.init(); } init() { const { width, height } = this.scene.scale;
this.hitArea = new Phaser.Geom.Rectangle(0, 0, width, height); this.guideMask = this.scene.add.graphics(); this.guideMask.name = 'GuideMask'; this.add(this.guideMask);
this.maskGraphics = this.scene.make.graphics( { fillStyle: { color: 0xffffff } }, false );
if (!this.scene.anims.exists('hand_click_anim')) { this.scene.anims.create({ key: 'hand_click_anim', frames: [{ key: 'finger_down' }, { key: 'finger_up' }], frameRate: 2.5, repeat: -1, yoyo: false, }); }
this.handSprite = this.scene.add.sprite(0, 0, 'finger_down'); this.handSprite.setOrigin(0, 0); this.handSprite.setAlpha(0); this.add(this.handSprite);
this.setVisible(false); this.scene.add.existing(this); }
show(holes: IHoleConfig | IHoleConfig[], config?: IShowConfig) { const { alpha = 0.5, touchClose = true } = config;
const hitAreaCallback = ( _: Phaser.Geom.Rectangle, x: number, y: number ) => { return true; if (Array.isArray(holes)) { for (const hole of holes) { if (hole.bounds.contains(x, y)) { return true; } }
return false; } else { return holes.bounds.contains(x, y); } };
this.guideMask.clear(); this.maskGraphics.clear();
this.guideMask.fillStyle(0x000000, alpha); this.guideMask.fillRect(0, 0, this.hitArea.width, this.hitArea.height);
this.guideMask.setInteractive(this.hitArea, hitAreaCallback); this.guideMask.on('pointerdown', (pointer: Phaser.Input.Pointer) => { if (Array.isArray(holes)) { if (holes.some((hole) => hole.bounds.contains(pointer.x, pointer.y))) { this.hide(); const hitObjects = this.scene.input.hitTestPointer(pointer); for (const obj of hitObjects) { if (obj !== this.guideMask && obj.input?.enabled) { obj.emit( 'pointerdown', pointer, obj.input.localX, obj.input.localY ); break; } } } } });
if (Array.isArray(holes)) { holes.forEach((hole) => this.makeHole(hole)); this.handSprite.setPosition( holes[0].bounds.x + holes[0].bounds.width * 0.5, holes[0].bounds.y + holes[0].bounds.height * 0.5 ); } else { this.makeHole(holes); this.handSprite.setPosition( holes.bounds.x + holes.bounds.width * 0.5, holes.bounds.y + holes.bounds.height * 0.5 ); } this.handSprite.play('hand_click_anim'); this.handSprite.setAlpha(1);
const highlight = this.maskGraphics.createGeometryMask(); highlight.setInvertAlpha(true); this.guideMask.setMask(highlight);
this.setVisible(true); }
makeHole(hole: IHoleConfig) { const { bounds, padding = 20, radius = 20, shape = 'rect' } = hole;
if (shape === 'circle') { this.maskGraphics.fillCircle( bounds.x + bounds.width * 0.5, bounds.y + bounds.height * 0.5, Math.max(bounds.width, bounds.height) * 0.5 + padding ); } else { this.maskGraphics.fillRoundedRect( bounds.x - padding, bounds.y - padding, bounds.width + padding * 2, bounds.height + padding * 2, radius ); } }
hide() { if (this.handSprite) { this.handSprite.stop(); this.handSprite.setAlpha(0); }
this.guideMask.removeInteractive(); this.guideMask.removeAllListeners();
this.setVisible(false); } }
|