-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCarSpawner.gd
156 lines (108 loc) · 4.13 KB
/
CarSpawner.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
extends Node3D
# @export_range(0, 4)
# var nrOfCars = 4
class_name CarSpawner
var HudScene := preload("HUD/HUD.tscn")
var FollowingCamera := preload("FollowingCamera.gd")
var CarObjectScene := preload("Car.tscn")
@export
var nrLaps: int = 3
@export
var nrCarsSpawned: int = 0
func _ready():
pass
func spawnForLocalGame(nrOfCars: int):
nrCarsSpawned = nrOfCars
if nrOfCars == 1:
%VerticalSplitBottom.visible = false
else:
%VerticalSplitBottom.visible = true
var mainMenu = get_parent().get_node("%MainMenu")
var playernames = []
for i in range(1,5):
var lineEdit = mainMenu.get_node("%LocalNicknameP" + str(i))
playernames.append(lineEdit.text)
for i in nrOfCars:
var color = Color(randf(), randf(), randf())
if i == 0 && playernames[i] != GlobalProperties.PLAYER_NAME:
color = GlobalProperties.PLAYER_COLOR
var car: CarRigidBody = CarObjectScene.instantiate()
car.frameColor = color
car.playerIndex = i + 1
if playernames[i] != "":
car.playerName = playernames[i]
car.respawnPosition = global_position
car.spawnPosition = car.respawnPosition
var cpSystem = %CheckPointSystem
for cp in cpSystem.get_children():
cp.bodyEnteredCheckpoint.connect(car.onCheckpoint_bodyEntered)
car.nrCheckpoints = cpSystem.get_child_count()
car.nrLaps = nrLaps
%UniversalCanvas/Countdown.countdownFinished.connect(car.onCountdown_finished)
car.finishedRacing.connect(onCarFinishedRacing)
car.respawn()
add_child(car)
var camera = FollowingCamera.new(car)
var viewPortContainer = SubViewportContainer.new()
viewPortContainer.stretch = true
viewPortContainer.size_flags_horizontal = SubViewportContainer.SIZE_EXPAND_FILL
viewPortContainer.size_flags_vertical = SubViewportContainer.SIZE_EXPAND_FILL
var viewPort = SubViewport.new()
viewPort.audio_listener_enable_3d = true
viewPort.render_target_update_mode = SubViewport.UPDATE_ALWAYS
var canvasLayer = CanvasLayer.new()
canvasLayer.follow_viewport_enabled = true
viewPort.add_child(canvasLayer)
var timeTrialManager = TimeTrialManager.new()
car.add_child(timeTrialManager)
%UniversalCanvas/Countdown.countdownFinished.connect(timeTrialManager.startTimeTrial)
car.timeTrialManager = timeTrialManager
# var ingameHUD = HUD.new(car, timeTrialManager, nrOfCars)
var ingameHUD := HudScene.instantiate()
ingameHUD.init(car, timeTrialManager, nrOfCars)
%UniversalCanvas/Countdown.countdownFinished.connect(ingameHUD.onCountdown_finished)
%UniversalCanvas/Countdown.shouldReset.connect(ingameHUD.onReset)
canvasLayer.add_child(ingameHUD)
viewPortContainer.add_child(viewPort)
viewPort.add_child(camera)
if i % 2 == 0:
%VerticalSplitTop.add_child(viewPortContainer)
else:
%VerticalSplitBottom.add_child(viewPortContainer)
func spawnCar(peer_id: int):
var color = GlobalProperties.PLAYER_COLOR
if peer_id == -1:
peer_id = get_tree().get_multiplayer().get_unique_id()
# else:
# color = Color(randf(), randf(), randf())
if has_node(str(peer_id)):
return
var car: CarRigidBody = CarObjectScene.instantiate()
car.name = str(peer_id)
car.frameColor = color
car.finishedRacing.connect(onCarFinishedRacing)
car.playerIndex = 1
var nrPeers = get_tree().get_multiplayer().get_peers().size()
print("Friends of peer ", peer_id, ": ", nrPeers)
# car.global_transform.origin = Vector3(0, 0, 10 * nrPeers)
car.respawnPosition = global_position + Vector3(0, 0, nrPeers * 10)
car.spawnPosition = car.respawnPosition
# print("respawnPosition of ", peer_id, ": ", car.respawnPosition)
car.respawnRotation = car.global_rotation
var cpSystem = %CheckPointSystem
for cp in cpSystem.get_children():
cp.bodyEnteredCheckpoint.connect(car.onCheckpoint_bodyEntered)
car.nrCheckpoints = cpSystem.get_child_count()
car.nrLaps = nrLaps
add_child(car)
# car.respawn()
nrCarsSpawned += 1
var finishedCars: int = 0
func onCarFinishedRacing():
finishedCars += 1
if finishedCars == nrCarsSpawned:
get_parent().get_node("%LeaderboardUI/%List").refreshLists()
get_parent().get_node("%LeaderboardUI/%List").show()
get_parent().get_node("%MusicPlayer").playMenuMusic()
# func _process(delta):
# print("nr cars: ", nrCarsSpawned)