project_teddy/scripts/HUD.gd

121 lines
3.7 KiB
GDScript

extends Control
var roundTimer := 300.0
var minutes
var seconds
var scores: Array[int] = []
var scoresInitalized = false
var runnerSelected = false
func _ready():
minutes = roundTimer / 60
seconds = fmod(roundTimer, 60)
$timer.text = "%02d:%02d" % [minutes, seconds]
func _process(delta):
$startLabel.text = Global.HUDStartLabelText
$Fatigue.value = Global.fatigue
$Health.value = Global.playerHealth
if Global.iAmRunner:
rpc("sync_runner_hp", $Health.value)
if Global.musicUpdated == true:
Global.musicUpdated = false
$musicPanel/playingLabel.text = Global.musicName
$musicPanel/authorLabel.text = Global.musicAuthor
$musicPanel.visible = true
await get_tree().create_timer(3).timeout
$musicPanel.visible = false
if Global.roundInSession == true and roundTimer >= 0:
if scoresInitalized == false:
#Global.HUDStartLabelText = ""
scoresInitalized = true
fill_scores_with_zeros(Global.connectedPlayers.size())
if Global.gamemode == "Runner":
if Global.teddyAuthorityID == 1:
if runnerSelected == false:
runnerSelected = true
select_runner(Global.connectedPlayers, Global.connectedPlayersByPeerID)
if Global.runnerDead == true:
print("RUNNER IS DEAD")
rpc("runner_dead", Global.chosenRunner)
runner_dead(Global.chosenRunner)
roundTimer -= delta
minutes = roundTimer / 60
seconds = fmod(roundTimer, 60)
$timer.text = "%02d:%02d" % [minutes, seconds]
if roundTimer <= 0.0:
Global.HUDStartLabelText = "GAME OVER"
if Global.HUDPlayerDied:
Global.HUDPlayerDied = false
rpc("update_scores", Global.lastPersonToHitMe)
update_scores(Global.lastPersonToHitMe)
if Global.roundInSession == false:
for i in range(Global.connectedPlayers.size()):
if Global.connectedPlayers[i]:
var objectLabel = "player" + str(i) + "Label"
var numlabel = i + 1
get_node("player" + str(numlabel) + "Label").text = Global.connectedPlayers[i] + ": " + "0"
@rpc("any_peer", "reliable")
func update_scores(name):
for i in range(Global.connectedPlayers.size()):
if Global.connectedPlayers[i] == str(name):
var objectLabel = "player" + str(i) + "Label"
var numlabel = i + 1
var currentScore = scores[i] + 1
scores[i] += 1
get_node("player" + str(numlabel) + "Label").text = Global.connectedPlayers[i] + ": " + str(currentScore)
else:
print("Name ", i, " wasn't found in the list!")
func fill_scores_with_zeros(num_of_players):
scores.resize(num_of_players)
for i in num_of_players:
scores[i] = 0
func select_runner(players, playersID):
var numOfPlayers = players.size()
var getIndexSize = numOfPlayers - 1
var playerChosen = RandomNumberGenerator.new().randi_range(0, getIndexSize)
var runner = players[playerChosen]
Global.chosenRunner = runner
rpc("selected_runner", runner)
selected_runner(runner)
var runnerID = playersID[playerChosen]
if runnerID == 1:
runner_stats()
else:
rpc_id(runnerID, "runner_stats")
print("Runner: ", runner, ", ID: ", runnerID)
@rpc("any_peer", "reliable")
func selected_runner(player):
print(Global.connectedPlayersByPeerID)
Global.chosenRunner = player
if Global.playerName != player:
not_runner(player)
func not_runner(player):
$RunnerPanel.visible = true
Global.HUDStartLabelText = str(player + " is the runner!")
@rpc("any_peer", "reliable")
func runner_stats():
await get_tree().create_timer(0.1).timeout
Global.iAmRunner = true
$Health.max_value = 2000
Global.playerHealth = 2000
Global.HUDStartLabelText = "You are the runner!"
$RunnerPanel.visible = false
print(Global.HUDStartLabelText)
@rpc("any_peer", "reliable")
func runner_dead(runner):
Global.roundInSession = false
Global.HUDStartLabelText = "Runner has died! Killers win!"
@rpc("any_peer", "reliable")
func sync_runner_hp(value):
$RunnerPanel/Health.value = value