Files
PuzzleColorBall/Assets/Scripts/GroundController.cs

145 lines
5.1 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GroundController : MonoBehaviour
{
private GameObject[] ground;
2023-01-31 15:32:49 +01:00
public GameObject[] loadFrom;
2023-02-08 11:50:07 +01:00
public GameObject[] sideObjects;
2023-04-29 17:07:23 +02:00
public GameObject[] sideObjectsSpawned;
2023-03-25 16:26:23 +01:00
public Material[] materials;
public int materialIndex = 0;
2023-02-27 09:41:16 +01:00
public float groundMoveSpeed = 10f;
2023-04-29 17:07:23 +02:00
private Vector3 lastSideObjectPos = new Vector3(0,0,0);
2023-02-27 09:41:16 +01:00
2023-02-28 13:31:31 +01:00
//private CollectibleSpawner cs;
private void Awake() {
2023-02-27 12:09:47 +01:00
//cs = FindObjectOfType<CollectibleSpawner>();
2023-03-14 16:45:09 +01:00
2023-02-08 11:50:07 +01:00
//Loading modules
loadFrom = LoadPrefabs("Prefabs/Modulok");
2023-05-02 13:46:38 +02:00
sideObjects = LoadPrefabs("Prefabs/WorldObjects/World1/");
2023-02-08 11:50:07 +01:00
Debug.Log("loadFrom length: " + loadFrom.Length);
Debug.Log("sideObjects Length: " + sideObjects.Length);
//getting all of the ground objects by the tag
ground = GameObject.FindGameObjectsWithTag("Ground");
if(ground.Length == 0){
Debug.Log("Nem talalt ground objectet");
}else{
Debug.Log("ground length: " + ground.Length);
}
}
private void Update() {
2023-01-31 15:32:49 +01:00
ground = GameObject.FindGameObjectsWithTag("Ground"); //torles miatt ujra le kell kerni a ground objecteket
2023-05-02 13:46:38 +02:00
sideObjectsSpawned = GameObject.FindGameObjectsWithTag("SideObject");
OrderArrayByZ(ground); //rendezzuk z szerint a talajt
OrderArrayByZ(sideObjectsSpawned);
if(sideObjectsSpawned.Length > 0){
lastSideObjectPos = sideObjectsSpawned[sideObjectsSpawned.Length-1].transform.position;
}
2023-01-31 15:32:49 +01:00
for (int i = 0; i < ground.Length; i++){ //ground objecteket mozgatja
ground[i].transform.position = ground[i].transform.position + new Vector3(0,0, -groundMoveSpeed * Time.deltaTime);
2023-05-02 13:46:38 +02:00
}
for(int i = 0; i < sideObjectsSpawned.Length; i++){
sideObjectsSpawned[i].transform.position = sideObjectsSpawned[i].transform.position + new Vector3(0,0, -groundMoveSpeed * Time.deltaTime);
}
2023-01-31 15:32:49 +01:00
//uj ground letrehozas
2023-03-14 16:45:09 +01:00
if(ground[ground.Length-1].transform.position.z <= 120){
2023-01-31 15:32:49 +01:00
CreateNewGround();
2023-05-02 13:46:38 +02:00
CreateNewSideObjects(false);
2023-03-25 16:26:23 +01:00
ground = GameObject.FindGameObjectsWithTag("Ground");
for(int i = 0; i < ground.Length; i++){
/*foreach (GameObject child in ground[i].transform){
if (child.name == "Lane1" || child.name == "Lane2" || child.name == "Lane3"){
Debug.Log(child.name + " " + transform.gameObject.name);
}
}*/
Transform[] lanes = new Transform[3];
lanes[0] = ground[i].transform.Find("Lane1");
lanes[1] = ground[i].transform.Find("Lane2");
lanes[2] = ground[i].transform.Find("Lane3");
foreach(var item in lanes){
item.GetComponent<MeshRenderer>().material = materials[materialIndex];
}
}
2023-01-31 15:32:49 +01:00
}
2023-03-14 16:45:09 +01:00
//ellenorzi hogy torolheto e az object || mar nem szukseges mert van egy trigger box
2023-03-09 14:07:44 +01:00
/*foreach (var item in ground){
2023-01-31 15:32:49 +01:00
if(CheckGroundToDestroy(item)){
Destroy(item);
}
2023-03-09 14:07:44 +01:00
}*/
2023-02-27 09:41:16 +01:00
2023-02-27 12:09:47 +01:00
//cs.SpawnCoin();
}
2023-02-08 11:50:07 +01:00
private GameObject[] LoadPrefabs(string path){ //toltese be a palya objecteket a resources mappabol pl: "Prefabs/Modulok"
GameObject[] arr = Resources.LoadAll<GameObject>(path);
return arr;
2023-01-31 15:32:49 +01:00
}
2023-05-02 13:46:38 +02:00
private void CreateNewSideObjects(bool isLeftSide){
2023-04-29 17:07:23 +02:00
int random = UnityEngine.Random.Range(0, sideObjects.Length);
2023-05-02 13:46:38 +02:00
GameObject inst = sideObjects[random];
Vector3 pos = new Vector3(0,0,0);
if(inst.name == "haz1" && !isLeftSide){
pos = new Vector3(4,0,0); //check pos in editor TODO!!
}else if(inst.name == "haz2"){
pos = new Vector3(9,0,0);
}
if(isLeftSide) pos.x = -pos.x;
Instantiate(inst, lastSideObjectPos + pos, ground[0].transform.rotation);
2023-04-29 17:07:23 +02:00
}
2023-03-25 16:26:23 +01:00
public void changeMaterialIndex(){
}
2023-01-31 15:32:49 +01:00
private bool CheckGroundToDestroy(GameObject toCheck){
//z = -80 -nal lehet torolni
2023-01-31 15:32:49 +01:00
if(toCheck.transform.position.z <= -80){
Debug.Log("elerte " + toCheck.name);
return true; //torolheto
}
2023-01-31 15:32:49 +01:00
return false; //nem torolheto
}
2023-05-02 13:46:38 +02:00
private void OrderArrayByZ(GameObject[] array){
2023-01-31 15:32:49 +01:00
GameObject csere;
2023-05-02 13:46:38 +02:00
for (int i = 0; i < array.Length; i++){
2023-01-31 15:32:49 +01:00
for(int j = 0; j < i; j++){
2023-05-02 13:46:38 +02:00
if(array[j].transform.position.z > array[j+1].transform.position.z){
csere = array[j];
array[j] = array[j+1];
array[j+1] = csere;
2023-01-31 15:32:49 +01:00
}
}
}
}
private void CreateNewGround(){
int random = UnityEngine.Random.Range(0, loadFrom.Length);
2023-03-25 16:26:23 +01:00
//egy modullal elobb tolt be, annak az iranyanak megfeleloen, +80 a ket modul hossza
2023-02-28 13:31:31 +01:00
Instantiate(loadFrom[random], new Vector3(0,0, ground[ground.Length-1].transform.position.z + 40), ground[ground.Length-1].transform.rotation);
2023-01-31 15:32:49 +01:00
}
2023-02-16 14:55:48 +01:00
}