Files
caravels-community-simulation/js/time.js
rafaeldpsilva adbbf6bf50 first commit
2025-12-10 12:32:12 +00:00

93 lines
3.2 KiB
JavaScript

const pauseButton = document.getElementById('pause-button');
const playButton = document.getElementById('play-button');
const fastButton = document.getElementById('fast-button');
const timeDisplay = document.getElementById('time-display');
// --- Time System Variables ---
const NORMAL_SPEED = 60; // 1 real sec = 1 game min
const FAST_SPEED = 1000; // 1 real sec = 10 game min
let timeScaleMultiplier = NORMAL_SPEED; // Start at normal speed
let totalGameSecondsElapsed = 0;
let currentDay = 80;
let currentHour = 7.0; // Start at 8:00 AM
let lastTimestamp = 0; // For delta time calculation
let lastUpdateRowIndex = 0;
function initTime(){
// Time Control Button Listeners
pauseButton.addEventListener('click', () => setTimeScale(0));
playButton.addEventListener('click', () => setTimeScale(NORMAL_SPEED));
fastButton.addEventListener('click', () => setTimeScale(FAST_SPEED));
// Initialize time
totalGameSecondsElapsed = (currentDay - 1) * 86400 + currentHour * 3600;
updateTimeDisplay(); // Initial display update
updateButtonStates(); // Set initial button active state
}
// --- Time System Functions ---
function updateTime(deltaTime) {
if (timeScaleMultiplier <= 0) return; // Don't update if paused
totalGameSecondsElapsed += deltaTime * timeScaleMultiplier;
const SECONDS_IN_DAY = 86400;
const SECONDS_IN_HOUR = 3600;
let previousDay = currentDay;
currentDay = Math.floor(totalGameSecondsElapsed / SECONDS_IN_DAY) + 1;
currentHour = (totalGameSecondsElapsed % SECONDS_IN_DAY) / SECONDS_IN_HOUR;
if (currentDay !== previousDay) {
console.log(`Day ${currentDay} has begun!`);
// Add any daily logic here
}
updateTimeDisplay();
}
function formatTime(hour) {
const hours = Math.floor(hour);
const minutes = Math.floor((hour - hours) * 60);
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}`;
}
function updateTimeDisplay() {
timeDisplay.textContent = `Day ${currentDay}, ${formatTime(currentHour)}`;
}
function setTimeScale(newScale) {
timeScaleMultiplier = newScale;
console.log(`Time scale set to: ${timeScaleMultiplier}`);
updateButtonStates();
}
function updateButtonStates() {
pauseButton.classList.toggle('active', timeScaleMultiplier === 0);
playButton.classList.toggle('active', timeScaleMultiplier === NORMAL_SPEED);
fastButton.classList.toggle('active', timeScaleMultiplier === FAST_SPEED);
}
function animateUpdateTime(timestamp){
// Calculate Delta Time
if (lastTimestamp === 0) {
lastTimestamp = timestamp; // Initialize on first frame
}
const deltaTime = (timestamp - lastTimestamp) / 1000; // Delta time in seconds
lastTimestamp = timestamp;
// --- Update Time ---
updateTime(deltaTime);
return totalGameSecondsElapsed;
}
function getCurrentRowIndex(){
// --- Update buildings every 15 game minutes ---
const SECONDS_IN_DAY = 86400;
const SECONDS_IN_HOUR = 3600;
const MINUTES_PER_ROW = 15;
const SECONDS_PER_ROW = MINUTES_PER_ROW * 60;
// Calculate which row should be active based on totalGameSecondsElapsed
const rowIndex = Math.floor((totalGameSecondsElapsed % SECONDS_IN_DAY) / SECONDS_PER_ROW);
return rowIndex;
}