Exemple #1
0
        public Level Parse(Texture2D mapImage)
        {
            Level level = new Level();

            _levelScaleFactor = 4;
            _imageHeight = mapImage.Height;
            _imageWidth = mapImage.Width;

            Color[] colorData;
            colorData = new Color[_imageWidth * _imageHeight];
            mapImage.GetData<Color>(colorData);

            for (int y = 0; y < _imageHeight; y++)
            {
                for (int x = 0; x < _imageWidth; x++)
                {
                    Color currentPixel = colorData[_imageWidth * y + x];
                    if (currentPixel == Color.White)
                        continue;

                    Point position = new Point(x, y);

                    //Wall if all are the same
                    if (currentPixel.R == currentPixel.G && currentPixel.G == currentPixel.B)
                        HandleWall(position, currentPixel, colorData, level);
                    else if (currentPixel == PLAYER_COLOR)
                        HandlePlayer(position, currentPixel, colorData, level);
                    else if (currentPixel == FRIENDLY_COLOR)
                        HandleFriendly(position, currentPixel, colorData, level);
                    else if (currentPixel == ENEMY_COLOR)
                        HandleEnemy(position, currentPixel, colorData, level);
                }
            }
            return level;
        }
Exemple #2
0
        private void HandleWall(Point currentPoint, Color wallColor, Color[] colorData, Level level)
        {
            Vertices vertices = new Vertices();
            // Add each adjacent pixel of the same color as a vertice and then white it
            do
            {
                vertices.Add(PointToPosition(currentPoint));
                colorData[ToArrayIndex(currentPoint.X, currentPoint.Y)] = Color.White;
            } while (GetAdjacent(currentPoint, wallColor, colorData, ref currentPoint));

            if (vertices.Count < 3)
                return;

            Wall.WallType wallType = Wall.WallType.Inner;

            if (wallColor.R % 2 == 0)
                wallType = Wall.WallType.Outer;

            level.AddWall(new Wall(vertices, wallType, PlayWindow));
        }
Exemple #3
0
 private void HandlePlayer(Point currentPoint, Color dotColor, Color[] colorData, Level level)
 {
     Point position = GetCenterPoint(currentPoint, dotColor, colorData);
     PlayerCell player = new PlayerCell(PlayWindow, PointToPosition(position));
     level.SetPlayer(player);
 }
Exemple #4
0
 private void HandleFriendly(Point currentPoint, Color dotColor, Color[] colorData, Level level)
 {
     Point position = GetCenterPoint(currentPoint, dotColor, colorData);
     GoodCell goodCell = new GoodCell(PlayWindow, PointToPosition(position));
     level.AddFriendly(goodCell);
 }
Exemple #5
0
 private void HandleEnemy(Point currentPoint, Color dotColor, Color[] colorData, Level level)
 {
     Point position = GetCenterPoint(currentPoint, dotColor, colorData);
     Virus virus = new Virus(PlayWindow, PointToPosition(position));
     level.AddEnemy(virus);
 }
Exemple #6
0
        private void LoadLevel(String file)
        {
            World.Clear();
            GoodCellList.Clear();
            VirusList.Clear();

            _level = _levelParser.Parse(Texture2D.FromStream(Game1.graphics.GraphicsDevice, new FileStream(file, FileMode.Open)));
            _wallList = _level.GetWalls();

            foreach (var item in _level.GetFriendlies())
            {
                GoodCellList.Add((GoodCell)item);
            }

            foreach (var item in _level.GetEnemies())
            {
                VirusList.Add((Virus)item);
            }

            _objectList = _level.GetFriendlies();
            _objectList.AddRange(_level.GetEnemies());

            _player = _level.Player;
        }