diff --git a/project.godot b/project.godot index 4bcc1c4..94d40cf 100644 --- a/project.godot +++ b/project.godot @@ -60,6 +60,11 @@ change_train={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194306,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } +horn={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null) +] +} [rendering] diff --git a/resources/train kit/Textures/colormap.png.import b/resources/train kit/Textures/colormap.png.import index 522eb8a..fb2a274 100644 --- a/resources/train kit/Textures/colormap.png.import +++ b/resources/train kit/Textures/colormap.png.import @@ -2,7 +2,7 @@ importer="texture" type="CompressedTexture2D" -uid="uid://cdwwcby54pecs" +uid="uid://b0ktb1ocwlp1f" path.s3tc="res://.godot/imported/colormap.png-d3d02b00fd834f010bf45b2b9259ea63.s3tc.ctex" metadata={ "imported_formats": ["s3tc_bptc"], diff --git a/resources/train kit/Textures/colormap.png.import~RF15a29b7c.TMP b/resources/train kit/Textures/colormap.png.import~RF15a29b7c.TMP new file mode 100644 index 0000000..522eb8a --- /dev/null +++ b/resources/train kit/Textures/colormap.png.import~RF15a29b7c.TMP @@ -0,0 +1,41 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://cdwwcby54pecs" +path.s3tc="res://.godot/imported/colormap.png-d3d02b00fd834f010bf45b2b9259ea63.s3tc.ctex" +metadata={ +"imported_formats": ["s3tc_bptc"], +"vram_texture": true +} + +[deps] + +source_file="res://resources/train kit/Textures/colormap.png" +dest_files=["res://.godot/imported/colormap.png-d3d02b00fd834f010bf45b2b9259ea63.s3tc.ctex"] + +[params] + +compress/mode=2 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=true +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=0 diff --git a/resources/train kit/Textures/colormap.png.import~RF15a29bca.TMP b/resources/train kit/Textures/colormap.png.import~RF15a29bca.TMP new file mode 100644 index 0000000..522eb8a --- /dev/null +++ b/resources/train kit/Textures/colormap.png.import~RF15a29bca.TMP @@ -0,0 +1,41 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://cdwwcby54pecs" +path.s3tc="res://.godot/imported/colormap.png-d3d02b00fd834f010bf45b2b9259ea63.s3tc.ctex" +metadata={ +"imported_formats": ["s3tc_bptc"], +"vram_texture": true +} + +[deps] + +source_file="res://resources/train kit/Textures/colormap.png" +dest_files=["res://.godot/imported/colormap.png-d3d02b00fd834f010bf45b2b9259ea63.s3tc.ctex"] + +[params] + +compress/mode=2 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=true +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=0 diff --git a/scenes/trainjourney.tscn b/scenes/trainjourney.tscn index 276c153..8a4420c 100644 --- a/scenes/trainjourney.tscn +++ b/scenes/trainjourney.tscn @@ -106,7 +106,8 @@ text = "Comandi: \"A / D - Ruota camera\", \"SPACE - Cambia camera\", \"TAB - Cambia treno\", -\"ESC - Esci\"" +\"ESC - Esci\", +\"Spazio - Horn\"" label_settings = SubResource("LabelSettings_tefeu") [node name="totaldistance_label" type="Label" parent="HUD" unique_id=544099778] diff --git a/scripts/train.gd b/scripts/train.gd index ee19aa7..e471d85 100644 --- a/scripts/train.gd +++ b/scripts/train.gd @@ -38,6 +38,7 @@ var distance_traveled: float = 0.0 var locomotive: Node3D var wagons: Array = [] var wagon_distances: Array = [] +var _horn_player: AudioStreamPlayer func _ready() -> void: @@ -46,6 +47,7 @@ func _ready() -> void: await get_tree().process_frame _create_train() current_speed = base_speed + _create_horn() func _remove_train(): @@ -144,6 +146,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"): + _play_horn() func _update_movement(delta: float) -> void: distance_traveled += current_speed * delta @@ -187,3 +192,54 @@ 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 := 22050 + var duration := 1.8 + var num_samples := int(sample_rate * duration) + + var audio := AudioStreamWAV.new() + audio.format = AudioStreamWAV.FORMAT_16_BITS + audio.mix_rate = sample_rate + audio.stereo = false + + var data := PackedByteArray() + data.resize(num_samples * 2) + + var freq1 := 277.0 # C#4 + var freq2 := 370.0 # F#4 + var freq3 := 311.0 # Eb4 + + for i in num_samples: + var t := float(i) / sample_rate + var envelope := 1.0 + if t < 0.15: + envelope = t / 0.15 + elif t > duration - 0.4: + envelope = (duration - t) / 0.4 + envelope = clampf(envelope, 0.0, 1.0) + + var sample := sin(t * freq1 * TAU) * 0.4 + sample += sin(t * freq2 * TAU) * 0.3 + sample += sin(t * freq3 * TAU) * 0.2 + sample += sin(t * freq1 * 2.0 * TAU) * 0.1 + sample *= envelope + + var value := clampi(int(sample * 24000.0), -32768, 32767) + data[i * 2] = value & 0xFF + data[i * 2 + 1] = (value >> 8) & 0xFF + + audio.data = data + _horn_player.stream = audio + + +func _play_horn() -> void: + if _horn_player and not _horn_player.playing: + _horn_player.play()