From 9853edec4472fe1c3d9f2063b68830d5243c145f Mon Sep 17 00:00:00 2001 From: cha0s Date: Sat, 14 Sep 2024 17:33:41 -0500 Subject: [PATCH] feat: let there be light! --- app/ecs/components/time.js | 2 +- app/react/components/pixi/ecs.jsx | 89 +++++++++++++++++++++++-- app/react/components/pixi/entity.js | 1 + app/react/components/pixi/extensions.js | 5 +- 4 files changed, 89 insertions(+), 8 deletions(-) diff --git a/app/ecs/components/time.js b/app/ecs/components/time.js index ca1fddc..30a2ba8 100644 --- a/app/ecs/components/time.js +++ b/app/ecs/components/time.js @@ -18,6 +18,6 @@ export default class Time extends Component { }; } static properties = { - irlSeconds: {defaultValue: 10 * realSecondsPerGameHour, type: 'uint16'}, + irlSeconds: {defaultValue: 6 * realSecondsPerGameHour, type: 'uint16'}, }; } diff --git a/app/react/components/pixi/ecs.jsx b/app/react/components/pixi/ecs.jsx index 815e8ff..f3c563d 100644 --- a/app/react/components/pixi/ecs.jsx +++ b/app/react/components/pixi/ecs.jsx @@ -1,4 +1,4 @@ -import {Container} from '@pixi/react'; +import {Container, useApp} from '@pixi/react'; import {useCallback, useState} from 'react'; import {useEcsTick} from '@/react/context/ecs.js'; @@ -11,9 +11,9 @@ import TileLayer from './tile-layer.jsx'; import Water from './water.jsx'; export default function Ecs({camera, monopolizers, particleWorker, scale}) { + const app = useApp(); const mainEntityRef = useMainEntity(); const [layers, setLayers] = useState([]); - const [hour, setHour] = useState(10); const [projected, setProjected] = useState([]); const [position, setPosition] = useState({x: 0, y: 0}); const [water, setWater] = useState(); @@ -28,7 +28,88 @@ export default function Ecs({camera, monopolizers, particleWorker, scale}) { setLayers(Object.values(master.TileLayers.$$layersProxies)); } if (update.Time) { - setHour(Math.round(ecs.get(1).Time.hour * 60) / 60); + const {hour} = ecs.get(1).Time; + let brightness, color; + // 0 - 5 night + // 21 - 0 night + if ( + (hour >= 0 && hour < 5) + || hour >= 21 + ) { + brightness = 0.25; + color = 0x2244cc; + } + // 5 - 6 blue + // 20 - 21 blue + if ( + (hour >= 5 && hour < 6) + || (hour >= 20 && hour < 21) + ) { + const mag = hour < 20 ? (hour - 5) : (21 - hour); + brightness = 0.25 + (0.75 * mag); + color = 0x2244cc; + } + // 6 - 7 morning golden + if (hour >= 6 && hour < 7) { + if (hour < 6.25) { + const [r, g, b] = [0xff - 0x22, 0xd3 - 0x44, 0x7a - 0xcc]; + const fraction = ((hour - 6) * 4); + brightness = 1 + (fraction * 0.25); + color = ( + (0x22 + (r * fraction)) << 16 + | (0x44 + (g * fraction)) << 8 + | (0xcc + (b * fraction)) + ); + } + else if (hour >= 6.75) { + const [r, g, b] = [0xff - 0xff, 0xd3 - 0xff, 0x7a - 0xff]; + const fraction = ((7 - hour) * 4); + brightness = 1 + (fraction * 0.25); + color = ( + (0xff + (r * fraction)) << 16 + | (0xff + (g * fraction)) << 8 + | (0xff + (b * fraction)) + ); + } + else { + brightness = 1.25; + color = 0xffd37a; + } + } + // 19 - 20 evening golden + if (hour >= 19 && hour < 20) { + if (hour < 19.25) { + const [r, g, b] = [0xff - 0xff, 0xd3 - 0xff, 0x7a - 0xff]; + const fraction = ((hour - 19) * 4); + brightness = 1 + (fraction * 0.25); + color = ( + (0xff + (r * fraction)) << 16 + | (0xff + (g * fraction)) << 8 + | (0xff + (b * fraction)) + ); + } + else if (hour >= 19.75) { + const [r, g, b] = [0xff - 0x22, 0xd3 - 0x44, 0x7a - 0xcc]; + const fraction = ((20 - hour) * 4); + brightness = 1 + (fraction * 0.25); + color = ( + (0x22 + (r * fraction)) << 16 + | (0x44 + (g * fraction)) << 8 + | (0xcc + (b * fraction)) + ); + } + else { + brightness = 1.25; + color = 0xffd37a; + } + } + // 7 - 19 day + if (hour >= 7 && hour < 19) { + brightness = 1; + color = 0xffffff; + } + app.ambientLight.brightness = brightness; + app.ambientLight.color = color; } if (update.Water) { setWater(master.Water.water); @@ -42,7 +123,7 @@ export default function Ecs({camera, monopolizers, particleWorker, scale}) { setPosition(Position.toJSON()); setProjected(Wielder.activeItem()?.project(Position.tile, Direction.quantize(4))); } - }, [mainEntityRef]); + }, [app.ambientLight, mainEntityRef]); useEcsTick(onEcsTick); return (