-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColorManager.ts
More file actions
72 lines (59 loc) · 2.33 KB
/
ColorManager.ts
File metadata and controls
72 lines (59 loc) · 2.33 KB
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
import gameConfig from './config.json';
class ColorManager {
private availableColors: string[];
private assignedColors: Set<string> = new Set();
private playerColor: string = gameConfig.colors.playerDefault;
constructor() {
this.availableColors = [...gameConfig.colors.palette];
}
setPlayerColor(color: string): void {
// If the player's color was previously assigned to a bot, make it available again
if (this.assignedColors.has(this.playerColor)) {
this.assignedColors.delete(this.playerColor);
if (!this.availableColors.includes(this.playerColor)) {
this.availableColors.push(this.playerColor);
}
}
this.playerColor = color;
// Remove player color from available pool if it's there
const playerColorIndex = this.availableColors.indexOf(this.playerColor);
if (playerColorIndex > -1) {
this.availableColors.splice(playerColorIndex, 1);
}
// Ensure it's not marked as assigned to a bot
this.assignedColors.delete(this.playerColor);
}
getBotColor(): string | null {
// Filter out the current player color just in case
const validColors = this.availableColors.filter(c => c !== this.playerColor);
if (validColors.length === 0) {
console.warn("ColorManager: No unique colors left in the palette!");
// Fallback: maybe reuse colors or return a default?
return gameConfig.colors.palette[Math.floor(Math.random() * gameConfig.colors.palette.length)];
}
// Simple strategy: pick a random available color
const colorIndex = Math.floor(Math.random() * validColors.length);
const assignedColor = validColors[colorIndex];
// Remove from available and add to assigned
this.availableColors.splice(this.availableColors.indexOf(assignedColor), 1);
this.assignedColors.add(assignedColor);
return assignedColor;
}
releaseBotColor(color: string): void {
if (this.assignedColors.has(color)) {
this.assignedColors.delete(color);
// Add back to available only if it's not the player's color
if (color !== this.playerColor && !this.availableColors.includes(color)) {
this.availableColors.push(color);
}
}
}
reset(): void {
this.assignedColors.clear();
this.availableColors = [...gameConfig.colors.palette];
// Ensure player color constraint is reapplied after reset
this.setPlayerColor(this.playerColor);
}
}
// Export a singleton instance
export const colorManager = new ColorManager();