File size: 5,994 Bytes
e812b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class GameState {
    constructor() {
        this.resources = {
            gold: 0,
            wood: 100,
            stone: 50,
            population: 10
        };
        
        this.buildings = {
            house: { level: 0, production: 1, cost: { wood: 10, stone: 5 } },
            farm: { level: 0, production: 2, cost: { wood: 20, stone: 10 } },
            mine: { level: 0, production: 5, cost: { wood: 30, stone: 20 } }
        };
        
        this.upgrades = {};
        this.lastUpdate = Date.now();
        this.gameSpeed = 1;
    }

    canAfford(cost) {
        return Object.entries(cost).every(([resource, amount]) => 
            this.resources[resource] >= amount
        );
    }

    purchase(cost) {
        if (this.canAfford(cost)) {
            Object.entries(cost).forEach(([resource, amount]) => {
                this.resources[resource] -= amount;
            });
            return true;
        }
        return false;
    }

    updateResources() {
        const now = Date.now();
        const deltaTime = (now - this.lastUpdate) / 1000;
        
        // Auto-generate resources from buildings
        Object.values(this.buildings).forEach(building => {
            if (building.level > 0) {
                this.resources.gold += building.production * building.level * deltaTime * this.gameSpeed;
            }
        });
        
        this.lastUpdate = now;
        this.saveGame();
    }

    saveGame() {
        localStorage.setItem('idleEmpireSave', JSON.stringify({
            resources: this.resources,
            buildings: this.buildings,
            upgrades: this.upgrades,
            lastUpdate: this.lastUpdate
        }));
    }

    loadGame() {
        const save = localStorage.getItem('idleEmpireSave');
        if (save) {
            const data = JSON.parse(save);
            Object.assign(this, data);
            
            // Calculate offline progress
            const offlineTime = (Date.now() - this.lastUpdate) / 1000;
            if (offlineTime > 1) {
                Object.values(this.buildings).forEach(building => {
                    if (building.level > 0) {
                        this.resources.gold += building.production * building.level * offlineTime * 0.5; // 50% offline efficiency
                    }
                });
            }
        }
    }
}

class GameManager {
    constructor() {
        this.gameState = new GameState();
        this.uiComponents = [];
        this.isRunning = false;
    }

    init() {
        this.gameState.loadGame();
        this.startGameLoop();
        this.setupEventListeners();
    }

    startGameLoop() {
        this.isRunning = true;
        this.gameLoop();
    }

    gameLoop() {
        if (this.isRunning) {
            this.gameState.updateResources();
            this.updateUI();
            requestAnimationFrame(() => this.gameLoop());
        }
    }

    updateUI() {
        this.uiComponents.forEach(component => {
            if (typeof component.update === 'function') {
                component.update(this.gameState);
            }
        });
    }

    registerUIComponent(component) {
        this.uiComponents.push(component);
    }

    setupEventListeners() {
        // Global keyboard shortcuts
        document.addEventListener('keydown', (e) => {
            if (e.ctrlKey && e.key === 's') {
                e.preventDefault();
                this.gameState.saveGame();
                this.showNotification('Game saved!', 'success');
            }
        });
    }

    showNotification(message, type = 'info') {
        const notification = document.createElement('div');
        notification.className = `fixed top-4 right-4 glass-effect p-4 rounded-lg shadow-lg transform translate-x-full transition-transform duration-300 z-50 ${
            type === 'success' ? 'border-l-4 border-green-400' : 
            type === 'error' ? 'border-l-4 border-red-400' : 
            'border-l-4 border-primary-400'
        }`;
        notification.innerHTML = `
            <div class="flex items-center space-x-2">
                <i data-feather="${type === 'success' ? 'check-circle' : type === 'error' ? 'alert-circle' : 'info'}"></i>
                <span>${message}</span>
            </div>
        `;
        
        document.body.appendChild(notification);
        feather.replace();
        
        setTimeout(() => {
            notification.classList.remove('translate-x-full');
            notification.classList.add('translate-x-0');
        }, 100);
        
        setTimeout(() => {
            notification.classList.remove('translate-x-0');
            notification.classList.add('translate-x-full');
            setTimeout(() => notification.remove(), 300);
        }, 3000);
    }
}

// Initialize game manager
const gameManager = new GameManager();

// Utility functions
function formatNumber(num) {
    if (num >= 1000000) {
        return (num / 1000000).toFixed(1) + 'M';
    }
    if (num >= 1000) {
        return (num / 1000).toFixed(1) + 'K';
    }
    return Math.floor(num).toString();
}

function createResourceDisplay(resource, amount) {
    return `
        <div class="flex items-center justify-between glass-effect p-3 rounded-lg">
            <div class="flex items-center space-x-2">
                <i data-feather="${getResourceIcon(resource)}" class="w-5 h-5"></i>
                <span class="font-semibold capitalize">${resource}</span>
            </div>
            <span class="number-count font-mono">${formatNumber(amount)}</span>
        </div>
    `;
}

function getResourceIcon(resource) {
    const icons = {
        gold: 'dollar-sign',
        wood: 'tree',
        stone: 'square',
        population: 'users'
    };
    return icons[resource] || 'circle';
}

// Export for use in components
window.gameManager = gameManager;
window.formatNumber = formatNumber;
window.createResourceDisplay = createResourceDisplay;
window.getResourceIcon = getResourceIcon;