public static List<Tile> FindPath(Tile originTile, Tile destinationTile, Vector2[] occupied) {
		List<Tile> closed = new List<Tile>();
		List<TilePath> open = new List<TilePath>();
		
		TilePath originPath = new TilePath();
		originPath.addTile(originTile);
		
		open.Add(originPath);
		
		while (open.Count > 0) {
			//open = open.OrderBy(x => x.costOfPath).ToList();
			TilePath current = open[0];
			open.Remove(open[0]);
			
			if (closed.Contains(current.lastTile)) {
				continue;
			} 
			if (current.lastTile == destinationTile) {
				current.listOfTiles.Distinct();
				current.listOfTiles.Remove(originTile);
				return current.listOfTiles;
			}
			
			closed.Add(current.lastTile);
			
			foreach (Tile t in current.lastTile.neighbors) {
				if (t.impassible || occupied.Contains(t.gridPosition)) continue;
				TilePath newTilePath = new TilePath(current);
				newTilePath.addTile(t);
				open.Add(newTilePath);
			}
		}
		return null;
	}
    public static List<Tile> FindHighlight(Tile originTile, int movementPoints, Vector2[] occupied)
    {
        List<Tile> closed = new List<Tile>();
        List<TilePath> open = new List<TilePath>();

        TilePath originPath = new TilePath();
        originPath.addTile(originTile);

        open.Add(originPath);

        while (open.Count > 0) {
            TilePath current = open[0];
            open.Remove(open[0]);

            if (closed.Contains(current.lastTile)) {
                continue;
            }
            if (current.costOfPath > movementPoints + 1) {
                continue;
            }

            closed.Add(current.lastTile);

            foreach (Tile t in current.lastTile.neighbours) {
                if (t.impassible || occupied.Contains(t.gridPosition)) continue;
                TilePath newTilePath = new TilePath(current);
                newTilePath.addTile(t);
                open.Add(newTilePath);
            }
        }
        closed.Remove(originTile);
        closed.Distinct();
        return closed;
    }
Beispiel #3
0
 /// <summary>
 /// Checks whether this Input is active at this time / receiving input at this time.
 /// </summary>
 /// <param name="currenTime">The current time along the trigger</param>
 /// <returns>True if active, false otherwise.</returns>
 public void Update(float currentTime)
 {
     if (Range.Contains(currentTime))
     {
         if (State != InputState.Active)
         {
             State = InputState.Ready;
         }
     }
     else
     {
         State = InputState.Inactive;
     }
 }
    public static List<Tile> FindHighlight(Tile originTile, int movementPoints, Vector2[] occupied, bool staticRange, bool dontremoveorigin)
    {
        List<Tile> closed = new List<Tile>();
        List<TilePath> open = new List<TilePath>();

        TilePath originPath = new TilePath();
        if (staticRange) originPath.addStaticTile(originTile);
        else originPath.addTile(originTile);

        open.Add(originPath);

        while (open.Count > 0) {
            TilePath current = open[0];
            open.Remove(open[0]);

            if (closed.Contains(current.lastTile)) {
                continue;
            }
            if (current.costOfPath > movementPoints + 1.5f) {
                continue;
            }

        closed.Add(current.lastTile);

            foreach (Tile t in current.lastTile.neighbors) {

                if (t.impassible ||
                    occupied.Contains(t.gridPosition)||
                    (GameManager.instance.GetComponent<GameManager>(). playerTurns[GameManager.instance.GetComponent<GameManager>().PlayerTurnIndex].GetPhalanx()==true&&t.frontLiners<16)) continue;
                TilePath newTilePath = new TilePath(current);
                if (staticRange) newTilePath.addStaticTile(t);
                else newTilePath.addTile(t);
                open.Add(newTilePath);
            }
        }

        closed.Remove(originTile);
        closed.Distinct();

        if (dontremoveorigin && !occupied.Contains(originTile.gridPosition))
        closed.Add(originTile);
        return closed;
    }
Beispiel #5
0
        private static IEnumerable <Tuple <Vector2, Vector2> > FindRoute(Vector2 startingBuilding,
                                                                         Vector2 destinationBuilding, IEnumerable <Tuple <Vector2, Vector2> > roads)
        {
            List <Tuple <Vector2, Vector2> > finalPath = new List <Tuple <Vector2, Vector2> >();

            //int roadsNumber = roads.Count() + 1;
            Vector2[] nodes = new Vector2[2500];
            bool[,] matrix = new bool[2500, 2500];
            int cnt = 0;

            //set everything in matrix to false
            for (int i = 0; i < nodes.Length; i++)
            {
                for (int j = 0; j < nodes.Length; j++)
                {
                    matrix[i, j] = false;
                }
            }

            // generate adjacency matrix
            foreach (Tuple <Vector2, Vector2> road in roads)
            {
                if (!nodes.Contains(road.Item1) && !nodes.Contains(road.Item2))
                {
                    nodes[cnt]       = road.Item1;
                    matrix[cnt, cnt] = true;
                    cnt++;
                    nodes[cnt]           = road.Item2;
                    matrix[cnt, cnt]     = true;
                    matrix[cnt - 1, cnt] = true;
                    matrix[cnt, cnt - 1] = true;
                    cnt++;
                }
                else if (nodes.Contains(road.Item1) && !nodes.Contains(road.Item2))
                {
                    nodes[cnt]       = road.Item2;
                    matrix[cnt, cnt] = true;
                    int index = Array.IndexOf(nodes, road.Item1);
                    matrix[index, cnt] = true;
                    matrix[cnt, index] = true;
                    cnt++;
                }
                else if (!nodes.Contains(road.Item1) && nodes.Contains(road.Item2))
                {
                    nodes[cnt]       = road.Item1;
                    matrix[cnt, cnt] = true;
                    int index = Array.IndexOf(nodes, road.Item2);
                    matrix[index, cnt] = true;
                    matrix[cnt, index] = true;
                    cnt++;
                }
                else if (nodes.Contains(road.Item1) && nodes.Contains(road.Item2))
                {
                    int index1 = Array.IndexOf(nodes, road.Item1);
                    int index2 = Array.IndexOf(nodes, road.Item2);
                    matrix[index1, index2] = true;
                    matrix[index2, index1] = true;
                }
            }

            int          startIndex = Array.FindIndex(nodes, v => v.Equals(startingBuilding));    // index of startinghouse
            int          endIndex   = Array.FindIndex(nodes, v => v.Equals(destinationBuilding)); // index of destination
            int          nodeCount  = cnt;                                                        //amount of nodes
            List <int>   nodesIndex = new List <int>();                                           // list with indexes
            List <float> distances  = new List <float>();                                         // list with infite distances linked by index

            for (int i = 0; i < nodeCount; i++)
            {
                nodesIndex.Add(i);
                distances.Add(float.MaxValue);
            }

            distances[startIndex] = 0;
            int[] prev = new int[nodeCount]; // array with indexes of the path

            while (nodesIndex.Count > 0)
            {
                // find index of node with the smallest distance
                int   index    = -1;
                float shortest = float.MaxValue;
                for (int i = 0; i < distances.Count(); i++)
                {
                    if (nodesIndex.Contains(i))
                    {
                        if (distances[i] < shortest)
                        {
                            shortest = distances[i];
                            index    = i;
                        }
                    }
                }
                int closestIndex = index;

                for (int i = 0; i < nodeCount; i++)
                {
                    if (matrix[i, closestIndex])  //look in matrix if nodes are connected
                    {
                        float tempDist = distances[closestIndex] + Vector2.Distance(nodes[closestIndex], nodes[i]);

                        if (tempDist < distances[i])
                        {
                            distances[i] = tempDist;
                            prev[i]      = closestIndex;
                        }
                    }
                }
                nodesIndex.Remove(closestIndex);
            }

            int tempIndex = endIndex;

            while (tempIndex != startIndex)
            {
                int previous = prev[tempIndex];
                finalPath.Add(new Tuple <Vector2, Vector2>(nodes[tempIndex], nodes[previous]));
                tempIndex = prev[tempIndex];
            }

            return(finalPath);

            //var startingRoad = roads.Where(x => x.Item1.Equals(startingBuilding)).First();
            //List<Tuple<Vector2, Vector2>> fakeBestPath = new List<Tuple<Vector2, Vector2>>() { startingRoad };
            //var prevRoad = startingRoad;
            //for (int i = 0; i < 30; i++)
            //{
            //    prevRoad = (roads.Where(x => x.Item1.Equals(prevRoad.Item2)).OrderBy(x => Vector2.Distance(x.Item2, destinationBuilding)).First());
            //    fakeBestPath.Add(prevRoad);
            //}
            //return fakeBestPath;
        }
Beispiel #6
0
        //Runs for every tick
        private void Update(object sender, EventArgs e)
        {
            Bitmap headImage = Properties.Resources.head;
            Bitmap bodyImage = Properties.Resources.body;
            Bitmap turnImage = Properties.Resources.turn2;
            Bitmap tailImage = Properties.Resources.tail2;

            Vector2 previousHeadPosition = positions[0];
            Vector2 nextLocation         = positions[0] + nextMove;

            activeSprites[0].Image = RotateSnake(activeSprites[0], positions[0], nextMove, headImage);
            positions.Insert(0, positions[0] + nextMove);
            activeSprites[0].Location  = new Point(positions[0].x, positions[0].y); //Head position
            activeSprites[0].BackColor = headColor ? backColor1 : backColor2;       //Head backColor

            int    bodyIndex     = activeSprites.Count - 2;
            Bitmap replaceImage  = previousHeadPosition.facing != nextMove.facing ? turnImage : bodyImage; //Determine if snake turns
            bool   pickedUpApple = positions[0] == applePosition;

            if (pickedUpApple)
            {
                replaceImage = (Bitmap)RotateSnake(new PictureBox(), previousHeadPosition, nextMove, replaceImage);
                NewApplePosition();
                CreateSprite(replaceImage, positions[1], 1);
                bodyIndex++;
            }
            else
            {
                replaceImage = (Bitmap)RotateSnake(activeSprites[bodyIndex], previousHeadPosition, nextMove, replaceImage);
                positions.RemoveAt(positions.Count - 1);
                PictureBox temp = activeSprites[bodyIndex];
                activeSprites.RemoveAt(bodyIndex);
                activeSprites.Insert(1, temp);
                activeSprites[1].Location = new Point(positions[1].x, positions[1].y); //Moves item before tail to after head
            }
            activeSprites[1].BackColor = !headColor ? backColor1 : backColor2;

            headColor = !headColor;
            if (!pickedUpApple)
            {
                tailColor = !tailColor;
            }
            int tailIndex = activeSprites.Count - 1;

            activeSprites[tailIndex].Image     = RotateSnake(activeSprites[tailIndex], positions[tailIndex], positions[bodyIndex], tailImage); //tail rotation
            activeSprites[tailIndex].Location  = new Point(positions[tailIndex].x, positions[tailIndex].y);                                    //tail location
            activeSprites[tailIndex].BackColor = tailColor ? backColor1 : backColor2;

            if (Vector2.Contains(positions, nextLocation))
            {
                int at = Vector2.ContainsAt(positions, nextLocation);
                activeSprites[0].BackgroundImage       = activeSprites[at].Image;
                activeSprites[0].BackgroundImageLayout = ImageLayout.Stretch;
                GameEnd();
            }

            if (nextLocation.x >= ClientRectangle.Width || nextLocation.x < xOffset || nextLocation.y >= ClientRectangle.Height || nextLocation.y < yOffset)
            {
                if (nextLocation.y < yOffset)
                {
                    activeSprites[0].BackColor = backgroundColor;
                }
                GameEnd();
            }

            previousMove = nextMove;
        }
Beispiel #7
0
 private bool CorrectTile(float tileHeight, float tileTemp, float tileHumidity) => height.Contains(tileHeight) && temp.Contains(tileTemp) && humidity.Contains(tileHumidity);
Beispiel #8
0
 public bool IsValidTile(Tile tile) => !tile.IsWater && heightRange.Contains(tile.height) && tempRange.Contains(tile.temp) && humidityRange.Contains(tile.humidity);