diff --git a/Catamaran/AbstractMap.cs b/Catamaran/AbstractMap.cs new file mode 100644 index 0000000..a6c19cb --- /dev/null +++ b/Catamaran/AbstractMap.cs @@ -0,0 +1,164 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal abstract class AbstractMap + { + private IDrawingObject _drawingObject = null; + protected int[,] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected readonly Random _random = new Random(); + protected readonly int _freeRoad = 0; + protected readonly int _barrier = 1; + public Bitmap CreateMap(int width, int height, IDrawingObject drawingObject) + { + _width = width; + _height = height; + _drawingObject = drawingObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + public Bitmap MoveObject(Direction direction) + { + bool isFree = true; + int startPosX = (int)(_drawingObject.GetCurrentPosition().Left / _size_x); + int startPosY = (int)(_drawingObject.GetCurrentPosition().Right / _size_y); + int boatWidth = (int)(_drawingObject.GetCurrentPosition().Top / _size_x); + int boatHeight = (int)(_drawingObject.GetCurrentPosition().Bottom / _size_y); + + switch (direction) + { + // вправо + case Direction.Right: + for (int i = boatWidth; i <= boatWidth + (int)(_drawingObject.Step / _size_x); i++) + { + for (int j = startPosY; j <= boatHeight; j++) + { + if (_map[i, j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + //влево + case Direction.Left: + for (int i = startPosX; i >= (int)(_drawingObject.Step / _size_x); i--) + { + for (int j = startPosY; j <= boatHeight; j++) + { + if (_map[i, j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + //вверх + case Direction.Up: + for (int i = startPosX; i <= boatWidth; i++) + { + for (int j = startPosY; j >= (int)(_drawingObject.Step / _size_y); j--) + { + if (_map[i, j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + //вниз + case Direction.Down: + for (int i = startPosX; i <= boatWidth; i++) + { + for (int j = boatHeight; j <= boatHeight + (int)(_drawingObject.Step / _size_y); j++) + { + if (_map[i, j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + } + + if (isFree) + { + _drawingObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + private bool SetObjectOnMap() + { + if (_drawingObject == null || _map == null) + { + return false; + } + int x = _random.Next(0, 10); + int y = _random.Next(0, 10); + _drawingObject.SetObject(x, y, _width, _height); + // TODO првоерка, что объект не "накладывается" на закрытые участки + _drawingObject.SetObject(x, y, _width, _height); + int startPosX = (int)(_drawingObject.GetCurrentPosition().Left / _size_x); + int startPosY = (int)(_drawingObject.GetCurrentPosition().Right / _size_y); + int boatWidth = (int)(_drawingObject.GetCurrentPosition().Top / _size_x); + int boatHeight = (int)(_drawingObject.GetCurrentPosition().Bottom / _size_y); + for (int i = startPosX; i <= boatWidth; i++) + { + for (int j = startPosY; j <= boatHeight; j++) + { + if (_map[i, j] == _barrier) + { + return false; + } + } + } + return true; + } + private Bitmap DrawMapWithObject() + { + Bitmap bmp = new Bitmap(_width, _height); + if (_drawingObject == null || _map == null) + { + return bmp; + } + Graphics gr = Graphics.FromImage(bmp); + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + if (_map[i, j] == _freeRoad) + { + DrawRoadPart(gr, i, j); + } + else if (_map[i, j] == _barrier) + { + DrawBarrierPart(gr, i, j); + } + } + } + _drawingObject.DrawingObject(gr); + return bmp; + } + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(Graphics g, int i, int j); + protected abstract void DrawBarrierPart(Graphics g, int i, int j); + + } +} diff --git a/Catamaran/Catamaran.csproj b/Catamaran/Catamaran.csproj index 76631a9..67092f0 100644 --- a/Catamaran/Catamaran.csproj +++ b/Catamaran/Catamaran.csproj @@ -46,19 +46,35 @@ - + + - + + + + Form - - CatamaranForm.cs + + FormBoat.cs + + Form + + + FormMap.cs + + - - CatamaranForm.cs + + + + FormBoat.cs + + + FormMap.cs ResXFileCodeGenerator diff --git a/Catamaran/Direction.cs b/Catamaran/Direction.cs index 97c9f4e..93cb90b 100644 --- a/Catamaran/Direction.cs +++ b/Catamaran/Direction.cs @@ -11,6 +11,7 @@ namespace Catamaran /// internal enum Direction { + None = 0, Up = 1, Down = 2, Left = 3, diff --git a/Catamaran/DrawingBoat.cs b/Catamaran/DrawingBoat.cs new file mode 100644 index 0000000..8b31e8e --- /dev/null +++ b/Catamaran/DrawingBoat.cs @@ -0,0 +1,191 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal class DrawingBoat + { + /// + /// Класс-сущность + /// + public EntityBoat Catamaran { set; get; } + /// + /// Левая координата отрисовки лодки + /// + protected float _startPosX; + /// + /// Верхняя кооридната отрисовки лодки + /// + protected float _startPosY; + /// + /// Ширина окна отрисовки + /// + protected int? _pictureWidth = null; + /// + /// Высота окна отрисовки + /// + protected int? _pictureHeight = null; + /// + /// Ширина отрисовки лодки + /// + protected readonly int _catamaranWidth = 80; + /// + /// Высота отрисовки лодки + /// + protected readonly int _catamaranHeight = 50; + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес лодки + /// Цвет лодки + public DrawingBoat(int speed, float weight, Color bodyColor) + { + Catamaran = new EntityBoat(speed, weight, bodyColor); + } + + /// + /// Получение текущей позиции объекта + /// + /// + public (float Left, float Right, float Top, float Bottom) + GetCurrentPosition() + { + return (_startPosX, _startPosY, _startPosX + _catamaranWidth, _startPosY + + _catamaranHeight); + } + + /// + /// Установка позиции лодки + /// + /// Координата X + /// Координата Y + /// Ширина картинки + /// Высота картинки + public void SetPosition(int x, int y, int width, int height) + { + if (x < 0 || y < 0 || x + _catamaranWidth > width || y + _catamaranHeight > height) return; + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(Direction direction) + { + if (!_pictureWidth.HasValue || !_pictureHeight.HasValue) + { + return; + } + switch (direction) + { + // вправо + case Direction.Right: + if (_startPosX + _catamaranWidth + Catamaran.Step < _pictureWidth) + { + _startPosX += Catamaran.Step; + } + break; + //влево + case Direction.Left: + if (_startPosX - Catamaran.Step > 0) + { + _startPosX -= Catamaran.Step; + } + break; + //вверх + case Direction.Up: + if (_startPosY - Catamaran.Step > 0) + { + _startPosY -= Catamaran.Step; + } + break; + //вниз + case Direction.Down: + if (_startPosY + _catamaranHeight + Catamaran.Step < _pictureHeight) + { + _startPosY += Catamaran.Step; + } + break; + } + } + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес автомобиля + /// Цвет кузова + /// Ширина отрисовки автомобиля + /// Высота отрисовки автомобиля + protected DrawingBoat(int speed, float weight, Color bodyColor, int + catamaranWidth, int catamaranHeight) : + this(speed, weight, bodyColor) + { + _catamaranWidth = catamaranWidth; + _catamaranHeight = catamaranHeight; + } + + /// + /// Отрисовка лодки + /// + /// + public virtual void DrawTransport(Graphics g) + { + if (_startPosX < 0 || _startPosY < 0 + || !_pictureHeight.HasValue || !_pictureWidth.HasValue) + { + return; + } + Pen pen = new Pen(Color.Black); + //границы + g.DrawRectangle(pen, _startPosX, _startPosY, _catamaranWidth * 3 / 4, _catamaranHeight); + + Point point_1 = new Point((int)(_startPosX + _catamaranWidth * 3 / 4), (int)_startPosY); + Point point_2 = new Point((int)(_startPosX + _catamaranWidth), (int)(_startPosY + _catamaranHeight / 2)); + Point point_3 = new Point((int)(_startPosX + _catamaranWidth * 3 / 4), (int)(_startPosY + _catamaranHeight)); + Point[] pointsArray = {point_1, point_2, point_3}; + g.DrawPolygon(pen, pointsArray); + Brush br = new SolidBrush(Catamaran?.BodyColor ?? Color.Black); + g.FillRectangle(br, _startPosX, _startPosY, _catamaranWidth * 3 / 4, _catamaranHeight); + g.FillPolygon(br, pointsArray); + + // середина + g.DrawEllipse(pen, _startPosX + 5, _startPosY + 5, _catamaranWidth - 15, _catamaranHeight - 11); + Brush brBlue = new SolidBrush(Color.LightBlue); + g.FillEllipse(brBlue, _startPosX + 5, _startPosY + 5, _catamaranWidth - 15, _catamaranHeight - 11); + } + /// + /// Смена границ формы отрисовки + /// + /// Ширина картинки + /// Высота картинки + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _catamaranWidth || _pictureHeight <= _catamaranHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _catamaranWidth > _pictureWidth) + { + _startPosX = _pictureWidth.Value - _catamaranWidth; + } + if (_startPosY + _catamaranHeight > _pictureHeight) + { + _startPosY = _pictureHeight.Value - _catamaranHeight; + } + } + } +} + + diff --git a/Catamaran/DrawingCatamaran.cs b/Catamaran/DrawingCatamaran.cs index 574e993..534143f 100644 --- a/Catamaran/DrawingCatamaran.cs +++ b/Catamaran/DrawingCatamaran.cs @@ -7,158 +7,56 @@ using System.Threading.Tasks; namespace Catamaran { - internal class DrawingCatamaran + internal class DrawingCatamaran : DrawingBoat { - /// - /// Класс-сущность - /// - public EntityCatamaran Catamaran { private set; get; } - /// - /// Левая координата отрисовки лодки - /// - private float _startPosX; - /// - /// Верхняя кооридната отрисовки лодки - /// - private float _startPosY; - /// - /// Ширина окна отрисовки - /// - private int? _pictureWidth = null; - /// - /// Высота окна отрисовки - /// - private int? _pictureHeight = null; - /// - /// Ширина отрисовки лодки - /// - private readonly int _catamaranWidth = 80; - /// - /// Высота отрисовки лодки - /// - private readonly int _catamaranHeight = 50; /// /// Инициализация свойств /// /// Скорость - /// Вес лодки - /// Цвет лодки - public void Init(int speed, float weight, Color bodyColor) + /// Вес автомобиля + /// Цвет кузова + /// Дополнительный цвет + /// Признак наличия обвеса + /// Признак наличия антикрыла + public DrawingCatamaran(int speed, float weight, Color bodyColor, Color + dopColor, bool Floats, bool Sail) : + base(speed, weight, bodyColor, 110, 60) { - Catamaran = new EntityCatamaran(); - Catamaran.Init(speed, weight, bodyColor); + Catamaran = new EntityCatamaran(speed, weight, bodyColor, dopColor, Floats, + Sail); } - /// - /// Установка позиции лодки - /// - /// Координата X - /// Координата Y - /// Ширина картинки - /// Высота картинки - public void SetPosition(int x, int y, int width, int height) + public override void DrawTransport(Graphics g) { - if (x < 0 || y < 0 || x + _catamaranWidth > width || y + _catamaranHeight > height) return; - _startPosX = x; - _startPosY = y; - _pictureWidth = width; - _pictureHeight = height; - } - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(Direction direction) - { - if (!_pictureWidth.HasValue || !_pictureHeight.HasValue) - { - return; - } - switch (direction) - { - // вправо - case Direction.Right: - if (_startPosX + _catamaranWidth + Catamaran.Step < _pictureWidth) - { - _startPosX += Catamaran.Step; - } - break; - //влево - case Direction.Left: - if (_startPosX - Catamaran.Step > 0) - { - _startPosX -= Catamaran.Step; - } - break; - //вверх - case Direction.Up: - if (_startPosY - Catamaran.Step > 0) - { - _startPosY -= Catamaran.Step; - } - break; - //вниз - case Direction.Down: - if (_startPosY + _catamaranHeight + Catamaran.Step < _pictureHeight) - { - _startPosY += Catamaran.Step; - } - break; - } - } - /// - /// Отрисовка лодки - /// - /// - public void DrawTransport(Graphics g) - { - if (_startPosX < 0 || _startPosY < 0 - || !_pictureHeight.HasValue || !_pictureWidth.HasValue) + if (!(Catamaran is EntityCatamaran catamaranPro)) { return; } Pen pen = new Pen(Color.Black); - //границы - g.DrawRectangle(pen, _startPosX, _startPosY, _catamaranWidth * 3 / 4, _catamaranHeight); + Brush dopBrush = new SolidBrush(catamaranPro.DopColor); + if (catamaranPro.Floats) + { + g.DrawEllipse(pen, _startPosX, _startPosY, (int)(_catamaranWidth / 2), (int)(_catamaranHeight / 2)); + g.FillEllipse(dopBrush, _startPosX, _startPosY, (int)(_catamaranWidth / 2), (int)(_catamaranHeight / 2)); + g.DrawEllipse(pen, _startPosX, 10 + _startPosY + (int)(_catamaranHeight / 2), (int)(_catamaranWidth / 2), (int)(_catamaranHeight / 2)); + g.FillEllipse(dopBrush, _startPosX, 10 + _startPosY + (int)(_catamaranHeight / 2), (int)(_catamaranWidth / 2), (int)(_catamaranHeight / 2)); - Point point_1 = new Point((int)(_startPosX + _catamaranWidth * 3 / 4), (int)_startPosY); - Point point_2 = new Point((int)(_startPosX + _catamaranWidth), (int)(_startPosY + _catamaranHeight / 2)); - Point point_3 = new Point((int)(_startPosX + _catamaranWidth * 3 / 4), (int)(_startPosY + _catamaranHeight)); - Point[] pointsArray = {point_1, point_2, point_3}; - g.DrawPolygon(pen, pointsArray); - Brush br = new SolidBrush(Catamaran?.BodyColor ?? Color.Black); - g.FillRectangle(br, _startPosX, _startPosY, _catamaranWidth * 3 / 4, _catamaranHeight); - g.FillPolygon(br, pointsArray); + } + _startPosX += 10; + _startPosY += 5; + base.DrawTransport(g); + _startPosX -= 10; + _startPosY -= 5; + if (catamaranPro.Sail) + { + Point point_1 = new Point((int)(_startPosX + _catamaranWidth * 2 / 4), (int)_startPosY + 5); + Point point_2 = new Point((int)(_startPosX + _catamaranWidth), (int)(_startPosY + _catamaranHeight / 2)); + Point point_3 = new Point((int)(_startPosX + _catamaranWidth * 2 / 4), (int)(_startPosY + _catamaranHeight)); + Point[] pointsArray = { point_1, point_2, point_3 }; + g.DrawPolygon(pen, pointsArray); + g.FillPolygon(dopBrush, pointsArray); - // середина - g.DrawEllipse(pen, _startPosX + 5, _startPosY + 5, _catamaranWidth - 15, _catamaranHeight - 15); - Brush brBlue = new SolidBrush(Color.LightBlue); - g.FillEllipse(brBlue, _startPosX + 5, _startPosY + 5, _catamaranWidth - 15, _catamaranHeight - 15); - } - /// - /// Смена границ формы отрисовки - /// - /// Ширина картинки - /// Высота картинки - public void ChangeBorders(int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - if (_pictureWidth <= _catamaranWidth || _pictureHeight <= _catamaranHeight) - { - _pictureWidth = null; - _pictureHeight = null; - return; - } - if (_startPosX + _catamaranWidth > _pictureWidth) - { - _startPosX = _pictureWidth.Value - _catamaranWidth; - } - if (_startPosY + _catamaranHeight > _pictureHeight) - { - _startPosY = _pictureHeight.Value - _catamaranHeight; } + } } } - - diff --git a/Catamaran/DrawingObjectBoat.cs b/Catamaran/DrawingObjectBoat.cs new file mode 100644 index 0000000..01c436c --- /dev/null +++ b/Catamaran/DrawingObjectBoat.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal class DrawingObjectBoat : IDrawingObject + { + private DrawingBoat _catamaran = null; + public DrawingObjectBoat(DrawingBoat catamaran) + { + _catamaran = catamaran; + } + public float Step => _catamaran?.Catamaran?.Step ?? 0; + public (float Left, float Right, float Top, float Bottom) + GetCurrentPosition() + { + return _catamaran?.GetCurrentPosition() ?? default; + } + public void MoveObject(Direction direction) + { + _catamaran?.MoveTransport(direction); + } + public void SetObject(int x, int y, int width, int height) + { + _catamaran.SetPosition(x, y, width, height); + } + public void DrawingObject(Graphics g) + { + + _catamaran.DrawTransport(g); + } + + } +} diff --git a/Catamaran/EntityBoat.cs b/Catamaran/EntityBoat.cs new file mode 100644 index 0000000..499bbbe --- /dev/null +++ b/Catamaran/EntityBoat.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal class EntityBoat + { + /// + /// Скорость + /// + public int Speed { get; private set; } + /// + /// Вес + /// + public float Weight { get; private set; } + /// + /// Цвет + /// + public Color BodyColor { get; private set; } + /// + /// Шаг перемещения + /// + public float Step => Speed * 100 / Weight; + /// + /// Инициализация полей объекта-класса + /// + /// + /// + /// + /// + public EntityBoat(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.Next(50, 150) : speed; + Weight = weight <= 0 ? rnd.Next(40, 70) : weight; + BodyColor = bodyColor; + } + } +} diff --git a/Catamaran/EntityCatamaran.cs b/Catamaran/EntityCatamaran.cs index 2b103ab..00c487d 100644 --- a/Catamaran/EntityCatamaran.cs +++ b/Catamaran/EntityCatamaran.cs @@ -7,37 +7,33 @@ using System.Threading.Tasks; namespace Catamaran { - internal class EntityCatamaran + internal class EntityCatamaran : EntityBoat { + public Color DopColor { get; private set; } /// - /// Скорость + /// Признак наличия поплавков /// - public int Speed { get; private set; } + public bool Floats { get; private set; } /// - /// Вес + /// Признак наличия паруса /// - public float Weight { get; private set; } + public bool Sail { get; private set; } /// - /// Цвет + /// Инициализация свойств /// - public Color BodyColor { get; private set; } - /// - /// Шаг перемещения - /// - public float Step => Speed * 100 / Weight; - /// - /// Инициализация полей объекта-класса - /// - /// - /// - /// - /// - public void Init(int speed, float weight, Color bodyColor) + /// /// Скорость + /// Вес лодки + /// Цвет + /// Дополнительный цвет + /// Признак наличия поплавков + /// Признак наличия паруса + public EntityCatamaran(int speed, float weight, Color bodyColor, Color + dopColor, bool Floats, bool Sail) : + base(speed, weight, bodyColor) { - Random rnd = new Random(); - Speed = speed <= 0 ? rnd.Next(50, 150) : speed; - Weight = weight <= 0 ? rnd.Next(40, 70) : weight; - BodyColor = bodyColor; + DopColor = dopColor; + this.Floats = Floats; + this.Sail = Sail; } } } diff --git a/Catamaran/CatamaranForm.Designer.cs b/Catamaran/FormBoat.Designer.cs similarity index 97% rename from Catamaran/CatamaranForm.Designer.cs rename to Catamaran/FormBoat.Designer.cs index 70467ce..42a6fb2 100644 --- a/Catamaran/CatamaranForm.Designer.cs +++ b/Catamaran/FormBoat.Designer.cs @@ -1,6 +1,6 @@ namespace Catamaran { - partial class CatamaranForm + partial class FormBoat { /// /// Обязательная переменная конструктора. @@ -72,7 +72,6 @@ this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor"; this.toolStripStatusLabelColor.Size = new System.Drawing.Size(55, 25); this.toolStripStatusLabelColor.Text = "Color"; - this.toolStripStatusLabelColor.Click += new System.EventHandler(this.toolStripStatusLabel3_Click); // // pictureBoxCatamaran // @@ -158,7 +157,6 @@ this.Controls.Add(this.statusStrip1); this.Name = "CatamaranForm"; this.Text = "Catamaran"; - this.Load += new System.EventHandler(this.CatamaranForm_Load); this.statusStrip1.ResumeLayout(false); this.statusStrip1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCatamaran)).EndInit(); diff --git a/Catamaran/CatamaranForm.cs b/Catamaran/FormBoat.cs similarity index 83% rename from Catamaran/CatamaranForm.cs rename to Catamaran/FormBoat.cs index 623dde3..16c0872 100644 --- a/Catamaran/CatamaranForm.cs +++ b/Catamaran/FormBoat.cs @@ -10,10 +10,10 @@ using System.Windows.Forms; namespace Catamaran { - public partial class CatamaranForm : Form + public partial class FormBoat : Form { - private DrawingCatamaran _catamaran; - public CatamaranForm() + private DrawingBoat _catamaran; + public FormBoat() { InitializeComponent(); } @@ -24,20 +24,11 @@ namespace Catamaran _catamaran?.DrawTransport(gr); pictureBoxCatamaran.Image = bmp; } - - private void CatamaranForm_Load(object sender, EventArgs e) - { - - } - private void toolStripStatusLabel3_Click(object sender, EventArgs e) - { - - } + private void buttonCreate_Click(object sender, EventArgs e) { Random rnd = new Random(); - _catamaran = new DrawingCatamaran(); - _catamaran.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), + _catamaran = new DrawingBoat(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); _catamaran.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxCatamaran.Width, pictureBoxCatamaran.Height); diff --git a/Catamaran/CatamaranForm.resx b/Catamaran/FormBoat.resx similarity index 100% rename from Catamaran/CatamaranForm.resx rename to Catamaran/FormBoat.resx diff --git a/Catamaran/FormMap.Designer.cs b/Catamaran/FormMap.Designer.cs new file mode 100644 index 0000000..d8212bb --- /dev/null +++ b/Catamaran/FormMap.Designer.cs @@ -0,0 +1,209 @@ +namespace Catamaran +{ + partial class FormMap + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.statusStrip1 = new System.Windows.Forms.StatusStrip(); + this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); + this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); + this.toolStripStatusLabelColor = new System.Windows.Forms.ToolStripStatusLabel(); + this.pictureBoxCatamaran = new System.Windows.Forms.PictureBox(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.buttonCreateModif = new System.Windows.Forms.Button(); + this.statusStrip1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCatamaran)).BeginInit(); + this.SuspendLayout(); + // + // statusStrip1 + // + this.statusStrip1.ImageScalingSize = new System.Drawing.Size(24, 24); + this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripStatusLabelSpeed, + this.toolStripStatusLabelWeight, + this.toolStripStatusLabelColor}); + this.statusStrip1.Location = new System.Drawing.Point(0, 436); + this.statusStrip1.Name = "statusStrip1"; + this.statusStrip1.Size = new System.Drawing.Size(826, 32); + this.statusStrip1.TabIndex = 0; + this.statusStrip1.Text = "statusStrip1"; + // + // toolStripStatusLabelSpeed + // + this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; + this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(62, 25); + this.toolStripStatusLabelSpeed.Text = "Speed"; + // + // toolStripStatusLabelWeight + // + this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; + this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(68, 25); + this.toolStripStatusLabelWeight.Text = "Weight"; + // + // toolStripStatusLabelColor + // + this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor"; + this.toolStripStatusLabelColor.Size = new System.Drawing.Size(55, 25); + this.toolStripStatusLabelColor.Text = "Color"; + // + // pictureBoxCatamaran + // + this.pictureBoxCatamaran.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxCatamaran.Location = new System.Drawing.Point(0, 0); + this.pictureBoxCatamaran.Name = "pictureBoxCatamaran"; + this.pictureBoxCatamaran.Size = new System.Drawing.Size(826, 436); + this.pictureBoxCatamaran.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; + this.pictureBoxCatamaran.TabIndex = 1; + this.pictureBoxCatamaran.TabStop = false; + // + // buttonUp + // + this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUp.BackgroundImage = global::Catamaran.Properties.Resources.Up; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonUp.Location = new System.Drawing.Point(683, 344); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.TabIndex = 2; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click); + // + // buttonDown + // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.BackgroundImage = global::Catamaran.Properties.Resources.Down; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonDown.Location = new System.Drawing.Point(683, 380); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.TabIndex = 3; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click); + // + // buttonLeft + // + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLeft.BackgroundImage = global::Catamaran.Properties.Resources.Left; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonLeft.Location = new System.Drawing.Point(647, 372); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.TabIndex = 4; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click); + // + // buttonRight + // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::Catamaran.Properties.Resources.Right; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonRight.Location = new System.Drawing.Point(719, 372); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(30, 30); + this.buttonRight.TabIndex = 5; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click); + // + // 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(14, 380); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(70, 30); + this.buttonCreate.TabIndex = 6; + this.buttonCreate.Text = "Cteate"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "SimpleMap", + "SecondMap"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 28); + this.comboBoxSelectorMap.TabIndex = 7; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.comboBoxSelectorMap_SelectedIndexChanged); + // + // 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(117, 380); + this.buttonCreateModif.Name = "buttonCreateModif"; + this.buttonCreateModif.Size = new System.Drawing.Size(102, 30); + this.buttonCreateModif.TabIndex = 8; + this.buttonCreateModif.Text = "Modification"; + this.buttonCreateModif.UseVisualStyleBackColor = true; + this.buttonCreateModif.Click += new System.EventHandler(this.buttonCreateModif_Click); + // + // FormMap + // + this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(826, 468); + this.Controls.Add(this.buttonCreateModif); + this.Controls.Add(this.comboBoxSelectorMap); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.buttonRight); + this.Controls.Add(this.buttonLeft); + this.Controls.Add(this.buttonDown); + this.Controls.Add(this.buttonUp); + this.Controls.Add(this.pictureBoxCatamaran); + this.Controls.Add(this.statusStrip1); + this.Name = "FormMap"; + this.Text = "Map"; + this.statusStrip1.ResumeLayout(false); + this.statusStrip1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCatamaran)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + private System.Windows.Forms.StatusStrip statusStrip1; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelSpeed; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelWeight; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelColor; + private System.Windows.Forms.PictureBox pictureBoxCatamaran; + private System.Windows.Forms.Button buttonUp; + private System.Windows.Forms.Button buttonDown; + private System.Windows.Forms.Button buttonLeft; + private System.Windows.Forms.Button buttonRight; + private System.Windows.Forms.Button buttonCreate; + private System.Windows.Forms.ComboBox comboBoxSelectorMap; + private System.Windows.Forms.Button buttonCreateModif; + } +} \ No newline at end of file diff --git a/Catamaran/FormMap.cs b/Catamaran/FormMap.cs new file mode 100644 index 0000000..57c3e70 --- /dev/null +++ b/Catamaran/FormMap.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Catamaran +{ + public partial class FormMap : Form + { + private AbstractMap _abstractMap; + + public FormMap() + { + InitializeComponent(); + _abstractMap = new SimpleMap(); + + } + /// + /// Заполнение информации по объекту + /// + /// + private void SetData(DrawingBoat catamaran) + { + toolStripStatusLabelSpeed.Text = $"Скорость: {catamaran.Catamaran.Speed}"; + toolStripStatusLabelWeight.Text = $"Вес: {catamaran.Catamaran.Weight}"; + toolStripStatusLabelColor.Text = $"Цвет: { catamaran.Catamaran.BodyColor.Name}"; + pictureBoxCatamaran.Image = _abstractMap.CreateMap(pictureBoxCatamaran.Width,pictureBoxCatamaran.Height,new DrawingObjectBoat(catamaran)); + } + + private void buttonCreate_Click(object sender, EventArgs e) + { + Random rnd = new Random(); + var _catamaran = new DrawingBoat(rnd.Next(100, 300), rnd.Next(1000, 2000), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + //_catamaran.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), + //pictureBoxCatamaran.Width, pictureBoxCatamaran.Height); + SetData(_catamaran); + } + private void buttonMove_Click(object sender, EventArgs e) + { + //получаем имя кнопки + string name = ((Button)sender)?.Name ?? string.Empty; + Direction dir = Direction.None; + + switch (name) + { + case "buttonUp": + dir = Direction.Up; + break; + case "buttonDown": + dir = Direction.Down; + break; + case "buttonLeft": + dir = Direction.Left; + break; + case "buttonRight": + dir = Direction.Right; + break; + + } + pictureBoxCatamaran.Image = _abstractMap?.MoveObject(dir); + } + + private void comboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorMap.Text) + { + case "SimpleMap": + _abstractMap = new SimpleMap(); + break; + case "SecondMap": + _abstractMap = new SecondMap(); + break; + } + + } + + private void buttonCreateModif_Click(object sender, EventArgs e) + { + Random rnd = new Random(); + var catamaran = new DrawingCatamaran(rnd.Next(100, 300), rnd.Next(1000, + 2000), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, + 256)), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, + 256)), + Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, + 2))); + SetData(catamaran); + } + } +} diff --git a/Catamaran/FormMap.resx b/Catamaran/FormMap.resx new file mode 100644 index 0000000..174ebc7 --- /dev/null +++ b/Catamaran/FormMap.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/Catamaran/IDrawingObject.cs b/Catamaran/IDrawingObject.cs new file mode 100644 index 0000000..ce8d31a --- /dev/null +++ b/Catamaran/IDrawingObject.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal interface IDrawingObject + { + /// + /// Шаг перемещения объекта + /// + float Step { get; } + /// + /// Установка позиции объекта + /// + /// Координата X + /// Координата Y + /// Ширина полотна + /// Высота полотна + void SetObject(int x, int y, int width, int height); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(Direction direction); + /// + /// Отрисовка объекта + /// + /// + void DrawingObject(Graphics g); + /// + /// Получение текущей позиции объекта + /// + /// + (float Left, float Right, float Top, float Bottom) + GetCurrentPosition(); + + } +} diff --git a/Catamaran/Program.cs b/Catamaran/Program.cs index ed74524..9b40c48 100644 --- a/Catamaran/Program.cs +++ b/Catamaran/Program.cs @@ -16,7 +16,7 @@ namespace Catamaran { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new CatamaranForm()); + Application.Run(new FormMap()); } } } diff --git a/Catamaran/SecondMap.cs b/Catamaran/SecondMap.cs new file mode 100644 index 0000000..ea44619 --- /dev/null +++ b/Catamaran/SecondMap.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal class SecondMap : AbstractMap + { + private readonly Brush barrierColor = new SolidBrush(Color.Black); + private readonly Brush roadColor = new SolidBrush(Color.DarkCyan); + 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 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 < 20) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + } +} + diff --git a/Catamaran/SimpleMap.cs b/Catamaran/SimpleMap.cs new file mode 100644 index 0000000..275ec42 --- /dev/null +++ b/Catamaran/SimpleMap.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal class SimpleMap : AbstractMap + { + /// + /// Цвет участка закрытого + /// + private readonly Brush barrierColor = new SolidBrush(Color.Black); + /// + /// Цвет участка открытого + /// + private readonly Brush roadColor = new SolidBrush(Color.Gray); + 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 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 < 50) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + } +}