Agliullov D.A. Lab Work 2 Base #2

Closed
d.agliullov wants to merge 17 commits from Lab2 into Lab1
6 changed files with 103 additions and 76 deletions
Showing only changes of commit 20b682b2ef - Show all commits

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -9,6 +10,7 @@ namespace AirBomber
internal abstract class AbstractMap
{
private IDrawningObject _drawningObject = null;
private Bitmap? _staticBitMap;
protected int[,] _map = null;
protected int _width;
protected int _height;
@ -20,6 +22,7 @@ namespace AirBomber
public Bitmap CreateMap(int width, int height, IDrawningObject drawningObject)
{
_staticBitMap = null;
_width = width;
_height = height;
_drawningObject = drawningObject;
@ -30,10 +33,56 @@ namespace AirBomber
}
return DrawMapWithObject();
}
/// <summary>Проверяет наличие непроходимых участков в заданной области</summary>
/// <param name="area">Заданная область</param>
/// <param name="iBarrier">i-ый индекс первого барьера, который был найден в области</param>
/// <param name="jBarrier">j-ый индекс первого барьера, который был найден в области</param>
/// <returns>Есть ли барьеры</returns>
protected bool BarriersInArea(RectangleF area, ref int iBarrier, ref int jBarrier)
{
if (!(0 < area.Left && area.Right < _width && 0 < area.Top && area.Bottom < _height))
{
return true; // Если область попала за карту, считаем что она столкнулась с барьером
}
int rightArea = (int)Math.Ceiling(area.Right / _size_x);
int bottomArea = (int)Math.Ceiling(area.Bottom / _size_y);
for (int i = (int)(area.Left / _size_x); i < rightArea; i++)
{
for (int j = (int)(area.Top / _size_y); j < bottomArea; j++)
{
if (_map[i, j] == _barrier)
{
iBarrier = i;
jBarrier = j;
return true;
}
}
}
return false;
}
protected bool BarriersInArea(RectangleF area)
{
int a = 0, b = 0;
return BarriersInArea(area, ref a, ref b);
}
public Bitmap MoveObject(Direction direction)
{
// TODO проверка, что объект может переместится в требуемом направлении
if (true)
var rect = _drawningObject.GetCurrentPosition();
var step = _drawningObject.Step;
// Вычисляем области смещения объекта
RectangleF? area = null;
if (direction == Direction.Left)
area = new(rect.Left - step, rect.Top, step, rect.Height);
else if (direction == Direction.Right)
area = new(rect.Right, rect.Top, step, rect.Height);
else if (direction == Direction.Up)
area = new(rect.Left, rect.Top - step, rect.Width, step);
else if (direction == Direction.Down)
area = new(rect.Left, rect.Bottom, rect.Width, step);
if (area.HasValue && !BarriersInArea(area.Value))
{
_drawningObject.MoveObject(direction);
}
@ -48,17 +97,37 @@ namespace AirBomber
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
return true;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
if (_drawningObject == null || _map == null)
// Если натыкаемся на барьер помещаем левый верхний угол чуть ниже этого барьера
// если при этом выходим за карту, пермещаем правый нижный угол чуть выше этого барьера
// если объект выходит за карту, генирируем новые координаты рандомно
int currI = 0, currJ = 0;
var areaObject = _drawningObject.GetCurrentPosition();
int cntOut = 10000; // Количество итераций до выхода из цикла
while (BarriersInArea(areaObject, ref currI, ref currJ) && --cntOut >= 0)
{
return bmp;
if ((currJ + 1) * _size_y + areaObject.Height <= _height)
{
areaObject.Location = new PointF((currI + 1) * _size_x, (currJ + 1) * _size_y);
}
else if ((currI - 1) * _size_x - areaObject.Width >= 0)
{
areaObject = new((currI - 1) * _size_x - areaObject.Width, (currJ - 1) * _size_y - areaObject.Height, areaObject.Width, areaObject.Height);
}
else
{
areaObject.Location = new PointF(_random.Next(0, _width - (int)areaObject.Width),
_random.Next(0, _height - (int)areaObject.Height));
}
}
Graphics gr = Graphics.FromImage(bmp);
_drawningObject.SetObject((int)areaObject.X, (int)areaObject.Y, _width, _height);
return cntOut >= 0;
}
private void DrawMap()
{
if (_staticBitMap != null) return;
_staticBitMap = new(_width, _height);
Graphics gr = Graphics.FromImage(_staticBitMap);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
@ -73,6 +142,20 @@ namespace AirBomber
}
}
}
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
if (_drawningObject == null || _map == null)
{
return bmp;
}
Graphics gr = Graphics.FromImage(bmp);
if (_staticBitMap == null)
DrawMap();
if (_staticBitMap != null)
gr.DrawImage(_staticBitMap, 0, 0);
_drawningObject.DrawningObject(gr);
return bmp;
}

View File

@ -211,9 +211,9 @@
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
public RectangleF GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _airplaneWidth, _startPosY + _airplaneHeight);
return new(_startPosX, _startPosY, _airplaneWidth, _airplaneHeight);
}
}
}

View File

@ -17,7 +17,7 @@ namespace AirBomber
public float Step => _airplane?.Airplane?.Step ?? 0;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
public RectangleF GetCurrentPosition()
{
return _airplane.GetCurrentPosition();
}

View File

@ -144,6 +144,7 @@
//
// buttonCreateModif
//
this.buttonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateModif.Location = new System.Drawing.Point(104, 390);
this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(110, 23);

View File

@ -1,64 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
@ -117,4 +57,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="statusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -38,6 +38,6 @@ namespace AirBomber
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
RectangleF GetCurrentPosition();
}
}