Exemple #1
0
 void RemoveMovementDirection(GridIndex Index, Enums.MoveDirections dir)
 {
     if (!Index.IsValid())
     {
         return;
     }
     _GridMovements[Index.X, Index.Y, Index.Z] &= (byte)~dir;
 }
Exemple #2
0
 public GridIndex[] GetRight(int count)
 {
     GridIndex[] indexArray = new GridIndex[count];
     for (int i = 0; i < count; i++)
     {
         indexArray[i] = new GridIndex(1 + i, 0, 0) + this;
     }
     return(indexArray);
 }
Exemple #3
0
 public GridIndex(GridIndex other)
 {
     if (other == null)
     {
         throw new ArgumentNullException("other");
     }
     X = other.X;
     Y = other.Y;
     Z = other.Z;
 }
Exemple #4
0
 public GridIndex[] GetLeft(int count)
 {
     GridIndex[] indexArray = new GridIndex[count];
     for (int i = 0; i < count; i++)
     {
         indexArray[i] = new GridIndex(-1 - i, 0, 0) + this;
     }
     Array.Reverse(indexArray); // Reverse, so we always count from left to right
     return(indexArray);
 }
Exemple #5
0
        public bool OtherIndexIsInDirection(GridIndex otherIndex, Enums.MoveDirections dir)
        {
            if (otherIndex == null)
            {
                return(false);
            }

            switch (dir)
            {
            case Enums.MoveDirections.Top:
                if ((otherIndex.Y - Y) > 0)
                {
                    return(true);
                }
                break;

            case Enums.MoveDirections.Bottom:
                if ((Y - otherIndex.Y) > 0)
                {
                    return(true);
                }
                break;

            case Enums.MoveDirections.Left:
                if ((X - otherIndex.X) > 0)
                {
                    return(true);
                }
                break;

            case Enums.MoveDirections.Right:
                if ((otherIndex.X - X) > 0)
                {
                    return(true);
                }
                break;

            case Enums.MoveDirections.Front:
                if ((Z - otherIndex.Z) > 0)
                {
                    return(true);
                }
                break;

            case Enums.MoveDirections.Back:
                if ((otherIndex.Z - Z) > 0)
                {
                    return(true);
                }
                break;
            }

            return(false);
        }
Exemple #6
0
        public static List <GridIndex> FindPath(GridIndex startIndex, GridIndex endIndex, TileMap gridMovements)
        {
            if (gridMovements == null)
            {
                return(new List <GridIndex>());
            }

            List <List <GridIndex> > allPaths  = new List <List <GridIndex> >();
            List <GridIndex>         firstPath = new List <GridIndex>();

            firstPath.Add(startIndex);
            allPaths.Add(firstPath);

            bool Working      = true;
            int  CycleCounter = 0;

            while (Working)
            {
                Working = false;
                CycleCounter++;

                List <List <GridIndex> > allPathsTmp = new List <List <GridIndex> >();

                foreach (List <GridIndex> onePath in allPaths)
                {
                    GridIndex lastIndex = onePath[onePath.Count - 1];

                    foreach (Enums.MoveDirections Dir in Enum.GetValues(typeof(Enums.MoveDirections)))
                    {
                        if (gridMovements.GridTileHasDirection(lastIndex, Dir))
                        {
                            GridIndex nextIndex = lastIndex.GetAdjacent(Dir);
                            if (onePath.Contains(nextIndex) == false)
                            {
                                List <GridIndex> newPath = new List <GridIndex>(onePath);
                                newPath.Add(nextIndex);
                                if (nextIndex == endIndex)
                                {
                                    CleanUpPath(ref newPath);
                                    return(newPath);
                                }
                                Working = true;
                                allPathsTmp.Add(newPath);
                            }
                        }
                    }
                }

                allPaths = allPathsTmp;
            }

            return(new List <GridIndex>());
        }
Exemple #7
0
 public bool GridTileHasDirection(GridIndex index, Enums.MoveDirections dir)
 {
     if (index == null)
     {
         return(false);
     }
     if ((_GridMovements[index.X, index.Y, index.Z] & (byte)dir) != 0)
     {
         return(true);
     }
     return(false);
 }
Exemple #8
0
        public int CountLinksFromGridTile(GridIndex index)
        {
            int Count = 0;

            foreach (Enums.MoveDirections Dir in Enum.GetValues(typeof(Enums.MoveDirections)))
            {
                if (GridTileHasDirection(index, Dir))
                {
                    Count++;
                }
            }
            return(Count);
        }
Exemple #9
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            GridIndex p = obj as GridIndex;

            if ((object)p == null)
            {
                return(false);
            }
            return(this == p);
        }
Exemple #10
0
        public void ClearMovements(GridIndex index)
        {
            if (index == null)
            {
                return;
            }

            // Also considering linked surrounding tiles. Just removing all possible
            RemoveMovementDirection(index.GetBelow(), Enums.MoveDirections.Top);
            RemoveMovementDirection(index.GetAbove(), Enums.MoveDirections.Bottom);
            RemoveMovementDirection(index.GetLeft(), Enums.MoveDirections.Right);
            RemoveMovementDirection(index.GetRight(), Enums.MoveDirections.Left);
            RemoveMovementDirection(index.GetFront(), Enums.MoveDirections.Back);
            RemoveMovementDirection(index.GetBack(), Enums.MoveDirections.Front);
            _GridMovements[index.X, index.Y, index.Z] = 0;
        }
Exemple #11
0
        public static GridIndex operator -(GridIndex c1, GridIndex c2)
        {
            if (c1 == null)
            {
                throw new ArgumentNullException("c1");
            }
            if (c2 == null)
            {
                throw new ArgumentNullException("c2");
            }

            GridIndex r = new GridIndex();

            r.X = c1.X - c2.X;
            r.Y = c1.Y - c2.Y;
            r.Z = c1.Z - c2.Z;
            return(r);
        }
Exemple #12
0
        public bool IsAdjacentTo(GridIndex otherIndex)
        {
            if (otherIndex == null)
            {
                return(false);
            }

            if (Math.Abs(X - otherIndex.X) > 1)
            {
                return(false);
            }
            if (Math.Abs(Y - otherIndex.Y) > 1)
            {
                return(false);
            }
            if (Math.Abs(Z - otherIndex.Z) > 1)
            {
                return(false);
            }
            return(true);
        }
Exemple #13
0
        public GridIndex GetAbove()
        {
            GridIndex a = new GridIndex(0, 1, 0) + this;

            return(a);
        }
Exemple #14
0
 public GridIndex Subtract(GridIndex other)
 {
     return(this - other);
 }
Exemple #15
0
 public GridIndex Add(GridIndex other)
 {
     return(this + other);
 }
Exemple #16
0
 public void LinkTiles(GridIndex index1, GridIndex index2, Enums.MoveDirections dir)
 {
     AddMovementDirection(index2, GetOppositeMovementDirection(dir));
     AddMovementDirection(index1, dir);
 }
Exemple #17
0
 void AddMovementDirection(GridIndex Index, Enums.MoveDirections dir)
 {
     _GridMovements[Index.X, Index.Y, Index.Z] |= (byte)dir;
 }
Exemple #18
0
        public GridIndex GetBack()
        {
            GridIndex b = new GridIndex(0, 0, 1) + this;

            return(b);
        }
Exemple #19
0
        public GridIndex GetFront()
        {
            GridIndex a = new GridIndex(0, 0, -1) + this;

            return(a);
        }
Exemple #20
0
        public GridIndex GetBelow()
        {
            GridIndex b = new GridIndex(0, -1, 0) + this;

            return(b);
        }
Exemple #21
0
        public GridIndex GetRight()
        {
            GridIndex r = new GridIndex(1, 0, 0) + this;

            return(r);
        }
Exemple #22
0
        public GridIndex GetLeft()
        {
            GridIndex l = new GridIndex(-1, 0, 0) + this;

            return(l);
        }