leaderboard feature added
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 35a6de0bf5636ce4cbfb0040efc4ea14
|
||||
@@ -1,5 +1,6 @@
|
||||
using Darkmatter.Core;
|
||||
using Darkmatter.Domain;
|
||||
using Darkmatter.Presentation;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
8
Assets/DarkMatter/Code/Presentation/Player.meta
Normal file
8
Assets/DarkMatter/Code/Presentation/Player.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f6e45fe30a723f84a9cca4bb35524779
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
84
Assets/DarkMatter/Code/Presentation/Player/Player.cs
Normal file
84
Assets/DarkMatter/Code/Presentation/Player/Player.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using Darkmatter.Core;
|
||||
using System.Collections;
|
||||
using Unity.Cinemachine;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
namespace Darkmatter.Presentation
|
||||
{
|
||||
public class Player : MonoBehaviour, IPlayer
|
||||
{
|
||||
[SerializeField] private float jumpForce = 3f;
|
||||
[SerializeField] private Rigidbody rb;
|
||||
[SerializeField] private GameObject splashObject;
|
||||
[SerializeField] private ParticleSystem deadParticle;
|
||||
[SerializeField] private ParticleSystem jumpParticle;
|
||||
[SerializeField] private CinemachineImpulseSource cinemachineImpulseSource;
|
||||
|
||||
[SerializeField] private Material playerMaterial;
|
||||
[SerializeField] Color[] playerMaterialColors;
|
||||
|
||||
public bool isDead { get; private set; }
|
||||
|
||||
[Inject] private IDeathScreenController IdeathScreenController;
|
||||
[Inject] private IInputReader IinputReader;
|
||||
[Inject] private IAudioController IaudioController;
|
||||
[Inject] private IPool<Splash> ISplashPool;
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
isDead = false;
|
||||
playerMaterial.color = playerMaterialColors[Random.Range(0, playerMaterialColors.Length)];
|
||||
|
||||
}
|
||||
|
||||
private void OnCollisionEnter(Collision collision)
|
||||
{
|
||||
ShowAndHideSplash(collision);
|
||||
if (isDead) return;
|
||||
|
||||
if (collision.gameObject.CompareTag("Safe") && rb.linearVelocity.y <= 0.5f)
|
||||
{
|
||||
rb.linearVelocity = Vector3.up * jumpForce;
|
||||
jumpParticle.Play();
|
||||
IaudioController.PlayJumpSound();
|
||||
}
|
||||
else if (collision.gameObject.CompareTag("Death"))
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowAndHideSplash(Collision collision)
|
||||
{
|
||||
float splashYPos = collision.transform.position.y + 0.155f;
|
||||
ContactPoint contact = collision.contacts[0];
|
||||
Vector3 surfacePoint = new Vector3(contact.point.x, splashYPos, contact.point.z);
|
||||
|
||||
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()
|
||||
{
|
||||
isDead = true;
|
||||
IinputReader.LockInput();
|
||||
deadParticle.Play();
|
||||
this.GetComponent<Renderer>().enabled = false;
|
||||
IaudioController.PlayDeathSound();
|
||||
Handheld.Vibrate(); //Vibration
|
||||
cinemachineImpulseSource.GenerateImpulseWithForce(1);
|
||||
StartCoroutine(DieRoutine());
|
||||
}
|
||||
|
||||
IEnumerator DieRoutine()
|
||||
{
|
||||
yield return new WaitForSeconds(1f);
|
||||
IdeathScreenController.ShowDeathScreen();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e7d04c655849ccf438cd955f139e9789
|
||||
@@ -44,6 +44,7 @@ namespace Darkmatter.Presentation
|
||||
return obj;
|
||||
}
|
||||
T returningObj = pool.Dequeue();
|
||||
returningObj.gameObject.SetActive(true);
|
||||
return returningObj;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,10 @@
|
||||
"GUID:b0214a6008ed146ff8f122a6a9c2f6cc",
|
||||
"GUID:6055be8ebefd69e48b49212b09b47b2f",
|
||||
"GUID:219208e14f3a1e1439abafd1ff0ae402",
|
||||
"GUID:2073209246492244c9ad62c89d8d37bb"
|
||||
"GUID:4307f53044263cf4b835bd812fc161a4",
|
||||
"GUID:5540e30183c82e84b954c033c388e06c",
|
||||
"GUID:2073209246492244c9ad62c89d8d37bb",
|
||||
"GUID:fe25561d224ed4743af4c60938a59d0b"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user