- Home /
Need to combine several gameobject sets into a specific result. Please help!!
I am trying to build a god-terraforming game where you create biomes. And when you combine biomes it should can result with different kind of biomes. For example in the first screenshot Grassland and Mud created a Wetland.
All my triangles are independent gameobjects. And I'm selecting them with a basic sphere collider and listing them as selected. But in my build I can make only 1 combination at a time. But I need to be able to combine couple of them at the same time. How can I achieve that? I am struggling with this for days.. Any kind of help is appreciated. And sorry about the messyness of the code Im kind of a newbie.
Also my combinations are scriptable objects with 2 ingridients and 1 result.
this is my card class.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class card : MonoBehaviour
{
public GameObject brushPrefab;
bool isMagnifying = false;
bool onDrag = false;
bool brushActive = false;
bool canCombine;
brush brushData;
GameObject canvas;
GameObject manager;
GameObject cardHolder;
GameObject brush;
GameObject cardText;
Text nameText;
Vector3 startScale;
public string cardEffect;
public Material effectMat;
public Color effectColor;
public float effectRadius = 1;
public List<GameObject> objectsToSpawn;
string combinationName;
Color combinationColor;
public CombinationList combinationList;
List<StateCombination> combinations;
public List<string> existingStates;
// Start is called before the first frame update
void Start()
{
canvas = GameObject.Find("Canvas");
manager = GameObject.Find("Main Camera");
cardHolder = GameObject.Find("CardHolder");
cardText = transform.GetChild(0).gameObject;
nameText = cardText.GetComponent<Text>();
startScale = GetComponent<RectTransform>().localScale;
combinations = combinationList.combinations;
effectMat.color = effectColor;
nameText.text = cardEffect;
}
// Update is called once per frame
void Update()
{
if (!existingStates.Contains(cardEffect))
{
existingStates.Add(cardEffect);
}
if (onDrag)
{
if(brushActive == false)
{
brush = Instantiate(brushPrefab);
brush.transform.localScale *= effectRadius;
brush.SetActive(true);
brushData = brush.GetComponent<brush>();
brushActive = true;
}
manager.GetComponent<gameManager>().FindBrush();
transform.position = Input.mousePosition;
GetComponent<RectTransform>().localScale = startScale;
if (manager.GetComponent<gameManager>().mouseOnPlanet == true)
{
CheckForCombinations();
GetComponent<Image>().enabled = false;
//planet.GetComponent<Planet>().GetTriangles(effectName, combinationName, objectsToSpawn, effectSize, extrudeSize, propCount, effectColor, combinationColor);
}
else
{
GetComponent<Image>().enabled = true;
}
}
}
public void Magnify()
{
//Debug.Log("bakcam");
GetComponent<RectTransform>().localScale = GetComponent<RectTransform>().localScale + GetComponent<RectTransform>().localScale;
isMagnifying = true;
}
public void unMagnify()
{
//Debug.Log("bakmıcam");
GetComponent<RectTransform>().localScale = startScale;
isMagnifying = false;
}
public void Drag()
{
if (isMagnifying == true)
{
transform.SetParent(canvas.transform);
onDrag = true;
}
}
public void unDrag()
{
if (manager.GetComponent<gameManager>().mouseOnPlanet == false)
{
onDrag = false;
brushActive = false;
Destroy(brush);
transform.SetParent(cardHolder.transform);
transform.position = new Vector3(transform.position.x, transform.position.y, cardHolder.transform.position.z);
GetComponent<Image>().enabled = true;
}
else
{
ActivateCardEffect();
onDrag = false;
brushActive = false;
canCombine = false;
Destroy(brush);
GetComponent<Image>().enabled = false;
Destroy(this.gameObject, 0.01f);
}
}
public void CheckForCombinations()
{
if(brushData.trisToCombine.Count > 0) // check for combinable list
{
for (int i = 0; i < brushData.trisToCombine.Count; i++) // get all states in combinable list
{
if (!existingStates.Contains(brushData.trisToCombine[i].GetComponent<Tri>().plotState))
{
existingStates.Add(brushData.trisToCombine[i].GetComponent<Tri>().plotState);
}
}
for (int i = 0; i < combinations.Count; i++) //search for all combinations
{
if (CompareLists(existingStates, combinations[i].StatesToCombine)) // if a combination matches
{
Debug.Log("should create" + combinations[i].resultState);
canCombine = true;
combinationColor = combinations[i].resultColor;
combinationName = combinations[i].resultState;
break; //stop searching
}
else
{
// no combination
Debug.Log("so close");
canCombine = false;
}
}
}
else
{
// no combination
Debug.Log("nopee");
existingStates.Clear(); // clear list
canCombine = false;
}
}
public void ActivateCardEffect()
{
//basic application of card effect to selected triangles
for(int i = 0; i < brush.GetComponent<brush>().selectedTris.Count; i++)
{
brush.GetComponent<brush>().selectedTris[i].GetComponent<Tri>().ChangePlotCharacter(cardEffect, effectColor);
}
if (canCombine)
{
for (int i = 0; i < brushData.trisToCombine.Count; i++)
{
brush.GetComponent<brush>().trisToCombine[i].GetComponent<Tri>().ChangePlotCharacter(combinationName, combinationColor);
}
}
else
{
for (int i = 0; i < brushData.trisToCombine.Count; i++)
{
brush.GetComponent<brush>().trisToCombine[i].GetComponent<Tri>().ChangePlotCharacter(cardEffect, effectColor);
}
}
}
public void SpawnObject(List<GameObject> possiblePlots)
{
Instantiate(objectsToSpawn[Random.Range(0, objectsToSpawn.Count)], possiblePlots[Random.Range(0, possiblePlots.Count)].transform);
}
public static bool CompareLists<T>(List<T> aListA, List<T> aListB)
{
if (aListA == null || aListB == null) //aListA.Count != aListB.Count
return false;
if (aListA.Count == 0)
return true;
Dictionary<T, int> lookUp = new Dictionary<T, int>();
// create index for the first list
for (int i = 0; i < aListA.Count; i++)
{
int count = 0;
if (!lookUp.TryGetValue(aListA[i], out count))
{
lookUp.Add(aListA[i], 1);
continue;
}
lookUp[aListA[i]] = count + 1;
}
for (int i = 0; i < aListB.Count; i++)
{
int count = 0;
if (!lookUp.TryGetValue(aListB[i], out count))
{
// early exit as the current value in B doesn't exist in the lookUp (and not in ListA)
return false;
}
count--;
if (count <= 0)
lookUp.Remove(aListB[i]);
else
lookUp[aListB[i]] = count;
}
// if there are remaining elements in the lookUp, that means ListA contains elements that do not exist in ListB
return lookUp.Count == 0;
}
}
this is the brush class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class brush : MonoBehaviour
{
public List<GameObject> selectedTris;
public List<GameObject> trisToCombine;
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.GetComponent<Tri>() != null) //check if there is any triangle
{
other.gameObject.GetComponent<Tri>().selected = true; //highlight all tris inside the circle
if (other.gameObject.GetComponent<Tri>().plotState != "Arid") //if tri is not arid
{
trisToCombine.Add(other.gameObject); // add to combinable list
}
else // if it is
{
selectedTris.Add(other.gameObject); // add to normal list
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.GetComponent<Tri>() != null) // check for triangles again
{
other.gameObject.GetComponent<Tri>().selected = false; // unlight outside the circle
if (other.gameObject.GetComponent<Tri>().plotState != "Arid")
{
trisToCombine.Remove(other.gameObject);
}
else
{
selectedTris.Remove(other.gameObject);
}
}
}
}