Spaces:
Sleeping
Sleeping
File size: 5,904 Bytes
12d64f8 |
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 |
/**
* HintManager - Context-aware hint system for RTS game
* Shows helpful hints at the right moment with smooth animations
*/
class HintManager {
constructor() {
this.hintQueue = [];
this.currentHint = null;
this.shownHints = new Set();
this.enabled = true;
this.hintElement = null;
this.translations = {};
this.initializeHintElement();
this.loadHintHistory();
}
initializeHintElement() {
// Create hint container
this.hintElement = document.createElement('div');
this.hintElement.id = 'hint-display';
this.hintElement.className = 'hint-container hidden';
// Add to body
document.body.appendChild(this.hintElement);
console.log('[HintManager] Initialized');
}
loadHintHistory() {
// Load previously shown hints from localStorage
try {
const history = localStorage.getItem('rts_shown_hints');
if (history) {
this.shownHints = new Set(JSON.parse(history));
console.log(`[HintManager] Loaded ${this.shownHints.size} shown hints from history`);
}
} catch (error) {
console.warn('[HintManager] Failed to load hint history:', error);
}
}
saveHintHistory() {
// Save shown hints to localStorage
try {
localStorage.setItem('rts_shown_hints', JSON.stringify([...this.shownHints]));
} catch (error) {
console.warn('[HintManager] Failed to save hint history:', error);
}
}
setTranslations(translations) {
this.translations = translations;
}
translate(key) {
return this.translations[key] || key;
}
showHint(hintKey, options = {}) {
if (!this.enabled) return;
// Check if hint was already shown (unless force is true)
if (!options.force && this.shownHints.has(hintKey)) {
return;
}
// Default options
const config = {
duration: 3000, // 3 seconds
priority: 0, // Higher priority = shown first
force: false, // Show even if already shown
position: 'center', // 'center', 'top', 'bottom'
...options
};
// Add to queue with priority
this.hintQueue.push({ key: hintKey, config });
this.hintQueue.sort((a, b) => b.config.priority - a.config.priority);
// Mark as shown
this.shownHints.add(hintKey);
this.saveHintHistory();
// Show if no current hint
if (!this.currentHint) {
this.displayNext();
}
}
displayNext() {
if (this.hintQueue.length === 0) {
this.currentHint = null;
return;
}
const { key, config } = this.hintQueue.shift();
this.currentHint = { key, config };
// Get translated text
const text = this.translate(key);
// Update hint element
this.hintElement.textContent = text;
this.hintElement.className = `hint-container ${config.position}`;
// Fade in
setTimeout(() => {
this.hintElement.classList.add('visible');
}, 50);
// Auto-hide after duration
setTimeout(() => {
this.hide();
}, config.duration);
console.log(`[HintManager] Showing hint: ${key}`);
}
hide() {
if (!this.hintElement) return;
// Fade out
this.hintElement.classList.remove('visible');
// Wait for animation, then show next
setTimeout(() => {
this.displayNext();
}, 500); // Match CSS transition duration
}
enable() {
this.enabled = true;
console.log('[HintManager] Hints enabled');
}
disable() {
this.enabled = false;
this.hide();
console.log('[HintManager] Hints disabled');
}
toggle() {
this.enabled = !this.enabled;
if (!this.enabled) {
this.hide();
}
return this.enabled;
}
reset() {
// Clear history and show all hints again
this.shownHints.clear();
this.saveHintHistory();
console.log('[HintManager] Hint history reset');
}
// Contextual hint triggers
onGameStart() {
this.showHint('hint.game.welcome', { priority: 10, duration: 4000 });
setTimeout(() => {
this.showHint('hint.game.build_barracks', { priority: 9, duration: 4000 });
}, 4500);
}
onFirstUnitSelected() {
this.showHint('hint.controls.movement', { priority: 8 });
}
onFirstBuildingPlaced() {
this.showHint('hint.game.train_units', { priority: 7 });
}
onMultipleUnitsSelected(count) {
if (count >= 3) {
this.showHint('hint.controls.control_groups', { priority: 6 });
}
}
onLowCredits() {
this.showHint('hint.game.low_credits', { priority: 5 });
}
onLowPower() {
this.showHint('hint.game.low_power', { priority: 5 });
}
onFirstCombat() {
this.showHint('hint.combat.attack_move', { priority: 7 });
}
onControlGroupAssigned() {
this.showHint('hint.controls.control_groups_select', { priority: 6, duration: 4000 });
}
onCameraKeyboard() {
this.showHint('hint.controls.camera_keyboard', { priority: 4 });
}
onLanguageChanged() {
this.showHint('hint.ui.language_changed', { priority: 8, duration: 2000 });
}
}
// Export for use in game.js
if (typeof module !== 'undefined' && module.exports) {
module.exports = HintManager;
}
|