mirror of
https://github.com/danbulant/programovani-kostky
synced 2026-06-19 22:31:35 +00:00
working leaderboard
This commit is contained in:
parent
24235fb1cf
commit
615a897b48
4 changed files with 398 additions and 3 deletions
60
game/leaderboard.gd
Normal file
60
game/leaderboard.gd
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
extends Control
|
||||
|
||||
@onready var player1 = $VBoxContainer/Player
|
||||
@onready var player2 = $VBoxContainer/Player2
|
||||
@onready var player3 = $VBoxContainer/Player3
|
||||
@onready var player4 = $VBoxContainer/Player4
|
||||
@onready var player5 = $VBoxContainer/Player5
|
||||
@onready var title = $Title
|
||||
@onready var container = $VBoxContainer
|
||||
|
||||
var players = []
|
||||
signal new_game_requested
|
||||
signal back_to_menu
|
||||
|
||||
func _ready() -> void:
|
||||
players = [
|
||||
player1,
|
||||
player2,
|
||||
player3,
|
||||
player4,
|
||||
player5
|
||||
]
|
||||
|
||||
func apply() -> void:
|
||||
players.sort_custom(func(a,b) -> int:
|
||||
return b.score - a.score
|
||||
)
|
||||
var max_score = -1
|
||||
var max_player = 0
|
||||
for i in players.size():
|
||||
if players[i].name == "":
|
||||
players[i].visible = false
|
||||
continue
|
||||
if players[i].score >= max_score:
|
||||
max_score = players[i].score
|
||||
max_player = i
|
||||
var next_player_num = i+1
|
||||
if next_player_num >= players.size(): break
|
||||
var next_player = players[next_player_num]
|
||||
while next_player.name == "":
|
||||
next_player_num += 1
|
||||
if next_player_num >= players.size(): break
|
||||
next_player = players[next_player_num]
|
||||
if not next_player: break
|
||||
players[i].score_change = "+" + str(players[i].score - next_player.score)
|
||||
players[players.size()-1].score_change = "+" + str(players[players.size()-1].score)
|
||||
for player in players:
|
||||
container.remove_child(player)
|
||||
for player in players:
|
||||
container.add_child(player)
|
||||
title.text = "%s won with %d points!" % [players[max_player].name, players[max_player].score]
|
||||
|
||||
func _on_newbtn_pressed() -> void:
|
||||
new_game_requested.emit()
|
||||
|
||||
|
||||
func _on_exit_button_gui_input(event:InputEvent) -> void:
|
||||
if event is InputEventMouse:
|
||||
if event.is_pressed():
|
||||
back_to_menu.emit()
|
||||
315
game/leaderboard.tscn
Normal file
315
game/leaderboard.tscn
Normal file
|
|
@ -0,0 +1,315 @@
|
|||
[gd_scene load_steps=9 format=3 uid="uid://7t3pm5ta5qpf"]
|
||||
|
||||
[ext_resource type="Script" path="res://game/leaderboard.gd" id="1_2hdko"]
|
||||
[ext_resource type="Script" path="res://game/Player.gd" id="1_k6g0n"]
|
||||
[ext_resource type="Texture2D" uid="uid://c25g6oj4fnw0h" path="res://icons/singleplayer.png" id="2_cmyqt"]
|
||||
[ext_resource type="Texture2D" uid="uid://olq6rc614eq1" path="res://icons/medal2.png" id="3_uconj"]
|
||||
[ext_resource type="Script" path="res://game/dice-ui.gd" id="4_g87m1"]
|
||||
[ext_resource type="Texture2D" uid="uid://dgwgb7g0gca8m" path="res://icons/exit.png" id="5_6ttie"]
|
||||
|
||||
[sub_resource type="InputEventKey" id="InputEventKey_6n7ot"]
|
||||
device = -1
|
||||
keycode = 32
|
||||
unicode = 32
|
||||
|
||||
[sub_resource type="Shortcut" id="Shortcut_fydai"]
|
||||
events = [SubResource("InputEventKey_6n7ot")]
|
||||
|
||||
[node name="Leaderboard" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_2hdko")
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -92.5
|
||||
offset_top = -250.0
|
||||
offset_right = 92.5
|
||||
offset_bottom = 250.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
theme_override_constants/separation = 0
|
||||
|
||||
[node name="Player" type="HBoxContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("1_k6g0n")
|
||||
|
||||
[node name="TextureRect" type="TextureRect" parent="VBoxContainer/Player"]
|
||||
self_modulate = Color(0.329412, 0.690196, 1, 1)
|
||||
layout_mode = 2
|
||||
texture = ExtResource("2_cmyqt")
|
||||
|
||||
[node name="Medal" type="TextureRect" parent="VBoxContainer/Player/TextureRect"]
|
||||
layout_mode = 0
|
||||
offset_left = 36.0
|
||||
offset_top = 41.0
|
||||
offset_right = 101.0
|
||||
offset_bottom = 106.0
|
||||
texture = ExtResource("3_uconj")
|
||||
expand_mode = 1
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/Player"]
|
||||
layout_mode = 2
|
||||
alignment = 1
|
||||
|
||||
[node name="Label" type="Label" parent="VBoxContainer/Player/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Player 1"
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/Player/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
alignment = 2
|
||||
|
||||
[node name="dice1" type="Control" parent="VBoxContainer/Player/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="dice2" type="Control" parent="VBoxContainer/Player/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="dice3" type="Control" parent="VBoxContainer/Player/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="ScoreChange" type="Label" parent="VBoxContainer/Player/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "+10"
|
||||
|
||||
[node name="Player2" type="HBoxContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("1_k6g0n")
|
||||
|
||||
[node name="TextureRect" type="TextureRect" parent="VBoxContainer/Player2"]
|
||||
self_modulate = Color(0.780392, 0.529412, 0.992157, 1)
|
||||
layout_mode = 2
|
||||
texture = ExtResource("2_cmyqt")
|
||||
|
||||
[node name="Medal" type="TextureRect" parent="VBoxContainer/Player2/TextureRect"]
|
||||
layout_mode = 0
|
||||
offset_left = 36.0
|
||||
offset_top = 41.0
|
||||
offset_right = 101.0
|
||||
offset_bottom = 106.0
|
||||
texture = ExtResource("3_uconj")
|
||||
expand_mode = 1
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/Player2"]
|
||||
layout_mode = 2
|
||||
alignment = 1
|
||||
|
||||
[node name="Label" type="Label" parent="VBoxContainer/Player2/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Player 1"
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/Player2/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
alignment = 2
|
||||
|
||||
[node name="dice1" type="Control" parent="VBoxContainer/Player2/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="dice2" type="Control" parent="VBoxContainer/Player2/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="dice3" type="Control" parent="VBoxContainer/Player2/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="ScoreChange" type="Label" parent="VBoxContainer/Player2/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "+10"
|
||||
|
||||
[node name="Player3" type="HBoxContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("1_k6g0n")
|
||||
|
||||
[node name="TextureRect" type="TextureRect" parent="VBoxContainer/Player3"]
|
||||
self_modulate = Color(1, 0.47451, 0.501961, 1)
|
||||
layout_mode = 2
|
||||
texture = ExtResource("2_cmyqt")
|
||||
|
||||
[node name="Medal" type="TextureRect" parent="VBoxContainer/Player3/TextureRect"]
|
||||
layout_mode = 0
|
||||
offset_left = 36.0
|
||||
offset_top = 41.0
|
||||
offset_right = 101.0
|
||||
offset_bottom = 106.0
|
||||
texture = ExtResource("3_uconj")
|
||||
expand_mode = 1
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/Player3"]
|
||||
layout_mode = 2
|
||||
alignment = 1
|
||||
|
||||
[node name="Label" type="Label" parent="VBoxContainer/Player3/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Player 1"
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/Player3/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
alignment = 2
|
||||
|
||||
[node name="dice1" type="Control" parent="VBoxContainer/Player3/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="dice2" type="Control" parent="VBoxContainer/Player3/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="dice3" type="Control" parent="VBoxContainer/Player3/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="ScoreChange" type="Label" parent="VBoxContainer/Player3/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "+10"
|
||||
|
||||
[node name="Player4" type="HBoxContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("1_k6g0n")
|
||||
|
||||
[node name="TextureRect" type="TextureRect" parent="VBoxContainer/Player4"]
|
||||
self_modulate = Color(0.996078, 0.509804, 0, 1)
|
||||
layout_mode = 2
|
||||
texture = ExtResource("2_cmyqt")
|
||||
|
||||
[node name="Medal" type="TextureRect" parent="VBoxContainer/Player4/TextureRect"]
|
||||
layout_mode = 0
|
||||
offset_left = 36.0
|
||||
offset_top = 41.0
|
||||
offset_right = 101.0
|
||||
offset_bottom = 106.0
|
||||
texture = ExtResource("3_uconj")
|
||||
expand_mode = 1
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/Player4"]
|
||||
layout_mode = 2
|
||||
alignment = 1
|
||||
|
||||
[node name="Label" type="Label" parent="VBoxContainer/Player4/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Player 1"
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/Player4/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
alignment = 2
|
||||
|
||||
[node name="dice1" type="Control" parent="VBoxContainer/Player4/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="dice2" type="Control" parent="VBoxContainer/Player4/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="dice3" type="Control" parent="VBoxContainer/Player4/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="ScoreChange" type="Label" parent="VBoxContainer/Player4/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "+10"
|
||||
|
||||
[node name="Player5" type="HBoxContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("1_k6g0n")
|
||||
|
||||
[node name="TextureRect" type="TextureRect" parent="VBoxContainer/Player5"]
|
||||
self_modulate = Color(0, 0.780392, 0.556863, 1)
|
||||
layout_mode = 2
|
||||
texture = ExtResource("2_cmyqt")
|
||||
|
||||
[node name="Medal" type="TextureRect" parent="VBoxContainer/Player5/TextureRect"]
|
||||
layout_mode = 0
|
||||
offset_left = 36.0
|
||||
offset_top = 41.0
|
||||
offset_right = 101.0
|
||||
offset_bottom = 106.0
|
||||
texture = ExtResource("3_uconj")
|
||||
expand_mode = 1
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/Player5"]
|
||||
layout_mode = 2
|
||||
alignment = 1
|
||||
|
||||
[node name="Label" type="Label" parent="VBoxContainer/Player5/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Player 1"
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/Player5/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
alignment = 2
|
||||
|
||||
[node name="dice1" type="Control" parent="VBoxContainer/Player5/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="dice2" type="Control" parent="VBoxContainer/Player5/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="dice3" type="Control" parent="VBoxContainer/Player5/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
script = ExtResource("4_g87m1")
|
||||
|
||||
[node name="ScoreChange" type="Label" parent="VBoxContainer/Player5/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "+10"
|
||||
|
||||
[node name="ExitButton" type="TextureRect" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 1
|
||||
anchor_left = 1.0
|
||||
anchor_right = 1.0
|
||||
offset_left = -79.0
|
||||
offset_top = 18.0
|
||||
offset_right = -16.0
|
||||
offset_bottom = 81.0
|
||||
grow_horizontal = 0
|
||||
texture = ExtResource("5_6ttie")
|
||||
expand_mode = 1
|
||||
|
||||
[node name="newbtn" type="Button" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 3
|
||||
anchor_left = 1.0
|
||||
anchor_top = 1.0
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = -98.0
|
||||
offset_top = -59.0
|
||||
offset_right = -17.0
|
||||
offset_bottom = -14.0
|
||||
grow_horizontal = 0
|
||||
grow_vertical = 0
|
||||
tooltip_text = "Click or press space to roll dice"
|
||||
shortcut = SubResource("Shortcut_fydai")
|
||||
text = "New game"
|
||||
|
||||
[node name="Title" type="Label" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 5
|
||||
anchor_left = 0.5
|
||||
anchor_right = 0.5
|
||||
offset_left = -142.0
|
||||
offset_top = 22.0
|
||||
offset_right = 143.0
|
||||
offset_bottom = 39.0
|
||||
grow_horizontal = 2
|
||||
text = "Player 1 won with 105 points!"
|
||||
|
||||
[connection signal="gui_input" from="ExitButton" to="." method="_on_exit_button_gui_input"]
|
||||
[connection signal="pressed" from="newbtn" to="." method="_on_newbtn_pressed"]
|
||||
20
game/main.gd
20
game/main.gd
|
|
@ -11,6 +11,7 @@ extends Node3D
|
|||
@onready var menu_scene = $MenuBg
|
||||
@onready var player_select = $PlayerSelect
|
||||
@onready var base = $CSGMesh3D
|
||||
@onready var leaderboard = $Leaderboard
|
||||
|
||||
const physics_timeout := 2.5
|
||||
|
||||
|
|
@ -24,7 +25,8 @@ enum GameState {
|
|||
enum MasterState {
|
||||
LOGO,
|
||||
PLAYER_SELECT,
|
||||
GAME
|
||||
GAME,
|
||||
LEADERBOARD
|
||||
}
|
||||
|
||||
var state: GameState = GameState.START
|
||||
|
|
@ -32,7 +34,7 @@ var master_state: MasterState = MasterState.LOGO
|
|||
var current_player = 0
|
||||
var camera_speed := 1.3
|
||||
var physics_timer := Timer.new()
|
||||
var target_score = 100
|
||||
var target_score = 20
|
||||
|
||||
var camera_tween: Tween
|
||||
|
||||
|
|
@ -48,6 +50,8 @@ func _ready() -> void:
|
|||
menu_scene.connect("start_game", switch_to_player_select)
|
||||
player_select.connect("start_game", switch_to_game)
|
||||
player_select.connect("back_to_menu", switch_to_menu)
|
||||
leaderboard.connect("back_to_menu", switch_to_menu)
|
||||
leaderboard.connect("new_game_requested", switch_to_player_select)
|
||||
ui.connect("back_to_menu", switch_to_menu)
|
||||
switch_to_menu()
|
||||
ui.update_pointer()
|
||||
|
|
@ -59,9 +63,19 @@ func master_state_sync() -> void:
|
|||
menu_scene.ui_visible = MasterState.LOGO == master_state
|
||||
player_select.visible = MasterState.PLAYER_SELECT == master_state
|
||||
base.visible = MasterState.GAME == master_state
|
||||
leaderboard.visible = MasterState.LEADERBOARD == master_state
|
||||
if master_state != MasterState.GAME:
|
||||
physics_timer_timeout()
|
||||
rst_camera_tween()
|
||||
camera.global_transform = camera_starting.global_transform
|
||||
ui.update_medal()
|
||||
leaderboard_sync()
|
||||
|
||||
func leaderboard_sync() -> void:
|
||||
for i in ui.players.size():
|
||||
leaderboard.players[i].player_name = ui.players[i].player_name
|
||||
leaderboard.players[i].score = ui.players[i].score
|
||||
leaderboard.apply()
|
||||
|
||||
func switch_to_player_select() -> void:
|
||||
master_state = MasterState.PLAYER_SELECT
|
||||
|
|
@ -110,6 +124,8 @@ func next_player() -> void:
|
|||
func check_score() -> void:
|
||||
for i in ui.players:
|
||||
if i.score >= target_score:
|
||||
master_state = MasterState.LEADERBOARD
|
||||
master_state_sync()
|
||||
print("Player", i.player_name, "won with", i.score, "points!")
|
||||
break
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
[gd_scene load_steps=16 format=3 uid="uid://bxphc5t7er28i"]
|
||||
[gd_scene load_steps=17 format=3 uid="uid://bxphc5t7er28i"]
|
||||
|
||||
[ext_resource type="Script" path="res://game/main.gd" id="1_h1pkq"]
|
||||
[ext_resource type="ArrayMesh" uid="uid://6ygsl6phockr" path="res://game/dice.res" id="1_qesf4"]
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
[ext_resource type="PackedScene" uid="uid://c8jkqtbqtg7ub" path="res://game/ui.tscn" id="4_80via"]
|
||||
[ext_resource type="PackedScene" uid="uid://cmt0o37jd1gt6" path="res://menu/menu_bg.tscn" id="5_r5r22"]
|
||||
[ext_resource type="PackedScene" uid="uid://k2duc4ddir2m" path="res://player_select/player_select.tscn" id="6_xkr2b"]
|
||||
[ext_resource type="PackedScene" uid="uid://7t3pm5ta5qpf" path="res://game/leaderboard.tscn" id="7_ry0u7"]
|
||||
|
||||
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_kmncl"]
|
||||
bounce = 0.06
|
||||
|
|
@ -146,6 +147,9 @@ visible = false
|
|||
[node name="PlayerSelect" parent="." instance=ExtResource("6_xkr2b")]
|
||||
visible = false
|
||||
|
||||
[node name="Leaderboard" parent="." instance=ExtResource("7_ry0u7")]
|
||||
visible = false
|
||||
|
||||
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
|
||||
environment = SubResource("Environment_64y34")
|
||||
camera_attributes = SubResource("CameraAttributesPractical_k5qa7")
|
||||
|
|
|
|||
Loading…
Reference in a new issue