130 lines
3.7 KiB
C#
130 lines
3.7 KiB
C#
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;
|
|
[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 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();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
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.gameObject.SetActive(true);
|
|
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.gameObject.SetActive(true);
|
|
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);
|
|
}
|
|
}
|
|
|