public void StopDragging() { List <GameObject> objectsPlaced = new List <GameObject>(); GameObject FirstObject = Instantiate(GameObject_DragFrom); BeyondComponent BC_DragFrom = GameObject_DragFrom.GetComponent <BeyondComponent>(); FirstObject.GetComponent <BeyondComponent>().CopyValues(BC_DragFrom); SetBlueprintFromGhost(FirstObject); objectsPlaced.Add(FirstObject); Destroy(GameObject_DragFrom); for (int i = 0; i < draggedObjects.Count; i++) // Note: a for loop is better than a foreach that can miss some objects { // TO DO : We need to check a bit more than that: can't have a bunch of objects split in several groups because they failed constraint. All draggedObject should form 1 block // And, as stated in the AvoidCollision Constraint, if adding to an existing group we should check the object is in a valid group position: // As above: no isolated objects, but also no 2 same objects in the same cell, all objects have at least one Snaptarget, etc if (draggedObjects[i].activeSelf && Constraint.CheckRootConstraint(draggedObjects[i])) { GameObject ThisObject = Instantiate(draggedObjects[i]); BeyondComponent BC_ThisObject = draggedObjects[i].GetComponent <BeyondComponent>(); ThisObject.GetComponent <BeyondComponent>().CopyValues(BC_ThisObject); SetBlueprintFromGhost(ThisObject); objectsPlaced.Add(ThisObject); } Destroy(draggedObjects[i]); } draggedObjects.Clear(); DestroyActiveBlueprint(); // When snapping, the Active blueprint is already in a group, which we should use, otherwise create a new one BeyondGroup bg = null; if (SnappedToObject != null) { bg = SnappedToObject.GetComponent <BeyondComponent>().Group; //Debug.Log("I should add all these to the group of the object I was snapped to: "+bg.Name); SnappedToObject = null; } // Create BeyondGroup, add all objects to it foreach (GameObject go in objectsPlaced) { if (bg == null) { bg = CreateNewBeyondGroup(go); } bg.AddObject(go); // Only set the layer at the end, so objects don't collide into each other and fail the constraint check go.layer = LayerMask.NameToLayer("Buildings"); } }
//TO DO : refactor that with love & care public BeyondGroup CreateNewBeyondGroup(GameObject go, string name = null) { // Auto give name if (name == null) { name = String.Format("Group {0:0000}", GameManager.instance.Place.BeyondGroups.Count); } BeyondComponent bc = go.GetComponent <BeyondComponent>(); if (bc != null) { // bc.transform.position - bc.template.pivotOffset : THIS IS ESSENTIAL - This allows us to properly set the pivot of the group BeyondGroup group = new BeyondGroup(name, bc.transform.position - bc.Template.CellCentre, bc.transform.rotation); group.AddObject(go); GameManager.instance.Place.BeyondGroups.Add(group); return(group); } else { Debug.LogError("CreateNewBeyondGroup attempted to create an emtpy group"); } return(null); }