/// <summary> /// Belts that input to splitter /// </summary> /// <param name="beltElement"></param> public void SetPrevBeltElement(IBeltElement beltElement) { if (beltElement.GetOutputOrientation() != this.outputOrientation) { return; } Vector2 beltPos = beltElement.GetPosition(); switch (outputOrientation) { case ConveyorBelt.OutputOrientation.East: if (beltPos.y > this.transform.position.y) { leftInput = beltElement; } else if (beltPos.y < this.transform.position.y) { rightInput = beltElement; } break; case ConveyorBelt.OutputOrientation.North: if (beltPos.x < this.transform.position.x) { leftInput = beltElement; } else if (beltPos.x > this.transform.position.x) { rightInput = beltElement; } break; case ConveyorBelt.OutputOrientation.West: if (beltPos.y > this.transform.position.y) { leftInput = beltElement; } else if (beltPos.y < this.transform.position.y) { rightInput = beltElement; } break; default: case ConveyorBelt.OutputOrientation.South: if (beltPos.x < this.transform.position.x) { leftInput = beltElement; } else if (beltPos.x > this.transform.position.x) { rightInput = beltElement; } break; } }
public void InitializeRotated(int rotation) { ContactFilter2D filter = new ContactFilter2D(); filter.useLayerMask = true; filter.layerMask = 1 << LayerMask.NameToLayer("ConveyorBelt"); int countRegular = 0; int countUnderground = 0; Collider2D[] regular = new Collider2D[1]; Collider2D[] underground = new Collider2D[searchDist + 2]; items = new ItemEntity[4]; switch (rotation) { case 0: //east input outputOrientation = ConveyorBelt.OutputOrientation.East; transitionGround = TransitionGround.input; countRegular = Physics2D.OverlapBox((Vector2)this.transform.position - new Vector2(1, 0), new Vector2(0.5f, 0.5f), 0, filter, regular); countUnderground = Physics2D.OverlapBox((Vector2)this.transform.position + new Vector2(searchDist / 2.0f, 0), new Vector2(searchDist, 0.5f), 0, filter, underground); if (countRegular >= 1) { IBeltElement temp = regular[0].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == outputOrientation) { input = temp; temp.SetNextBeltElement(this); } } for (int i = 0; i < countUnderground; i++) { UndergroundBelt temp; if (underground[i].TryGetComponent(out temp) && (output == null || output.GetPosition().x > temp.GetPosition().x) && //check if one found is closer than current connection (temp.transitionGround == TransitionGround.output && temp.outputOrientation == outputOrientation) //check orientation and that it is output ) { output = temp; temp.SetPrevBeltElement(this); } } break; case 1: //east output outputOrientation = ConveyorBelt.OutputOrientation.East; transitionGround = TransitionGround.output; countRegular = Physics2D.OverlapBox((Vector2)this.transform.position + new Vector2(1, 0), new Vector2(0.5f, 0.5f), 0, filter, regular); countUnderground = Physics2D.OverlapBox((Vector2)this.transform.position - new Vector2(searchDist / 2.0f, 0), new Vector2(searchDist, 0.5f), 0, filter, underground); if (countRegular >= 1) { IBeltElement temp = regular[0].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { output = temp; temp.SetPrevBeltElement(this); } } for (int i = 0; i < countUnderground; i++) { UndergroundBelt temp; if (underground[i].TryGetComponent(out temp) && (input == null || input.GetPosition().x < temp.GetPosition().x) && //check if one found is closer than current connection (temp.transitionGround == TransitionGround.input && temp.outputOrientation == outputOrientation) //check orientation and that it is output ) { input = temp; input.SetNextBeltElement(this); } } break; case 2: //north input outputOrientation = ConveyorBelt.OutputOrientation.North; transitionGround = TransitionGround.input; countRegular = Physics2D.OverlapBox((Vector2)this.transform.position - new Vector2(0, 1), new Vector2(0.5f, 0.5f), 0, filter, regular); countUnderground = Physics2D.OverlapBox((Vector2)this.transform.position + new Vector2(0, searchDist / 2.0f), new Vector2(0.5f, searchDist), 0, filter, underground); if (countRegular >= 1) { IBeltElement temp = regular[0].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == outputOrientation) { input = temp; temp.SetNextBeltElement(this); } } for (int i = 0; i < countUnderground; i++) { UndergroundBelt temp; if (underground[i].TryGetComponent(out temp) && (output == null || output.GetPosition().y > temp.GetPosition().y) && //check if one found is closer than current connection (temp.transitionGround == TransitionGround.output && temp.outputOrientation == outputOrientation) //check orientation and that it is output ) { output = temp; output.SetPrevBeltElement(this); } } break; case 3: //north output outputOrientation = ConveyorBelt.OutputOrientation.North; transitionGround = TransitionGround.output; countRegular = Physics2D.OverlapBox((Vector2)this.transform.position + new Vector2(0, 1), new Vector2(0.5f, 0.5f), 0, filter, regular); countUnderground = Physics2D.OverlapBox((Vector2)this.transform.position - new Vector2(0, searchDist / 2.0f), new Vector2(0.5f, searchDist), 0, filter, underground); if (countRegular >= 1) { IBeltElement temp = regular[0].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { output = temp; temp.SetPrevBeltElement(this); } } for (int i = 0; i < countUnderground; i++) { UndergroundBelt temp; if (underground[i].TryGetComponent(out temp) && (input == null || input.GetPosition().y < temp.GetPosition().y) && //check if one found is closer than current connection (temp.transitionGround == TransitionGround.input && temp.outputOrientation == outputOrientation) //check orientation and that it is output ) { input = temp; input.SetNextBeltElement(this); } } break; case 4: //west input outputOrientation = ConveyorBelt.OutputOrientation.West; transitionGround = TransitionGround.input; countRegular = Physics2D.OverlapBox((Vector2)this.transform.position + new Vector2(1, 0), new Vector2(0.5f, 0.5f), 0, filter, regular); countUnderground = Physics2D.OverlapBox((Vector2)this.transform.position - new Vector2(searchDist / 2.0f, 0), new Vector2(searchDist, 0.5f), 0, filter, underground); if (countRegular >= 1) { IBeltElement temp = regular[0].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == outputOrientation) { input = temp; temp.SetNextBeltElement(this); } } for (int i = 0; i < countUnderground; i++) { UndergroundBelt temp; if (underground[i].TryGetComponent(out temp) && (output == null || output.GetPosition().x < temp.GetPosition().x) && //check if one found is closer than current connection (temp.transitionGround == TransitionGround.output && temp.outputOrientation == outputOrientation) //check orientation and that it is output ) { output = temp; temp.SetPrevBeltElement(this); } } break; case 5: //west output outputOrientation = ConveyorBelt.OutputOrientation.West; transitionGround = TransitionGround.output; countRegular = Physics2D.OverlapBox((Vector2)this.transform.position - new Vector2(1, 0), new Vector2(0.5f, 0.5f), 0, filter, regular); countUnderground = Physics2D.OverlapBox((Vector2)this.transform.position + new Vector2(searchDist / 2.0f, 0), new Vector2(searchDist, 0.5f), 0, filter, underground); if (countRegular >= 1) { IBeltElement temp = regular[0].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { output = temp; temp.SetPrevBeltElement(this); } } for (int i = 0; i < countUnderground; i++) { UndergroundBelt temp; if (underground[i].TryGetComponent(out temp) && (input == null || input.GetPosition().x > temp.GetPosition().x) && //check if one found is closer than current connection (temp.transitionGround == TransitionGround.input && temp.outputOrientation == outputOrientation) //check orientation and that it is output ) { input = temp; input.SetNextBeltElement(this); } } break; case 6: //south input outputOrientation = ConveyorBelt.OutputOrientation.South; transitionGround = TransitionGround.input; countRegular = Physics2D.OverlapBox((Vector2)this.transform.position + new Vector2(0, 1), new Vector2(0.5f, 0.5f), 0, filter, regular); countUnderground = Physics2D.OverlapBox((Vector2)this.transform.position - new Vector2(0, searchDist / 2.0f), new Vector2(0.5f, searchDist), 0, filter, underground); if (countRegular >= 1) { IBeltElement temp = regular[0].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == outputOrientation) { input = temp; temp.SetNextBeltElement(this); } } for (int i = 0; i < countUnderground; i++) { UndergroundBelt temp; if (underground[i].TryGetComponent(out temp) && (output == null || output.GetPosition().y < temp.GetPosition().y) && //check if one found is closer than current connection (temp.transitionGround == TransitionGround.output && temp.outputOrientation == outputOrientation) //check orientation and that it is output ) { output = temp; output.SetPrevBeltElement(this); } } break; default: //south output case 7: outputOrientation = ConveyorBelt.OutputOrientation.South; transitionGround = TransitionGround.output; countRegular = Physics2D.OverlapBox((Vector2)this.transform.position - new Vector2(0, 1), new Vector2(0.5f, 0.5f), 0, filter, regular); countUnderground = Physics2D.OverlapBox((Vector2)this.transform.position + new Vector2(0, searchDist / 2.0f), new Vector2(0.5f, searchDist), 0, filter, underground); if (countRegular >= 1) { IBeltElement temp = regular[0].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { output = temp; temp.SetPrevBeltElement(this); } } for (int i = 0; i < countUnderground; i++) { UndergroundBelt temp; if (underground[i].TryGetComponent(out temp) && (input == null || input.GetPosition().y < temp.GetPosition().y) && //check if one found is closer than current connection (temp.transitionGround == TransitionGround.input && temp.outputOrientation == outputOrientation) //check orientation and that it is output ) { input = temp; input.SetNextBeltElement(this); } } break; } GetComponent <SpriteRenderer>().sprite = AnimationPropertiesPool.Instance.rotatedSpriteProperties[rotatedSpritePropertyId].sprites[rotation]; }