Skip to content

Commit 9b60b75

Browse files
feat(menu): enhance navigation and retry functionality
1 parent 863e954 commit 9b60b75

File tree

7 files changed

+182
-25
lines changed

7 files changed

+182
-25
lines changed

Assets/GameOver.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,43 @@
33

44
public class GameOverMenu : MonoBehaviour
55
{
6+
[Tooltip("If set, this scene name will be loaded when Retry is pressed.")]
7+
public string retrySceneName = "";
8+
9+
[Tooltip("Fallback build index to load when Retry is pressed. Use -1 to ignore.")]
10+
public int retrySceneBuildIndex = -1;
11+
12+
[Tooltip("Optional PlayerPrefs key that stores the last played level build index. If set and present, it will be used as a fallback.")]
13+
public string lastLevelPrefsKey = "LastLevelIndex";
14+
615
public void Retry()
716
{
17+
// 1) If scene name provided in inspector, load it
18+
if (!string.IsNullOrEmpty(retrySceneName))
19+
{
20+
SceneManager.LoadScene(retrySceneName);
21+
return;
22+
}
23+
24+
// 2) If a valid build index is provided in inspector, load it
25+
if (retrySceneBuildIndex >= 0)
26+
{
27+
SceneManager.LoadScene(retrySceneBuildIndex);
28+
return;
29+
}
30+
31+
// 3) If a PlayerPrefs key exists with last level index, use that
32+
if (!string.IsNullOrEmpty(lastLevelPrefsKey) && PlayerPrefs.HasKey(lastLevelPrefsKey))
33+
{
34+
int idx = PlayerPrefs.GetInt(lastLevelPrefsKey, -1);
35+
if (idx >= 0)
36+
{
37+
SceneManager.LoadScene(idx);
38+
return;
39+
}
40+
}
41+
42+
// 4) Fallback to the previous hard-coded behaviour (scene index 1)
843
SceneManager.LoadScene(1);
944
}
1045

Assets/MainMenu.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,23 @@
33

44
public class MainMenu : MonoBehaviour
55
{
6+
[Tooltip("Name of the scene to open when Play is clicked. If empty, Build Index below will be used.")]
7+
public string levelSelectSceneName = "Levels";
8+
9+
[Tooltip("Fallback build index to load if the scene name is empty or not set in Inspector.")]
10+
public int levelSelectBuildIndex = 1;
11+
12+
// Called by the Play button on the main menu. By default it will open the Level Select scene.
613
public void PlayGame()
714
{
8-
SceneManager.LoadScene(1);
15+
if (!string.IsNullOrEmpty(levelSelectSceneName))
16+
{
17+
SceneManager.LoadScene(levelSelectSceneName);
18+
}
19+
else
20+
{
21+
SceneManager.LoadScene(levelSelectBuildIndex);
22+
}
923
}
1024

1125
public void QuitGame()

Assets/Scenes/GameOver.unity

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ MonoBehaviour:
452452
m_Script: {fileID: 11500000, guid: 8a00ec074449e254bb0b5528c9d91e83, type: 3}
453453
m_Name:
454454
m_EditorClassIdentifier:
455+
retrySceneName:
456+
retrySceneBuildIndex: -1
457+
lastLevelPrefsKey: LastLevelIndex
455458
--- !u!114 &263816792
456459
MonoBehaviour:
457460
m_ObjectHideFlags: 0
@@ -1049,7 +1052,7 @@ Transform:
10491052
m_GameObject: {fileID: 1394071635}
10501053
serializedVersion: 2
10511054
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
1052-
m_LocalPosition: {x: 0, y: 0, z: 0}
1055+
m_LocalPosition: {x: -85.3, y: 0, z: 0}
10531056
m_LocalScale: {x: 1, y: 1, z: 1}
10541057
m_ConstrainProportionsScale: 0
10551058
m_Children: []
@@ -1248,7 +1251,7 @@ MonoBehaviour:
12481251
m_OnCullStateChanged:
12491252
m_PersistentCalls:
12501253
m_Calls: []
1251-
m_text: 10 POINTS
1254+
m_text: YOUR HITS REACHED LIMIT
12521255
m_isRightToLeft: 0
12531256
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
12541257
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
@@ -1275,8 +1278,8 @@ MonoBehaviour:
12751278
m_faceColor:
12761279
serializedVersion: 2
12771280
rgba: 4294967295
1278-
m_fontSize: 40
1279-
m_fontSizeBase: 40
1281+
m_fontSize: 15.66
1282+
m_fontSizeBase: 15.66
12801283
m_fontWeight: 400
12811284
m_enableAutoSizing: 0
12821285
m_fontSizeMin: 18
@@ -1313,7 +1316,7 @@ MonoBehaviour:
13131316
m_VertexBufferAutoSizeReduction: 0
13141317
m_useMaxVisibleDescender: 1
13151318
m_pageToDisplay: 1
1316-
m_margin: {x: 0, y: 0, z: 0, w: 0}
1319+
m_margin: {x: 0, y: 0, z: 60.280952, w: 0}
13171320
m_isUsingLegacyAnimationComponent: 0
13181321
m_isVolumetricText: 0
13191322
m_hasFontAssetChanged: 0
@@ -1327,6 +1330,37 @@ CanvasRenderer:
13271330
m_PrefabAsset: {fileID: 0}
13281331
m_GameObject: {fileID: 1639911982}
13291332
m_CullTransparentMesh: 1
1333+
--- !u!1 &1787307105
1334+
GameObject:
1335+
m_ObjectHideFlags: 0
1336+
m_CorrespondingSourceObject: {fileID: 0}
1337+
m_PrefabInstance: {fileID: 0}
1338+
m_PrefabAsset: {fileID: 0}
1339+
serializedVersion: 6
1340+
m_Component:
1341+
- component: {fileID: 1787307106}
1342+
m_Layer: 0
1343+
m_Name: LevelRestart
1344+
m_TagString: Untagged
1345+
m_Icon: {fileID: 0}
1346+
m_NavMeshLayer: 0
1347+
m_StaticEditorFlags: 0
1348+
m_IsActive: 1
1349+
--- !u!4 &1787307106
1350+
Transform:
1351+
m_ObjectHideFlags: 0
1352+
m_CorrespondingSourceObject: {fileID: 0}
1353+
m_PrefabInstance: {fileID: 0}
1354+
m_PrefabAsset: {fileID: 0}
1355+
m_GameObject: {fileID: 1787307105}
1356+
serializedVersion: 2
1357+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
1358+
m_LocalPosition: {x: 381.88745, y: 214.25581, z: -126.49521}
1359+
m_LocalScale: {x: 1, y: 1, z: 1}
1360+
m_ConstrainProportionsScale: 0
1361+
m_Children: []
1362+
m_Father: {fileID: 0}
1363+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
13301364
--- !u!1 &2002443023
13311365
GameObject:
13321366
m_ObjectHideFlags: 0
@@ -1468,3 +1502,4 @@ SceneRoots:
14681502
- {fileID: 173229603}
14691503
- {fileID: 17068334}
14701504
- {fileID: 1394071638}
1505+
- {fileID: 1787307106}

Assets/Scenes/MainMenu.unity

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,52 @@ MonoBehaviour:
453453
m_LightCookieSize: {x: 1, y: 1}
454454
m_LightCookieOffset: {x: 0, y: 0}
455455
m_SoftShadowQuality: 0
456+
--- !u!1 &894188157
457+
GameObject:
458+
m_ObjectHideFlags: 0
459+
m_CorrespondingSourceObject: {fileID: 0}
460+
m_PrefabInstance: {fileID: 0}
461+
m_PrefabAsset: {fileID: 0}
462+
serializedVersion: 6
463+
m_Component:
464+
- component: {fileID: 894188159}
465+
- component: {fileID: 894188158}
466+
m_Layer: 0
467+
m_Name: Gamemanager
468+
m_TagString: Untagged
469+
m_Icon: {fileID: 0}
470+
m_NavMeshLayer: 0
471+
m_StaticEditorFlags: 0
472+
m_IsActive: 1
473+
--- !u!114 &894188158
474+
MonoBehaviour:
475+
m_ObjectHideFlags: 0
476+
m_CorrespondingSourceObject: {fileID: 0}
477+
m_PrefabInstance: {fileID: 0}
478+
m_PrefabAsset: {fileID: 0}
479+
m_GameObject: {fileID: 894188157}
480+
m_Enabled: 1
481+
m_EditorHideFlags: 0
482+
m_Script: {fileID: 11500000, guid: 4ad9aa5a516c4de408fea3a53ba07d0f, type: 3}
483+
m_Name:
484+
m_EditorClassIdentifier:
485+
levelSelectSceneName: Levels
486+
levelSelectBuildIndex: 3
487+
--- !u!4 &894188159
488+
Transform:
489+
m_ObjectHideFlags: 0
490+
m_CorrespondingSourceObject: {fileID: 0}
491+
m_PrefabInstance: {fileID: 0}
492+
m_PrefabAsset: {fileID: 0}
493+
m_GameObject: {fileID: 894188157}
494+
serializedVersion: 2
495+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
496+
m_LocalPosition: {x: 381.88745, y: 214.25581, z: -126.49521}
497+
m_LocalScale: {x: 1, y: 1, z: 1}
498+
m_ConstrainProportionsScale: 0
499+
m_Children: []
500+
m_Father: {fileID: 0}
501+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
456502
--- !u!1 &944479914
457503
GameObject:
458504
m_ObjectHideFlags: 0
@@ -603,6 +649,8 @@ MonoBehaviour:
603649
m_Script: {fileID: 11500000, guid: 4ad9aa5a516c4de408fea3a53ba07d0f, type: 3}
604650
m_Name:
605651
m_EditorClassIdentifier:
652+
levelSelectSceneName: Levels
653+
levelSelectBuildIndex: 1
606654
--- !u!1 &1140263439
607655
GameObject:
608656
m_ObjectHideFlags: 0
@@ -822,7 +870,7 @@ MonoBehaviour:
822870
m_OnClick:
823871
m_PersistentCalls:
824872
m_Calls:
825-
- m_Target: {fileID: 944479919}
873+
- m_Target: {fileID: 894188158}
826874
m_TargetAssemblyTypeName: MainMenu, Assembly-CSharp
827875
m_MethodName: PlayGame
828876
m_Mode: 1
@@ -945,7 +993,7 @@ Transform:
945993
m_GameObject: {fileID: 1668237878}
946994
serializedVersion: 2
947995
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
948-
m_LocalPosition: {x: 0, y: 0, z: 0}
996+
m_LocalPosition: {x: -56.6, y: 0, z: 0}
949997
m_LocalScale: {x: 1, y: 1, z: 1}
950998
m_ConstrainProportionsScale: 0
951999
m_Children: []
@@ -1269,3 +1317,4 @@ SceneRoots:
12691317
- {fileID: 944479917}
12701318
- {fileID: 1920255394}
12711319
- {fileID: 1668237881}
1320+
- {fileID: 894188159}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using UnityEngine;
2+
using UnityEngine.SceneManagement;
3+
4+
public static class LastLevelRecorder
5+
{
6+
private const string DefaultPrefsKey = "LastLevelIndex";
7+
8+
public static void SaveCurrentLevel(string prefsKey = DefaultPrefsKey)
9+
{
10+
int idx = SceneManager.GetActiveScene().buildIndex;
11+
PlayerPrefs.SetInt(prefsKey, idx);
12+
PlayerPrefs.Save();
13+
Debug.Log($"Saved last level index {idx} to PlayerPrefs key '{prefsKey}'");
14+
}
15+
public static void SaveAndLoad(string sceneToLoad, string prefsKey = DefaultPrefsKey)
16+
{
17+
SaveCurrentLevel(prefsKey);
18+
SceneManager.LoadScene(sceneToLoad);
19+
}
20+
21+
public static void SaveAndLoad(int sceneBuildIndex, string prefsKey = DefaultPrefsKey)
22+
{
23+
SaveCurrentLevel(prefsKey);
24+
SceneManager.LoadScene(sceneBuildIndex);
25+
}
26+
}

Assets/Scripts/LastLevelRecorder.cs.meta

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

Assets/Scripts/Scorer.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1+
12
using UnityEngine;
23
using UnityEngine.SceneManagement;
34

45
public class Scorer : MonoBehaviour
56
{
7+
[SerializeField] private int defaultLimit = 15; // fallback if level not listed
68
private int hits = 0;
7-
private int limit_hits;
89

910
private void Start()
1011
{
11-
// Set hit limits based on scene/level
12-
int currentScene = SceneManager.GetActiveScene().buildIndex;
13-
switch (currentScene)
12+
// set hit limit based on level
13+
int buildIndex = SceneManager.GetActiveScene().buildIndex;
14+
switch (buildIndex)
1415
{
15-
case 4:
16-
limit_hits = 8; // More hits allowed for level 4
17-
break;
18-
case 5:
19-
limit_hits = 4; // Fewer hits allowed for level 5 (more challenging)
20-
break;
21-
default:
22-
limit_hits = 15; // Default hit limit for other levels
23-
break;
16+
case 1: defaultLimit = 15; break; // Level 1
17+
case 4: defaultLimit = 8; break; // Level 4
18+
case 5: defaultLimit = 4; break; // Level 5
19+
default: defaultLimit = 15; break; // other levels
2420
}
2521
}
2622

@@ -30,11 +26,11 @@ private void OnCollisionEnter(Collision other)
3026

3127
hits++;
3228
Debug.Log($"You bumped into a thing this many times: {hits}");
33-
if (hits == limit_hits)
29+
30+
if (hits >= defaultLimit)
3431
{
35-
Debug.Log("You have reached the maximum number of hits!");
36-
Scene currentScene = SceneManager.GetActiveScene();
37-
SceneManager.LoadScene(currentScene.buildIndex);
32+
Debug.Log("Max hit limit reached! Loading Game Over scene...");
33+
LastLevelRecorder.SaveAndLoad("GameOver"); // This saves current level and loads GameOver
3834
}
3935
}
4036
}

0 commit comments

Comments
 (0)