-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcard.js
150 lines (133 loc) · 4.33 KB
/
card.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
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
import { Building } from "./building.js"
import { tetrisShapes } from "./shapes.js"
import { BuildingNames } from "./shapes.js"
export class Card{
constructor(index, cards){
this.cardFields = []
this.things = []
this.Create()
this.isSelect = false
this.index = index
this.cards = cards
}
static shape =[
[null, null],
[null, null],
[null, null],
[null, null]
]
static Reset(){
Card.shape = [
[null, null],
[null, null],
[null, null],
[null, null]
]
}
static getShape() {
return this.shape;
}
Create(){
const cardsDiv = document.querySelector(".cardsDiv")
const cardDiv = document.createElement("div")
cardDiv.classList.add("cardDiv")
cardDiv.addEventListener('click', () => {
this.#Select();
});
const random = Math.floor(Math.random() * tetrisShapes.length);
cardDiv.innerHTML += this.#AddImgAndTexts()
cardDiv.innerHTML += this.#AddTable(tetrisShapes[random], random)
cardsDiv.appendChild(cardDiv)
this.cardDiv = cardDiv;
}
#Select(){
this.cards.forEach(card => {
if (card == this){
card.isSelect = true
const cardElement = document.querySelectorAll(".cardDiv .card")[card.index]
cardElement.id = "cardSelected";
this.#CreateShape(card)
}else{
card.isSelect = false
const cardElement = document.querySelectorAll(".cardDiv .card")[card.index]
cardElement.id = ""
}
const alertText = document.getElementById("alertText")
alertText.innerHTML = "Helyezd el a választott tetrist!"
alertText.style.color = "white"
});
}
#AddImgAndTexts(){
const randomStr = this.#generateRandomString();
const randomStr2 = this.#generateRandomString();
return `
<div class="card cardImg">
<p class="cardText">${randomStr}</p>
<svg class="cardTitle" viewBox="0 0 500 500">
<path id="curve" d="M73.2,148.6c4-6.1,65.5-96.8,178.6-95.6c111.3,1.2,170.8,90.3,175.1,97"/>
<text width="500">
<textPath xlink:href="#curve">
${randomStr2}
</textPath>
</text>
</svg>
`
}
#generateRandomString() {
const length = 14;
let randomString = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
for (var i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
randomString += characters.charAt(randomIndex);
}
return randomString;
}
#AddTable(matrix, ShapeRandom){
let result = `<table class="cardTable ${(ShapeRandom == 0) ? "OffsetCardShape" : ""}">`
matrix.forEach(x => {
result += "<tr class=\"cardTr\">"
x.forEach(i =>{
if (i === 1){
const random = Math.floor(Math.random() * BuildingNames.length*2.5);
let buildingName = Building.None;
if (random < BuildingNames.length) {
buildingName = BuildingNames[random];
}
if (random > BuildingNames.length*2.25) {
buildingName = Building.Sword;
}
result += `<td class=\"cardTd tile\">
<img src="${buildingName}" alt="BuildingImage" class="BuildingImg">
</td>`
this.things.push(buildingName)
}else{
this.things.push(null)
result += "<td class=\"cardTd\"></td>"
}
})
result += "</tr>"
});
result += "</table></div>"
return result
}
#CreateShape(card){
let x = 0
let y = 0
Card.shape = [
[null, null],
[null, null],
[null, null],
[null, null]
]
card.things.forEach(element => {
Card.shape[x][y] = element
if (y == 1){
y = 0
x++
}else{
y++
}
});
}
}