Ejemplo n.º 1
0
        public World()
        {
            map = new Map(40, 5);
            towers = new List<Tower>();
            mobs = new List<Mob>();

            map.map[3, 0].Type = 1;
            map.map[4, 0].Type = 1;
            map.map[3, 1].Type = 1;
            map.map[4, 1].Type = 1;

            map.map[5,0] = new TowerTile(new Tower{Range = 10, Rate = 2000});
            map.map[5, 0].Type = 2;

            map.map[1, 0].Walkable = false;
            map.map[1, 1].Walkable = false;
            map.map[1, 3].Walkable = false;
            map.map[1, 4].Walkable = false;

            camera = new Camera(400*Tile.Width, 200*Tile.Height);

            Random rng = new Random(DateTime.Now.Millisecond);

            spawn = new Point(0, rng.Next(0, (int)map.Size.Y));
            goal = new Point((int)map.Size.X - 1, rng.Next(0, (int)map.Size.Y));

            map.map[spawn.X, spawn.Y] = new SpawnTile();
            map.map[goal.X, goal.Y] = new GoalTile();

            Node n = AStar.FindPath(map, spawn, goal);
            path = new List<Node>();

            while(n.Parent != null)
            {
                path.Insert(0, n);
                n = n.Parent;
            }

            towers.Add(((TowerTile)map.map[5,0]).Tower);

            Mob m = new Mob(path, spawn);
            mobs.Add(m);
        }
Ejemplo n.º 2
0
        public void Update(GameTime time)
        {
            KeyboardState keyboard_state = Keyboard.GetState();

            if (keyboard_state.IsKeyDown(Key.A))
                camera.Move(new Vector2(-2, 0));
            if (keyboard_state.IsKeyDown(Key.W))
                camera.Move(new Vector2(0, -2));
            if (keyboard_state.IsKeyDown(Key.D))
                camera.Move(new Vector2(2, 0));
            if (keyboard_state.IsKeyDown(Key.S))
                camera.Move(new Vector2(0, 2));

            //Pathfinding debug test
            if (keyboard_state.IsKeyUp(Key.Enter) && old_keyboard.IsKeyDown(Key.Enter))
            {
                DateTime start = DateTime.Now;
                Node n = AStar.FindPath(map, new Point(0, 0), new Point(2, 0));
                DateTime end = DateTime.Now;

                Console.WriteLine("Time to path: {0}", (end-start).TotalMilliseconds);
                if (n == null)
                {
                    Console.WriteLine("Goal is unreachable.");
                }
                else
                {
                    while (n.Parent != null)
                    {
                        Console.Write("{0}->", n.location.ToString());
                        n = n.Parent;
                    }

                    Console.WriteLine("{0}", n.location.ToString());
                }
            }

            if (keyboard_state.IsKeyUp(Key.ShiftRight) && old_keyboard.IsKeyDown(Key.ShiftRight))
            {
                Mob m = new Mob(path, spawn){Speed = 1};
                mobs.Add(m);
            }

            old_keyboard = keyboard_state;

            map.Update(time);
            for (int i = mobs.Count - 1; i >= 0;i--)
            {
                Mob m = mobs[i];
                if (m.Dead)
                    mobs.Remove(m);
                else
                    m.Update(time);
            }
        }