public GameObject CreateFacility(CFacilityInterface.EType _eType, uint _uiFacilityId = uint.MaxValue, uint _uiExpansionPortId = uint.MaxValue, uint _uiAttachToId = uint.MaxValue) { CExpansionPortInterface cExpansionPort = null; if (_uiExpansionPortId != uint.MaxValue && _uiAttachToId != uint.MaxValue) { cExpansionPort = m_mFacilities[_uiFacilityId].GetComponent <CFacilityExpansion>().GetExpansionPort(_uiExpansionPortId).GetComponent <CExpansionPortInterface>(); if (cExpansionPort.HasAttachedFacility == true) { Debug.LogWarning("Failed to create new room. Port is already in use."); return(null); } } // Generate facility identifier uint uiFacilityId = m_uiFacilityIdCount; // Retrieve the facility prefab CGameRegistrator.ENetworkPrefab eRegisteredPrefab = CFacilityInterface.GetPrefabType(_eType); // Create facility GameObject cNewFacilityObject = CNetwork.Factory.CreateObject(eRegisteredPrefab); // Set facility properties CFacilityInterface cFacilityInterface = cNewFacilityObject.GetComponent <CFacilityInterface>(); cFacilityInterface.FacilityId = uiFacilityId; cFacilityInterface.FacilityType = _eType; m_uiFacilityIdCount++; // Set facility parent cNewFacilityObject.GetComponent <CNetworkView>().SetParent(GetComponent <CNetworkView>().ViewId); // Attach facility expansion port to parent expansion port if (cExpansionPort != null) { cExpansionPort.Attach(_uiAttachToId, cNewFacilityObject); } // Initialise the facility expansion ports cNewFacilityObject.GetComponent <CFacilityExpansion>().InitialiseExpansionPorts(); // Sync position & rotation cNewFacilityObject.GetComponent <CNetworkView>().SyncTransformPosition(); cNewFacilityObject.GetComponent <CNetworkView>().SyncTransformRotation(); // Server adds the facility instantaniously AddNewlyCreatedFacility(cNewFacilityObject, uiFacilityId, _eType); // Notify facility creation observers if (EventOnFaciltiyCreate != null) { EventOnFaciltiyCreate(cNewFacilityObject); } return(cNewFacilityObject); }
void AllignUpVector(CExpansionPortInterface _newPort, GameObject _objNewFacility) { Vector3 UpVector; if (m_bInvertUpVector) { UpVector = transform.up * -1; } else { UpVector = transform.up; } //Allign the Up vector float rotationAngle = Vector3.Angle(_newPort.transform.up, UpVector); Vector3 crossResult = Vector3.Cross(_newPort.transform.up, UpVector); if (crossResult.y > 0.0f) { Debug.Log("Cross product result is positive"); } else if (crossResult.y < -0.0001f && crossResult.y > -1.0001f) { Debug.Log("Cross product result is negative"); rotationAngle = 360 - rotationAngle; crossResult *= -1; } //If the result is neither positive nor negative, it is approximately zero. else { Debug.Log("Cross product result is zero"); crossResult = _newPort.transform.right; } //Apply rotation Vector3 rotationPos = transform.position; _objNewFacility.transform.RotateAround(rotationPos, crossResult, rotationAngle); }
public void Orient(int _portID, GameObject _objNewFacility) { // _objNewFacility.transform.position = transform.position; // _objNewFacility.transform.rotation = Quaternion.identity; // Debug.Log("Current Port Pos " + transform.position.x + "," + transform.position.y + "," + transform.position.z); // // //m_attachedPorts[_portID].renderer.material.color = Color.green; // // //Subtract the offset of the selected port from the position of the new object // Vector3 offset = m_attachedPorts[_portID].localPosition; // Vector3 currentPos = _objNewFacility.transform.position - offset; // Debug.Log("Offset: " + offset.x + "," + offset.y + "," + offset.z); // _objNewFacility.transform.position = currentPos; // // //Adjust rotation // //The center of rotation should be the currently selected port of the new hull // CExpansionPortInterface newPort = (CExpansionPortInterface)m_attachedPorts[_portID].GetComponent("CExpansionPortInterface"); // newPort.HasAttachedFacility = true; // // AllignForwardVector(newPort, _objNewFacility); // //AllignRightVector(newPort, _objNewFacility); // //AllignUpVector(newPort, _objNewFacility); // CExpansionPortInterface newPort = m_attachedPorts[_portID].GetComponent <CExpansionPortInterface>(); newPort.HasAttachedFacility = true; /*** Code for rotating the new facility using quaternions. Rotates all the axis to be aligned correctly ***/ Quaternion Rot1 = transform.rotation * m_attachedPorts[_portID].parent.rotation * Quaternion.Inverse(m_attachedPorts[_portID].rotation); Quaternion Rot2 = Quaternion.AngleAxis(180.0f, Vector3.up); _objNewFacility.transform.rotation = Rot1 * Rot2; // Apply the new position (this expansion port position plus the rotated other expansionport local position) _objNewFacility.transform.position = transform.position - (_objNewFacility.transform.rotation * m_attachedPorts[_portID].localPosition); }
void AllignForwardVector(CExpansionPortInterface _newPort, GameObject _objNewFacility) { //Forward rotation// //The normal of this new port should be the inverse of the normal attached to this port Vector3 inverseNormal = transform.forward * -1; float rotationAngle = Vector3.Angle(_newPort.transform.forward, inverseNormal); //Figure out if this is a left or right rotation Vector3 crossResult = Vector3.Cross(_newPort.transform.forward, inverseNormal); if (crossResult.y > 0.0f) { Debug.Log("Cross product result is positive"); } else if (crossResult.y < -0.0001f && crossResult.y > -1.0001f) { Debug.Log("Cross product result is negative" + crossResult.ToString("n10")); rotationAngle = 360 - rotationAngle; crossResult *= -1; } //If the result is neither positive nor negative, it is approximately zero. else { Debug.Log("Cross product result is zero " + crossResult.ToString("n10")); crossResult = _newPort.transform.up; Debug.Log("Setting result to up vector" + crossResult.ToString("n10")); } //Apply rotation Vector3 rotationPos = transform.position; _objNewFacility.transform.RotateAround(rotationPos, crossResult, rotationAngle); Debug.Log("Rotated " + rotationAngle.ToString() + " around " + crossResult.ToString()); }
void AllignUpVector(CExpansionPortInterface _newPort, GameObject _objNewFacility) { Vector3 UpVector; if(m_bInvertUpVector) { UpVector = transform.up * -1; } else { UpVector = transform.up; } //Allign the Up vector float rotationAngle = Vector3.Angle(_newPort.transform.up, UpVector); Vector3 crossResult = Vector3.Cross(_newPort.transform.up, UpVector); if (crossResult.y > 0.0f) { Debug.Log("Cross product result is positive"); } else if (crossResult.y < -0.0001f && crossResult.y > -1.0001f) { Debug.Log("Cross product result is negative"); rotationAngle = 360 - rotationAngle; crossResult *= -1; } //If the result is neither positive nor negative, it is approximately zero. else { Debug.Log("Cross product result is zero"); crossResult = _newPort.transform.right; } //Apply rotation Vector3 rotationPos = transform.position; _objNewFacility.transform.RotateAround(rotationPos, crossResult, rotationAngle); }