public bool GetPossibleWay(Vector2 point, TileSides from, out Vector2 nextPoint) { List <TileSides> possibleWays = railWayMap[(int)point.x, (int)point.y].GetPossibleSidesToWay(from); //define one of sides nextPoint = point; if (possibleWays.Count == 0) { return(false); } int randomSideIndex = Random.Range(0, possibleWays.Count); TileSides side = possibleWays[randomSideIndex]; switch (side) { case TileSides.Top: nextPoint += Vector2.up; break; case TileSides.Right: nextPoint += Vector2.right; break; case TileSides.Bottom: nextPoint += Vector2.down; break; case TileSides.Left: nextPoint += Vector2.left; break; } return(true); }
int FindGeneralPoints(List <SidesLink> links) { int val = 0; for (int i = 0; i < links.Count; i++) { TileSides side = links[0].begin; for (int j = i + 1; j < links.Count; j++) { if (side == links[j].begin) { val++; } if (side == links[j].end) { val++; } } side = links[0].end; for (int j = i + 1; j < links.Count; j++) { if (side == links[j].begin) { val++; } if (side == links[j].end) { val++; } } } return(val); }
void TrainTurn(Train train) { if (!train.IsTrainEnable()) { return; } Vector2 currentPos = train.GetPositionHead(); TileSides from = train.GetFromArrive(); // station halt /*int halt = railContr.GetHaltTimeByPosition(currentPos); * if(halt > 0) { * * }*/ Vector2 nextPos; if (railContr.GetPossibleWay(currentPos, from, out nextPos)) { train.MoveToPosition(nextPos); } else { Debug.Log("Train is in endup."); train.SetEnable(false); } }
// -------------- Initialize ------------------ public Train(TrainElement head, Color color, TileSides from, int rate) { this.head = head; trainColor = color; fromArrive = from; isEnable = true; clockPerTurn = rate; head.SetColor(color); }
public void MoveToPosition(Vector2 position) { Vector2 offset = head.Position - position; TileSides newFromArrive = CalculateSideByOffset(offset); if (newFromArrive != fromArrive) { head.SetReadyToRotation(newFromArrive); } fromArrive = newFromArrive; head.MoveForwardChainPosition(position); }
TileSides GetEmptySide(List <SidesLink> links) { for (int i = 0; i < 4; i++) { TileSides side = (TileSides)i; int count = 0; for (int j = 0; j < links.Count; j++) { if (side == links[j].begin || side == links[j].end) { count++; break; } } if (count == 0) { return(side); } } return(TileSides.None); }
int ChooseDegreeByInitSide(TileSides initSide, TileSides currentSides) { int sideIndex = (int)initSide; if (currentSides == (TileSides)((sideIndex + 1) & 3)) { return(1); } else if (currentSides == (TileSides)((sideIndex + 2) & 3)) { return(2); } else if (currentSides == (TileSides)((sideIndex + 3) & 3)) { return(3); } else { return(0); } }
int GetRotateDegreeByLinks(List <SidesLink> links, TypeRailWay type) { int degree = 0; int offsetDegree = 90; TileSides generalSide; switch (type) { case TypeRailWay.Horizontal: case TypeRailWay.Vertical: case TypeRailWay.Cross: break; case TypeRailWay.Curve: // init state = left TileSides righterSide = GetRighterSide(links[0]); degree = ChooseDegreeByInitSide(TileSides.Left, righterSide); break; case TypeRailWay.TwoCurve: // init state = bottom generalSide = GetGeneralSide(links[0], links[1]); degree = ChooseDegreeByInitSide(TileSides.Bottom, generalSide); break; case TypeRailWay.Endup: // init state = bottom degree = ChooseDegreeByInitSide(TileSides.Bottom, links[0].begin); break; case TypeRailWay.OneInOneRight: // init state = top case TypeRailWay.OneInOneLeft: generalSide = GetGeneralSide(links[0], links[1]); degree = ChooseDegreeByInitSide(TileSides.Top, generalSide); break; case TypeRailWay.ThreeCurve: // init state = left TileSides emptySides = GetEmptySide(links); degree = ChooseDegreeByInitSide(TileSides.Left, emptySides); break; } return(degree * offsetDegree); }
public List <TileSides> GetPossibleSidesToWay(TileSides from) { List <TileSides> sides = new List <TileSides>(); for (int i = 0; i < links.Count; i++) { if (links[i].begin == links[i].end) { continue; } if (links[i].begin == from) { sides.Add(links[i].end); } else if (links[i].end == from) { sides.Add(links[i].begin); } } return(sides); }
public void SetReadyToRotation(TileSides from) { nextRotation = true; switch (from) { case TileSides.Right: rotateDegree = 0; break; case TileSides.Bottom: rotateDegree = 90; break; case TileSides.Left: rotateDegree = 180; break; case TileSides.Top: rotateDegree = 270; break; } localFromArrive = from; }
public SuburbanTrain(TrainElement head, Color color, TileSides from, int rate) : base(head, color, from, rate) { }
public void AddNeighbor(TileSides side, PreciseTile tile) { Neighbors[(int)side] = tile; CalcAutoTileID(); }
TypeRailWay ChooseSpriteByLinks(List <SidesLink> links) { if (links.Count == 1) { if (FindEndUp(links)) { // одна связь, точки к самой себе return(TypeRailWay.Endup); } else if (CalculateLink(links[0]) % 2 == 1) { //одна связь, сумма индексов сторон нечётная return(TypeRailWay.Curve); } else if (CalculateLink(links[0]) % 2 == 0) { if ((int)links[0].begin % 2 == 1 && (int)links[0].end % 2 == 1) { //одна связь, сумма индексов сторон чётная, сами индексы сторон чётные return(TypeRailWay.Horizontal); } else if ((int)links[0].begin % 2 == 0 && (int)links[0].end % 2 == 0) { //одна связь, сумма индексов сторон чётная, сами индексы сторон нечётные return(TypeRailWay.Vertical); } } else { Debug.Log("Error. In ONE link happend some trouble."); } } else if (links.Count == 2 && !FindEndUp(links)) { if (CalculateLink(links[0]) % 2 == 1 && CalculateLink(links[1]) % 2 == 1 && FindGeneralPoints(links) == 1) { //две связи, сумма индексов сторон у связей нечётная, одна общая стороная у связей return(TypeRailWay.TwoCurve); } else if (CalculateLink(links[0]) % 2 != CalculateLink(links[1]) % 2 && FindGeneralPoints(links) == 1) { //две связи, сумма индексов сторон у связей разная по чётности, одна общая стороная у связей //определяем левосторонняя или правосторонняя эта связь, дабы облегчить себе жизнь на этапе выбора правильной ротации TileSides generalSides = GetGeneralSide(links[0], links[1]); TileSides righerSide = CalculateLink(links[0]) % 2 == 1 ? GetRighterSide(links[0]) : GetRighterSide(links[1]); if (generalSides == righerSide) { return(TypeRailWay.OneInOneLeft); } else { return(TypeRailWay.OneInOneRight); } } else if (CalculateLink(links[0]) % 2 == 0 && CalculateLink(links[1]) % 2 == 0 && FindGeneralPoints(links) == 0) { //две связи, сумма индексов сторон у связей чётная, ни одной общей стороны у связей return(TypeRailWay.Cross); } else { Debug.Log("Error. In TWO link happend some trouble."); } } else if (links.Count == 3 && !FindEndUp(links)) { if (FindGeneralPoints(links) == 3) { //три связи, три общих точки у связей return(TypeRailWay.ThreeCurve); } else { Debug.Log("Error. In THREE link happend some trouble."); } } else { Debug.Log("Error. Uncorrect links input count."); } return(TypeRailWay.None); }
public ExpressTrain(TrainElement head, Color color, TileSides from, int rate) : base(head, color, from, rate) { }