public static IGPos GetDesiredPosition(Direction dir, IGPos position) { switch (dir) { case Direction.UP: { return(new IGPos(position.X + 1, position.Y - 1, position.Layer)); } case Direction.DOWN: { return(new IGPos(position.X - 1, position.Y + 1, position.Layer)); } case Direction.LEFT: { return(new IGPos(position.X - 1, position.Y, position.Layer)); } case Direction.RIGHT: { return(new IGPos(position.X + 1, position.Y, position.Layer)); } } return(new IGPos(-1, -1, -1)); }
public virtual Tile CanMove(Direction dir, IGPos pos) { if (CheckSkill("SKILL_MOVE") != null) { return(Level.CheckPlatform(Common.GetDesiredPosition(dir, pos), pos)); } return(null); }
public Tile CanMove(Direction dir, Tile p) { if (CheckSkill("SKILL_MOVE") != null) { IGPos pos = new IGPos(p.x, p.y, p.layer); return(Level.CheckPlatform(Common.GetDesiredPosition(dir, pos), pos)); } return(null); }
/// <summary> /// ctor /// </summary> public Entity() { r = new Random(); lives = 1; speed = 5; chasingTarget = new IGPos(0, 0, 0); Game.I.eventManager.OnTurnEnd += eventManager_OnTurnEnd; Skills = new List <ISkill>(); InitGfx(); AssignSkills(); }
public Direction GetDirection(IGPos iGPos, Tile px) { if (iGPos.X + 1 == px.x && iGPos.Y - 1 == px.y) { return(Direction.UP); } if (iGPos.X - 1 == px.x && iGPos.Y + 1 == px.y) { return(Direction.DOWN); } if (iGPos.X - 1 == px.x && iGPos.Y == px.y) { return(Direction.LEFT); } if (iGPos.X + 1 == px.x && iGPos.Y == px.y) { return(Direction.RIGHT); } return(Direction.UP); }
/// <summary> /// Highlights the path. /// </summary> /// <returns>The path.</returns> /// <param name="from">From.</param> /// <param name="to">To.</param> public List <Tile> HighlightPath(IGPos from, IGPos to) { ClearHighlightedPath(highlightedPlatforms); List <Tile> pth = FindPath(from, to); if (pth != null) { foreach (var platform in pth) { if (platform != null) { platform.isHighLighted = true; } } } else { Console.WriteLine("Impossible to highlight path for: {0} at position {1}", type, position); } return(pth); }
public static Tile CheckPlatform(IGPos desiredPosition, IGPos position) { //search for platform on same layer Tile pTile = Game.I.level.GetPlatform(desiredPosition.X, desiredPosition.Y, position.Layer, true); if (pTile == null) { // search for platform on higher layer pTile = Game.I.level.GetPlatform(desiredPosition.X, desiredPosition.Y, position.Layer + 1, true); if (pTile == null) { // search for platform on lower layer pTile = Game.I.level.GetPlatform(desiredPosition.X, desiredPosition.Y, position.Layer - 1, true); } } if (pTile == null) { //we can have multiple elevators.... List <Tile> pElevatorPlatforms = Game.I.level.GetElevators(desiredPosition.X, desiredPosition.Y); if ((pElevatorPlatforms != null) && (pElevatorPlatforms.Count > 0)) { //je to dostatecne blizko tomu co stojim? Tile ppp = pElevatorPlatforms[0]; if (ppp.elevator.Current == position.Layer) { pTile = ppp; } if (ppp.elevator.Current + 1 == position.Layer) { pTile = ppp; } if (ppp.elevator.Current - 1 == position.Layer) { pTile = ppp; } } } // if no platform we cannot simply go there if (pTile == null) { return(null); } //we have platform... // is it elevator? Can we get on? if (pTile.elevator != null) { if (pTile.elevator.Current == position.Layer) { return(pTile); } if (pTile.elevator.Current == position.Layer - 1) { return(pTile); } if (pTile.elevator.Current == position.Layer + 1) { return(pTile); } } // is platform moving? Can we get on? if (pTile.moving != null) { if (!(pTile.moving.Current.X == desiredPosition.X && pTile.moving.Current.Y == desiredPosition.Y)) { //platform is somewhere else...we cannot get on. return(null); } } // if we are here no other universal obstackle is in place return(pTile); }
public Tile GetPlatform(IGPos pos) { return(GetPlatform(pos.X, pos.Y, pos.Layer)); }
/// <summary> /// Checks the surroundings. /// </summary> /// <returns>The surroundings.</returns> /// <param name="qTodo">Q todo.</param> /// <param name="qProcessed">Q processed.</param> /// <param name="to">To.</param> /// <param name="value">Value.</param> private int CheckSurroundings(Dictionary <int, List <Tile> > qTodo, Queue <Tuple <int, Tile> > qProcessed, IGPos to, int value) { //add new layer qTodo.Add(value + 1, new List <Tile>()); // Dejme tomu, že se potřebujeme dostat z bodu S do bodu F. Do fronty tedy zapíšeme bod S a dáme mu hodnotu 0. // Fronta : [4;7] List <Tile> lp = qTodo[value]; foreach (var platform in lp) { if (platform == null) { Console.WriteLine("problem"); } // mark as processed qProcessed.Enqueue(new Tuple <int, Tile>(value, platform)); if (platform != null) { platform.pathfindingValues[id] = value; // Vyjmeme 1. bod ve frontě (tedy [4;7]) a pokud je v jednom ze 4 směrů volno, // uděláme tam hodnotu o jednu větší, než je bod sám (bod S je nula, takže tam napíšeme jedničku). // Já jsem si zvolil, že první udělám tu nahoře, pak nalevo, dole a poslední tu vpravo. // Políčka, na které jsme položili jedničky, si zapíšeme do fronty. // Fronta : [4;6][3;7][4;8][5;7] Tile pUp = CanMove(Direction.UP, platform); Tile pDown = CanMove(Direction.DOWN, platform); Tile pRight = CanMove(Direction.RIGHT, platform); Tile pLeft = CanMove(Direction.LEFT, platform); Tile pSelected = null; if ((pUp != null) && (pUp.GetPathFindingValue(id) == -1)) { pUp.pathfindingValues[id] = value; qTodo[value + 1].Add(pUp); pSelected = pUp; if (pSelected.x == to.X && pSelected.y == to.Y && pSelected.layer == to.Layer) { return(value); } } if ((pDown != null) && (pDown.GetPathFindingValue(id) == -1)) { pDown.pathfindingValues[id] = value; qTodo[value + 1].Add(pDown); pSelected = pDown; if (pSelected.x == to.X && pSelected.y == to.Y && pSelected.layer == to.Layer) { return(value); } } if ((pRight != null) && (pRight.GetPathFindingValue(id) == -1)) { pRight.pathfindingValues[id] = value; qTodo[value + 1].Add(pRight); pSelected = pRight; if (pSelected.x == to.X && pSelected.y == to.Y && pSelected.layer == to.Layer) { return(value); } } if ((pLeft != null) && (pLeft.GetPathFindingValue(id) == -1)) { pLeft.pathfindingValues[id] = value; qTodo[value + 1].Add(pLeft); pSelected = pLeft; if (pSelected.x == to.X && pSelected.y == to.Y && pSelected.layer == to.Layer) { return(value); } } } } // for safety if (value >= MAX) { return(value); } if (qTodo.Count != 0) { return(CheckSurroundings(qTodo, qProcessed, to, ++value)); } // Opět vyjmeme 1. bod ve frontě ([4;6]) a systémem nahoru, doleva, dolů, doprava napíšeme do políček, // které jsou prázdné, číslo, které je zase o jednu větší, než to z fronty (takže 2). Body opět přidám do fronty. // Fronta : [3;7][4;8][5;7][5;6] // Toto se stále opakuje... return(value); }
/// <summary> /// Finds the path. /// </summary> /// <returns>The path.</returns> /// <param name="from">From.</param> /// <param name="to">To.</param> public List <Tile> FindPath(IGPos from, IGPos to) { // // http://www.devbook.cz/algoritmus-sireni-do-sirky-vlna-hledani-cesty-v-bludisti Dictionary <int, List <Tile> > qTodo = new Dictionary <int, List <Tile> >(); List <Tile> lp = new List <Tile>(); Tile ppp = Game.I.level.GetPlatform(from); if (ppp == null) { return(null); } lp.Add(ppp); qTodo.Add(1, lp); //List< t<int, List<Tile>> qTodo = new //Queue<Tuple<int, Tile>> qTodo = new Queue<Tuple<int, Tile>>(); //qTodo.Enqueue(new Tuple<int, Tile>(0,Game.I.level.GetPlatform(from))); Queue <Tuple <int, Tile> > qProcessed = new Queue <Tuple <int, Tile> >(); int depth = 0; int maxValue = CheckSurroundings(qTodo, qProcessed, to, ++depth); List <Tuple <int, Tile> > processedList = qProcessed.ToList(); List <Tile> lPlatformsOnPath = new List <Tile>(); lPlatformsOnPath.Add(Game.I.level.GetPlatform(to)); // ...dokud není kolem políčka, které jsme sebrali z fronty, políčko F. // Teď je krásně vidět sestupně očíslovaná cesta od políčka F k políčku S. // Nyní stačí jen projít se od F k S technikou: jestli je nahoře nebo vlevo nebo dole nebo // vpravo číslo o jednu menší, než to, na kterém stojím. Čísla si ukládám obráceně do fronty, // abych měl souřadnici políčka F na konci. // // Tak a je to, ve frontě máme zapsanou cestu bod po bodu, jak se dostat od bodu S do bodu F. int processingValue = maxValue + 1; while (true) { processingValue--; Tile previousTile = lPlatformsOnPath[lPlatformsOnPath.Count - 1]; if (previousTile != null) { var xUp = (from i in processedList where i.Item1 == processingValue && i.Item2.x == previousTile.x + 1 && i.Item2.y == previousTile.y - 1 select i ).FirstOrDefault(); var xDown = (from i in processedList where i.Item1 == processingValue && i.Item2.x == previousTile.x - 1 && i.Item2.y == previousTile.y + 1 select i ).FirstOrDefault(); var xLeft = (from i in processedList where i.Item1 == processingValue && i.Item2.x == previousTile.x - 1 && i.Item2.y == previousTile.y select i ).FirstOrDefault(); var xRight = (from i in processedList where i.Item1 == processingValue && i.Item2.x == previousTile.x + 1 && i.Item2.y == previousTile.y select i ).FirstOrDefault(); if (xUp != null) { lPlatformsOnPath.Add(xUp.Item2); } else if (xDown != null) { lPlatformsOnPath.Add(xDown.Item2); } else if (xLeft != null) { lPlatformsOnPath.Add(xLeft.Item2); } else if (xRight != null) { lPlatformsOnPath.Add(xRight.Item2); } } if (processingValue == 0) { break; } } return(lPlatformsOnPath); }