day night cycle
This commit is contained in:
parent
c21e97e2ce
commit
e7cd369b63
3 changed files with 53 additions and 23 deletions
|
@ -16,6 +16,10 @@ export default {
|
||||||
redraw: null
|
redraw: null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
watch: {
|
||||||
|
// x () { this.refresh() },
|
||||||
|
time () { this.refresh() }
|
||||||
|
},
|
||||||
mounted () {
|
mounted () {
|
||||||
const canvasSize = 512
|
const canvasSize = 512
|
||||||
const godraysSize = 128
|
const godraysSize = 128
|
||||||
|
@ -33,6 +37,15 @@ export default {
|
||||||
this.refresh()
|
this.refresh()
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
/* time value to sun position conversion
|
||||||
|
*
|
||||||
|
* The time value rotates from 0 to 1000
|
||||||
|
* sunY convertes it to values between 0 and -100,
|
||||||
|
* while -100 is high sun position (aka day)
|
||||||
|
* and 0 is low (aka night).
|
||||||
|
* My adaption of Solar Quartet renders a static night sky from -30 upwards
|
||||||
|
* and a static day at -70 or lower
|
||||||
|
*/
|
||||||
sunY () {
|
sunY () {
|
||||||
// time is between 0 and 1000
|
// time is between 0 and 1000
|
||||||
const p = Math.PI / 1000
|
const p = Math.PI / 1000
|
||||||
|
@ -41,8 +54,9 @@ export default {
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
refresh () {
|
refresh () {
|
||||||
|
// console.time('draw background')
|
||||||
this.redraw(this.x, this.sunY)
|
this.redraw(this.x, this.sunY)
|
||||||
this.timeout = setTimeout(() => this.refresh(), 50)
|
// console.timeEnd('draw background')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
<div id="player" :class="[player_direction]" />
|
<div id="player" :class="[player_direction]" />
|
||||||
<div id="level-indicator">x:{{ floorX }}, y:{{ floorY }} (@{{time}})</div>
|
<div id="level-indicator">x:{{ floorX }}, y:{{ floorY }} ({{clock}})</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ export default {
|
||||||
player_velocity_y: 9,
|
player_velocity_y: 9,
|
||||||
gravity: 8.0 / 20,
|
gravity: 8.0 / 20,
|
||||||
moving: false,
|
moving: false,
|
||||||
time: 600
|
time: 250
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mounted () {
|
mounted () {
|
||||||
|
@ -84,19 +84,25 @@ export default {
|
||||||
|
|
||||||
if (t >= 80 && t < 120) return "morning0"
|
if (t >= 80 && t < 120) return "morning0"
|
||||||
if (t >= 120 && t < 150) return "morning1"
|
if (t >= 120 && t < 150) return "morning1"
|
||||||
if (t >= 150 && t < 250) return "morning2"
|
if (t >= 150 && t < 240) return "morning2"
|
||||||
|
|
||||||
if (t >= 700 && t < 800) return "evening0"
|
if (t >= 700 && t < 800) return "evening0"
|
||||||
if (t >= 800 && t < 850) return "evening1"
|
if (t >= 800 && t < 850) return "evening1"
|
||||||
if (t >= 850 && t < 900) return "evening2"
|
if (t >= 850 && t < 900) return "evening2"
|
||||||
|
|
||||||
return "day"
|
return "day"
|
||||||
|
},
|
||||||
|
clock () {
|
||||||
|
const t = this.time * 86.4 // 1000 ticks to 86400 seconds (per day)
|
||||||
|
const h = ~~(t / 3600.0)
|
||||||
|
const m = ~~((t / 3600.0 - h) * 60.0)
|
||||||
|
return `${(h + 2) % 24}:${m < 10 ? '0' : ''}${m}`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
move () {
|
move () {
|
||||||
// set time of day in ticks
|
// set time of day in ticks
|
||||||
this.time = (this.time + 1) % 1000
|
this.time = (this.time + 0.1) % 1000
|
||||||
|
|
||||||
const x = this.x
|
const x = this.x
|
||||||
const y = this.y
|
const y = this.y
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// sunY sets the height of the sun and with this the time of the day
|
// sunY sets the height of the sun and with this the time of the day
|
||||||
// it should be an offset from the middle somewhere between -24 and 24
|
// where 0 is lowest (night) and -100 is highest (day), other values are possible
|
||||||
|
// but don't make much sense / difference
|
||||||
export default function drawFrame (canvas, ctx, width, height, grCanvas, grCtx, grWidth, grHeight, frame, sunY) {
|
export default function drawFrame (canvas, ctx, width, height, grCanvas, grCtx, grWidth, grHeight, frame, sunY) {
|
||||||
// reset canvas state
|
// reset canvas state
|
||||||
canvas.width = width
|
canvas.width = width
|
||||||
|
@ -30,12 +31,15 @@ export default function drawFrame (canvas, ctx, width, height, grCanvas, grCtx,
|
||||||
// If you're not space-bound you can add another stop or two, maybe fade out to black,
|
// If you're not space-bound you can add another stop or two, maybe fade out to black,
|
||||||
// but this actually looks good enough.
|
// but this actually looks good enough.
|
||||||
|
|
||||||
emissionGradient.addColorStop(.1, '#0C0804') // pixels in radius 0 to 4.4 (44 * .1).
|
// we use different gradients for different day times (sunY):
|
||||||
emissionGradient.addColorStop(.2, '#060201') // everything past radius 8.8.
|
// > -25: night (black sky)
|
||||||
/* TODO: NIGHT
|
// < -70: day (blue sky)
|
||||||
* emissionGradient.addColorStop(.1, '#000') // pixels in radius 0 to 4.4 (44 * .1).
|
// otherwise red (dawn / sunset)
|
||||||
* emissionGradient.addColorStop(.2, '#000') // everything past radius 8.8.
|
|
||||||
*/
|
if (sunY < -25 && sunY > -70) { // dawn or sunset
|
||||||
|
emissionGradient.addColorStop(.1, '#0C0804') // pixels in radius 0 to 4.4 (44 * .1).
|
||||||
|
emissionGradient.addColorStop(.2, '#060201') // everything past radius 8.8.
|
||||||
|
}
|
||||||
|
|
||||||
// Now paint the gradient all over our godrays canvas.
|
// Now paint the gradient all over our godrays canvas.
|
||||||
grCtx.fillRect(0, 0, grWidth, grHeight)
|
grCtx.fillRect(0, 0, grWidth, grHeight)
|
||||||
|
@ -44,14 +48,17 @@ export default function drawFrame (canvas, ctx, width, height, grCanvas, grCtx,
|
||||||
grCtx.fillStyle = '#000'
|
grCtx.fillStyle = '#000'
|
||||||
|
|
||||||
// Paint the sky
|
// Paint the sky
|
||||||
// TODO: can this be used for day and night, too?
|
|
||||||
const skyGradient = ctx.createLinearGradient(0, 0, 0, height)
|
const skyGradient = ctx.createLinearGradient(0, 0, 0, height)
|
||||||
skyGradient.addColorStop(0, '#2a3e55') // Blueish at the top.
|
if (sunY > -25) { // night
|
||||||
skyGradient.addColorStop(.7, '#8d4835') // Reddish at the bottom.
|
skyGradient.addColorStop(0, '#001') // slight blue hue
|
||||||
/* TODO: NIGHT
|
skyGradient.addColorStop(.7, '#004') // but mainly black
|
||||||
* skyGradient.addColorStop(0, '#000') // Blueish at the top.
|
} else if (sunY < -70) { // day
|
||||||
* skyGradient.addColorStop(.7, '#000') // Reddish at the bottom.
|
skyGradient.addColorStop(0, '#79F') // blue with a light gradient
|
||||||
*/
|
skyGradient.addColorStop(.7, '#33A') // into more blue
|
||||||
|
} else { // dawn / sunset
|
||||||
|
skyGradient.addColorStop(0, '#2a3e55') // Blueish at the top.
|
||||||
|
skyGradient.addColorStop(.7, '#8d4835') // Reddish at the bottom.
|
||||||
|
}
|
||||||
ctx.fillStyle = skyGradient
|
ctx.fillStyle = skyGradient
|
||||||
ctx.fillRect(0, 0, width, height)
|
ctx.fillRect(0, 0, width, height)
|
||||||
|
|
||||||
|
@ -68,10 +75,13 @@ export default function drawFrame (canvas, ctx, width, height, grCanvas, grCtx,
|
||||||
for(let i = 0; i < 4; i++) {
|
for(let i = 0; i < 4; i++) {
|
||||||
// Set the main canvas fillStyle to a shade of brown with variable lightness
|
// Set the main canvas fillStyle to a shade of brown with variable lightness
|
||||||
// (darker at the front)
|
// (darker at the front)
|
||||||
ctx.fillStyle = `hsl(7, 23%, ${23-i*6}%)`;
|
if (sunY > -25) { // night
|
||||||
/* TODO: NIGHT
|
ctx.fillStyle = `hsl(5, 23%, ${4-i}%)`
|
||||||
* ctx.fillStyle = `hsl(5, 23%, ${4-i}%)`;
|
} else if (sunY < -70) { // day (TODO)
|
||||||
*/
|
ctx.fillStyle = `hsl(${220 - i*40}, 23%, ${33-i*6}%)`
|
||||||
|
} else { // dawn / sunset
|
||||||
|
ctx.fillStyle = `hsl(7, 23%, ${23-i*6}%)`
|
||||||
|
}
|
||||||
|
|
||||||
// For each column in our canvas...
|
// For each column in our canvas...
|
||||||
for(let x = width; x--;) {
|
for(let x = width; x--;) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue