private void OnLoad(object sender, EventArgs e) { // Object creation WinObj = new Window(); MapStoreObj = new MapStore(); MapObj = new Map("map0.bmp", ref MapStoreObj); ShellStoreObj = new ShellStore(); PlrObj = new Player(MapObj.XPlrPos, MapObj.YPlrPos, 0.0f); PlrObj.GiveWeapon(10.0f, 1.0f, 2.0f, 5.0f, "Bullet", "MGun", 400, 10.6f); PlrObj.SelectWeapon("MGun"); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.ClientSize = new Size(WinObj.FMH + MapObj.Size + WinObj.FMO, WinObj.FMV + MapObj.Size + WinObj.FMV); this.FpsTimer.Interval = WinObj.Fps; //this.RoundsProgressBar.SetBounds(2 * WinObj.FMH + MapObj.Size, WinObj.FMV, WinObj.FMO - 2 * WinObj.FMH, 2 * WinObj.FMV); }
/// <summary> /// Draws map /// </summary> /// <param name="g">Graphics reference</param> /// <param name="plrObj">Player object reference</param> /// <param name="mapStoreObj">Map store object</param> public void drawMap(ref Graphics g, MapStore mapStoreObj, ref Player plrObj) { SolidBrush wallBrush = new SolidBrush(Color.Gray); Rectangle wallBlock; for (int i = 0; i < _blockCount; i++) { for (int j = 0; j < _blockCount; j++) { if (mapStoreObj.GetBlock(i, j) == 2) // Draw walls { wallBlock = new Rectangle( DefaultWindow.FRAME_MARGIN_H + i * DefaultMap.BLOCK_SIZE, DefaultWindow.FRAME_MARGIN_V + j * DefaultMap.BLOCK_SIZE, DefaultMap.BLOCK_SIZE, DefaultMap.BLOCK_SIZE); g.FillRectangle(wallBrush, wallBlock); } } } }
////////////////////////////// // Constructors: ////////////////////////////// /// <summary> /// /// </summary> /// <param name="mapName">Name of map</param> /// <param name="BlockSize">Size of block in px</param> public Map(string mapName, ref MapStore mapStoreObj) { _blockCount = getMap(mapName, ref mapStoreObj); _size = _blockCount * DefaultMap.BLOCK_SIZE; }
////////////////////////////// // Methodes: ////////////////////////////// /// <summary> /// Get map using .bmp image and returns map size in px /// /// White (#FFFFFF) = air block /// Red (#FF0000) = player start /// Grey (#808080) = wall block /// .... /// Add new types of block and description in this summary block /// </summary> /// <exception cref="Vertical map size != horizontal map size">Vertical map size != horizontal map size</exception> /// <exception cref="Map doesn't exist">Map doesn't exist</exception> /// <exception cref="Map hasn't player start">Map hasn't player start</exception> /// <exception cref="Map has multiple player starts">Map has multiple player starts</exception> public int getMap(string mapName, ref MapStore mapStoreObj) { // Check for map existance try { bool isHavePlrStartPos = false; Bitmap mapImg = new Bitmap(Environment.CurrentDirectory + DefaultMap.PATH + mapName); // Check for squared map size try { if (mapImg.Height != mapImg.Width) { throw new NotSquareMap(mapName); } } catch (NotSquareMap e) { Program.MenuWinObj.PlayWinObj.Close(); MessageBox.Show("Map size isn't square: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Fill map store mapStoreObj.Init(mapImg.Width); // Prepare dyn array for storing for (int i = 0; i < mapImg.Width; i++) { for (int j = 0; j < mapImg.Width; j++) { Color pixelColor = mapImg.GetPixel(i, j); ////////////////////////////// Air block ////////////////////////////// if (pixelColor.R == 255 && pixelColor.G == 255 && pixelColor.B == 255) { mapStoreObj.SetBlock(i, j, 0); } ////////////////////////////// Player start ////////////////////////////// if (pixelColor.R == 255 && pixelColor.G == 0 && pixelColor.B == 0) { // Multiple player start check try { if (isHavePlrStartPos == true) { throw new MultiplePlrStart(mapName); } } catch (MultiplePlrStart e) { Program.MenuWinObj.PlayWinObj.Close(); MessageBox.Show("Map has multiple player starts: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return -1; } mapStoreObj.SetBlock(i, j, 1); _plrXStartPos = i * DefaultMap.BLOCK_SIZE; _plrYStartPos = j * DefaultMap.BLOCK_SIZE; isHavePlrStartPos = true; } ////////////////////////////// Wall block ////////////////////////////// if (pixelColor.R == 128 && pixelColor.G == 128 && pixelColor.B == 128) { mapStoreObj.SetBlock(i, j, 2); } } } // Check for player start position try { if (isHavePlrStartPos == false) { throw new NoPlrStart(mapName); } } catch (NoPlrStart e) { Program.MenuWinObj.PlayWinObj.Close(); MessageBox.Show("There is no player start position in map: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return mapImg.Width; } catch (ArgumentException) { Program.MenuWinObj.PlayWinObj.Close(); MessageBox.Show("There is no map: " + mapName, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return -1; } }