Initial commit
This commit is contained in:
27
Assets/Scripts/PlatformScript/Platform.cs
Normal file
27
Assets/Scripts/PlatformScript/Platform.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
public class Platform : MonoBehaviour
|
||||
{
|
||||
private Transform playerTransform;
|
||||
[Inject] private PlatformPool pool;
|
||||
[Inject] private PlatformManager manager;
|
||||
[Inject] private IInputReader inputReader;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if((playerTransform.position.y-transform.position.y)<-5)
|
||||
{
|
||||
pool.ReturnToPool(this.gameObject);
|
||||
manager.BuildPlatform();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/PlatformScript/Platform.cs.meta
Normal file
2
Assets/Scripts/PlatformScript/Platform.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 67c2916908f5f5e4b80dc9c59b8b1592
|
||||
38
Assets/Scripts/PlatformScript/PlatformManager.cs
Normal file
38
Assets/Scripts/PlatformScript/PlatformManager.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
|
||||
public class PlatformManager : MonoBehaviour
|
||||
{
|
||||
private GameObject currentPlatfrom;
|
||||
[Inject] private PlatformPool pool;
|
||||
|
||||
private int yPos=0;
|
||||
|
||||
void Start()
|
||||
{
|
||||
ShowInitialPlatforms();
|
||||
}
|
||||
|
||||
public void ShowInitialPlatforms()
|
||||
{
|
||||
|
||||
foreach(var platfrom in pool.platformPool)
|
||||
{
|
||||
platfrom.gameObject.SetActive(true);
|
||||
platfrom.transform.position = new Vector3(0,yPos,0);
|
||||
yPos--;
|
||||
}
|
||||
}
|
||||
public void BuildPlatform()
|
||||
{
|
||||
currentPlatfrom = pool.GetPlatformFromPool();
|
||||
if(currentPlatfrom!=null)
|
||||
{
|
||||
currentPlatfrom.transform.position = new Vector3(0,yPos,0);
|
||||
currentPlatfrom.SetActive(true);
|
||||
yPos--;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
2
Assets/Scripts/PlatformScript/PlatformManager.cs.meta
Normal file
2
Assets/Scripts/PlatformScript/PlatformManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5581fca74af43824995a825437415c12
|
||||
52
Assets/Scripts/PlatformScript/PlatformPool.cs
Normal file
52
Assets/Scripts/PlatformScript/PlatformPool.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
using VContainer.Unity;
|
||||
|
||||
public class PlatformPool : MonoBehaviour
|
||||
{
|
||||
public List<GameObject> platformPool = new List<GameObject>();
|
||||
public int amountToPool=10;
|
||||
public GameObject[] platformPrefab;
|
||||
public GameObject PlatformContainer; //parent
|
||||
[Inject] private IObjectResolver container;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
for(int i=0; i<amountToPool;i++)
|
||||
{
|
||||
GameObject obj = Instantiate(platformPrefab[Random.Range(0,platformPrefab.Length)]);
|
||||
obj.SetActive(false);
|
||||
obj.transform.SetParent(PlatformContainer.transform);
|
||||
container.InjectGameObject(obj);
|
||||
platformPool.Add(obj);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public GameObject GetPlatformFromPool()
|
||||
{
|
||||
for(int i=0;i<platformPool.Count;i++)
|
||||
{
|
||||
if(!platformPool[i].activeInHierarchy)
|
||||
{
|
||||
return platformPool[i];
|
||||
}
|
||||
}
|
||||
|
||||
GameObject newObj = Instantiate(platformPrefab[Random.Range(0,platformPrefab.Length)]);
|
||||
platformPool.Add(newObj);
|
||||
newObj.transform.SetParent(PlatformContainer.transform);
|
||||
container.InjectGameObject(newObj);
|
||||
return newObj;
|
||||
}
|
||||
|
||||
public void ReturnToPool(GameObject obj)
|
||||
{
|
||||
obj.SetActive(false);
|
||||
}
|
||||
|
||||
}
|
||||
2
Assets/Scripts/PlatformScript/PlatformPool.cs.meta
Normal file
2
Assets/Scripts/PlatformScript/PlatformPool.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d775e1dfaaa1884088214bc3e182dfa
|
||||
Reference in New Issue
Block a user