Example #1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public Pathfinder(Map map)
        {
            levelWidth = map.numberColumns;
            levelHeight = map.numberRows;

            InitializeSearchNodes(map);
        }
Example #2
0
 public PursuitEnemy(Model model, Vector3 position,GraphicsDevice device, Camera camera,Tank tank)
     : base(model, device, camera)
 {
     tankBox = new BoundingBox(MIN, MAX);
     CurrentPosition = position;
     pickPosition = CurrentPosition;
     translation = Matrix.CreateTranslation(Map.MapToWorld(new Point(10, 10)));
     velocity = Vector3.Zero;
     new Tank(model, device, camera);
     steer = new Steering(100f, 100f);
     map = new Map();
     pathfinder = new Pathfinder(map);
     isMoving = false;
     initialAngle = MathHelper.PiOver2;
     moveorder = 0;
     mousepick = new MousePicking(device, camera);
     targetTank = tank;
 }
Example #3
0
 public ModelManager(Game game)
     : base(game)
 {
     map = new Map();
     pathfinder = new Pathfinder(map);
     wallstone = new Wallstone[map.barrierList.Count];
     XElement level = XElement.Load(@"Content/config/levelsetting.xml");
     //tank1 = new Tank1(Game.Content.Load<Model>(@"Models/Tank/tank"), ((Game1)Game).GraphicsDevice,
     //   ((Game1)Game).camera);
     foreach (XElement levelnumber in level.Elements()) {
     foreach (XElement parameters in levelnumber.Elements())
     {
         levelInfoList.Add(new LevelInfo(int.Parse(parameters.Attribute("minSpawnTime").Value),
                         int.Parse(parameters.Attribute("maxSpawnTime").Value),
                         int.Parse(parameters.Attribute("numberEnemies").Value),
                         int.Parse(parameters.Attribute("minspeed").Value),
                         int.Parse(parameters.Attribute("maxSpeed").Value),
                         int.Parse(parameters.Attribute("missAllowed").Value),
                         int.Parse(parameters.Attribute("numHuman").Value)
           ));
      }
     }
     /*    levelInfoList.Add(new LevelInfo(10,100,5,2,21,10));
     levelInfoList.Add(new LevelInfo(900,2800,10,3,6,9));
     levelInfoList.Add(new LevelInfo(800, 2600, 15, 4, 6, 8));
     levelInfoList.Add(new LevelInfo(700, 2400,20, 5, 7, 7));
     levelInfoList.Add(new LevelInfo(600, 2200, 25, 6, 7, 6));
     levelInfoList.Add(new LevelInfo(500, 2000, 30, 7, 7, 5));
     levelInfoList.Add(new LevelInfo(400, 1800, 35, 8, 7, 4));
     levelInfoList.Add(new LevelInfo(300, 1600, 40, 8, 8, 3));
     levelInfoList.Add(new LevelInfo(200, 1400, 45, 8, 8, 2));
     levelInfoList.Add(new LevelInfo(100, 1200, 55, 8, 9, 1));
     levelInfoList.Add(new LevelInfo(50, 1000, 60, 8, 9, 0));
     levelInfoList.Add(new LevelInfo(50, 800, 65,8, 9, 0));
     levelInfoList.Add(new LevelInfo(50, 600, 70, 8, 10, 0));
     levelInfoList.Add(new LevelInfo(25, 400, 75, 8, 10, 0));
     levelInfoList.Add(new LevelInfo(0, 200, 80, 8, 20, 0));
      */
     this.game = game;
 }
Example #4
0
        // <summary>
        /// Splits our level up into a grid of nodes.
        /// </summary>
        private void InitializeSearchNodes(Map map)
        {
            searchNodes = new SearchNode[levelWidth, levelHeight];

            //For each of the tiles in our map, we
            // will create a search node for it.
            for (int x = 0; x < levelWidth; x++)
            {
                for (int y = 0; y < levelHeight; y++)
                {
                    //Create a search node to represent this tile.
                    SearchNode node = new SearchNode();
                    node.Position = new Point(x, y);

                    // Our enemies can only walk on grass tiles.
                    node.Walkable = map.mapTiles[x, y] == MapTileType.Empty;

                    // We only want to store nodes
                    // that can be walked on.
                    if (node.Walkable == true)
                    {
                        node.Neighbors = new SearchNode[4];
                        searchNodes[x, y] = node;//searchnode是保存可走的二维数组
                    }
                }
            }

            // Now for each of the search nodes, we will
            // connect it to each of its neighbours.
            for (int x = 0; x < levelWidth; x++)
            {
                for (int y = 0; y < levelHeight; y++)
                {
                    SearchNode node = searchNodes[x, y];

                    // We only want to look at the nodes that
                    // our enemies can walk on.
                    if (node == null || node.Walkable == false)
                    {
                        continue;
                    }

                    // An array of all of the possible neighbors this
                    // node could have. (We will ignore diagonals for now.)
                    Point[] neighbors = new Point[]
                    {
                        new Point (x, y - 1), // The node above the current node
                        new Point (x, y + 1), // The node below the current node.
                        new Point (x - 1, y), // The node left of the current node.
                        new Point (x + 1, y), // The node right of the current node
                        //new Point (x-1,y-1),
                        //new Point (x-1,y+1),
                        //new Point (x+1,y-1),
                        //new Point (x+1,y+1),
                    };

                    // We loop through each of the possible neighbors
                    for (int i = 0; i < neighbors.Length; i++)
                    {
                        Point position = neighbors[i];

                        // We need to make sure this neighbour is part of the level.
                        if (position.X < 0 || position.X > levelWidth - 1 ||
                            position.Y < 0 || position.Y > levelHeight - 1)
                        {
                            continue;
                        }

                        SearchNode neighbor = searchNodes[position.X, position.Y];

                        // We will only bother keeping a reference
                        // to the nodes that can be walked on.
                        if (neighbor == null || neighbor.Walkable == false)
                        {
                            continue;
                        }

                        // Store a reference to the neighbor.
                        node.Neighbors[i] = neighbor;
                    }
                }
            }
        }
Example #5
0
 public Scene(SpriteBatch sb,Map m,Settings set,Dictionary<string,Person> persons = null)
 {
     this.sb = sb;
     backgroundMap = m;
     tileWidth = m.defaultSprite.width;
     tileHeight = m.defaultSprite.height;
     winRect = new Rectangle(set.x, set.y, set.width, set.height);
     if (persons == null) persons = new Dictionary<string, Person>();
     else this.persons = persons;
     bgNeedRedraw = true;
     for (int i = 0;i<backgroundMap.height;i++)
         for (int j = 0; j < backgroundMap.width; j++)
         {
             backgroundMap.data[i, j].x = j * tileWidth;
             backgroundMap.data[i, j].y = i * tileHeight;
         }
     collisionRect = new Rectangle(0, 0, 0, 0);
     foreach (Sprite s in backgroundMap.data)
     {
         if (s.width > collisionRect.Width) collisionRect.Width = s.width;
         if (s.height > collisionRect.Height) collisionRect.Height = s.height;
     }
     foreach (Person kv in persons.Values)
     {
         if (kv.width > collisionRect.Width) collisionRect.Width = kv.width;
         if (kv.height > collisionRect.Height) collisionRect.Height = kv.height;
     }
     ReindexCollisions();
 }
Example #6
0
        void mapParse(XmlDocument xmlDc)
        {
            Maps = new Dictionary<string, Map>();
            foreach (XmlNode node in xmlDc.SelectNodes("Resource/Map"))
            {
                Map m = new Map();
                m.options = new Dictionary<string, string>();
                int width = -1;
                int height = -1;
                Sprite defaultSprite = badSprite;
                List<mapObj> objs = new List<mapObj>();
                foreach (XmlNode child in node.ChildNodes)
                {
                    child.InnerText = child.InnerText.Trim();
                    switch (child.Name.ToLower())
                    {
                        case "name":
                            m.name = child.InnerText;
                            break;
                        case "width":
                            try { width = Convert.ToInt32(child.InnerText); }
                            catch { Game1.dbg.Log("Map incorrect width"); }
                            break;
                        case "height":
                            try { height = Convert.ToInt32(child.InnerText); }
                            catch { Game1.dbg.Log("Map incorrect height"); }
                            break;
                        case "maintilename":
                            defaultSprite = getSprite(child.InnerText.ToLower());
                            break;
                        default:
                            if (child.Name != "object")
                                m.options.Add(child.Name, child.InnerText);
                            break;
                    }
                }
                int counter = 0;

                foreach (XmlNode objNode in xmlDc.SelectNodes("Resource/object"))
                {
                    mapObj mobj = new mapObj();
                    foreach (XmlNode childObj in objNode.ChildNodes)
                    {
                        childObj.InnerText = childObj.InnerText.Trim().ToLower();
                        switch (childObj.Name.ToLower())
                        {
                            case "name":
                                mobj.name = childObj.InnerText;
                                break;
                            case "sprite":
                                mobj.sprite = getSprite(childObj.InnerText);
                                break;
                            case "x":
                                try { mobj.x = Convert.ToInt32(childObj.InnerText); }
                                catch { Game1.dbg.Log("Map incorrect object x"); }
                                break;
                            case "y":
                                try { mobj.y = Convert.ToInt32(childObj.InnerText); }
                                catch { Game1.dbg.Log("Map incorrect object y"); }
                                break;
                        }
                    }
                    if (mobj.name == null) mobj.name = "Anonym" + counter;
                    objs.Add(mobj);
                }
                if (width < 0 || height < 0)
                    Game1.dbg.Log("Map width or height is incorrect", GameException.LevelCatastrophic);
                if (defaultSprite.Equals(badSprite))
                    Game1.dbg.Log("Default sprite not defined", GameException.LevelCatastrophic);
                m.data = new Sprite[width, height];
                for (int i = 0; i < height; i++)
                    for (int j = 0; j < width; j++)
                        m.data[i, j] = defaultSprite.Copy();
                m.defaultSprite = defaultSprite;
                for (int i = 0; i < objs.Count; i++)
                {
                    if (objs[i].x < 0 || objs[i].y < 0)
                        Game1.dbg.Log("Object coordinates incorrect", GameException.LevelCatastrophic);
                    m.data[objs[i].y, objs[i].x] = objs[i].sprite.Copy();
                }
                m.width = width;
                m.height = height;
                Maps.Add(m.name, m);
            }
        }