public bool AddDoubleEdgedPipe(Coordinate position, EdgeType firstEdge, EdgeType secondEdge) { var pipe = new DoubleEdgedPipe(firstEdge, secondEdge); var block = GetBlock(position); if (block != null) { if (!CheckIfDoubleEdgeAlreadyExists(pipe, block)) { if (!(block.HasShipComponent())) { var intersectingPipe = HasIntersectingPipes(block, pipe); if (intersectingPipe != null) { blueprint.PlaceShipComponent(position, emptyShipComponentFactory.Create(block)); TransformDoubleEdgedPipeIntoConnectingPipe(position, intersectingPipe); TransformDoubleEdgedPipeIntoConnectingPipe(position, pipe); ClearPipes(position, block.PipesWithBothEdges); } else { blueprint.PlacePipe(position, pipe); } return(true); } else { TransformDoubleEdgedPipeIntoConnectingPipe(position, pipe); return(true); } } return(false); } return(false); }
public bool DeleteDoubleEdgedPipe(Coordinate position, DoubleEdgedPipe pipe) { var block = GetBlock(position); if (block != null) { if (CheckIfDoubleEdgeAlreadyExists(pipe, block)) { var p = GetDoubleEdgedPipe(pipe, block); blueprint.RemovePipe(position, p); return(true); } if (CheckIfDoubleEdgedPipeCanBeComposedOfTwoConnectingPipes(pipe, block)) { var list = GetConnectingPipesThatComposeTwoEdgedPipe(pipe, block); foreach (var p in list) { blueprint.RemovePipe(position, p); } return(true); } } return(false); }
public void PlacePipe(Coordinate position, DoubleEdgedPipe pipe) { blocks.Get(position).AddPipe(pipe); foreach (var observer in observers) { observer.DoubleEdgePipeAdded(this, position, pipe); } }
public void RemovePipe(Coordinate position, DoubleEdgedPipe pipe) { blocks.Get(position).DeletePipe(pipe); foreach (var observer in observers) { observer.DoubleEdgePipeDeleted(this, position, pipe); } }
private DoubleEdgedPipe GetDoubleEdgedPipe(DoubleEdgedPipe pipe, IConstBlock block) { foreach (var p in block.PipesWithBothEdges) { if (p.IsEqualTo(pipe)) { return(p); } } return(null); }
private List <ConnectingPipe> GetConnectingPipesThatComposeTwoEdgedPipe(DoubleEdgedPipe pipe, IConstBlock block) { var list = new List <ConnectingPipe>(); foreach (var connectingPipe in block.PipesWithOneEdge) { if (connectingPipe.Edge == pipe.FirstEdge || connectingPipe.Edge == pipe.SecondEdge) { list.Add(connectingPipe); } } return(list); }
private bool DoPipesIntersect(DoubleEdgedPipe pipe1, DoubleEdgedPipe pipe2) { if ((pipe1.FirstEdge == EdgeType.UP && pipe1.SecondEdge == EdgeType.DOWN) || (pipe1.FirstEdge == EdgeType.DOWN && pipe1.SecondEdge == EdgeType.UP)) { if ((pipe2.FirstEdge == EdgeType.LEFT && pipe2.SecondEdge == EdgeType.RIGHT) || (pipe2.FirstEdge == EdgeType.RIGHT && pipe2.SecondEdge == EdgeType.LEFT)) { return(true); } } if ((pipe1.FirstEdge == EdgeType.LEFT && pipe1.SecondEdge == EdgeType.RIGHT) || (pipe1.FirstEdge == EdgeType.RIGHT && pipe1.SecondEdge == EdgeType.LEFT)) { if ((pipe2.FirstEdge == EdgeType.UP && pipe2.SecondEdge == EdgeType.DOWN) || (pipe2.FirstEdge == EdgeType.DOWN && pipe2.SecondEdge == EdgeType.UP)) { return(true); } } return(false); }
private bool CheckIfDoubleEdgedPipeCanBeComposedOfTwoConnectingPipes(DoubleEdgedPipe pipe, IConstBlock block) { return(block.PipesWithOneEdge.Select(p => p.Edge).ToList().Any(p => p == pipe.FirstEdge) && block.PipesWithOneEdge.Select(p => p.Edge).ToList().Any(p => p == pipe.SecondEdge)); }
private bool CheckIfDoubleEdgeAlreadyExists(DoubleEdgedPipe pipe, IConstBlock block) { return(block.PipesWithBothEdges.Any(p => p.IsEqualTo(pipe))); }
private void TransformDoubleEdgedPipeIntoConnectingPipe(Coordinate position, DoubleEdgedPipe pipe) { AddConnectingPipe(position, pipe.FirstEdge); AddConnectingPipe(position, pipe.SecondEdge); }
private DoubleEdgedPipe HasIntersectingPipes(IConstBlock block, DoubleEdgedPipe pipeToCheck) { return(block.PipesWithBothEdges.FirstOrDefault(pipe => DoPipesIntersect(pipe, pipeToCheck))); }
public void DeletePipe(DoubleEdgedPipe pipe) { pipesWithBothEdges.Remove(pipe); }
public void AddPipe(DoubleEdgedPipe pipe) { pipesWithBothEdges.Add(pipe); }
public bool IsEqualTo(DoubleEdgedPipe pipe) { return((FirstEdge == pipe.FirstEdge && SecondEdge == pipe.SecondEdge) || (FirstEdge == pipe.SecondEdge && SecondEdge == pipe.FirstEdge)); }