files arranged using assembly defn
This commit is contained in:
88
Assets/DarkMatter/Code/Presentation/Platforms/Platform.cs
Normal file
88
Assets/DarkMatter/Code/Presentation/Platforms/Platform.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using Darkmatter.Core;
|
||||
using Darkmatter.Domain;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
public class Platform : MonoBehaviour, IPlatform
|
||||
{
|
||||
[SerializeField] private List<GameObject> _platformPiece = new List<GameObject>();
|
||||
[SerializeField] private Material _safeMaterial;
|
||||
[SerializeField] private Material _deathMaterial;
|
||||
[SerializeField] private ParticleSystem _particleSystem;
|
||||
|
||||
[SerializeField]private Color[] safeMaterialColors;
|
||||
[SerializeField] private Color[] deathMaterialColors;
|
||||
|
||||
public List<GameObject> platformPiece => _platformPiece;
|
||||
public Material safeMaterial => _safeMaterial;
|
||||
public Material deathMaterial => _deathMaterial;
|
||||
|
||||
private IPlatformRule _platformRule;
|
||||
private bool hasAchievedScore = false;
|
||||
|
||||
[Inject] Player player;
|
||||
[Inject] IPool<Platform> pool;
|
||||
[Inject] IPlatformManager platformManager;
|
||||
[Inject] IScoreService IscoreService;
|
||||
[Inject] IAudioController IaudioController;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if(_particleSystem == null) _particleSystem = GetComponent<ParticleSystem>();
|
||||
_safeMaterial.color = ReturnMaterialColor(safeMaterialColors);
|
||||
_deathMaterial.color = ReturnMaterialColor(deathMaterialColors);
|
||||
}
|
||||
|
||||
Color ReturnMaterialColor(Color[] color)
|
||||
{
|
||||
return color[Random.Range(0,color.Length)];
|
||||
}
|
||||
public void SetPlatformRule(IPlatformRule platformRule)
|
||||
{
|
||||
_platformRule = platformRule;
|
||||
_platformRule.Execute(this);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if(other.CompareTag("Player") && !hasAchievedScore)
|
||||
{
|
||||
hasAchievedScore = true;
|
||||
Debug.Log("Score Increased");
|
||||
IscoreService.AddScore();
|
||||
IaudioController.PlayScoredSound();
|
||||
_particleSystem.Play();
|
||||
HideThisPlatfromPiece();
|
||||
|
||||
}
|
||||
}
|
||||
void HideThisPlatfromPiece()
|
||||
{
|
||||
foreach(var piece in _platformPiece)
|
||||
{
|
||||
piece.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (player.transform.position.y < transform.position.y-5f)
|
||||
{
|
||||
hasAchievedScore = false;
|
||||
pool.ReturnToPool(this);
|
||||
platformManager.BuildNewPlatform();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 428d0179d16f0ec4a8923516aeff1a94
|
||||
@@ -0,0 +1,64 @@
|
||||
using Darkmatter.Core;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
public class PlatformManager : MonoBehaviour,IPlatformManager
|
||||
{
|
||||
[Inject] private IPool<Platform> pool;
|
||||
[Inject] private IInputReader inputReader;
|
||||
|
||||
private float yPos = 0f;
|
||||
|
||||
[SerializeField] private float inputScale = 360f;
|
||||
float rotAmount = 0;
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
ShowPlatforms();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
inputReader.OnDragValueChanged += HandleDrag;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
inputReader.OnDragValueChanged -= HandleDrag;
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
#if UNITY_EDITOR || UNITY_STANDALONE
|
||||
if (!inputReader.isMouseButtonPressed) return;
|
||||
#endif
|
||||
|
||||
// Apply rotation
|
||||
transform.Rotate(Vector3.up * rotAmount, Space.World);
|
||||
}
|
||||
|
||||
private void HandleDrag(Vector2 drag)
|
||||
{
|
||||
rotAmount = -drag.x/Screen.width * inputScale;
|
||||
}
|
||||
|
||||
private void ShowPlatforms()
|
||||
{
|
||||
foreach (Platform platform in pool.All)
|
||||
{
|
||||
platform.gameObject.SetActive(true);
|
||||
platform.transform.position = new Vector3(0, yPos, 0);
|
||||
yPos--;
|
||||
}
|
||||
}
|
||||
|
||||
public void BuildNewPlatform()
|
||||
{
|
||||
Platform platform = pool.GetFromPool();
|
||||
platform.transform.position = new Vector3(0, yPos, 0);
|
||||
yPos--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 393df497de02fce4399040e619446397
|
||||
@@ -0,0 +1,63 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4977d8686efb09b40a678a0fb6037eee
|
||||
Reference in New Issue
Block a user