File size: 1,601 Bytes
670a607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const GetValue = Phaser.Utils.Objects.GetValue;
class Input {
    constructor(bejeweled, config) {
        this.bejeweled = bejeweled;      // Bejeweled
        this.scene = bejeweled.scene; // Bejeweled.scene

        this.setEnable(GetValue(config, 'input.enable', true));
        this.boot();
    }

    boot() {
        // Touch control
        this.scene.input
            .on('pointerdown', this.selectChess1, this)
            .on('pointermove', this.selectChess2, this);
    }

    shutdown() {
        this.scene.input
            .off('pointerdown', this.selectChess1, this)
            .off('pointermove', this.selectChess2, this);
        this.bejeweled = undefined;
        this.scene = undefined;
    }

    destroy() {
        this.shutdown();
        return this;
    }

    setEnable(enabled) {
        if (enabled === undefined) {
            enabled = true;
        }
        this.enable = enabled;
        return this;
    }

    selectChess1(pointer) {
        if (!this.enable) {
            return this;
        }
        var chess = this.bejeweled.worldXYToChess(pointer.worldX, pointer.worldY);
        if (chess) {
            this.bejeweled.selectChess1(chess);
        }
    }

    selectChess2(pointer) {
        if (!this.enable) {
            return this;
        }

        if (!pointer.isDown) {
            return;
        }
        var chess = this.bejeweled.worldXYToChess(pointer.worldX, pointer.worldY);
        if (chess && (chess !== this.bejeweled.getSelectedChess1())) {
            this.bejeweled.selectChess2(chess);
        }
    }
}

export default Input;