void StartDrag() { ForwardOffsetBeforeDragging = UIController.Instance.forwardOffset; if (ConstraintController.CanPlace(currentBC)) { string templateName = currentBC.template.name; string name = templateName + "_" + (nbObjectsPlaced++); // Place the first Ghost = still green, still not collidin' TemplateController.PlaceObject(currentBC, name, BC_State.Ghost); draggedBC.Add(currentBC); draggingGroup = currentBC.beyondGroup; lastGroupPosition = currentBC.groupPosition; currentBC = null; CreateNewPlaceableObject(templateName); // TODO : another hardcoded position. Not good. Yet, I must move the currentBC to where I started dragging or else I'm going to drag to some unknown place. currentBC.transform.position = draggedBC[0].transform.position; currentBC.transform.rotation = draggedBC[0].transform.rotation; // Instantiate a big bunch of placeable object based on what we are currently dragging for (int i = 0; i < MaxDraggedObjectCount; i++) { BeyondComponent bc = TemplateController.CreateObject(templateName); //TODO Better names, please name = templateName + "_Ghost" + i; bc.SetBCinGroup(draggingGroup, lastGroupPosition); TemplateController.PlaceObject(bc, name, BC_State.Ghost); bc.gameObject.SetActive(false); draggedBC.Add(bc); } } }
private void Snap() { // Unset the group before trying to snap or weird things happen currentBC.unsetObjectGroup(); // 1 - what groups are close to currentPlaceableObject ? BeyondGroup closestGroup; findCloseGroups(currentBC, out closestGroup); if (closestGroup != null) { //Debug.Log("closestGroup"+closestGroup.name); // Find the centre where I should place the object based on the group's "root" position and the Vector3Int difference between there and here // 1 - where would the cell centre be, considering the offset of this placeable object's template Vector3 pointWithOffset = currentBC.transform.position - currentBC.template.pivotOffset; // 2 - calculate the difference between this point and the group's centre and apply the inverse rotation for the group so we can compare in a non-rotated way pointWithOffset = Utility.RotateAroundPoint(pointWithOffset, closestGroup.position, Quaternion.Inverse(closestGroup.rotation)); // 3 - Obtain the equivalent Integer Vector3, which represents how many cells the object is from the group's centre Vector3Int diffInt2 = Vector3Int.RoundToInt(pointWithOffset - closestGroup.position); UIController.Instance.positionInGroup = diffInt2; // 4 - This is the position of the centre of the cell Vector3 snappedPosition = closestGroup.position + (Vector3)diffInt2; // 5 - Rotate it by the group's rotation for final result snappedPosition = Utility.RotateAroundPoint(snappedPosition, closestGroup.position, closestGroup.rotation); // TODO : move this constraint to ConstraintController // Test to see if foundation is above groud like in GetValidPositionFromMouse //TODO: Hardcode for the time being as I tweak GetPointOnLayer //TODO : I have removed minY so don't need the code below. See if that works. CanSnapTo should ideally prevent being inside terrain anyway //Vector3 point = ConstraintController.GetPointOnLayer(currentBC , currentBC.transform.position , ConstraintController.getTerrainMask()) ; //float minY = point.y ; //minY = 0; //if (snappedPosition.y>=minY) //{ // Snap in place only if object's top is above ground // Not needed aymore-angle between the rotation of the group and the rotation of the object on the Y axis : float angle = Mathf.Abs(currentPlaceableObject.transform.rotation.eulerAngles.y - closestGroup.rotation.eulerAngles.y ); //if (ConstraintController.CanSnapToGroupHere(closestGroup , diffInt2 , currentBC.template , snappedPosition - pointWithOffset , currentBC.transform.rotation , out snapToSide)) if (ConstraintController.CanSnapTo(currentBC, closestGroup, diffInt2)) { currentBC.SetBCinGroup(closestGroup, diffInt2); } // else Debug.Log("Can't snap here because of group constraints"); //} } else { currentBC.unsetObjectGroup(); // TODO : gros caca UIController.Instance.positionInGroup = new Vector3Int(-999, -999, -999); } }
public void CreateNewBeyondGroup(BeyondComponent bc, string name = null) { if (name == null) { // Auto give name name = String.Format("Group {0:0000}", place.beyondGroups.Count); } // 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.pivotOffset, bc.transform.rotation); if (bc != null) { group.addBeyondComponent(bc); // Vector3Int.zero because the first object in a group is at position [0,0,0] bc.SetBCinGroup(group, Vector3Int.zero, true); } place.beyondGroups.Add(group); }