generic classes

This commit is contained in:
Володя 2022-10-03 09:38:22 +03:00
parent ee7a0def18
commit 94bd87f67f
12 changed files with 380 additions and 26 deletions

View File

@ -32,12 +32,66 @@ namespace AirPlaneWithRadar
}
public Bitmap MoveObject(Direction direction)
{
// TODO проверка, что объект может переместится в требуемом направлении
if (true)
switch (direction)
{
_drawningObject.MoveObject(direction);
}
case Direction.Up:
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier && ((_drawningObject.GetCurrentPosition().Left < i * _size_x && _drawningObject.GetCurrentPosition().Top > i * _size_x)||
(_drawningObject.GetCurrentPosition().Left < i * (_size_x+1) && _drawningObject.GetCurrentPosition().Top > i * (_size_x+1)))&&
j*(_size_y+1)< _drawningObject.GetCurrentPosition().Right && _drawningObject.GetCurrentPosition().Right - j * (_size_y + 1) <= _drawningObject.Step)
return DrawMapWithObject();
}
}
break;
case Direction.Down:
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier && ((_drawningObject.GetCurrentPosition().Left < i * _size_x && _drawningObject.GetCurrentPosition().Top > i * _size_x) ||
(_drawningObject.GetCurrentPosition().Left < i * (_size_x + 1) && _drawningObject.GetCurrentPosition().Top > i * (_size_x + 1))) &&
j * _size_y > _drawningObject.GetCurrentPosition().Bottom && j * _size_y - _drawningObject.GetCurrentPosition().Bottom <= _drawningObject.Step)
return DrawMapWithObject();
}
}
break;
case Direction.Left:
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier && ((_drawningObject.GetCurrentPosition().Right < j * _size_y && _drawningObject.GetCurrentPosition().Bottom > j * _size_y) ||
(_drawningObject.GetCurrentPosition().Right < j * (_size_y + 1) && _drawningObject.GetCurrentPosition().Bottom > j * (_size_y + 1))) &&
i * (_size_x+1)< _drawningObject.GetCurrentPosition().Left && _drawningObject.GetCurrentPosition().Left - i * (_size_x + 1) <= _drawningObject.Step)
return DrawMapWithObject();
}
}
break;
case Direction.Right:
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier && ((_drawningObject.GetCurrentPosition().Right < j * _size_y && _drawningObject.GetCurrentPosition().Bottom > j * _size_y) ||
(_drawningObject.GetCurrentPosition().Right < j * (_size_y + 1) && _drawningObject.GetCurrentPosition().Bottom > j * (_size_y + 1))) &&
i * (_size_x ) > _drawningObject.GetCurrentPosition().Top && i * (_size_x ) - _drawningObject.GetCurrentPosition().Top <= _drawningObject.Step)
return DrawMapWithObject();
}
}
break;
}
_drawningObject.MoveObject(direction);
return DrawMapWithObject();
}
private bool SetObjectOnMap()
{
@ -48,7 +102,18 @@ namespace AirPlaneWithRadar
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
if(_drawningObject is DrawingObjectPlane )
{
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier && _drawningObject.GetCurrentPosition().Left < i*_size_x && _drawningObject.GetCurrentPosition().Right < j*_size_y && _drawningObject.GetCurrentPosition().Top > i*_size_x && _drawningObject.GetCurrentPosition().Bottom > j*_size_y)
return false;
}
}
}
return true;
}
private Bitmap DrawMapWithObject()

View File

@ -1,16 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace AirPlaneWithRadar
{
internal class DrawingObject : IDrawingObject
internal class DrawingObjectPlane : IDrawingObject
{
private DrawingPlain _plain;
private DrawingPlain _plain = null;
public DrawingObject(DrawingPlain plain)
public DrawingObjectPlane(DrawingPlain plain)
{
_plain = plain;
}
@ -19,7 +20,7 @@ namespace AirPlaneWithRadar
public void DrawningObject(Graphics g)
{
//TODO
_plain?.DrawTransoprt(g);
}
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()

View File

@ -15,7 +15,7 @@ namespace AirPlaneWithRadar
private int? pictureWidth = null;
private int? pictureHeight = null;
protected readonly int plainWidth = 120;
protected readonly int plainHeight = 70;
protected readonly int plainHeight = 80;
public DrawingPlain(int speed, float weight, Color bodycolor)
{
Plain = new EntetyPlain(speed, weight, bodycolor);

View File

@ -9,7 +9,7 @@ namespace AirPlaneWithRadar
{
internal class DrawingRadarPlane : DrawingPlain
{
public DrawingRadarPlane(int speed, float weight, Color bodyColor,Color dopColor, bool radar, bool oilBox) : base(speed, weight, bodyColor, 110, 60)
public DrawingRadarPlane(int speed, float weight, Color bodyColor,Color dopColor, bool radar, bool oilBox) : base(speed, weight, bodyColor, 120, 80)
{
Plain = new RadioPlane(speed, weight, bodyColor, dopColor, radar, oilBox);
}

View File

@ -49,7 +49,7 @@
this.pictureBoxPlain.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxPlain.Location = new System.Drawing.Point(0, 0);
this.pictureBoxPlain.Name = "pictureBoxPlain";
this.pictureBoxPlain.Size = new System.Drawing.Size(535, 292);
this.pictureBoxPlain.Size = new System.Drawing.Size(908, 470);
this.pictureBoxPlain.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxPlain.TabIndex = 0;
this.pictureBoxPlain.TabStop = false;
@ -61,9 +61,9 @@
this.toolStripStatusLabelSpeed,
this.toolStripStatusLabelWeight,
this.toolStripStatusLabelColor});
this.statusStrip.Location = new System.Drawing.Point(0, 292);
this.statusStrip.Location = new System.Drawing.Point(0, 470);
this.statusStrip.Name = "statusStrip";
this.statusStrip.Size = new System.Drawing.Size(535, 26);
this.statusStrip.Size = new System.Drawing.Size(908, 26);
this.statusStrip.TabIndex = 1;
//
// toolStripStatusLabelSpeed
@ -89,7 +89,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AirPlaneWithRadar.Properties.Resources.up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(383, 223);
this.buttonUp.Location = new System.Drawing.Point(756, 401);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 3;
@ -102,7 +102,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AirPlaneWithRadar.Properties.Resources.left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(347, 259);
this.buttonLeft.Location = new System.Drawing.Point(720, 437);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 4;
@ -115,7 +115,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AirPlaneWithRadar.Properties.Resources.down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(383, 259);
this.buttonDown.Location = new System.Drawing.Point(756, 437);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 5;
@ -128,7 +128,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AirPlaneWithRadar.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(419, 259);
this.buttonRight.Location = new System.Drawing.Point(792, 437);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 6;
@ -139,7 +139,7 @@
// ButtonCreate
//
this.ButtonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ButtonCreate.Location = new System.Drawing.Point(12, 259);
this.ButtonCreate.Location = new System.Drawing.Point(12, 437);
this.ButtonCreate.Name = "ButtonCreate";
this.ButtonCreate.Size = new System.Drawing.Size(94, 29);
this.ButtonCreate.TabIndex = 7;
@ -149,7 +149,8 @@
//
// buttonCreateModif
//
this.buttonCreateModif.Location = new System.Drawing.Point(125, 259);
this.buttonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateModif.Location = new System.Drawing.Point(112, 437);
this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(134, 29);
this.buttonCreateModif.TabIndex = 8;
@ -159,12 +160,15 @@
//
// comboBoxMap
//
this.comboBoxMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxMap.FormattingEnabled = true;
this.comboBoxMap.Items.AddRange(new object[] {
"Простая карта"});
"Простая карта",
"Пользовательская карта №1",
"Пользовательская карта №2"});
this.comboBoxMap.Location = new System.Drawing.Point(24, 12);
this.comboBoxMap.Name = "comboBoxMap";
this.comboBoxMap.Size = new System.Drawing.Size(151, 28);
this.comboBoxMap.Size = new System.Drawing.Size(188, 28);
this.comboBoxMap.TabIndex = 9;
this.comboBoxMap.SelectedIndexChanged += new System.EventHandler(this.comboBoxMap_SelectedIndexChanged);
//
@ -172,7 +176,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(535, 318);
this.ClientSize = new System.Drawing.Size(908, 496);
this.Controls.Add(this.comboBoxMap);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.ButtonCreate);

View File

@ -23,7 +23,7 @@ namespace AirPlaneWithRadar
toolStripStatusLabelSpeed.Text = $"Скорость: {plain.Plain.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {plain.Plain.Weight}";
toolStripStatusLabelColor.Text = $"Цвет: {plain.Plain.BodyColor.Name}";
pictureBoxPlain.Image = _abstractMap.CreateMap(pictureBoxPlain.Width, pictureBoxPlain.Height,new DrawingObject(plain)) ;
pictureBoxPlain.Image = _abstractMap.CreateMap(pictureBoxPlain.Width, pictureBoxPlain.Height,new DrawingObjectPlane(plain)) ;
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
@ -73,6 +73,13 @@ namespace AirPlaneWithRadar
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Пользовательская карта №1":
_abstractMap = new UserMap_BigBox();
break;
case "Пользовательская карта №2":
_abstractMap = new UserMap_Colums();
break;
}
}
}

View File

@ -14,6 +14,7 @@ namespace AirPlaneWithRadar
void DrawningObject(Graphics g);
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
}
}

View File

@ -0,0 +1,126 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirPlaneWithRadar
{
internal class MapWithSetPlainGeneric<T, U>
where T : class, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 180;
private readonly int _placeSizeHeight = 90;
private readonly SetPlaneGeneric<T> _setPlains;
private readonly U _map;
public MapWithSetPlainGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setPlains = new SetPlaneGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static bool operator +(MapWithSetPlainGeneric<T, U> map, T car)
{
return map._setPlains.Insert(car);
}
public static bool operator -(MapWithSetPlainGeneric<T, U> map, int position)
{
return map._setPlains.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawPlains(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setPlains.Count; i++)
{
var car = _setPlains.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
}
return new(_pictureWidth, _pictureHeight);
}
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
private void Shaking()
{
int j = _setPlains.Count - 1;
for (int i = 0; i < _setPlains.Count; i++)
{
if (_setPlains.Get(i) == null)
{
for (; j > i; j--)
{
var car = _setPlains.Get(j);
if (car != null)
{
_setPlains.Insert(car, i);
_setPlains.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.White, 5);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawPlains(Graphics g)
{
for (int i = 0; i < _setPlains.Count; i++)
{
_setPlains.Get(i)?.DrawningObject(g);
}
}
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirPlaneWithRadar
{
internal class SetPlaneGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
public SetPlaneGeneric(int count)
{
_places = new T[count];
}
public bool Insert(T plain)
{
return true;
}
public bool Insert(T plain, int position)
{
return true;
}
public bool Remove(int position)
{
return false;
}
public T Get(int position)
{
return _places[position];
}
}
}

View File

@ -9,7 +9,7 @@ namespace AirPlaneWithRadar
internal class SimpleMap : AbstractMap
{
Brush barrierColor = new SolidBrush(Color.DarkGray);
Brush roadColor = new SolidBrush(Color.Blue);
Brush roadColor = new SolidBrush(Color.Aqua);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor,i*_size_x,j*_size_y,i*(_size_x+1),j*(_size_y+1));

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirPlaneWithRadar
{
internal class UserMap_BigBox : AbstractMap
{
Brush barrierColor = new SolidBrush(Color.DarkGray);
Brush roadColor = new SolidBrush(Color.Aqua);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int sizeKub = 5;
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}while(counter < 10)
{
Random rand = new Random();
int i = rand.Next(0,100);
int j = rand.Next(0,100);
if (i > _map.GetLength(0) - sizeKub || j > _map.GetLength(0) - sizeKub)
continue;
for(int iKub = i; iKub < i+sizeKub; iKub++)
{
for(int jKub = j; jKub < j+ sizeKub; jKub++)
{
_map[iKub,jKub] = _barrier;
}
}
counter++;
}
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirPlaneWithRadar
{
internal class UserMap_Colums : AbstractMap
{
Brush barrierColor = new SolidBrush(Color.DarkGray);
Brush roadColor = new SolidBrush(Color.Aqua);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int sizeHole = 30;
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
} while (counter < 3)
{
Random rand = new Random();
int iWall = rand.Next(0, 100);
int jWall = rand.Next(0, 100);
if (iWall > _map.GetLength(0) - sizeHole)
continue;
for (int i = 0; i < _map.GetLength(0); i++)
{
if(i<iWall || i>iWall+sizeHole)
_map[jWall, i] = _barrier;
}
counter++;
}
}
}
}