private void DecombinePreview(bool _isStart) { if (_isStart && m_previewStreet.GetStartConnection().m_OtherConnection != null) { Connection oldConnection = m_previewStreet.GetStartConnection().m_OtherConnection; Connection.DeCombine(m_previewStreet.GetStartConnection(), m_previewStreet.GetStartConnection().m_OtherConnection); if (oldConnection.m_Owner is Street) { StreetComponentManager.CreateDeadEnd((Street)oldConnection.m_Owner, oldConnection.m_OwnerStart); } else if (oldConnection.m_Owner is Cross) { Cross c = (Cross)oldConnection.m_Owner; StreetComponentManager.CreateDeadEnd((Cross)oldConnection.m_Owner, c.GetIndexByConnection(oldConnection)); } } else if (m_previewStreet.m_EndConnection.m_OtherConnection != null) { Connection oldConnection = m_previewStreet.m_EndConnection.m_OtherConnection; Connection.DeCombine(m_previewStreet.m_EndConnection, m_previewStreet.m_EndConnection.m_OtherConnection); if (oldConnection.m_Owner is Street) { StreetComponentManager.CreateDeadEnd((Street)oldConnection.m_Owner, oldConnection.m_OwnerStart); } else if (oldConnection.m_Owner is Cross) { Cross c = (Cross)oldConnection.m_Owner; StreetComponentManager.CreateDeadEnd((Cross)oldConnection.m_Owner, c.GetIndexByConnection(oldConnection)); } } }
private void PlacePreview() { m_crossColl.GetComponent <MeshRenderer>().material = StreetComponentManager.Instance.streetMatColl; m_crossObj.GetComponent <MeshRenderer>().material.color = Color.white; m_cross.SetOP(); m_cross.CreateDeadEnds(); m_cross.SetID(); m_cross.CheckGridCollision(); StreetComponentManager.AddCross(m_cross); }
private void RemoveStartConnection() { Connection otherConnection = m_cross.GetStartConnection().m_OtherConnection; if (otherConnection == null) { return; } Connection.DeCombine(m_cross.GetStartConnection(), otherConnection); StreetComponentManager.CreateDeadEnd((Street)otherConnection.m_Owner, otherConnection.m_OwnerStart); }
/// <summary> /// Update the PreviewStreet /// </summary> /// <param name="_tangent1">The new Pos of the 1.Tangent</param> /// <param name="_tangent2">The new Pos of the 2.Tangent</param> /// <param name="_endPos">The new Pos of the EndPoint</param> private void UpdatePreview(Vector3 _tangent1, Vector3 _tangent2, Vector3 _endPos) { if (!isTangent1Locked) { StreetComponentManager.UpdateStreetTangent1Pos(m_previewStreet, _tangent1); } if (!isTangent2Locked) { StreetComponentManager.UpdateStreetTangent2Pos(m_previewStreet, _tangent2); } StreetComponentManager.UpdateStreetEndPos(m_previewStreet, _endPos); }
void Start() { Vector3 midPoint = (streetStart.position - streetEnd.position) * 0.5f + streetEnd.position; Vector3 tangent1Pos = (midPoint - streetStart.position) * 0.1f + streetStart.position; Vector3 tangent2Pos = (midPoint - streetEnd.position) * 0.1f + streetEnd.position; Street s = StreetComponentManager.InitStreetForPreview(streetStart.position); StreetComponentManager.UpdateStreet(s, streetStart.position, streetEnd.position, tangent1Pos, tangent2Pos); StreetComponentManager.CreateStreet(s); Destroy(s.gameObject); }
/// <summary> /// Check für Collider with a OverlapSphere /// </summary> /// <returns>return true if another Cell from a diffrent Street ID collid</returns> public bool CheckForCollision() { List<Cell> cellToCheck = new List<Cell>(); List<StreetSegment> segToCheck = new List<StreetSegment>(); List<Cross> crossToCheck = new List<Cross>(); //Sphere Sphere //Cell Cell foreach (Cell c in GridManager.m_AllCells) if (c.ID != this.ID && MyCollision.SphereSphere(this.m_PosCenter, this.m_Radius, c.m_PosCenter, c.m_Radius)) cellToCheck.Add(c); List<StreetComponent> allComponetns = StreetComponentManager.GetAllStreetComponents(); //Cell Segment for (int i = 0; i < allComponetns.Count; i++) { StreetComponent comp = allComponetns[i]; if (comp.ID == this.ID) continue; if (comp is Street) { Street s = (Street)comp; foreach (StreetSegment seg in s.m_Segments) if (MyCollision.SphereSphere(this.m_PosCenter, this.m_Radius, seg.m_Center, seg.m_CollisionRadius)) segToCheck.Add(seg); } else if (comp is Cross) { Cross c = (Cross)comp; if (MyCollision.SphereSphere(this.m_PosCenter, this.m_Radius, c.m_center, 1.7f)) crossToCheck.Add(c); } } //Poly Poly //Cell Cross foreach (Cross c in crossToCheck) if (MyCollision.PolyPoly(this.m_Corner, c.m_corners)) return true; //Cell Segment foreach (StreetSegment seg in segToCheck) if (MyCollision.PolyPoly(this.m_Corner, seg.m_CornerPos)) return true; //Cell Cell foreach (Cell c in cellToCheck) if (MyCollision.PolyPoly(this.m_Corner, c.m_Corner)) return true; return false; }
private void ConnectCrossToStreet(Street _street, bool _isStreetStart) { if (m_cross.GetStartConnection().m_OtherComponent == null) { if (_isStreetStart) { OrientedPoint op = _street.m_Spline.GetFirstOrientedPoint(); SetCrossTransform(op.Position - op.LocalToWorldDirection(Vector3.forward) * 1.2f, op.Rotation * Quaternion.Euler(0, 180f, 0)); StreetComponentManager.DestroyDeadEnd((DeadEnd)_street.GetStartConnection().m_OtherComponent); Connection.Combine(m_cross.GetStartConnection(), _street.GetStartConnection()); } else { OrientedPoint op = _street.m_Spline.GetLastOrientedPoint(); SetCrossTransform(op.Position + op.LocalToWorldDirection(Vector3.forward) * 1.2f, op.Rotation); StreetComponentManager.DestroyDeadEnd((DeadEnd)_street.m_EndConnection.m_OtherComponent); Connection.Combine(m_cross.GetStartConnection(), _street.m_EndConnection); } } }
public override void ToolUpdate() { if (Input.GetKeyUp(KeyCode.C)) //Change to Curve Tool { SetCurveLineTool(false); UIManager.Instance.HighlightButton(UIManager.Instance.CurveButton); } else if (Input.GetKeyUp(KeyCode.L)) //Change to Line Tool { SetCurveLineTool(true); UIManager.Instance.HighlightButton(UIManager.Instance.LineButton); } if (m_validHit) { if (pos1Set == false && pos2Set == false && pos3Set == false) { FindClosestConnection(m_hitPos); //if no Point is set, look for not far away Street possible combinations } if (isCurrendToolLine) { if (pos1Set == true && pos3Set == false && m_previewStreet != null) //Line: First Point Set -> Missing Sec Point { if (Input.GetKey(KeyCode.LeftShift)) //Easy Placement { Vector3 StartEnd = pos1 - m_hitPos; if (Mathf.Abs(StartEnd.x) > Mathf.Abs(StartEnd.z)) { m_hitPos.z = pos1.z; } else { m_hitPos.x = pos1.x; } } Cursor.SetPosition(m_hitPos); Vector3 pos2Tmp = m_previewStreet.m_Spline.GetCentredOrientedPoint().Position; //Get the Midpoint on the Spline Vector3 tangent1 = (pos1 + pos2Tmp) * 0.5f; // 1.Tanget must be between MidPoint and Start Vector3 tangent2 = (pos2Tmp + m_hitPos) * 0.5f; // 2. Tanget must be between MidPoint and End (MousePos) UpdatePreview(tangent1, tangent2, m_hitPos); //Update the Preview (update if Tanget is not locked) CheckForCombine(m_hitPos, false); //If a Combine is possible it Combine (overwrite Tanget Pos) } } else { if (pos1Set == true && pos2Set == false && pos3Set == false && m_previewStreet != null) // Curve: First Point Set -> Wait for Sec Point { Vector3 pos2Tmp = (pos1 + m_hitPos) * 0.5f; //Get the Midpoint between Start and End Vector3 tangent1 = (pos1 + pos2Tmp) * 0.5f; //The 1.Tangent is between the Start and the Mid Vector3 tangent2 = (pos2Tmp + m_hitPos) * 0.5f; //The 2. Tangent is between the End and the Mid UpdatePreview(tangent1, tangent2, m_hitPos); //Update the Preview (update if Tanget is not locked) //The Scecond Point cant Combine to an another Spline } if (pos1Set == true && pos2Set == true && pos3Set == false && m_previewStreet != null) { Vector3 tangent1 = (pos1 + pos2) * 0.5f; //The 1.Tanget is between Start and Sec Point Vector3 tangent2 = (pos2 + m_hitPos) * 0.5f; // The 2.Tanget is between Sec Point and EndPoint (MousePos) UpdatePreview(tangent1, tangent2, m_hitPos); //Update the Preview (update if Tanget is not locked) CheckForCombine(m_hitPos, false); //If a Combine is possible it Combine (overwrite Tanget Pos) } } if (Input.GetMouseButtonDown(1)) { if (m_previewStreet != null) { DecombinePreview(true); DecombinePreview(false); } ResetTool(); return; } if (m_previewStreet != null) { m_currendCost = -m_previewStreet.m_Spline.GetLength() * streetCostMultiplier; UIManager.Instance.SetStreetCost(m_currendCost); if (!CheckForValidForm() || CheckForCollision()) { m_previewStreet.m_IsInvalid = false; return; } else { m_previewStreet.m_IsInvalid = true; } } if (Input.GetMouseButtonDown(0) && !StreetComponentManager.BlockStreetComponentPlacement) { if (pos1Set == false) { pos1 = m_hitPos; pos1Set = true; m_previewStreet = StreetComponentManager.InitStreetForPreview(pos1); //Init a Preview Street (with invalid ID) CheckForCombine(m_hitPos, true); //Check if the Start in the Preview Street can be combined pos1 = Cursor.GetPosition(); //Reset the Position for correct Easy Shift Placement return; } if (!isCurrendToolLine) // In Curve Tool -> Set Sec Pos { if (pos2Set == false) { pos2 = m_hitPos; pos2Set = true; return; } } if (pos3Set == false) { if (Mathf.Abs(m_currendCost) <= Inventory.Instance.m_MoneyAmount) { Inventory.Instance.m_MoneyAmount += m_currendCost; //currend Cost is negative UIManager.Instance.UpdateMoneyUI(); //dont wait for next tick to update UI pos3 = m_hitPos; CheckForCombine(m_hitPos, false); //Check if the End in the Preview Street can be combined Street newStreet = StreetComponentManager.CreateStreet(m_previewStreet); //Create a valid Street from the preview Street ResetTool(); //Reset return; } } } } }
/// <summary> /// Check on the given Pos if a combine to another Street is possible /// </summary> /// <param name="_hitPoint">The Pos to check around</param> /// <param name="_isStart">Is it the Start of the PreviewStreet</param> /// <returns></returns> private void CheckForCombine(Vector3 _hitPoint, bool _isStart) { Connection closestConnection = FindClosestConnection(_hitPoint); if (closestConnection != null && m_previewLastConnected != closestConnection) { //Recreate Dead End + Decombine DecombinePreview(false); } if (closestConnection == null) // if there is no valid Street GameObject around { //Recreate Dead End + Decombine DecombinePreview(false); //Unlock the Tangent of the Pos which is not Set if (pos1 == Vector3.zero) { isTangent1Locked = false; } if (pos3 == Vector3.zero) { isTangent2Locked = false; } return; } m_previewLastConnected = closestConnection; //check next Update if new closestConnection has chanded to create a DeadEnd on the old place if (m_previewStreet.GetStartConnection().m_OtherConnection == closestConnection) { return; } if (closestConnection.m_Owner is Street) { Street otherStreet = (Street)closestConnection.m_Owner; if (!closestConnection.m_OwnerStart) { if (_isStart) { //other Street End and previewStreet Start -> Combine Vector3 dir = -(otherStreet.m_Spline.Tangent2Pos - otherStreet.m_Spline.EndPos).normalized; //Get the point symmetrical Direction StreetComponentManager.UpdateStreetTangent1AndStartPoint( m_previewStreet, dir * 5f + otherStreet.m_Spline.EndPos, //Set the Tanget to the half point symmetrical Position otherStreet.m_Spline.EndPos ); //Set the EndPos isTangent1Locked = true; //Combine(_others End, preview Start) + Remove others DeadEnd at End if (otherStreet.m_EndConnection.m_OtherComponent is DeadEnd) { StreetComponentManager.DestroyDeadEnd((DeadEnd)otherStreet.m_EndConnection.m_OtherComponent); } CombinePreview(otherStreet, false, true); return; } else { //other Street End and previewStreet End -> Combine Vector3 dir = -(otherStreet.m_Spline.Tangent2Pos - otherStreet.m_Spline.EndPos).normalized; StreetComponentManager.UpdateStreetTangent2AndEndPoint( m_previewStreet, dir * 5f + otherStreet.m_Spline.EndPos, otherStreet.m_Spline.EndPos ); isTangent2Locked = true; //Combine(_others End, preview End) + Remove others DeadEnd at End if (otherStreet.m_EndConnection.m_OtherComponent is DeadEnd) { StreetComponentManager.DestroyDeadEnd((DeadEnd)otherStreet.m_EndConnection.m_OtherComponent); } CombinePreview(otherStreet, false, false); return; } } else if (closestConnection.m_OwnerStart) { if (_isStart) { //other Street Start and previewStreet Start -> Combine Vector3 dir = -(otherStreet.m_Spline.Tangent1Pos - otherStreet.m_Spline.StartPos).normalized; StreetComponentManager.UpdateStreetTangent1AndStartPoint( m_previewStreet, dir * 5f + otherStreet.m_Spline.StartPos, otherStreet.m_Spline.StartPos ); isTangent1Locked = true; //Combine(_others Start, preview Start) + Remove others DeadEnd at Start if (otherStreet.GetStartConnection().m_OtherComponent is DeadEnd) { StreetComponentManager.DestroyDeadEnd((DeadEnd)otherStreet.GetStartConnection().m_OtherComponent); } CombinePreview(otherStreet, true, true); return; } else { //other Street Start and previewStreet End -> Combine Vector3 dir = -(otherStreet.m_Spline.Tangent1Pos - otherStreet.m_Spline.StartPos).normalized; StreetComponentManager.UpdateStreetTangent2AndEndPoint( m_previewStreet, dir * 5f + otherStreet.m_Spline.StartPos, otherStreet.m_Spline.StartPos ); isTangent2Locked = true; //Combine(_others Start, preview Start) + Remove others DeadEnd at Start if (otherStreet.GetStartConnection().m_OtherComponent is DeadEnd) { StreetComponentManager.DestroyDeadEnd((DeadEnd)otherStreet.GetStartConnection().m_OtherComponent); } CombinePreview(otherStreet, true, false); return; } } } if (closestConnection.m_Owner is Cross) { Cross otherCross = (Cross)closestConnection.m_Owner; int otherCrossConnectionIndex = otherCross.GetIndexByConnection(closestConnection); OrientedPoint op = otherCross.m_OPs[otherCrossConnectionIndex]; if (otherCross.m_Connections[otherCrossConnectionIndex].m_OtherComponent is DeadEnd) { StreetComponentManager.DestroyDeadEnd((DeadEnd)otherCross.m_Connections[otherCrossConnectionIndex].m_OtherComponent); } if (_isStart) { StreetComponentManager.UpdateStreetTangent1AndStartPoint(m_previewStreet, op.Position + op.Rotation * Vector3.forward * 3f, op.Position); isTangent1Locked = true; Connection.Combine(m_previewStreet.GetStartConnection(), closestConnection); } else { StreetComponentManager.UpdateStreetTangent2AndEndPoint(m_previewStreet, op.Position + op.Rotation * Vector3.forward * 3f, op.Position); isTangent2Locked = true; Connection.Combine(m_previewStreet.m_EndConnection, closestConnection); } } }