Exemplo n.º 1
0
        public Vector2 GetOffset(Mobile unit, Map myMap)
        {
            //  Save tiles for leader and unit position:
            Tile leaderTile = myMap.GetContainingTile(leader);
            Tile unitTile = myMap.GetContainingTile(unit);

            if (leaderTile == null || unitTile == null)
                return new Vector2(0, -0);

            // Upper left
            if (unitTile.xPosition < leaderTile.xPosition && unitTile.yPosition <= leaderTile.yPosition)
            {
                ulCounter++;
                if(ulCounter == 1)
                    return new Vector2(-32, 0);
                else
                    return new Vector2(-32, -32);
            }

            //  Upper right
            if (unitTile.xPosition >= leaderTile.xPosition && unitTile.yPosition < leaderTile.yPosition)
            {
                urCounter++;
                if (urCounter == 1)
                    return new Vector2(0, -32);
                else
                    return new Vector2(32, -32);
            }

            //  Bottom right
            if (unitTile.xPosition > leaderTile.xPosition && unitTile.yPosition >= leaderTile.yPosition)
            {
                brCounter++;
                if(brCounter == 1)
                    return new Vector2(32, 0);
                else
                    return new Vector2(32, 32);
            }

            // Bottom left
            if (unitTile.xPosition <= leaderTile.xPosition && unitTile.yPosition > leaderTile.yPosition)
            {
                blCounter++;
                if (blCounter == 1)
                    return new Vector2(32, 0);
                else
                    return new Vector2(-32, 32);
            }

            else
                return new Vector2(0, 0);
        }
Exemplo n.º 2
0
        public Vector2 GetOffset(Mobile unit, Map myMap)
        {
            //  Save tiles for leader and unit position:
            Tile leaderTile = myMap.GetContainingTile(leader);
            Tile unitTile   = myMap.GetContainingTile(unit);

            if (leaderTile == null || unitTile == null)
            {
                return(new Vector2(0, -0));
            }

            // Upper left
            if (unitTile.xPosition < leaderTile.xPosition && unitTile.yPosition <= leaderTile.yPosition)
            {
                ulCounter++;
                if (ulCounter == 1)
                {
                    return(new Vector2(-32, 0));
                }
                else
                {
                    return(new Vector2(-32, -32));
                }
            }

            //  Upper right
            if (unitTile.xPosition >= leaderTile.xPosition && unitTile.yPosition < leaderTile.yPosition)
            {
                urCounter++;
                if (urCounter == 1)
                {
                    return(new Vector2(0, -32));
                }
                else
                {
                    return(new Vector2(32, -32));
                }
            }

            //  Bottom right
            if (unitTile.xPosition > leaderTile.xPosition && unitTile.yPosition >= leaderTile.yPosition)
            {
                brCounter++;
                if (brCounter == 1)
                {
                    return(new Vector2(32, 0));
                }
                else
                {
                    return(new Vector2(32, 32));
                }
            }

            // Bottom left
            if (unitTile.xPosition <= leaderTile.xPosition && unitTile.yPosition > leaderTile.yPosition)
            {
                blCounter++;
                if (blCounter == 1)
                {
                    return(new Vector2(32, 0));
                }
                else
                {
                    return(new Vector2(-32, 32));
                }
            }

            else
            {
                return(new Vector2(0, 0));
            }
        }
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();
            }
        }