Browse Source

:tada: Play music when inside a vehicle

DricomDragon 1 year ago
parent
commit
93f2e63bba

+ 4 - 0
first3d/component/entity/walker/walker.gd

@@ -3,6 +3,8 @@ extends CharacterBody3D
 
 
 signal focus_required(me: Node3D)
+signal got_in
+signal got_out
 
 ## How fast the player moves in meters per second.
 @export var speed = 14
@@ -112,6 +114,7 @@ func _get_in_vehicle(commander: LocalInput) -> void:
 	add_collision_exception_with(_vehicle)
 	_vehicle.drive_with(commander)
 	_seat = _find_seat_on(_vehicle)
+	got_in.emit()
 
 
 ## Get out of the current vehicle
@@ -122,6 +125,7 @@ func _get_out_vehicle() -> void:
 	_vehicle = null
 	_seat = null
 	_get_head_up()
+	got_out.emit()
 
 
 ## Make the player stand up

BIN
first3d/flow/music_player/00-Wesh-tone-realaze.mp3


+ 19 - 0
first3d/flow/music_player/00-Wesh-tone-realaze.mp3.import

@@ -0,0 +1,19 @@
+[remap]
+
+importer="mp3"
+type="AudioStreamMP3"
+uid="uid://c3l60bt307wjh"
+path="res://.godot/imported/00-Wesh-tone-realaze.mp3-7ff17dee421488845aec68673b837b9a.mp3str"
+
+[deps]
+
+source_file="res://flow/music_player/00-Wesh-tone-realaze.mp3"
+dest_files=["res://.godot/imported/00-Wesh-tone-realaze.mp3-7ff17dee421488845aec68673b837b9a.mp3str"]
+
+[params]
+
+loop=false
+loop_offset=0
+bpm=0
+beat_count=0
+bar_beats=4

+ 23 - 0
first3d/flow/music_player/music_player.gd

@@ -0,0 +1,23 @@
+class_name MusicPlayer
+extends AudioStreamPlayer
+
+
+func resume() -> void:
+	if stream_paused:
+		stream_paused = false
+	else:
+		play()
+
+
+func pause() -> void:
+	stream_paused = true
+
+
+func _on_driver_got_in():
+	print("play")
+	resume()
+
+
+func _on_driver_got_out():
+	print("stop")
+	pause()

+ 8 - 0
first3d/flow/music_player/music_player.tscn

@@ -0,0 +1,8 @@
+[gd_scene load_steps=3 format=3 uid="uid://wde2y0rin5b2"]
+
+[ext_resource type="AudioStream" uid="uid://c3l60bt307wjh" path="res://flow/music_player/00-Wesh-tone-realaze.mp3" id="1_k8gsu"]
+[ext_resource type="Script" path="res://flow/music_player/music_player.gd" id="2_fda0p"]
+
+[node name="MusicPlayer" type="AudioStreamPlayer"]
+stream = ExtResource("1_k8gsu")
+script = ExtResource("2_fda0p")

+ 9 - 1
first3d/run/levels/infinite_level.tscn

@@ -1,4 +1,4 @@
-[gd_scene load_steps=11 format=3 uid="uid://7ivipmwaw24t"]
+[gd_scene load_steps=13 format=3 uid="uid://7ivipmwaw24t"]
 
 [ext_resource type="Texture2D" uid="uid://cgwafs6ukpm7y" path="res://component/entity/ground/ground051.jpg" id="1_3ytcp"]
 [ext_resource type="Script" path="res://run/levels/camera_tracker.gd" id="1_qdm47"]
@@ -7,6 +7,8 @@
 [ext_resource type="PackedScene" uid="uid://bcpri2x5afoyb" path="res://component/entity/box/box.tscn" id="2_e8xto"]
 [ext_resource type="PackedScene" uid="uid://dao17ylietgyl" path="res://component/entity/vehicles/plane/tiny_plane.tscn" id="4_tbpfc"]
 [ext_resource type="PackedScene" uid="uid://cj5dmtqadl5nf" path="res://component/entity/vehicles/jeep/Jeep.tscn" id="6_nnvc5"]
+[ext_resource type="Script" path="res://flow/music_player/music_player.gd" id="8_iulsm"]
+[ext_resource type="AudioStream" uid="uid://c3l60bt307wjh" path="res://flow/music_player/00-Wesh-tone-realaze.mp3" id="8_ndffw"]
 
 [sub_resource type="BoxShape3D" id="BoxShape3D_ctk0t"]
 size = Vector3(500, 10, 500)
@@ -86,7 +88,13 @@ transform = Transform3D(-0.0560131, 0.291646, 0.954885, 0, 0.956386, -0.292105,
 [node name="Jeep2" parent="." instance=ExtResource("6_nnvc5")]
 transform = Transform3D(1, 0, 0, 0, 0.956386, -0.292105, 0, 0.292105, 0.956386, -13.4204, 0.313514, -7.30216)
 
+[node name="MusicPlayer" type="AudioStreamPlayer" parent="."]
+stream = ExtResource("8_ndffw")
+script = ExtResource("8_iulsm")
+
 [connection signal="focus_required" from="MainWalker" to="CameraTracker" method="_on_focus_required"]
+[connection signal="got_in" from="MainWalker" to="MusicPlayer" method="_on_driver_got_in"]
+[connection signal="got_out" from="MainWalker" to="MusicPlayer" method="_on_driver_got_out"]
 [connection signal="dir_changed" from="MainWalker/Control" to="MainWalker" method="_on_dir_changed"]
 [connection signal="get_in_action" from="MainWalker/Control" to="MainWalker" method="_on_get_in_action"]
 [connection signal="main_action" from="MainWalker/Control" to="MainWalker" method="_on_main_action"]