Skip to content

Commit 210a1fb

Browse files
authored
Merge pull request #4 from bitbrain/day-night-cycle
Day night cycle tutorial
2 parents e53d001 + 7a92d0c commit 210a1fb

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,49 @@
11
extends CanvasModulate
22

3+
const MINUTES_PER_DAY = 1440
4+
const MINUTES_PER_HOUR = 60
5+
const INGAME_TO_REAL_MINUTE_DURATION = (2 * PI) / MINUTES_PER_DAY
6+
37

48
signal time_tick(day:int, hour:int, minute:int)
59

610

711
@export var gradient_texture:GradientTexture1D
8-
@export var TIME_MULTIPLIER = 20.0 # speeds up the time
9-
@export var INITIAL_HOUR = 12 # can be between 0-23
12+
@export var INGAME_SPEED = 20.0
13+
@export var INITIAL_HOUR = 12:
14+
set(h):
15+
INITIAL_HOUR = h
16+
time = INGAME_TO_REAL_MINUTE_DURATION * MINUTES_PER_HOUR * INITIAL_HOUR
1017

1118

12-
# let us divide one full cycle by the minutes of a day!
13-
var time_scale:float= ((2 * PI) / 1440) * TIME_MULTIPLIER
1419
var time:float= 0.0
1520
var past_minute:int= -1
16-
var real_time_minute_duration:float
1721

1822

1923
func _ready() -> void:
20-
real_time_minute_duration = (2 * PI) / 1440
21-
time += real_time_minute_duration * 60 * INITIAL_HOUR
24+
time = INGAME_TO_REAL_MINUTE_DURATION * MINUTES_PER_HOUR * INITIAL_HOUR
2225

2326

2427
func _process(delta: float) -> void:
25-
time += delta * time_scale
28+
time += delta * INGAME_TO_REAL_MINUTE_DURATION * INGAME_SPEED
2629

27-
var value = (sin(time - PI / 2) + 1.0) / 2.0
30+
var value = (sin(time - PI / 2.0) + 1.0) / 2.0
2831
self.color = gradient_texture.gradient.sample(value)
2932

3033
_recalculate_time()
3134

3235

3336
func _recalculate_time() -> void:
34-
var total_minutes = int(time / real_time_minute_duration)
35-
var minutes_in_day = total_minutes % 1440
36-
var hours = int(minutes_in_day / 60.0)
37-
var minutes = int(minutes_in_day % 60)
38-
var day = int(total_minutes / 1440.0)
39-
if past_minute != minutes:
40-
past_minute = minutes
41-
time_tick.emit(day, hours, minutes)
37+
var total_minutes = int(time / INGAME_TO_REAL_MINUTE_DURATION)
38+
39+
var day = int(total_minutes / MINUTES_PER_DAY)
40+
41+
var current_day_minutes = total_minutes % MINUTES_PER_DAY
42+
43+
var hour = int(current_day_minutes / MINUTES_PER_HOUR)
44+
var minute = int(current_day_minutes % MINUTES_PER_HOUR)
45+
46+
if past_minute != minute:
47+
past_minute = minute
48+
time_tick.emit(day, hour, minute)
4249

day-night-cycle/tutorial/daynightcycle_scene.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ animations = [{
15391539
"speed": 10.0
15401540
}]
15411541

1542-
[node name="DayNightCycleScene" type="Node2D"]
1542+
[node name="DayNightCycleScene2" type="Node2D"]
15431543
script = ExtResource("1_2tcb3")
15441544

15451545
[node name="SoundMachine" parent="." instance=ExtResource("2_jh0d1")]

default_bus_layout.tres

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
resource_name = "Reverb"
55
predelay_msec = 50.0
66
predelay_feedback = 0.18
7-
room_size = 0.43
8-
damping = 0.18
9-
spread = 0.33
7+
room_size = 0.66
8+
damping = 0.26
9+
spread = 0.5
1010
hipass = 0.1
11-
dry = 0.52
12-
wet = 0.66
11+
dry = 0.29
12+
wet = 0.74
1313

1414
[resource]
1515
bus/0/effect/0/effect = SubResource("AudioEffectReverb_bwuw6")

project.godot

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ run/main_scene="res://day-night-cycle/demo/daynightcycle_scene.tscn"
1515
config/features=PackedStringArray("4.0", "Forward Plus")
1616
config/icon="res://icon.png"
1717

18+
[audio]
19+
20+
driver/output_latency=30
21+
1822
[display]
1923

2024
window/size/viewport_width=384
@@ -23,7 +27,5 @@ window/size/window_width_override=1920
2327
window/size/window_height_override=1080
2428
window/stretch/mode="canvas_items"
2529
window/stretch/aspect="keep_height"
26-
27-
[rendering]
28-
29-
textures/canvas_textures/default_texture_filter=2
30+
window/per_pixel_transparency/allowed=true
31+
window/vsync/vsync_mode=2

0 commit comments

Comments
 (0)