generic pool made
This commit is contained in:
@@ -9,7 +9,8 @@ namespace Darkmatter.App
|
||||
{
|
||||
public class GameLifetimeScope : LifetimeScope
|
||||
{
|
||||
[SerializeField] private PlatformPool pool;
|
||||
[SerializeField] private PlatformPool platformPool;
|
||||
[SerializeField] private LeaderBoardDataPool leaderBoardDataPool;
|
||||
[SerializeField] private PlatformManager manager;
|
||||
[SerializeField] private InputReaderSO inputReader;
|
||||
[SerializeField] private GameSessionSO gameSession;
|
||||
@@ -24,7 +25,8 @@ namespace Darkmatter.App
|
||||
[SerializeField] private LeaderBoardView leaderBoardView;
|
||||
protected override void Configure(IContainerBuilder builder)
|
||||
{
|
||||
builder.RegisterComponent(pool).As<IPool<Platform>>();
|
||||
builder.RegisterComponent(platformPool).As<IPool<Platform>>();
|
||||
builder.RegisterComponent(leaderBoardDataPool).As<IPool<LeaderBoardData>>();
|
||||
builder.RegisterComponent(manager).As<IPlatformManager>();
|
||||
builder.RegisterInstance(inputReader).As<IInputReader>();
|
||||
builder.Register<ScoreService>(Lifetime.Singleton).As<IScoreService>();
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Darkmatter.Core
|
||||
{
|
||||
public interface ILeaderBoardData
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 940dab863760a4c4da63c6c64697fd4d
|
||||
10
Assets/DarkMatter/Code/Core/Contracts/IPoolable.cs
Normal file
10
Assets/DarkMatter/Code/Core/Contracts/IPoolable.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Darkmatter.Core
|
||||
{
|
||||
public interface IPoolable
|
||||
{
|
||||
void OnSpawn();
|
||||
void OnDespawn();
|
||||
}
|
||||
}
|
||||
2
Assets/DarkMatter/Code/Core/Contracts/IPoolable.cs.meta
Normal file
2
Assets/DarkMatter/Code/Core/Contracts/IPoolable.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bae72f3bdf0337048826015fe234b8f3
|
||||
@@ -1,13 +0,0 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Darkmatter.Domain
|
||||
{
|
||||
|
||||
public class LeaderboardData : MonoBehaviour
|
||||
{
|
||||
public TextMeshProUGUI posNumber;
|
||||
public TextMeshProUGUI playerName;
|
||||
public TextMeshProUGUI playerScore;
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a6ab63c7177ea94478032cb825071368
|
||||
@@ -3,15 +3,14 @@ using UnityEngine;
|
||||
|
||||
namespace Darkmatter.Domain
|
||||
{
|
||||
public class FirstPlatform : IPlatformRule
|
||||
public class FirstPlatformRule : IPlatformRule
|
||||
{
|
||||
public void Execute(IPlatform platform)
|
||||
{
|
||||
foreach (var piece in platform.platformPiece)
|
||||
{
|
||||
piece.SetActive(true);
|
||||
piece.GetComponent<Renderer>().material = platform.safeMaterial;
|
||||
piece.gameObject.SetActive(true);
|
||||
piece.GetComponent<Renderer>().material = platform.safeMaterial;
|
||||
piece.tag = "Safe";
|
||||
}
|
||||
platform.platformPiece[Random.Range(1, platform.platformPiece.Count)].gameObject.SetActive(false);
|
||||
@@ -3,16 +3,15 @@ using UnityEngine;
|
||||
|
||||
namespace Darkmatter.Domain
|
||||
{
|
||||
public class OtherPlatform : IPlatformRule
|
||||
public class OtherPlatformRule : IPlatformRule
|
||||
{
|
||||
int danger = 2;
|
||||
public void Execute(IPlatform platform)
|
||||
{
|
||||
foreach (var piece in platform.platformPiece)
|
||||
{
|
||||
piece.SetActive(true);
|
||||
piece.GetComponent<Renderer>().material = platform.safeMaterial;
|
||||
piece.gameObject.SetActive(true);
|
||||
piece.GetComponent<Renderer>().material = platform.safeMaterial;
|
||||
piece.tag = "Safe";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using Darkmatter.Core;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
namespace Darkmatter.Presentation
|
||||
{
|
||||
|
||||
public class LeaderBoardData : MonoBehaviour,IPoolable, ILeaderBoardData
|
||||
{
|
||||
public TextMeshProUGUI posNumber;
|
||||
public TextMeshProUGUI playerName;
|
||||
public TextMeshProUGUI playerScore;
|
||||
|
||||
public void OnDespawn()
|
||||
{
|
||||
Debug.Log("LB data Despawnned");
|
||||
}
|
||||
|
||||
public void OnSpawn()
|
||||
{
|
||||
Debug.Log("LB data spawnned");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 19899d49b8ee4db41a4ca8dd590ad4a1
|
||||
@@ -4,13 +4,14 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
public class Platform : MonoBehaviour, IPlatform
|
||||
public class Platform : MonoBehaviour, IPlatform , IPoolable
|
||||
{
|
||||
[SerializeField] private List<GameObject> _platformPiece = new List<GameObject>();
|
||||
[SerializeField] private Material _safeMaterial;
|
||||
[SerializeField] private Material _deathMaterial;
|
||||
[SerializeField] private ParticleSystem _particleSystem;
|
||||
|
||||
//for selecting random color
|
||||
[SerializeField]private Color[] safeMaterialColors;
|
||||
[SerializeField] private Color[] deathMaterialColors;
|
||||
|
||||
@@ -75,7 +76,15 @@ public class Platform : MonoBehaviour, IPlatform
|
||||
}
|
||||
}
|
||||
|
||||
public void OnSpawn()
|
||||
{
|
||||
hasAchievedScore = false;
|
||||
}
|
||||
|
||||
public void OnDespawn()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Darkmatter.Core;
|
||||
using Darkmatter.Domain;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
@@ -11,6 +12,7 @@ public class PlatformManager : MonoBehaviour,IPlatformManager
|
||||
|
||||
[SerializeField] private float inputScale = 360f;
|
||||
float rotAmount = 0;
|
||||
public bool hasFirstPlatformBuilt = false;
|
||||
|
||||
|
||||
private void Start()
|
||||
@@ -47,6 +49,15 @@ public class PlatformManager : MonoBehaviour,IPlatformManager
|
||||
{
|
||||
foreach (Platform platform in pool.All)
|
||||
{
|
||||
if (!hasFirstPlatformBuilt)
|
||||
{
|
||||
platform.SetPlatformRule(new FirstPlatformRule());
|
||||
hasFirstPlatformBuilt=true;
|
||||
}
|
||||
else
|
||||
{
|
||||
platform.SetPlatformRule(new OtherPlatformRule());
|
||||
}
|
||||
platform.gameObject.SetActive(true);
|
||||
platform.transform.position = new Vector3(0, yPos, 0);
|
||||
yPos--;
|
||||
@@ -56,6 +67,8 @@ public class PlatformManager : MonoBehaviour,IPlatformManager
|
||||
public void BuildNewPlatform()
|
||||
{
|
||||
Platform platform = pool.GetFromPool();
|
||||
platform.SetPlatformRule(new OtherPlatformRule());
|
||||
platform.gameObject.SetActive(true);
|
||||
platform.transform.position = new Vector3(0, yPos, 0);
|
||||
yPos--;
|
||||
}
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
using Darkmatter.Core;
|
||||
using Darkmatter.Domain;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
public class PlatformPool : MonoBehaviour, IPool<Platform>
|
||||
{
|
||||
[SerializeField] private Platform platformPrefab;
|
||||
[SerializeField] private Transform platformParent;
|
||||
[SerializeField] private int poolSize = 10;
|
||||
|
||||
private Queue<Platform> _queue = new Queue<Platform>();
|
||||
|
||||
public IReadOnlyCollection<Platform> All => _queue;
|
||||
|
||||
[Inject] IObjectResolver resolver;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
SetupPool();
|
||||
}
|
||||
|
||||
void SetupPool()
|
||||
{
|
||||
Platform instance = null;
|
||||
for(int i = 0; i < poolSize; i++)
|
||||
{
|
||||
instance = Instantiate(platformPrefab,platformParent);
|
||||
resolver.Inject(instance);
|
||||
if (i == 0) instance.SetPlatformRule(new FirstPlatform());
|
||||
else instance.SetPlatformRule(new OtherPlatform());
|
||||
instance.gameObject.SetActive(false);
|
||||
_queue.Enqueue(instance);
|
||||
}
|
||||
}
|
||||
|
||||
public Platform GetFromPool()
|
||||
{
|
||||
if(_queue.Count == 0)
|
||||
{
|
||||
Platform newObj = Instantiate(platformPrefab,platformParent);
|
||||
newObj.SetPlatformRule(new OtherPlatform());
|
||||
_queue.Enqueue(newObj);
|
||||
return newObj;
|
||||
}
|
||||
Platform pooledObj = _queue.Dequeue();
|
||||
pooledObj.SetPlatformRule(new OtherPlatform());
|
||||
pooledObj.gameObject.SetActive(true);
|
||||
return pooledObj;
|
||||
}
|
||||
|
||||
public void ReturnToPool(Platform obj)
|
||||
{
|
||||
obj.gameObject.SetActive(false);
|
||||
_queue.Enqueue(obj);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
8
Assets/DarkMatter/Code/Presentation/Pool.meta
Normal file
8
Assets/DarkMatter/Code/Presentation/Pool.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f2ae9fae8b6113648a92ce517dda7dd9
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,6 @@
|
||||
using Darkmatter.Domain;
|
||||
|
||||
namespace Darkmatter.Presentation
|
||||
{
|
||||
public class LeaderBoardDataPool : ObjectPool<LeaderBoardData> { }
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c15c9d815e963334384a40fbc8e84f23
|
||||
60
Assets/DarkMatter/Code/Presentation/Pool/ObjectPool.cs
Normal file
60
Assets/DarkMatter/Code/Presentation/Pool/ObjectPool.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Darkmatter.Core;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
using VContainer.Unity;
|
||||
|
||||
namespace Darkmatter.Presentation
|
||||
{
|
||||
public class ObjectPool<T> : MonoBehaviour,IPool<T> where T : MonoBehaviour, IPoolable
|
||||
{
|
||||
[SerializeField] T prefab;
|
||||
[SerializeField] private Transform prefabParent;
|
||||
[SerializeField] private int poolSize = 15;
|
||||
|
||||
public Queue<T> pool = new Queue<T>();
|
||||
[Inject] IObjectResolver resolver;
|
||||
|
||||
public IReadOnlyCollection<T> All => pool;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
CreateObjectPool();
|
||||
}
|
||||
|
||||
private void CreateObjectPool()
|
||||
{
|
||||
for(int i=0;i<poolSize; i++)
|
||||
{
|
||||
T obj = Instantiate(prefab, prefabParent);
|
||||
resolver.InjectGameObject(obj.gameObject);
|
||||
obj.OnSpawn();
|
||||
obj.gameObject.SetActive(false);
|
||||
pool.Enqueue(obj);
|
||||
}
|
||||
}
|
||||
|
||||
public T GetFromPool()
|
||||
{
|
||||
if(pool.Count == 0)
|
||||
{
|
||||
T obj = Instantiate(prefab, prefabParent);
|
||||
resolver.InjectGameObject(obj.gameObject);
|
||||
obj.OnSpawn();
|
||||
obj.gameObject.SetActive(true);
|
||||
pool.Enqueue(obj);
|
||||
return obj;
|
||||
}
|
||||
T returningObj = pool.Dequeue();
|
||||
return returningObj;
|
||||
}
|
||||
|
||||
public void ReturnToPool(T obj)
|
||||
{
|
||||
obj.OnDespawn();
|
||||
obj.gameObject.SetActive(false);
|
||||
pool.Enqueue(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6751a0040c85b7441a706516d36a4762
|
||||
9
Assets/DarkMatter/Code/Presentation/Pool/PlatformPool.cs
Normal file
9
Assets/DarkMatter/Code/Presentation/Pool/PlatformPool.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Darkmatter.Presentation;
|
||||
|
||||
|
||||
public class PlatformPool : ObjectPool<Platform> { }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Darkmatter.Presentation
|
||||
public GameObject leaderBoardScreen;
|
||||
public Button ExitButton;
|
||||
public Transform LBDataContainer;
|
||||
public LeaderboardData LBplayerData;
|
||||
public LeaderBoardData LBplayerData;
|
||||
|
||||
public void Show()
|
||||
{
|
||||
@@ -19,7 +19,7 @@ namespace Darkmatter.Presentation
|
||||
|
||||
public void UpdateData(int rank,string name, string score)
|
||||
{
|
||||
LeaderboardData data = Instantiate(LBplayerData, LBDataContainer);
|
||||
LeaderBoardData data = Instantiate(LBplayerData, LBDataContainer);
|
||||
data.posNumber.text = rank.ToString();
|
||||
data.playerName.text = name;
|
||||
data.playerScore.text = score;
|
||||
|
||||
Reference in New Issue
Block a user