leaderboard feature added

This commit is contained in:
Mausham
2025-12-17 22:16:41 +05:45
parent df4017f6be
commit f19bbb9d8c
25 changed files with 106 additions and 170 deletions

View File

@@ -5,5 +5,6 @@ namespace Darkmatter.Core
public interface ILeaderBoardController
{
void ShowLeaderBoard();
void UpdateLeaderBoardScore(int score);
}
}

View File

@@ -1,13 +1,23 @@
using Darkmatter.Core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Darkmatter.Presentation
{
public class LeaderBoardData : MonoBehaviour,ILeaderBoardData
{
public Image BackgroundPanel;
public TextMeshProUGUI playerPos;
public TextMeshProUGUI playerName;
public TextMeshProUGUI playerScore;
public void ResetData()
{
BackgroundPanel.color = Color.white;
playerPos.text = string.Empty;
playerName.text = string.Empty;
playerScore.text = string.Empty;
}
}
}

View File

@@ -0,0 +1,28 @@
using Unity.Services.Core;
using System.Threading.Tasks;
using Unity.Services.Authentication;
using UnityEngine;
namespace Darkmatter.Presentation
{
public class LeaderBoardInitializer : MonoBehaviour
{
private async void Awake()
{
await Init();
}
private async Task Init()
{
await UnityServices.InitializeAsync();
if (!AuthenticationService.Instance.IsSignedIn)
{
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
PlayerPrefs.SetString("PlayerID", AuthenticationService.Instance.PlayerId);
PlayerPrefs.Save();
}
}
}

View File

@@ -1,5 +1,6 @@
using Darkmatter.Core;
using Darkmatter.Domain;
using Darkmatter.Presentation;
using System.Collections.Generic;
using UnityEngine;
using VContainer;

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 898d4b8bb3246724b9838847b6a0ba77
guid: f6e45fe30a723f84a9cca4bb35524779
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@@ -4,14 +4,13 @@ using Unity.Cinemachine;
using UnityEngine;
using VContainer;
namespace Darkmatter.Domain
namespace Darkmatter.Presentation
{
public class Player : MonoBehaviour, IPlayer
{
[SerializeField] private float jumpForce = 3f;
[SerializeField] private Rigidbody rb;
[SerializeField] private GameObject splashObject;
[SerializeField] private Transform splashParent;
[SerializeField] private ParticleSystem deadParticle;
[SerializeField] private ParticleSystem jumpParticle;
[SerializeField] private CinemachineImpulseSource cinemachineImpulseSource;
@@ -24,6 +23,8 @@ namespace Darkmatter.Domain
[Inject] private IDeathScreenController IdeathScreenController;
[Inject] private IInputReader IinputReader;
[Inject] private IAudioController IaudioController;
[Inject] private IPool<Splash> ISplashPool;
private void Start()
{
@@ -54,9 +55,12 @@ namespace Darkmatter.Domain
float splashYPos = collision.transform.position.y + 0.155f;
ContactPoint contact = collision.contacts[0];
Vector3 surfacePoint = new Vector3(contact.point.x, splashYPos, contact.point.z);
GameObject instancedSplash = Instantiate(splashObject, surfacePoint, splashObject.transform.rotation, collision.gameObject.transform);
instancedSplash.transform.localScale = new Vector3(Random.Range(0.05f, 0.09f), Random.Range(0.05f, 0.09f), 1);
Destroy(instancedSplash, 2f);
Splash currentSplash = ISplashPool.GetFromPool();
currentSplash.transform.position = surfacePoint;
currentSplash.transform.SetParent(collision.gameObject.transform);
currentSplash.transform.localScale = new Vector3(Random.Range(0.05f, 0.09f), Random.Range(0.05f, 0.09f), 1);
currentSplash.ReturnToPool();
}
private void Die()
{

View File

@@ -44,6 +44,7 @@ namespace Darkmatter.Presentation
return obj;
}
T returningObj = pool.Dequeue();
returningObj.gameObject.SetActive(true);
return returningObj;
}

View File

@@ -6,7 +6,10 @@
"GUID:b0214a6008ed146ff8f122a6a9c2f6cc",
"GUID:6055be8ebefd69e48b49212b09b47b2f",
"GUID:219208e14f3a1e1439abafd1ff0ae402",
"GUID:2073209246492244c9ad62c89d8d37bb"
"GUID:4307f53044263cf4b835bd812fc161a4",
"GUID:5540e30183c82e84b954c033c388e06c",
"GUID:2073209246492244c9ad62c89d8d37bb",
"GUID:fe25561d224ed4743af4c60938a59d0b"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -1,18 +1,21 @@
using Darkmatter.Core;
using UnityEngine;
using VContainer;
namespace Darkmatter.Presentation
{
public class Splash : MonoBehaviour
{
public void OnDespawn()
[Inject] IPool<Splash> pool;
public void ReturnToPool()
{
Debug.Log("Despawned splash");
Invoke("PoolReturn", 2);
}
public void OnSpawn()
private void PoolReturn()
{
Debug.Log("Spawnned Splash");
pool.ReturnToPool(this);
}
}
}

View File

@@ -26,12 +26,14 @@ namespace Darkmatter.Presentation
private void OnLeaderBoardBtnClicked()
{
IaudioController.PlayBtnPressedSound();
IleaderBoardController.ShowLeaderBoard();
}
public void ShowDeathScreen()
{
IgameScreenController.HideGameScreen();
IleaderBoardController.UpdateLeaderBoardScore(IscoreService.score); //update leaderboard with current score when player dies
deathScreenView.Show(IscoreService.score, IscoreService.highScore);
}

View File

@@ -1,6 +1,8 @@
using Darkmatter.Core;
using System.Collections.Generic;
using Unity.Services.Leaderboards;
using UnityEngine;
using VContainer;
namespace Darkmatter.Presentation
@@ -10,6 +12,10 @@ namespace Darkmatter.Presentation
private const string leaderBoardID = "helix_leaderboard";
private LeaderBoardView leaderBoardView;
private List<LeaderBoardData> activeLBData = new List<LeaderBoardData>();
[Inject] IPool<LeaderBoardData> leaderBoardDataPool;
public LeaderBoardController( LeaderBoardView _leaderBoardView )
{
this.leaderBoardView = _leaderBoardView;
@@ -18,7 +24,13 @@ namespace Darkmatter.Presentation
private void OnExitBtnClicked()
{
Debug.Log("ExitBtnClicked From LeaderBoard");
foreach(var lbData in activeLBData)
{
lbData.ResetData();
leaderBoardDataPool.ReturnToPool(lbData);
}
activeLBData.Clear();
leaderBoardView.Hide();
}
public void ShowLeaderBoard()
@@ -33,11 +45,29 @@ namespace Darkmatter.Presentation
int rank = 1;
foreach(var entry in score.Results)
{
leaderBoardView.UpdateData(rank,entry.PlayerName,entry.Score.ToString());
LeaderBoardData LBData = leaderBoardDataPool.GetFromPool();
string playerID = PlayerPrefs.GetString("PlayerID");
if (entry.PlayerId == playerID)
{
LBData.BackgroundPanel.color = Color.yellow;
LBData.playerName.text = "You";
}
else LBData.playerName.text = entry.PlayerName;
LBData.playerPos.text = rank.ToString();
LBData.playerScore.text = entry.Score.ToString();
activeLBData.Add(LBData); //adds the active data to the list
rank++;
}
}
public async void UpdateLeaderBoardScore(int score)
{
await LeaderboardsService.Instance.AddPlayerScoreAsync(leaderBoardID, score);
}
public void HideLeaderBoard()
{
leaderBoardView.Hide();

View File

@@ -9,7 +9,6 @@ namespace Darkmatter.Presentation
{
public GameObject leaderBoardScreen;
public Button ExitButton;
public Transform LBDataContainer;
public LeaderBoardData LBplayerData;
public void Show()
@@ -17,14 +16,6 @@ namespace Darkmatter.Presentation
leaderBoardScreen.SetActive(true);
}
public void UpdateData(int rank,string name, string score)
{
LeaderBoardData data = Instantiate(LBplayerData, LBDataContainer);
data.playerPos.text = rank.ToString();
data.playerName.text = name;
data.playerScore.text = score;
}
public void Hide()
{
leaderBoardScreen.SetActive(false);

View File

@@ -1,22 +0,0 @@
using System.Threading.Tasks;
using Unity.Services.Authentication;
using Unity.Services.Core;
using UnityEngine;
public class LeaderBoardInitializer : MonoBehaviour
{
private async void Awake()
{
await Init();
}
private async Task Init()
{
await UnityServices.InitializeAsync();
if(!AuthenticationService.Instance.IsSignedIn)
{
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
Debug.Log("Signed in as :" + AuthenticationService.Instance.PlayerId);
}
}

View File

@@ -1,13 +0,0 @@
using Unity.Services.Leaderboards;
using UnityEngine;
public class LeaderBoardManager : MonoBehaviour
{
private const string leaderBoardID = "helix_leaderboard";
public async void SubmitScore(int score)
{
await LeaderboardsService.Instance.AddPlayerScoreAsync(leaderBoardID, score);
Debug.Log("Score submitted: " + score);
}
}

View File

@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 0a0aa7f91e319904e8628a3e89d1fc1b

View File

@@ -1,26 +0,0 @@
using System.Text;
using TMPro;
using Unity.Services.Leaderboards;
using UnityEngine;
public class LeaderBoardUI : MonoBehaviour
{
public TextMeshProUGUI scoretext;
private const string leaderBoardID = "helix_leaderboard";
public async void LoadLeaderBoard()
{
var score = await LeaderboardsService.Instance.GetScoresAsync(leaderBoardID, new GetScoresOptions { Limit = 10 });
StringBuilder sb = new StringBuilder();
int rank = 1;
foreach (var scoreEntry in score.Results)
{
sb.AppendLine($"{rank}.{scoreEntry.PlayerName}- {scoreEntry.Score}");
rank++;
}
scoretext.text = sb.ToString();
}
}

View File

@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: fd36a96f31ec0b7418cf99f05b66a0ec

View File

@@ -129,8 +129,8 @@ Material:
- _XRMotionVectorsPass: 1
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 0.735849, g: 0.09371662, b: 0.48520926, a: 0}
- _Color: {r: 0.735849, g: 0.093716584, b: 0.48520917, a: 0}
- _BaseColor: {r: 0.22148229, g: 0.13866144, b: 0.7169812, a: 0}
- _Color: {r: 0.22148225, g: 0.13866141, b: 0.7169812, a: 0}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []

View File

@@ -117,7 +117,7 @@ Material:
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 0.17342028, g: 0.754717, b: 0.0747597, a: 0}
- _Color: {r: 0.17342025, g: 0.75471693, b: 0.07475967, a: 0}
- _Color: {r: 0.8113207, g: 0.049750805, b: 0.049750805, a: 0}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []

View File

@@ -116,7 +116,7 @@ Material:
- _XRMotionVectorsPass: 1
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 0.122641504, g: 0.12090601, b: 0.12090601, a: 0}
- _BaseColor: {r: 0.7924528, g: 0.7552929, b: 0.063545756, a: 1}
- _Color: {r: 0.12264148, g: 0.12090597, b: 0.12090597, a: 0}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}

View File

@@ -189,6 +189,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: eca7710de6fa5b94fa7c532a021816e7, type: 3}
m_Name:
m_EditorClassIdentifier: PresentationAssembly::Darkmatter.Presentation.LeaderBoardData
BackgroundPanel: {fileID: 2882162719573765618}
playerPos: {fileID: 8966731343238371749}
playerName: {fileID: 7140963729975583955}
playerScore: {fileID: 7199945981426735824}
@@ -478,7 +479,7 @@ GameObject:
- component: {fileID: 4219921636454376189}
- component: {fileID: 2882162719573765618}
m_Layer: 5
m_Name: DataPanel
m_Name: BackgroundPanel
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
@@ -524,7 +525,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier: UnityEngine.UI::UnityEngine.UI.Image
m_Material: {fileID: 0}
m_Color: {r: 0.5, g: 0.5, b: 0.5, a: 0.627451}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1

View File

@@ -11279,7 +11279,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 74, y: 119}
m_AnchoredPosition: {x: 74, y: 119.00012}
m_SizeDelta: {x: 120, y: 120}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &501358099
@@ -11696,7 +11696,6 @@ MonoBehaviour:
jumpForce: 3
rb: {fileID: 581798941}
splashObject: {fileID: 9000420296853654146, guid: 28c1e4391077e5c4e98f5924dc346fd9, type: 3}
splashParent: {fileID: 1850425825}
deadParticle: {fileID: 239977915}
jumpParticle: {fileID: 185655302}
cinemachineImpulseSource: {fileID: 581798944}
@@ -12712,7 +12711,6 @@ MonoBehaviour:
m_EditorClassIdentifier: PresentationAssembly::Darkmatter.Presentation.LeaderBoardView
leaderBoardScreen: {fileID: 155034150}
ExitButton: {fileID: 501358099}
LBDataContainer: {fileID: 1836214634}
LBplayerData: {fileID: 0}
--- !u!1 &926831878
GameObject:
@@ -13538,7 +13536,7 @@ RectTransform:
m_Father: {fileID: 685216012}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchorMax: {x: 1, y: 0.9999998}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 20, y: 20}
m_Pivot: {x: 0.5, y: 0.5}
@@ -14031,77 +14029,6 @@ RectTransform:
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!1 &1398296650
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1398296651}
- component: {fileID: 1398296652}
- component: {fileID: 1398296653}
- component: {fileID: 1398296654}
m_Layer: 0
m_Name: LeaderBoard
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!4 &1398296651
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1398296650}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1398296652
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1398296650}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 35a6de0bf5636ce4cbfb0040efc4ea14, type: 3}
m_Name:
m_EditorClassIdentifier: Assembly-CSharp::LeaderBoardInitializer
--- !u!114 &1398296653
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1398296650}
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0a0aa7f91e319904e8628a3e89d1fc1b, type: 3}
m_Name:
m_EditorClassIdentifier: Assembly-CSharp::LeaderBoardManager
--- !u!114 &1398296654
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1398296650}
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fd36a96f31ec0b7418cf99f05b66a0ec, type: 3}
m_Name:
m_EditorClassIdentifier: Assembly-CSharp::LeaderBoardUI
scoretext: {fileID: 0}
--- !u!1 &1416564334
GameObject:
m_ObjectHideFlags: 0
@@ -14601,7 +14528,7 @@ GameObject:
- component: {fileID: 1741932666}
- component: {fileID: 1741932667}
m_Layer: 0
m_Name: APIInitializers
m_Name: API_Initializers
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
@@ -15938,6 +15865,5 @@ SceneRoots:
- {fileID: 791472102}
- {fileID: 2056622164}
- {fileID: 1783072554}
- {fileID: 1398296651}
- {fileID: 1741932666}
- {fileID: 1111723450}

Binary file not shown.