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]; }
/// <summary> /// TODO: handle case where position is == (a splitter leading into a splitter) /// </summary> /// <param name="rotation"></param> public void InitializeRotated(int rotation) { rotation = rotation % 4; ContactFilter2D filter = new ContactFilter2D(); filter.useLayerMask = true; filter.layerMask = 1 << LayerMask.NameToLayer("ConveyorBelt"); Collider2D[] resultsTopAndLeft = new Collider2D[3]; Collider2D[] resultsBottomAndRight = new Collider2D[3]; int countTop; int countBottom; int countLeft; int countRight; items = new ItemEntity[maxBufferCapacity]; switch (rotation) { case 0: outputOrientation = ConveyorBelt.OutputOrientation.East; GetComponent <BoxCollider2D>().size = new Vector2(1, 2); //rundown: drop overlapbox for left and right //if a belt is added later, setnextbelt and setprevbelt will take care of properly assigning belts countTop = Physics2D.OverlapBox(this.transform.position + new Vector3(0, 0.5f), new Vector2(2, 0.5f), 0, filter, resultsTopAndLeft); countBottom = Physics2D.OverlapBox(this.transform.position + new Vector3(0, -0.5f), new Vector2(2, 0.5f), 0, filter, resultsBottomAndRight); for (int i = 0; i < countTop; i++) { if (resultsTopAndLeft[i].gameObject == this.gameObject) { continue; } if (resultsTopAndLeft[i].transform.position.x < this.transform.position.x) { IBeltElement temp = resultsTopAndLeft[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == this.outputOrientation) { temp.SetNextBeltElement(this); leftInput = temp; } } else if (resultsTopAndLeft[i].transform.position.x > this.transform.position.x) { IBeltElement temp = resultsTopAndLeft[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { temp.SetPrevBeltElement(this); leftOutput = temp; } } } for (int i = 0; i < countBottom; i++) { if (resultsBottomAndRight[i].gameObject == this.gameObject) { continue; } if (resultsBottomAndRight[i].transform.position.x < this.transform.position.x) { IBeltElement temp = resultsBottomAndRight[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == this.outputOrientation) { temp.SetNextBeltElement(this); rightInput = temp; } } else if (resultsBottomAndRight[i].transform.position.x > this.transform.position.x) { IBeltElement temp = resultsBottomAndRight[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { temp.SetPrevBeltElement(this); rightOutput = temp; } } } break; case 1: outputOrientation = ConveyorBelt.OutputOrientation.North; GetComponent <BoxCollider2D>().size = new Vector2(2, 1); countLeft = Physics2D.OverlapBox(this.transform.position + new Vector3(-0.5f, 0), new Vector2(0.5f, 2), 0, filter, resultsTopAndLeft); countRight = Physics2D.OverlapBox(this.transform.position + new Vector3(0.5f, 0), new Vector2(0.5f, 2), 0, filter, resultsBottomAndRight); for (int i = 0; i < countLeft; i++) { if (resultsTopAndLeft[i].gameObject == this.gameObject) { continue; } if (resultsTopAndLeft[i].transform.position.y < this.transform.position.y) { IBeltElement temp = resultsTopAndLeft[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == this.outputOrientation) { temp.SetNextBeltElement(this); leftInput = temp; } } else if (resultsTopAndLeft[i].transform.position.y > this.transform.position.y) { IBeltElement temp = resultsTopAndLeft[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { temp.SetPrevBeltElement(this); leftOutput = temp; } } } for (int i = 0; i < countRight; i++) { if (resultsBottomAndRight[i].gameObject == this.gameObject) { continue; } if (resultsBottomAndRight[i].transform.position.y < this.transform.position.y) { IBeltElement temp = resultsBottomAndRight[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == this.outputOrientation) { temp.SetNextBeltElement(this); rightInput = temp; } } else if (resultsBottomAndRight[i].transform.position.y > this.transform.position.y) { IBeltElement temp = resultsBottomAndRight[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { temp.SetPrevBeltElement(this); rightOutput = temp; } } } break; case 2: outputOrientation = ConveyorBelt.OutputOrientation.West; GetComponent <BoxCollider2D>().size = new Vector2(1, 2); countTop = Physics2D.OverlapBox(this.transform.position + new Vector3(0, 0.5f), new Vector2(2, 0.5f), 0, filter, resultsTopAndLeft); countBottom = Physics2D.OverlapBox(this.transform.position + new Vector3(0, -0.5f), new Vector2(2, 0.5f), 0, filter, resultsBottomAndRight); for (int i = 0; i < countTop; i++) { if (resultsTopAndLeft[i].gameObject == this.gameObject) { continue; } if (resultsTopAndLeft[i].transform.position.x > this.transform.position.x) { IBeltElement temp = resultsTopAndLeft[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == this.outputOrientation) { temp.SetNextBeltElement(this); leftInput = temp; } } else if (resultsTopAndLeft[i].transform.position.x < this.transform.position.x) { IBeltElement temp = resultsTopAndLeft[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { temp.SetPrevBeltElement(this); leftOutput = temp; } } } for (int i = 0; i < countBottom; i++) { if (resultsBottomAndRight[i].gameObject == this.gameObject) { continue; } if (resultsBottomAndRight[i].transform.position.x > this.transform.position.x) { IBeltElement temp = resultsBottomAndRight[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == this.outputOrientation) { temp.SetNextBeltElement(this); rightInput = temp; } } else if (resultsBottomAndRight[i].transform.position.x < this.transform.position.x) { IBeltElement temp = resultsBottomAndRight[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { temp.SetPrevBeltElement(this); rightOutput = temp; } } } break; default: case 3: outputOrientation = ConveyorBelt.OutputOrientation.South; GetComponent <BoxCollider2D>().size = new Vector2(2, 1); countLeft = Physics2D.OverlapBox(this.transform.position + new Vector3(-0.5f, 0), new Vector2(0.5f, 2), 0, filter, resultsTopAndLeft); countRight = Physics2D.OverlapBox(this.transform.position + new Vector3(0.5f, 0), new Vector2(0.5f, 2), 0, filter, resultsBottomAndRight); for (int i = 0; i < countLeft; i++) { if (resultsTopAndLeft[i].gameObject == this.gameObject) { continue; } if (resultsTopAndLeft[i].transform.position.y > this.transform.position.y) { IBeltElement temp = resultsTopAndLeft[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == this.outputOrientation) { temp.SetNextBeltElement(this); leftInput = temp; } } else if (resultsTopAndLeft[i].transform.position.y < this.transform.position.y) { IBeltElement temp = resultsTopAndLeft[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { temp.SetPrevBeltElement(this); leftOutput = temp; } } } for (int i = 0; i < countRight; i++) { if (resultsBottomAndRight[i].gameObject == this.gameObject) { continue; } if (resultsBottomAndRight[i].transform.position.y > this.transform.position.y) { IBeltElement temp = resultsBottomAndRight[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == this.outputOrientation) { temp.SetNextBeltElement(this); rightInput = temp; } } else if (resultsBottomAndRight[i].transform.position.y < this.transform.position.y) { IBeltElement temp = resultsBottomAndRight[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { temp.SetPrevBeltElement(this); rightOutput = temp; } } } break; } GetComponent <SpriteRenderer>().sprite = AnimationPropertiesPool.Instance.rotatedSpriteProperties[rotatedSpritePropertyId].sprites[rotation]; }
public void InitializeRotated(int rotation) { rotation = rotation % 4; items = new ItemEntity[inputBuffer]; int countBelts = 0; int countMachines = 0; ContactFilter2D filter = new ContactFilter2D(); filter.useLayerMask = true; filter.layerMask = 1 << LayerMask.NameToLayer("ConveyorBelt"); ContactFilter2D machineFilter = new ContactFilter2D(); machineFilter.useLayerMask = true; machineFilter.layerMask = 1 << LayerMask.NameToLayer("Damageable"); Collider2D[] resultsBelts = new Collider2D[3]; Collider2D[] resultsMachines = new Collider2D[3]; switch (rotation) { case 0: //east output outputOrientation = ConveyorBelt.OutputOrientation.East; countBelts = Physics2D.OverlapBox(this.transform.position, new Vector2(2, 0.25f), 0, filter, resultsBelts); countMachines = Physics2D.OverlapBox(this.transform.position, new Vector2(2, 0.25f), 0, machineFilter, resultsMachines); for (int i = 0; i < countBelts; i++) { if (resultsBelts[i].gameObject == this.gameObject) { continue; } if (resultsBelts[i].transform.position.x < this.transform.position.x) { IBeltElement temp = resultsBelts[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == ConveyorBelt.OutputOrientation.East) { inputBelt = temp; temp.SetNextBeltElement(this); } } else if (resultsBelts[i].transform.position.x > this.transform.position.x) { IBeltElement temp = resultsBelts[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { outputBelt = temp; outputBelt.SetPrevBeltElement(this); } } } for (int i = 0; i < countMachines; i++) { if (resultsMachines[i].transform.position.x < this.transform.position.x) { resultsMachines[i].gameObject.TryGetComponent(out inputInventory); } else if (resultsMachines[i].transform.position.x > this.transform.position.x) { resultsMachines[i].gameObject.TryGetComponent(out outputInventory); } } break; case 1: //north output outputOrientation = ConveyorBelt.OutputOrientation.North; countBelts = Physics2D.OverlapBox(this.transform.position, new Vector2(0.25f, 2), 0, filter, resultsBelts); countMachines = Physics2D.OverlapBox(this.transform.position, new Vector2(0.25f, 2), 0, machineFilter, resultsMachines); for (int i = 0; i < countBelts; i++) { if (resultsBelts[i].gameObject == this.gameObject) { continue; } if (resultsBelts[i].transform.position.y < this.transform.position.y) { IBeltElement temp = resultsBelts[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == ConveyorBelt.OutputOrientation.North) { inputBelt = temp; temp.SetNextBeltElement(this); } } else if (resultsBelts[i].transform.position.y > this.transform.position.y) { IBeltElement temp = resultsBelts[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { outputBelt = temp; outputBelt.SetPrevBeltElement(this); } } } for (int i = 0; i < countMachines; i++) { if (resultsMachines[i].transform.position.y < this.transform.position.y) { resultsMachines[i].gameObject.TryGetComponent(out inputInventory); } else if (resultsMachines[i].transform.position.y > this.transform.position.y) { resultsMachines[i].gameObject.TryGetComponent(out outputInventory); } } break; case 2: //west output outputOrientation = ConveyorBelt.OutputOrientation.West; countBelts = Physics2D.OverlapBox(this.transform.position, new Vector2(2, 0.25f), 0, filter, resultsBelts); countMachines = Physics2D.OverlapBox(this.transform.position, new Vector2(2, 0.25f), 0, machineFilter, resultsMachines); for (int i = 0; i < countBelts; i++) { if (resultsBelts[i].gameObject == this.gameObject) { continue; } if (resultsBelts[i].transform.position.x > this.transform.position.x) { IBeltElement temp = resultsBelts[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == ConveyorBelt.OutputOrientation.West) { inputBelt = temp; temp.SetNextBeltElement(this); } } else if (resultsBelts[i].transform.position.x < this.transform.position.x) { IBeltElement temp = resultsBelts[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { outputBelt = temp; outputBelt.SetPrevBeltElement(this); } } } for (int i = 0; i < countMachines; i++) { if (resultsMachines[i].transform.position.x > this.transform.position.x) { resultsMachines[i].gameObject.TryGetComponent(out inputInventory); } else if (resultsMachines[i].transform.position.x < this.transform.position.x) { resultsMachines[i].gameObject.TryGetComponent(out outputInventory); } } break; default: case 3: //south output outputOrientation = ConveyorBelt.OutputOrientation.South; countBelts = Physics2D.OverlapBox(this.transform.position, new Vector2(0.25f, 2), 0, filter, resultsBelts); countMachines = Physics2D.OverlapBox(this.transform.position, new Vector2(0.25f, 2), 0, machineFilter, resultsMachines); for (int i = 0; i < countBelts; i++) { if (resultsBelts[i].gameObject == this.gameObject) { continue; } if (resultsBelts[i].transform.position.y > this.transform.position.y) { IBeltElement temp = resultsBelts[i].GetComponent <IBeltElement>(); if (temp.GetOutputOrientation() == ConveyorBelt.OutputOrientation.South) { inputBelt = temp; temp.SetNextBeltElement(this); } } else if (resultsBelts[i].transform.position.y < this.transform.position.y) { IBeltElement temp = resultsBelts[i].GetComponent <IBeltElement>(); if (temp.CanConnectToBeltElement(this)) { outputBelt = temp; outputBelt.SetPrevBeltElement(this); } } } for (int i = 0; i < countMachines; i++) { if (resultsMachines[i].transform.position.y > this.transform.position.y) { resultsMachines[i].gameObject.TryGetComponent(out inputInventory); } else if (resultsMachines[i].transform.position.y < this.transform.position.y) { resultsMachines[i].gameObject.TryGetComponent(out outputInventory); } } break; } this.GetComponent <SpriteRenderer>().sprite = AnimationPropertiesPool.Instance.rotatedSpriteProperties[rotateSpritePropertyId].sprites[rotation]; }