-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolBar.js
294 lines (262 loc) · 12 KB
/
toolBar.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
288
289
290
291
292
293
294
class ToolBar {
constructor({ position, width, height, color, padding, gap, toolCount, itemCount, verticalPadding, toolLabels, itemLabels, itemCounts, health, shield, hunger }) {
this.position = position;
this.width = width;
this.height = height;
this.color = color;
this.padding = padding; // ToolBar başındaki ve sonundaki boşluk
this.gap = gap; // Bölmeler arasındaki boşluk
this.toolCount = toolCount; // Tool bölme sayısı
this.itemCount = itemCount; // Item bölme sayısı
this.verticalPadding = verticalPadding; // Bölmelerin üstünde ve altında boşluk
this.toolLabels = toolLabels; // Tool bölmelerin etiketleri
this.itemLabels = itemLabels; // Item bölmelerin etiketleri
this.itemCounts = itemCounts; // Item bölmelerin sayıları
this.health = health; // Can barı
this.shield = shield; // Kalkan barı
this.hunger = hunger; // Açlık barı
this.toolSections = []; // Tool bölümleri
this.itemSections = []; // Item bölümleri
this.selectedSection = null; // Seçili bölüm
this.hoveredBar = null; // Üzerine gelinen bar
this.createSections(); // Parçaları oluştur
}
createSections() {
this.toolSections = []; // Tool bölümlerini temizle
this.itemSections = []; // Item bölümlerini temizle
const sectionSize = Math.min((this.width / 2 - 2 * this.padding - (Math.max(this.toolCount, this.itemCount) - 1) * this.gap) / Math.max(this.toolCount, this.itemCount), this.height - 2 * this.verticalPadding);
// Tool bölümlerini oluştur
for (let i = 0; i < this.toolCount; i++) {
this.toolSections.push({
x: this.position.x + this.padding + i * (sectionSize + this.gap),
y: this.position.y + this.verticalPadding,
size: sectionSize,
color: `#${Math.floor(Math.random() * 16777215).toString(16)}`,
label: this.toolLabels[i],
showLabel: true
});
}
// Item bölümlerini oluştur
for (let i = 0; i < this.itemCount; i++) {
this.itemSections.push({
x: this.position.x + this.width / 2 + this.padding + i * (sectionSize + this.gap),
y: this.position.y + this.verticalPadding,
size: sectionSize,
color: `#${Math.floor(Math.random() * 16777215).toString(16)}`,
label: this.itemLabels[i],
count: this.itemCounts[i],
showLabel: true
});
}
}
drawBars() {
// Health bar
const healthBarHeight = 40;
const healthBarWidth = this.width;
const healthBarX = this.position.x;
const healthBarY = this.position.y - healthBarHeight - 10;
ctx.fillStyle = '#ff0000AA';
ctx.fillRect(healthBarX, healthBarY, healthBarWidth * (this.health / 100), healthBarHeight);
ctx.strokeStyle = 'white';
ctx.strokeRect(healthBarX, healthBarY, healthBarWidth, healthBarHeight);
// Shield bar
const shieldBarHeight = healthBarHeight - 10;
const shieldBarWidth = this.width / 2 - 10;
const shieldBarX = this.position.x;
const shieldBarY = healthBarY - shieldBarHeight - 10;
ctx.fillStyle = '#0000ffAA';
ctx.fillRect(shieldBarX, shieldBarY, shieldBarWidth * (this.shield / 100), shieldBarHeight);
ctx.strokeRect(shieldBarX, shieldBarY, shieldBarWidth, shieldBarHeight);
// Hunger bar
const hungerBarHeight = healthBarHeight - 10;
const hungerBarWidth = this.width / 2 - 10;
const hungerBarX = this.position.x + shieldBarWidth + 20;
const hungerBarY = shieldBarY;
ctx.fillStyle = '#00ff00AA';
ctx.fillRect(hungerBarX, hungerBarY, hungerBarWidth * (this.hunger / 100), hungerBarHeight);
ctx.strokeRect(hungerBarX, hungerBarY, hungerBarWidth, hungerBarHeight);
// Hovered bar
if (this.hoveredBar) {
ctx.fillStyle = 'white';
ctx.font = 'bold 16px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'center';
let textX = healthBarX + healthBarWidth / 2;
let textY = healthBarY - 10;
let value = 0;
let label = '';
switch (this.hoveredBar) {
case 'health':
label = 'Health';
value = this.health;
textX = healthBarX + healthBarWidth / 2;
textY = healthBarY + healthBarHeight / 2;
break;
case 'shield':
label = 'Shield';
value = this.shield;
textX = shieldBarX + shieldBarWidth / 2;
textY = shieldBarY + shieldBarHeight / 2;
break;
case 'hunger':
label = 'Hunger';
value = this.hunger;
textX = hungerBarX + hungerBarWidth / 2;
textY = hungerBarY + hungerBarHeight / 2;
break;
}
ctx.fillText(`${label}: ${value}`, textX, textY);
}
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.position.x, this.position.y, this.width, this.height);
this.drawBars(); // Can, kalkan ve açlık barlarını çiz
// Her bir tool bölümü çiz
this.toolSections.forEach((section, index) => {
ctx.fillStyle = section.color;
ctx.globalAlpha = (this.selectedSection === index) ? 1.0 : 0.5; // Seçili bölüm tam renkte, diğerleri saydam
ctx.fillRect(section.x, section.y, section.size, section.size);
ctx.globalAlpha = 1.0; // Alpha değerini sıfırla
// Eğer label gösteriliyorsa, yazıyı çiz
if (section.showLabel) {
ctx.fillStyle = 'black';
ctx.font = 'bold 16px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const textY = section.y + section.size / 2;
ctx.fillText(section.label, section.x + section.size / 2, textY);
}
});
// Her bir item bölümü çiz
this.itemSections.forEach((section, index) => {
ctx.fillStyle = section.color;
ctx.globalAlpha = 1.0; // Tüm item bölümleri tam renkte
ctx.fillRect(section.x, section.y, section.size, section.size);
ctx.globalAlpha = 1.0; // Alpha değerini sıfırla
// Eğer label gösteriliyorsa, yazıyı çiz
ctx.fillStyle = 'black';
ctx.font = 'bold 16px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const textY = section.y + section.size / 2;
ctx.fillText(section.label, section.x + section.size / 2, textY - 10);
ctx.fillText(section.count, section.x + section.size / 2, textY + 10);
});
// Seçili silahın adını ekranda göster
if (this.selectedSection !== null) {
ctx.fillStyle = 'black';
ctx.font = 'bold 20px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.fillText(this.toolSections[this.selectedSection].label, canvas.width / 2, 10);
}
}
updatePosition() {
this.position.x = canvas.width / 2 - this.width / 2;
this.position.y = canvas.height - this.height - 10;
// ToolBar'ı canvas içinde tutmak için sınırlandırma
if (this.position.x < 0) this.position.x = 0;
if (this.position.x + this.width > canvas.width) this.position.x = canvas.width - this.width;
if (this.position.y < 0) this.position.y = 0;
if (this.position.y + this.height > canvas.height) this.position.y = canvas.height - this.height;
// Her bir bölümün pozisyonunu güncelle
const sectionSize = Math.min((this.width / 2 - 2 * this.padding - (Math.max(this.toolCount, this.itemCount) - 1) * this.gap) / Math.max(this.toolCount, this.itemCount), this.height - 2 * this.verticalPadding);
this.toolSections.forEach((section, index) => {
section.x = this.position.x + this.padding + index * (sectionSize + this.gap);
section.y = this.position.y + this.verticalPadding;
section.size = sectionSize;
});
this.itemSections.forEach((section, index) => {
section.x = this.position.x + this.width / 2 + this.padding + index * (sectionSize + this.gap);
section.y = this.position.y + this.verticalPadding;
section.size = sectionSize;
});
}
setItemCount(newCount) {
this.itemCount = newCount;
this.createSections();
}
handleMouseMove(mouseX, mouseY) {
let needsRedraw = false;
this.toolSections.forEach(section => {
const wasShowing = section.showLabel;
section.showLabel = mouseX >= section.x && mouseX <= section.x + section.size &&
mouseY >= section.y && mouseY <= section.y + section.size;
if (wasShowing !== section.showLabel) {
needsRedraw = true;
}
});
this.itemSections.forEach(section => {
section.showLabel = true; // Item bölümlerinde her zaman göster
});
const barX = this.position.x;
const healthBarY = this.position.y - 50;
const shieldBarY = this.position.y - 90;
const hungerBarY = this.position.y - 90;
const barWidth = this.width;
const halfBarWidth = this.width / 2 - 10;
const hoveredBar = (mouseX >= barX && mouseX <= barX + barWidth) && (
(mouseY >= healthBarY && mouseY <= healthBarY + 40 && (this.hoveredBar = 'health')) ||
(mouseX >= barX && mouseX <= barX + halfBarWidth && mouseY >= shieldBarY && mouseY <= shieldBarY + 20 && (this.hoveredBar = 'shield')) ||
(mouseX >= barX + halfBarWidth + 20 && mouseX <= barX + barWidth && mouseY >= hungerBarY && mouseY <= hungerBarY + 20 && (this.hoveredBar = 'hunger'))
);
if (!hoveredBar) {
this.hoveredBar = null;
}
return needsRedraw || hoveredBar;
}
handleKeyDown(key) {
switch (key) {
case '1':
this.selectedSection = 0;
break;
case '2':
this.selectedSection = 1;
break;
case '3':
this.selectedSection = 2;
break;
case 'q':
this.selectedSection = 3;
break;
case 'e':
this.selectedSection = 4;
break;
}
return true; // Always redraw on keydown
}
}
const toolbar = new ToolBar({
position: {
x: 0,
y: 0
},
width: 1000,
height: 110,
color: '#54AB7750',
padding: 10,
gap: 10,
toolCount: 5, // Başlangıçta 5 tool
itemCount: 5, // Başlangıçta 5 item
verticalPadding: 10, // Üstte ve altta 10 piksel boşluk
toolLabels: ['Balta', 'Kazma', 'Kılıç', 'Ok ve Yay', 'Bomba'],
itemLabels: ['Odun', 'Taş', 'Demir', 'Altın', 'Elmas'],
itemCounts: [0, 0, 0, 0, 0],
health: 100,
shield: 100,
hunger: 100
});
canvas.addEventListener('mousemove', (e) => {
const rect = canvas.getBoundingClientRect();
const mouseX = e.clientX - rect.left;
const mouseY = e.clientY - rect.top;
if (toolbar.handleMouseMove(mouseX, mouseY)) {
toolbar.draw();
}
});
window.addEventListener('keydown', (e) => {
if (toolbar.handleKeyDown(e.key)) {
toolbar.draw();
}
});