summaryrefslogtreecommitdiff
path: root/src/link.js
blob: cb61549f10410c8caabb9a71a7b73093562cb434 (plain)
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
import { placeOnBoard } from "./engine/visual/placeOnBoard.js"
import { Vector2 } from "./engine/vector2.js"
import { board } from "./board.js"
import { ASSETS_BLACK, ASSETS_WHITE, PLAYER } from "./constants.js"
import { Stone } from "./stone.js"
import { nextTurn, playerTurn } from "./engine/turns.js"


export class Link {
    constructor({
        position = new Vector2(0, 0),
    }) {
        this.team = playerTurn
        this.position = position

        this.createSpan()
        board.links[position.y][position.x] = this

        this.span.addEventListener("click", _ => {
            new Stone({
                position: position,
                team: this.team
            })
            nextTurn({move: position})
        });

        this.update()
    }

    createSpan() {
        this.span = document.createElement("span")
        this.span.className = `link`

        let img = document.createElement("img")
        img.src = this.team == PLAYER.BLACK ? ASSETS_BLACK : ASSETS_WHITE
        this.span.appendChild(img)

        document.body.appendChild(this.span)
    }

    update() {
        let response = placeOnBoard({
            position: this.position,
            board_size: board.size,
            use_link_size: true,
        })

        this.span.style.left = response.left
        this.span.style.top = response.top
        this.span.style.width = this.span.style.height = response.size
    }

    removeLink() {
        this.span.remove()
    }
}