Merge pull request #1025 from redbirdly/patch-5

Update sky.js
This commit is contained in:
slweeb 2025-02-22 22:53:13 -05:00 committed by GitHub
commit 66ca502763
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 53 additions and 51 deletions

View File

@ -2,7 +2,7 @@ if (!enabledMods.includes("mods/betterSettings.js")) { enabledMods.unshift("mods
var sky_settingsTab = new SettingsTab("Sky"); var sky_settingsTab = new SettingsTab("Sky");
var realtime_setting = new Setting("Use real life time for sky", "real_time", settingType.BOOLEAN, false, defaultValue=true); var realtime_setting = new Setting("Use real life time for sky", "real_time", settingType.BOOLEAN, false, defaultValue=false);
var initial_hour_setting = new Setting("Initial hour", "initial_hour", settingType.NUMBER, false, defaultValue=8); var initial_hour_setting = new Setting("Initial hour", "initial_hour", settingType.NUMBER, false, defaultValue=8);
var ticks_per_hour_setting = new Setting("Ticks per hour", "ticks_per_hour", settingType.NUMBER, false, defaultValue=150); var ticks_per_hour_setting = new Setting("Ticks per hour", "ticks_per_hour", settingType.NUMBER, false, defaultValue=150);
@ -12,78 +12,80 @@ sky_settingsTab.registerSettings("Ticks per hour", ticks_per_hour_setting);
settingsManager.registerTab(sky_settingsTab); settingsManager.registerTab(sky_settingsTab);
var hour = initial_hour_setting.value; //function lerpColor(start, end, t) {
// return start.map((s, i) => Math.round(s + (end[i] - s) * t));
function lerpColor(start, end, t) { //}
return start.map((s, i) => Math.round(s + (end[i] - s) * t)); // Destructuring makes it faster
function lerpColor([r1, g1, b1], [r2, g2, b2], t) {
return [r1 + (r2 - r1) * t, g1 + (g2 - g1) * t, b1 + (b2 - b1) * t].map(Math.round);
} }
function getSkyColors(hour) { function getSkyColors(hour) {
const SKY_COLOR_PAIRS = [ const SKY_COLOR_PAIRS = [
[[0, 0, 15], [0, 0, 30]], // midnight [[0, 0, 15], [0, 0, 30]], // midnight
[[10, 10, 40], [20, 20, 60]], [[10, 10, 40], [20, 20, 60]],
[[255, 100, 50], [255, 150, 100]], [[255, 100, 50], [255, 150, 100]],
[[135, 206, 235], [180, 230, 255]], [[135, 206, 235], [180, 230, 255]],
[[135, 206, 250], [135, 206, 255]], [[135, 206, 250], [135, 206, 255]],
[[135, 206, 250], [120, 190, 240]], [[135, 206, 250], [120, 190, 240]],
[[255, 150, 100], [120, 70, 70]], [[255, 150, 100], [120, 70, 70]],
[[30, 15, 60], [20, 10, 40]], [[30, 15, 60], [20, 10, 40]],
[[0, 0, 15], [0, 0, 30]], // midnight [[0, 0, 15], [0, 0, 30]], // midnight
]; ];
// Determine the interval (each interval is 3 hours) // Determine the interval (each interval is 3 hours)
const index = Math.floor(hour / 3); const index = Math.floor(hour / 3);
const t = (hour % 3) / 3; const t = (hour % 3) / 3;
const [bottomStart, topStart] = SKY_COLOR_PAIRS[index]; const [bottomStart, topStart] = SKY_COLOR_PAIRS[index];
const [bottomEnd, topEnd] = SKY_COLOR_PAIRS[index + 1]; const [bottomEnd, topEnd] = SKY_COLOR_PAIRS[index + 1];
return { return {
skyTop: `rgb(${lerpColor(topStart, topEnd, t).join(", ")})`, skyTop: `rgb(${lerpColor(topStart, topEnd, t).join(", ")})`,
skyBottom: `rgb(${lerpColor(bottomStart, bottomEnd, t).join(", ")})`, skyBottom: `rgb(${lerpColor(bottomStart, bottomEnd, t).join(", ")})`,
}; };
} }
function renderSky(ctx) { function renderSky(ctx) {
const { skyTop, skyBottom } = getSkyColors(hour); // Get sky colors and make gradient
const gradient = ctx.createLinearGradient(0, 0, 0, height * pixelSize); const { skyTop, skyBottom } = getSkyColors(hour);
const gradient = ctx.createLinearGradient(0, 0, 0, height * pixelSize);
gradient.addColorStop(0, skyTop);
gradient.addColorStop(1, skyBottom);
gradient.addColorStop(0, skyTop); ctx.fillStyle = gradient;
gradient.addColorStop(1, skyBottom); ctx.fillRect(0, 0, (width + 1) * pixelSize, (height + 1) * pixelSize);
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, width * pixelSize, height * pixelSize);
} }
function updateDayTime() { function updateDayTime() {
if (realtime_setting.value) { if (paused) {return;}
const now = new Date();
hour = now.getHours() + now.getMinutes() / 60; if (realtime_setting.value) {
} else { const now = new Date();
hour = (hour + (1 / ticks_per_hour_setting.value)) % 24; // Keep within 0-23 hour = now.getHours() + now.getMinutes() / 60;
} } else {
hour = (hour + (1 / ticks_per_hour_setting.value)) % 24; // Keep within 0-23
}
} }
// Make sure the sky gets VIP treatment in the render list // Sky should be first layer
function prioritizeRenderSky() { function prioritizeRenderSky() {
const idx = renderPrePixelList.indexOf(renderSky); const idx = renderPrePixelList.indexOf(renderSky);
if (idx !== -1) { if (idx !== -1) {
const [skyFn] = renderPrePixelList.splice(idx, 1); const [skyFn] = renderPrePixelList.splice(idx, 1);
renderPrePixelList.unshift(skyFn); renderPrePixelList.unshift(skyFn);
} }
} }
// Resetting canvas also resets time // Resetting canvas also resets time
function initializeCanvas() { function initializeSky() {
const resizeCanvas = autoResizeCanvas; hour = initial_hour_setting.value;
autoResizeCanvas = (clear) => {
resizeCanvas(clear);
hour = initial_hour_setting.value;
};
} }
// Hooks // Hooks
setTimeout(initializeCanvas, 500); runAfterReset(initializeSky);
runAfterLoad(prioritizeRenderSky); runAfterLoad(prioritizeRenderSky);
runEveryTick(updateDayTime); runEveryTick(updateDayTime);
renderPrePixel(renderSky); renderPrePixel(renderSky);
var hour = initial_hour_setting.value;