155 lines
4.3 KiB
JavaScript
155 lines
4.3 KiB
JavaScript
class StaminaSystem {
|
|
constructor(scene) {
|
|
this.scene = scene;
|
|
|
|
// Stamina properties
|
|
this.maxStamina = 100;
|
|
this.currentStamina = 100;
|
|
this.regenRate = 5; // Stamina per second
|
|
this.regenDelay = 2000; // Wait 2s after use before regen
|
|
this.lastUseTime = 0;
|
|
|
|
// Stamina costs
|
|
this.costs = {
|
|
'till': 5,
|
|
'plant': 3,
|
|
'harvest': 4,
|
|
'build': 10,
|
|
'attack': 8,
|
|
'sprint': 2 // per second
|
|
};
|
|
|
|
console.log('⚡ StaminaSystem: Initialized');
|
|
}
|
|
|
|
// Use stamina for action
|
|
useStamina(action) {
|
|
const cost = this.costs[action] || 0;
|
|
|
|
if (this.currentStamina < cost) {
|
|
console.log(`⚡ Not enough stamina! (${this.currentStamina}/${cost})`);
|
|
return false; // Not enough stamina
|
|
}
|
|
|
|
this.currentStamina -= cost;
|
|
this.lastUseTime = Date.now();
|
|
|
|
// Update UI
|
|
this.updateUI();
|
|
|
|
console.log(`⚡ Used ${cost} stamina (${action}). Remaining: ${this.currentStamina}`);
|
|
return true;
|
|
}
|
|
|
|
// Restore stamina (from food)
|
|
restoreStamina(amount) {
|
|
this.currentStamina = Math.min(this.maxStamina, this.currentStamina + amount);
|
|
this.updateUI();
|
|
|
|
console.log(`⚡ Restored ${amount} stamina. Current: ${this.currentStamina}`);
|
|
}
|
|
|
|
// Auto-regenerate stamina
|
|
update(delta) {
|
|
// Check if enough time has passed since last use
|
|
const timeSinceUse = Date.now() - this.lastUseTime;
|
|
|
|
if (timeSinceUse > this.regenDelay && this.currentStamina < this.maxStamina) {
|
|
// Regenerate stamina
|
|
const regenAmount = (this.regenRate * delta) / 1000;
|
|
this.currentStamina = Math.min(this.maxStamina, this.currentStamina + regenAmount);
|
|
|
|
// Update UI
|
|
this.updateUI();
|
|
}
|
|
}
|
|
|
|
// Update stamina bar in UI
|
|
updateUI() {
|
|
const uiScene = this.scene.scene.get('UIScene');
|
|
if (!uiScene) return;
|
|
|
|
// Update stamina bar (if exists)
|
|
if (uiScene.staminaBar) {
|
|
const percentage = this.currentStamina / this.maxStamina;
|
|
uiScene.staminaBar.clear();
|
|
|
|
// Background
|
|
uiScene.staminaBar.fillStyle(0x000000, 0.5);
|
|
uiScene.staminaBar.fillRect(20, 120, 200, 20);
|
|
|
|
// Stamina fill (yellow)
|
|
uiScene.staminaBar.fillStyle(0xffff00, 1.0);
|
|
uiScene.staminaBar.fillRect(20, 120, 200 * percentage, 20);
|
|
|
|
// Border
|
|
uiScene.staminaBar.lineStyle(2, 0xffffff, 1.0);
|
|
uiScene.staminaBar.strokeRect(20, 120, 200, 20);
|
|
}
|
|
|
|
// Update stamina text
|
|
if (uiScene.staminaText) {
|
|
uiScene.staminaText.setText(`⚡ ${Math.floor(this.currentStamina)}/${this.maxStamina}`);
|
|
}
|
|
}
|
|
|
|
// Create stamina UI
|
|
createUI() {
|
|
const uiScene = this.scene.scene.get('UIScene');
|
|
if (!uiScene) return;
|
|
|
|
// Stamina bar (below health)
|
|
uiScene.staminaBar = uiScene.add.graphics();
|
|
uiScene.staminaBar.setScrollFactor(0);
|
|
uiScene.staminaBar.setDepth(1000);
|
|
|
|
// Stamina text
|
|
uiScene.staminaText = uiScene.add.text(
|
|
20,
|
|
105,
|
|
`⚡ ${this.currentStamina}/${this.maxStamina}`,
|
|
{
|
|
fontSize: '14px',
|
|
color: '#ffff00',
|
|
fontStyle: 'bold'
|
|
}
|
|
);
|
|
uiScene.staminaText.setScrollFactor(0);
|
|
uiScene.staminaText.setDepth(1001);
|
|
|
|
// Initial update
|
|
this.updateUI();
|
|
|
|
console.log('⚡ Stamina UI created');
|
|
}
|
|
|
|
// Check if player has enough stamina
|
|
hasStamina(action) {
|
|
const cost = this.costs[action] || 0;
|
|
return this.currentStamina >= cost;
|
|
}
|
|
|
|
// Get stamina percentage (0-1)
|
|
getPercentage() {
|
|
return this.currentStamina / this.maxStamina;
|
|
}
|
|
|
|
// Save/Load
|
|
getSaveData() {
|
|
return {
|
|
currentStamina: this.currentStamina,
|
|
maxStamina: this.maxStamina
|
|
};
|
|
}
|
|
|
|
loadSaveData(data) {
|
|
if (data.currentStamina !== undefined) {
|
|
this.currentStamina = data.currentStamina;
|
|
}
|
|
if (data.maxStamina !== undefined) {
|
|
this.maxStamina = data.maxStamina;
|
|
}
|
|
this.updateUI();
|
|
}
|
|
}
|