-
Notifications
You must be signed in to change notification settings - Fork 0
/
slot.js
52 lines (46 loc) · 1.28 KB
/
slot.js
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
export const slot_type = {
foundation: 'foundation',
stock: 'stock',
discard: 'discard',
tableau: 'tableau'
}
export class slot_t {
constructor(element, type, on_click) {
this.element = element
this.element.addEventListener('click', e => {
if (e.target === this.element)
on_click(this)
})
this.type = type
this.cards = []
}
add(card) {
this.element.appendChild(card.element)
this.cards.push(card)
}
remove(card) {
this.element.removeChild(card.element)
for (let i = 0; i < this.cards.length; i++)
if (this.cards[i] === card)
this.cards.splice(i, 1)
}
get_card_stack(base_card) {
let card_stack = []
for (let card of this.cards) {
if (base_card === card || card_stack.length > 0) // get the pased card and every subsequent
card_stack.push(card)
}
return card_stack
}
top() {
if (this.cards.length > 0)
return this.cards[this.cards.length - 1]
}
}
export function setup_slots(elements, type, on_click) {
let slots = []
elements.forEach((element, i) => {
slots[i] = new slot_t(element, type, on_click)
})
return slots
}