-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.js
287 lines (273 loc) · 8.38 KB
/
chess.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import lib from "./lib.js";
import chessDefaultList from "./chessList.js";
class ChessPiece {
constructor(chessName, level) {
const chess = chessDefaultList[chessName];
this.level = level || 1;
this.backgroundElement = null;
this.element = null;
Object.assign(this, chess);
this.fullHealth = this.health;
this.fightAddition = {};
if (this.level > 1) {
this.setBonus();
}
}
// 攻擊對手棋子
attackPiece(opponentPiece, moveSpeed) {
const attackRandom = Math.floor(
this.attack * ((Math.random(4) + 8) / 10),
2
);
const getAddition = this.fightAddition?.attack?.val || 0;
const attackAddition = attackRandom + getAddition;
const _counterChess = this.getCounter();
if (_counterChess.includes(opponentPiece.race)) {
opponentPiece.health -= attackAddition * 2;
} else {
opponentPiece.health -= attackAddition;
}
if (opponentPiece.health <= 0) {
opponentPiece.health = 0;
setTimeout(() => {
opponentPiece.element.classList.add(`dead-${moveSpeed}`);
}, 250);
} else {
console.log(
`${this.name} attacked ${opponentPiece.name}. ${opponentPiece.name}'s health is now ${opponentPiece.health}.`
);
}
opponentPiece.backgroundElement.style.height = `${
(1 - opponentPiece.health / opponentPiece.fullHealth) * 100
}%`;
}
restore(treat) {
if (!this.health) return;
this.health += treat;
if (this.health > this.fullHealth) {
this.health = this.fullHealth;
}
this.backgroundElement.style.height = `${
(1 - this.health / this.fullHealth) * 100
}%`;
}
setBackgroundElement(element) {
this.backgroundElement = element;
}
setElement(element) {
this.element = element;
}
// 獲取種族
getRace() {
return this.race;
}
// 獲取效果
getCounter() {
return this.counter;
}
getHealth() {
return this.health;
}
getPrice() {
const chessNum = 3 ** (this.level - 1);
if (this.level === 1) return this.price;
return this.price * 0.8 * chessNum;
}
setFightAddition({ val, type, items }) {
if (!this.fightAddition[type]) this.fightAddition[type] = {};
if (!Object.keys(this.fightAddition[type]).length) {
this.fightAddition[type].val = val;
this.fightAddition[type].level = 1;
return;
}
this.fightAddition[type].val += val;
this.fightAddition[type].level = items;
}
setBonus() {
let defaultLevel = 1;
while (defaultLevel <= this.level) {
this.getBonus(defaultLevel);
defaultLevel++;
}
}
// 獲取加成
getBonus(_level) {
const levelBonusList = [1, 1.6, 1.5, 1.4, 1.3, 1.5];
// 假設加成是攻擊力的 10%
this.attack = Math.round(
this.attack * levelBonusList[_level || this.level - 1]
);
this.health = Math.round(
this.health * levelBonusList[_level || this.level - 1]
);
this.fullHealth = this.health;
}
levelUp() {
this.level++;
this.getBonus();
}
// 頁面事件
drag(element, type, typeIndex, Board, User, Shop) {
let touchCtl = false;
element.addEventListener("dragstart", (event) => {
if (touchCtl) return;
const { offsetX, offsetY } = event;
element.dataset.offsetX = offsetX;
element.dataset.offsetY = offsetY;
});
element.addEventListener("drag", (event) => {
if (touchCtl) return;
playerPieceDragMove(event, element, "mouse");
});
element.addEventListener(
"dragover",
(event) => {
event.preventDefault();
},
false
);
element.addEventListener("dragend", (event) => {
if (touchCtl) return;
playerPieceDragEnd(event, element, { Board, User, Shop, _this: this });
});
// touch 事件
element.addEventListener("touchstart", function (event) {
touchCtl = true;
const rect = event.target.getBoundingClientRect();
const offsetX = event.targetTouches[0].pageX - rect.left;
const offsetY = event.targetTouches[0].pageY - rect.top;
element.dataset.offsetX = offsetX;
element.dataset.offsetY = offsetY;
});
element.addEventListener("touchmove", (event) => {
playerPieceDragMove(event, element, "touch");
});
element.addEventListener("touchend", (event) => {
touchCtl = false;
playerPieceDragEnd(event, element, { Board, User, Shop, _this: this });
});
function playerPieceDragMove(mouse, element, type) {
let mouseX, mouseY, elementShow;
if (type === "mouse") {
const { clientX, clientY } = mouse;
mouseX = clientX;
mouseY = clientY;
elementShow = false;
} else if (type === "touch") {
const { pageX, pageY } = mouse.targetTouches[0];
mouseX = pageX;
mouseY = pageY;
elementShow = true;
}
const { offsetX, offsetY } = element.dataset;
const { offsetWidth, offsetHeight } = element;
const touchLeft = mouseX - offsetX;
const touchRight = mouseX + (offsetWidth - offsetX);
const touchTop = mouseY - offsetY;
const touchBottom = mouseY + (offsetHeight - offsetY);
if (touchLeft < 0 || touchTop < 0) return;
Object.assign(element.dataset, {
touchLeft,
touchRight,
touchTop,
touchBottom,
});
element.style.zIndex = 1;
element.style.position = "fixed";
element.style.opacity = 1;
// element.style.opacity = elementShow ? 1 : 0;
element.style.left = `${mouseX - offsetX}px`;
element.style.top = `${mouseY - offsetY}px`;
}
function playerPieceDragEnd(event, element, _thisObject) {
const { Board: _Board, Shop, User, _this } = _thisObject;
// const _Board = Board;
const { touchLeft, touchRight, touchTop, touchBottom } = element.dataset;
// 旗子被賣掉
// 待調整
if (Shop && Shop.getDisplay()) {
const {
left: ShopLeft,
right: ShopRight,
top: ShopTop,
bottom: SHopBottom,
} = Shop.getScope();
if (
ShopTop <= touchTop &&
ShopRight >= touchRight &&
SHopBottom >= touchBottom &&
ShopLeft <= touchLeft
) {
if (type === "user") {
User.removePiece(typeIndex);
User.renderStoragePiece();
} else {
_Board.removePiece(typeIndex);
_Board.renderBoard();
}
const sellPrice = _this.getPrice();
User.sellChess(sellPrice);
return;
}
}
// object
const boardScopeList = _Board.getColDivScope();
const boardIndex = boardScopeList.findIndex(
(item) =>
item.top <= touchTop &&
item.right >= touchRight &&
item.bottom >= touchBottom &&
item.left <= touchLeft
);
const storageIndex = User.storageDivScope.findIndex(
(item) =>
item.top <= touchTop &&
item.right >= touchRight &&
item.bottom >= touchBottom &&
item.left <= touchLeft
);
if (!~boardIndex && !~storageIndex) {
element.style.position = "relative";
element.style.zIndex = "0";
element.style.left = "0";
element.style.top = "0";
return;
}
let oldPiece = null;
if (~boardIndex) {
const _oldIndex = type === "board" ? typeIndex : null;
if (_oldIndex === boardIndex) {
_Board.renderBoard(_oldIndex);
return;
}
oldPiece = _Board.setPiece(_oldIndex, boardIndex, _this);
}
if (~storageIndex) {
const _oldIndex = type === "user" ? typeIndex : null;
if (_oldIndex === storageIndex) {
User.renderStoragePiece(_oldIndex);
return;
}
oldPiece = User.setPiece(_oldIndex, storageIndex, _this);
}
if (type === "user") User.removePiece(typeIndex);
if (type === "board") _Board.removePiece(typeIndex);
if (oldPiece) {
if (type === "user") {
User.setPiece(null, typeIndex, oldPiece);
} else {
_Board.setPiece(null, typeIndex, oldPiece);
}
}
}
}
// getBoardScope() {
// return this.Board.getColDivScope()
// }
}
export default ChessPiece;
// 創建一個自走棋棋子
// const piece = new ChessPiece(chessList[race]);
// 攻擊一個對手棋子
// const opponentPiece = new ChessPiece("Orc Warrior", "Orc", 30, 80, "None");
// piece.attackPiece(opponentPiece);