Initial commit
This commit is contained in:
101
Assets/Scripts/PlatformScripts/Platform.cs
Normal file
101
Assets/Scripts/PlatformScripts/Platform.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using NUnit.Framework;
|
||||
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;
|
||||
|
||||
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 pool;
|
||||
[Inject] IPlatformManager platformManager;
|
||||
[Inject] ScoreService scoreService;
|
||||
|
||||
|
||||
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");
|
||||
scoreService.AddScore();
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (player.transform.position.y < transform.position.y-5f)
|
||||
{
|
||||
hasAchievedScore = false;
|
||||
pool.ReturnToPool(this);
|
||||
platformManager.BuildNewPlatform();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public interface IPlatform
|
||||
{
|
||||
List<GameObject> platformPiece { get; }
|
||||
Material safeMaterial { get; }
|
||||
Material deathMaterial { get; }
|
||||
}
|
||||
|
||||
|
||||
public interface IPlatformRule
|
||||
{
|
||||
public void Execute(IPlatform platform);
|
||||
}
|
||||
|
||||
public class FirstPlatform : IPlatformRule
|
||||
{
|
||||
public void Execute(IPlatform platform)
|
||||
{
|
||||
foreach(var piece in platform.platformPiece)
|
||||
{
|
||||
piece.SetActive(true);
|
||||
piece.GetComponent<Renderer>().material = platform.safeMaterial;
|
||||
piece.tag = "Safe";
|
||||
}
|
||||
platform.platformPiece[Random.Range(1,platform.platformPiece.Count)].gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
public class OtherPlatform : 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.tag = "Safe";
|
||||
}
|
||||
|
||||
for (int i=0;i<danger;i++)
|
||||
{
|
||||
GameObject deadPlatform = platform.platformPiece[Random.Range(0, platform.platformPiece.Count)];
|
||||
deadPlatform.GetComponent<Renderer>().material = platform.deathMaterial;
|
||||
deadPlatform.tag = "Death";
|
||||
}
|
||||
platform.platformPiece[Random.Range(0, platform.platformPiece.Count)].gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
2
Assets/Scripts/PlatformScripts/Platform.cs.meta
Normal file
2
Assets/Scripts/PlatformScripts/Platform.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 428d0179d16f0ec4a8923516aeff1a94
|
||||
63
Assets/Scripts/PlatformScripts/PlatformManager.cs
Normal file
63
Assets/Scripts/PlatformScripts/PlatformManager.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
public class PlatformManager : MonoBehaviour,IPlatformManager
|
||||
{
|
||||
[Inject] private IPool pool;
|
||||
[Inject] private IInputReader inputReader;
|
||||
private float yPos = 0f;
|
||||
|
||||
public float rotSpeed = 5f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
ShowPlatforms();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
inputReader.OnDragValueChanged += HandleDrag;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
inputReader.OnDragValueChanged -= HandleDrag;
|
||||
}
|
||||
|
||||
void ShowPlatforms()
|
||||
{
|
||||
foreach (var 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--;
|
||||
}
|
||||
|
||||
private void HandleDrag(Vector2 drag)
|
||||
{
|
||||
float rotAmount = -drag.normalized.x * rotSpeed;
|
||||
|
||||
#if UNITY_EDITOR || UNITY_STANDALONE
|
||||
if (!inputReader.isMouseButtonPressed) return;
|
||||
transform.Rotate(Vector3.up * rotAmount);
|
||||
|
||||
#elif UNITY_IOS || UNITY_ANDROID
|
||||
transform.Rotate(Vector3.up * rotAmount);
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public interface IPlatformManager
|
||||
{
|
||||
void BuildNewPlatform();
|
||||
}
|
||||
2
Assets/Scripts/PlatformScripts/PlatformManager.cs.meta
Normal file
2
Assets/Scripts/PlatformScripts/PlatformManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 393df497de02fce4399040e619446397
|
||||
66
Assets/Scripts/PlatformScripts/PlatformPool.cs
Normal file
66
Assets/Scripts/PlatformScripts/PlatformPool.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
using VContainer.Unity;
|
||||
|
||||
public class PlatformPool : MonoBehaviour,IPool
|
||||
{
|
||||
[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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public interface IPool
|
||||
{
|
||||
IReadOnlyCollection<Platform> All { get; }
|
||||
void ReturnToPool(Platform obj);
|
||||
|
||||
Platform GetFromPool();
|
||||
}
|
||||
2
Assets/Scripts/PlatformScripts/PlatformPool.cs.meta
Normal file
2
Assets/Scripts/PlatformScripts/PlatformPool.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4977d8686efb09b40a678a0fb6037eee
|
||||
Reference in New Issue
Block a user