Files
PuzzleColorBall/Assets/Scripts/GroundController.cs

249 lines
7.4 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-05-13 15:30:19 +02:00
private Vector3 lastSideObjectPos = new Vector3(0, 0, 0);
2023-05-16 14:27:55 +02:00
[SerializeField] GameObject portalModul;
[SerializeField] private int groundCounter = 0;
[SerializeField] private int portalSpawnNumber = 15; //ennyi modulonkent spawnoljon portalt
2023-02-27 09:41:16 +01:00
2023-02-28 13:31:31 +01:00
//private CollectibleSpawner cs;
2023-05-29 15:36:27 +02:00
private Material newMaterial;
2023-05-13 15:30:19 +02:00
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");
2023-05-13 15:30:19 +02:00
if (ground.Length == 0)
{
Debug.Log("Nem talalt ground objectet");
2023-05-13 15:30:19 +02:00
}
else
{
Debug.Log("ground length: " + ground.Length);
}
2023-05-29 15:36:27 +02:00
newMaterial = materials[0];
}
2023-05-13 15:30:19 +02:00
private void Move(GameObject move)
{
move.transform.position = move.transform.position + new Vector3(0, 0, -groundMoveSpeed * Time.deltaTime);
}
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
2023-05-13 15:30:19 +02:00
OrderArrayByZ(sideObjectsSpawned);
2023-05-02 13:46:38 +02:00
2023-05-13 15:30:19 +02:00
for (int i = 0; i < ground.Length; i++)
2023-05-16 14:27:55 +02:00
{ //ground objecteket mozgatja
2023-05-13 15:30:19 +02:00
Move(ground[i]);
2023-05-02 13:46:38 +02:00
}
2023-01-31 15:32:49 +01:00
2023-05-13 15:30:19 +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);
Move(sideObjectsSpawned[i]);
}
2023-01-31 15:32:49 +01:00
2023-05-16 14:27:55 +02:00
if (sideObjectsSpawned[sideObjectsSpawned.Length - 1].transform.position.z < 145)
{
CreateNewSideObjects(false);
CreateNewSideObjects(true);
}
2023-01-31 15:32:49 +01:00
//uj ground letrehozas
2023-05-13 15:30:19 +02:00
if (ground[ground.Length - 1].transform.position.z <= 120)
{
2023-05-29 15:36:27 +02:00
if (groundCounter == portalSpawnNumber)
{
2023-05-16 14:27:55 +02:00
CreateNewGround(true);
groundCounter = 0; //ne menjen a vegtelensegig a counter
2023-05-29 15:36:27 +02:00
}
else
{
2023-05-16 14:27:55 +02:00
CreateNewGround();
2023-05-13 15:30:19 +02:00
}
2023-03-25 16:26:23 +01:00
ground = GameObject.FindGameObjectsWithTag("Ground");
2023-05-29 15:36:27 +02:00
ModuleColorChange();
2023-05-13 15:30:19 +02:00
}
2023-03-25 16:26:23 +01:00
2023-02-27 12:09:47 +01:00
//cs.SpawnCoin();
}
2023-05-13 15:30:19 +02:00
private GameObject[] LoadPrefabs(string path)
{ //toltese be a palya objecteket a resources mappabol pl: "Prefabs/Modulok"
2023-02-08 11:50:07 +01:00
GameObject[] arr = Resources.LoadAll<GameObject>(path);
return arr;
2023-01-31 15:32:49 +01:00
}
2023-05-13 15:30:19 +02:00
private void CreateNewSideObjects(bool isLeftSide)
{
List<GameObject> side = new List<GameObject>(); //eltarolja egy oldal sideObjectjeit, hogy a jo oldalhoz nezze a kovetkezo elemet;
foreach (var item in sideObjectsSpawned)
{
if (item.transform.position.x < 0 && isLeftSide)
{
2023-05-16 14:27:55 +02:00
side.Add(item); //bal oldal
2023-05-13 15:30:19 +02:00
}
2023-05-16 14:27:55 +02:00
else if (item.transform.position.x > 0 && !isLeftSide)
2023-05-13 15:30:19 +02:00
{
side.Add(item); //jobb oldal
}
}
2023-05-16 14:27:55 +02:00
int random = UnityEngine.Random.Range(0, sideObjects.Length); //random sorsolasa a modulhoz
//random = 0; //csak debug
2023-04-29 17:07:23 +02:00
2023-05-16 14:27:55 +02:00
GameObject inst = sideObjects[random]; //random modul object eltarolas
2023-05-02 13:46:38 +02:00
2023-05-13 15:30:19 +02:00
//remake to get width
2023-05-16 14:27:55 +02:00
Vector3 offset = new Vector3(0, 0, 0);
2023-05-13 15:30:19 +02:00
2023-05-16 14:27:55 +02:00
if (side.Count > 0)
2023-05-13 15:30:19 +02:00
{
2023-05-16 14:27:55 +02:00
if (side[side.Count - 1].gameObject.name.Contains("haz1")) //haz1Clone
offset = new Vector3(0, 0, 15f); //TODO adjust
else if (side[side.Count - 1].gameObject.name.Contains("haz2"))
2023-05-13 15:30:19 +02:00
offset = new Vector3(0, 0, 20f); //TODO adjust
2023-05-02 13:46:38 +02:00
}
2023-05-13 15:30:19 +02:00
//
2023-05-16 14:27:55 +02:00
Vector3 pos = new Vector3(9f, -5f, -10f);
Quaternion rotation = inst.transform.rotation;
2023-05-02 13:46:38 +02:00
2023-05-16 14:27:55 +02:00
if (side.Count > 0)
//pos = sideObjectsSpawned[sideObjectsSpawned.Length - 1].transform.position + offset;
pos = side[side.Count - 1].transform.position + offset;
2023-05-13 15:30:19 +02:00
else
pos = pos + offset;
2023-05-02 13:46:38 +02:00
2023-05-13 15:30:19 +02:00
2023-05-16 14:27:55 +02:00
if (isLeftSide && pos.x > 0) //x negativ hogy a bal oldalra keruljon
pos.x = -pos.x;
2023-05-29 15:36:27 +02:00
2023-05-16 14:27:55 +02:00
Instantiate(inst, pos, rotation);
2023-05-13 15:30:19 +02:00
sideObjectsSpawned = GameObject.FindGameObjectsWithTag("SideObject");
OrderArrayByZ(sideObjectsSpawned);
2023-04-29 17:07:23 +02:00
}
2023-05-29 15:36:27 +02:00
public void ModuleColorChange()
{
for (int i = 0; i < ground.Length; i++)
{
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 = newMaterial;
}
}
}
2023-05-13 15:30:19 +02:00
public void changeMaterialIndex()
{
int materialteszt;
bool teszteljtovabb = true;
2023-05-16 14:27:55 +02:00
while (teszteljtovabb)
2023-05-13 15:30:19 +02:00
{
materialteszt = UnityEngine.Random.Range(0, materials.Length);
Debug.Log(materialteszt);
2023-05-16 14:27:55 +02:00
2023-05-13 15:30:19 +02:00
if (materialteszt == materialIndex)
{
}
else
{
materialIndex = materialteszt;
teszteljtovabb = false;
}
}
teszteljtovabb = true;
2023-03-25 16:26:23 +01:00
}
2023-05-29 15:36:27 +02:00
public void ChangeMaterial(Material mat)
{
newMaterial = mat;
}
2023-05-13 15:30:19 +02:00
private bool CheckGroundToDestroy(GameObject toCheck)
{
//z = -80 -nal lehet torolni
2023-05-13 15:30:19 +02:00
if (toCheck.transform.position.z <= -80)
{
2023-01-31 15:32:49 +01:00
Debug.Log("elerte " + toCheck.name);
return true; //torolheto
}
2023-01-31 15:32:49 +01:00
return false; //nem torolheto
}
2023-05-13 15:30:19 +02:00
private void OrderArrayByZ(GameObject[] array)
{
2023-01-31 15:32:49 +01:00
GameObject csere;
2023-05-13 15:30:19 +02:00
for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < i; j++)
{
if (array[j].transform.position.z > array[j + 1].transform.position.z)
{
2023-05-02 13:46:38 +02:00
csere = array[j];
2023-05-13 15:30:19 +02:00
array[j] = array[j + 1];
array[j + 1] = csere;
2023-01-31 15:32:49 +01:00
}
}
}
}
2023-05-16 14:27:55 +02:00
private void CreateNewGround(bool portalModulSpawn = false)
2023-05-13 15:30:19 +02:00
{
2023-01-31 15:32:49 +01:00
int random = UnityEngine.Random.Range(0, loadFrom.Length);
2023-05-16 14:27:55 +02:00
GameObject inst;
2023-05-29 15:36:27 +02:00
if (!portalModulSpawn)
2023-05-16 14:27:55 +02:00
inst = loadFrom[random];
else
inst = portalModul;
2023-05-29 15:36:27 +02:00
2023-03-25 16:26:23 +01:00
//egy modullal elobb tolt be, annak az iranyanak megfeleloen, +80 a ket modul hossza
2023-05-22 14:37:24 +02:00
Instantiate(inst, new Vector3(ground[ground.Length - 1].transform.position.x, ground[ground.Length - 1].transform.position.y, ground[ground.Length - 1].transform.position.z + 40), ground[ground.Length - 1].transform.rotation);
2023-05-16 14:27:55 +02:00
groundCounter++;
2023-01-31 15:32:49 +01:00
}
2023-02-16 14:55:48 +01:00
}