Exemplo n.º 1
0
        public void SetPaths(Map myMap)
        {
            List<Tile> newCollisionList = new List<Tile>();

            leader.setDestination(destination);
            foreach(Mobile unit in units)
            {
                unit.isMoving = false;
                if(unit != leader)
                {
                    Vector2 newDestination = GetOffset(unit, myMap) + destination;
                    if(myMap.GetTile(newDestination) != null && myMap.GetTile(newDestination).isWalkable == true)
                    {
                        unit.setDestination(GetOffset(unit, myMap) + destination);
                    }
                    else
                        unit.setDestination(destination);
                }
                //unit.setDestination(destination);
                Vector2 startPoint = new Vector2(unit.Bounds.Center.X, unit.Bounds.Center.Y);
                unit.pathList.Clear();
                unit.pathList.AddRange(myMap.GetPath(startPoint, unit.destination, newCollisionList));
                unit.isMoving = true;
            }

        }
Exemplo n.º 2
0
        public void SetPaths(Map myMap)
        {
            List <Tile> newCollisionList = new List <Tile>();

            leader.setDestination(destination);
            foreach (Mobile unit in units)
            {
                unit.isMoving = false;
                if (unit != leader)
                {
                    Vector2 newDestination = GetOffset(unit, myMap) + destination;
                    if (myMap.GetTile(newDestination) != null && myMap.GetTile(newDestination).isWalkable == true)
                    {
                        unit.setDestination(GetOffset(unit, myMap) + destination);
                    }
                    else
                    {
                        unit.setDestination(destination);
                    }
                }
                //unit.setDestination(destination);
                Vector2 startPoint = new Vector2(unit.Bounds.Center.X, unit.Bounds.Center.Y);
                unit.pathList.Clear();
                unit.pathList.AddRange(myMap.GetPath(startPoint, unit.destination, newCollisionList));
                unit.isMoving = true;
            }
        }
Exemplo n.º 3
0
        //  Call movement method of all selected objects
        public void moveObjects(List <GameObject> playerUnits, List <GameObject> aiUnits)
        {
            List <GameObject> allUnits = new List <GameObject>();

            allUnits.AddRange(playerUnits);
            allUnits.AddRange(aiUnits);

            List <Tile> newCollisionList = new List <Tile>();

            UpdateGroups();

            for (int i = 0; i < allUnits.Count; i++)
            {
                if (allUnits[i].GetType().BaseType == typeof(Mobile) && ((Mobile)allUnits[i]).isMoving == true)
                {
                    // Iterate through all units again
                    for (int j = 0; j < allUnits.Count; j++)
                    {
                        //  If i collides with j and i and j are not the same
                        if (allUnits[i].collidesWith(allUnits[j]) && i != j)
                        {
                            //  If j is not moving, paused, or a tower, don't pause
                            if ((allUnits[j].GetType().BaseType == typeof(Mobile) && (((Mobile)allUnits[j]).isMoving == false || ((Mobile)allUnits[j]).isPaused == true)) ||
                                (allUnits[j].GetType() == typeof(Tower)) ||
                                aiUnits.Contains(allUnits[j])
                                )
                            {
                                newCollisionList.Add(myMap.GetContainingTile(allUnits[j]));
                                ((Mobile)allUnits[i]).isPaused = false;
                            }

                            //  If j is moving, pause i and end loop for this i
                            else if (GetGroup((Mobile)allUnits[i]) != null && GetGroup((Mobile)allUnits[i]).Contains((Mobile)allUnits[j]) == false)
                            {
                                newCollisionList.Add(myMap.GetContainingTile(allUnits[j]));
                                ((Mobile)allUnits[i]).isPaused   = true;
                                ((Mobile)allUnits[i]).pauseTimer = 25;
                                break;
                            }

                            else if (GetGroup((Mobile)allUnits[i]) != null &&
                                     GetGroup((Mobile)allUnits[i]).Contains((Mobile)allUnits[j]) == true &&
                                     myMap.GetContainingTile((Mobile)allUnits[i]) == myMap.GetContainingTile((Mobile)allUnits[j]) &&
                                     ((Mobile)allUnits[i]).pathList.Count <= 3
                                     )
                            {
                                newCollisionList.Add(myMap.GetContainingTile(allUnits[j]));
                                ((Mobile)allUnits[i]).isPaused   = true;
                                ((Mobile)allUnits[i]).pauseTimer = 25;
                                break;
                            }
                        }
                    }

                    //  If there were no collisions and unit is paused, unpause it
                    if (newCollisionList.Count == 0 && ((Mobile)allUnits[i]).isPaused == true)
                    {
                        if (((Mobile)allUnits[i]).pauseTimer <= 0)
                        {
                            ((Mobile)allUnits[i]).isPaused = false;
                            ((Mobile)allUnits[i]).collisionList.Clear();
                            break;
                        }
                        else
                        {
                            ((Mobile)allUnits[i]).pauseTimer--;
                        }
                    }


                    else if (CompareLists(newCollisionList, ((Mobile)allUnits[i]).collisionList) == true &&
                             ((Mobile)allUnits[i]).isPaused == true
                             )
                    {
                        if (((Mobile)allUnits[i]).pauseTimer <= 0)
                        {
                            ((Mobile)allUnits[i]).isPaused = false;
                            ((Mobile)allUnits[i]).collisionList.Clear();
                            break;
                        }
                        else
                        {
                            ((Mobile)allUnits[i]).pauseTimer--;
                        }
                    }


                    // Else if there was a new collision and the unit isn't paused, get a new path:
                    else if (CompareLists(newCollisionList, ((Mobile)allUnits[i]).collisionList) == false && ((Mobile)allUnits[i]).isPaused == false)
                    {
                        ((Mobile)allUnits[i]).pathList.Clear();
                        Vector2 startPoint = new Vector2(allUnits[i].Bounds.Center.X, allUnits[i].Bounds.Center.Y);
                        ((Mobile)allUnits[i]).pathList.AddRange(myMap.GetPath(startPoint, ((Mobile)allUnits[i]).destination, newCollisionList));
                    }

                    //  If a unit's collision list contains it's destination tile, stop it:
                    if (((Mobile)allUnits[i]).collisionList.Contains(myMap.GetTile(((Mobile)allUnits[i]).destination)))
                    {
                        ((Mobile)allUnits[i]).isMoving = false;
                        ((Mobile)allUnits[i]).isPaused = false;
                        break;
                    }

                    //  If unit i isn't paused and is moving, move it
                    if (((Mobile)allUnits[i]).isPaused == false && ((Mobile)allUnits[i]).isMoving == true)
                    {
                        if (allUnits[i].aiTarget != null && allUnits[i].IsInRange(allUnits[i].aiTarget))
                        {
                            ((Mobile)allUnits[i]).isMoving = false;
                            ((Mobile)allUnits[i]).isPaused = false;
                        }
                        else
                        {
                            ((Mobile)allUnits[i]).move();
                        }
                    }

                    ((Mobile)allUnits[i]).collisionList.Clear();
                    ((Mobile)allUnits[i]).collisionList.AddRange(newCollisionList);
                }

                newCollisionList.Clear();
            }
        }