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

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 35a6de0bf5636ce4cbfb0040efc4ea14

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);