add sky to train scene
This commit is contained in:
@@ -38,6 +38,8 @@ var distance_traveled: float = 0.0
|
||||
var locomotive: Node3D
|
||||
var wagons: Array = []
|
||||
var wagon_distances: Array = []
|
||||
var _horn_player: AudioStreamPlayer
|
||||
var _horn_playing: bool = false
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
@@ -46,6 +48,7 @@ func _ready() -> void:
|
||||
await get_tree().process_frame
|
||||
_create_train()
|
||||
current_speed = base_speed
|
||||
_create_horn()
|
||||
|
||||
|
||||
func _remove_train():
|
||||
@@ -144,6 +147,9 @@ func _handle_input(delta: float) -> void:
|
||||
elif Input.is_action_pressed("change_train"):
|
||||
_create_train()
|
||||
current_speed = base_speed
|
||||
|
||||
if Input.is_action_just_pressed("horn") and not _horn_playing:
|
||||
_play_horn()
|
||||
|
||||
func _update_movement(delta: float) -> void:
|
||||
distance_traveled += current_speed * delta
|
||||
@@ -187,3 +193,56 @@ func get_locomotive_position() -> Vector3:
|
||||
|
||||
func get_speed() -> float:
|
||||
return current_speed
|
||||
|
||||
|
||||
func _create_horn() -> void:
|
||||
_horn_player = AudioStreamPlayer.new()
|
||||
_horn_player.name = "HornPlayer"
|
||||
_horn_player.volume_db = -6.0
|
||||
_horn_player.bus = "Master"
|
||||
add_child(_horn_player)
|
||||
|
||||
var sample_rate: int = 22050
|
||||
var duration: float = 1.8
|
||||
var num_samples: int = int(sample_rate * duration)
|
||||
var fade_in: float = 0.15
|
||||
var fade_out: float = 0.4
|
||||
|
||||
var freqs: Array[float] = [277.18, 369.99, 311.13]
|
||||
|
||||
var data := PackedByteArray()
|
||||
data.resize(num_samples * 2)
|
||||
|
||||
for i in range(num_samples):
|
||||
var t: float = float(i) / sample_rate
|
||||
var sample: float = 0.0
|
||||
for freq in freqs:
|
||||
sample += sin(TAU * freq * t)
|
||||
sample /= freqs.size()
|
||||
|
||||
var envelope: float = 1.0
|
||||
if t < fade_in:
|
||||
envelope = t / fade_in
|
||||
elif t > duration - fade_out:
|
||||
envelope = (duration - t) / fade_out
|
||||
sample *= envelope
|
||||
|
||||
var value: int = clampi(int(sample * 16000), -32768, 32767)
|
||||
data[i * 2] = value & 0xFF
|
||||
data[i * 2 + 1] = (value >> 8) & 0xFF
|
||||
|
||||
var stream := AudioStreamWAV.new()
|
||||
stream.format = AudioStreamWAV.FORMAT_16_BITS
|
||||
stream.mix_rate = sample_rate
|
||||
stream.data = data
|
||||
_horn_player.stream = stream
|
||||
|
||||
|
||||
func _play_horn() -> void:
|
||||
_horn_playing = true
|
||||
_horn_player.play()
|
||||
_horn_player.finished.connect(_on_horn_finished, CONNECT_ONE_SHOT)
|
||||
|
||||
|
||||
func _on_horn_finished() -> void:
|
||||
_horn_playing = false
|
||||
|
||||
@@ -51,6 +51,11 @@ func _process(_delta: float) -> void:
|
||||
|
||||
if Input.is_action_just_pressed("ui_cancel"):
|
||||
SceneSwitcher.switch_scene(main_scene.resource_path)
|
||||
|
||||
if Input.is_action_just_pressed("toggle_debug"):
|
||||
var sky := get_node_or_null("DynamicSky") as DynamicSkyRoot
|
||||
if sky and sky._debug_canvas_layer:
|
||||
sky._debug_canvas_layer.visible = not sky._debug_canvas_layer.visible
|
||||
|
||||
|
||||
func _update_ui() -> void:
|
||||
|
||||
Reference in New Issue
Block a user