Skip to content

Commit fcd13c9

Browse files
committed
added main menu
1 parent 618c21c commit fcd13c9

File tree

10 files changed

+730
-4
lines changed

10 files changed

+730
-4
lines changed

Assets/Scenes/MainMenu.unity

Lines changed: 424 additions & 0 deletions
Large diffs are not rendered by default.

Assets/Scenes/MainMenu.unity.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/GameData/GameData.cs

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
1+
using System;
2+
using System.IO;
3+
using Unity.VisualScripting;
14
using UnityEngine;
25

36
[DefaultExecutionOrder(1)]
47
public class GameData : MonoBehaviour {
58

6-
public static GameData Instance { get; private set; }
9+
private const string saveFileName = "andorinha-data.json";
10+
11+
private string saveFilePath;
12+
713

14+
public static GameData Instance { get; private set; }
815

9-
public BestScore bestScore { get; private set; }
1016

17+
[DoNotSerialize]
1118
public string currentUsername;
1219

1320

21+
public Action<BestScore> onBestScoreChanged;
22+
public BestScore _bestScore;
23+
public BestScore BestScore {
24+
get => _bestScore;
25+
private set {
26+
if (_bestScore != value) {
27+
_bestScore = value;
28+
29+
onBestScoreChanged?.Invoke(
30+
_bestScore
31+
);
32+
}
33+
}
34+
}
35+
36+
1437
private void Awake() {
1538
if (Instance == null) {
39+
saveFilePath = $"{Application.persistentDataPath}/{saveFileName}";
40+
41+
LoadData();
42+
1643
Instance = this;
1744

1845
DontDestroyOnLoad(
@@ -27,12 +54,39 @@ private void Awake() {
2754
}
2855

2956

57+
private void LoadData() {
58+
if (File.Exists(saveFilePath)) {
59+
BestScore = JsonUtility.FromJson<BestScore>(
60+
File.ReadAllText(
61+
saveFilePath
62+
)
63+
);
64+
65+
} else {
66+
BestScore = new() {
67+
username = "[no one]",
68+
score = 0,
69+
};
70+
}
71+
}
72+
3073
public void RegisterIfBestScore(int score) {
31-
if (score > bestScore.score) {
32-
bestScore = new() {
74+
if (score > BestScore.score) {
75+
BestScore = new() {
3376
username = currentUsername,
3477
score = score,
3578
};
79+
80+
SaveData();
3681
}
3782
}
83+
84+
private void SaveData() {
85+
File.WriteAllText(
86+
saveFilePath,
87+
JsonUtility.ToJson(
88+
BestScore
89+
)
90+
);
91+
}
3892
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using UnityEngine;
2+
using UnityEngine.UIElements;
3+
using UnityEngine.SceneManagement;
4+
5+
#if UNITY_EDITOR
6+
using UnityEditor;
7+
#endif
8+
9+
[DefaultExecutionOrder(1000)]
10+
public class MainMenuController : MonoBehaviour {
11+
12+
private Label bestScoreText;
13+
private TextField usernameText;
14+
private Button startGameButton;
15+
private Button exitButton;
16+
17+
private struct ElementId {
18+
public const string BestScoreText = "best-score-text";
19+
public const string UsernameText = "username-text";
20+
public const string StartGameButton = "start-game-button";
21+
public const string ExitButton = "exit-button";
22+
}
23+
24+
25+
private void OnEnable() {
26+
var ui = GetComponent<UIDocument>().rootVisualElement;
27+
28+
bestScoreText = ui.Q<Label>(
29+
ElementId.BestScoreText
30+
);
31+
32+
if (GameData.Instance.BestScore != null) {
33+
UpdateBestScoreText(
34+
GameData.Instance.BestScore
35+
);
36+
}
37+
38+
GameData.Instance.onBestScoreChanged += UpdateBestScoreText;
39+
40+
usernameText = ui.Q<TextField>(
41+
ElementId.UsernameText
42+
);
43+
44+
startGameButton = ui.Q<Button>(
45+
ElementId.StartGameButton
46+
);
47+
startGameButton.clicked += StartGame;
48+
49+
exitButton = ui.Q<Button>(
50+
ElementId.ExitButton
51+
);
52+
exitButton.clicked += ExitGame;
53+
}
54+
55+
private void OnDisable() {
56+
GameData.Instance.onBestScoreChanged -= UpdateBestScoreText;
57+
58+
startGameButton.clicked -= StartGame;
59+
exitButton.clicked -= ExitGame;
60+
}
61+
62+
63+
private void UpdateBestScoreText(BestScore newValue) {
64+
bestScoreText.text = $"{newValue.username} - {newValue.score}";
65+
}
66+
67+
private void StartGame() {
68+
GameData.Instance.currentUsername = usernameText.text;
69+
70+
SceneManager.LoadScene(
71+
(int) SceneId.MainGame
72+
);
73+
}
74+
75+
private void ExitGame() {
76+
#if UNITY_EDITOR
77+
EditorApplication.ExitPlaymode();
78+
#else
79+
Application.Quit();
80+
#endif
81+
}
82+
}

Assets/Scripts/UI/MainMenuController.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/UI/MainMenu.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
VisualElement {
2+
}
3+
4+
.container {
5+
flex-grow: 1;
6+
background-color: rgb(59, 59, 59);
7+
justify-content: center;
8+
align-items: center;
9+
}
10+
11+
.title {
12+
-unity-font-style: bold;
13+
font-size: 40px;
14+
color: rgb(93, 220, 66);
15+
margin-bottom: 0;
16+
padding-bottom: 0;
17+
}
18+
19+
.sub-title {
20+
-unity-font-style: bold;
21+
font-size: 16px;
22+
color: rgb(84, 112, 224);
23+
margin-top: 0;
24+
}
25+
26+
.action-container {
27+
padding-top: 15px;
28+
padding-left: 0;
29+
}
30+
31+
#best-score-text {
32+
-unity-text-align: upper-center;
33+
-unity-font-style: bold-and-italic;
34+
font-size: 15px;
35+
padding-bottom: 10px;
36+
color: rgb(201, 71, 71);
37+
}
38+
39+
#username-text {
40+
min-width: 180px;
41+
color: rgb(95, 95, 95);
42+
}
43+
44+
#unity-text-input {
45+
margin-left: 0;
46+
border-top-width: 2px;
47+
border-right-width: 2px;
48+
border-bottom-width: 2px;
49+
border-left-width: 2px;
50+
border-left-color: rgb(95, 95, 95);
51+
border-right-color: rgb(95, 95, 95);
52+
border-top-color: rgb(95, 95, 95);
53+
border-bottom-color: rgb(95, 95, 95);
54+
border-top-left-radius: 5px;
55+
border-top-right-radius: 5px;
56+
border-bottom-right-radius: 5px;
57+
border-bottom-left-radius: 5px;
58+
transition-duration: 0.3s;
59+
transition-timing-function: ease-in-out;
60+
}
61+
62+
#unity-text-input:hover {
63+
border-left-color: rgb(136, 73, 211);
64+
border-right-color: rgb(136, 73, 211);
65+
border-top-color: rgb(136, 73, 211);
66+
border-bottom-color: rgb(136, 73, 211);
67+
}
68+
69+
.action-button {
70+
-unity-font-style: bold;
71+
border-top-width: 2px;
72+
border-right-width: 2px;
73+
border-bottom-width: 2px;
74+
border-left-width: 2px;
75+
border-top-left-radius: 20px;
76+
border-top-right-radius: 20px;
77+
border-bottom-right-radius: 20px;
78+
border-bottom-left-radius: 20px;
79+
transition-duration: 0.2s;
80+
transition-timing-function: ease-in-out;
81+
}
82+
83+
.action-button:hover {
84+
border-top-left-radius: 5px;
85+
border-top-right-radius: 5px;
86+
border-bottom-right-radius: 5px;
87+
border-bottom-left-radius: 5px;
88+
}
89+
90+
#start-game-button {
91+
color: rgb(44, 149, 131);
92+
border-left-color: rgb(44, 150, 132);
93+
border-right-color: rgb(44, 150, 132);
94+
border-top-color: rgb(44, 150, 132);
95+
border-bottom-color: rgb(44, 150, 132);
96+
background-color: rgb(174, 243, 232);
97+
}
98+
99+
#exit-button {
100+
color: rgb(157, 71, 20);
101+
border-left-color: rgb(157, 71, 20);
102+
border-right-color: rgb(157, 71, 20);
103+
border-top-color: rgb(157, 71, 20);
104+
border-bottom-color: rgb(157, 71, 20);
105+
background-color: rgb(228, 181, 153);
106+
}

Assets/UI/MainMenu/MainMenuStyle.uss.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/UI/MainMenu/MainMenuUI.uxml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
2+
<Style src="project://database/Assets/UI/MainMenu/MainMenuStyle.uss?fileID=7433441132597879392&amp;guid=ba4c3ed973fae154cbeed199fc8b8d75&amp;type=3#MainMenuStyle" />
3+
<ui:VisualElement class="container">
4+
<ui:Label tabindex="-1" text="Andorinha Faminta!" parse-escape-sequences="true" display-tooltip-when-elided="true" class="title" />
5+
<ui:Label tabindex="-1" text="The Hungry Swallow" parse-escape-sequences="true" display-tooltip-when-elided="true" class="sub-title" />
6+
<ui:GroupBox class="action-container">
7+
<ui:Label tabindex="-1" text="Best Score" parse-escape-sequences="true" display-tooltip-when-elided="true" name="best-score-text" />
8+
<ui:TextField picking-mode="Ignore" value="Username" name="username-text" />
9+
<ui:Button text="Start Game" parse-escape-sequences="true" display-tooltip-when-elided="true" name="start-game-button" class="action-button" />
10+
<ui:Button text="Exit" parse-escape-sequences="true" display-tooltip-when-elided="true" name="exit-button" class="action-button" />
11+
</ui:GroupBox>
12+
</ui:VisualElement>
13+
</ui:UXML>

Assets/UI/MainMenu/MainMenuUI.uxml.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)