From e84730b951fecf70f39d87a6cf5a0937d89cb726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Mon, 26 Sep 2022 20:56:35 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D0=B0=D0=B1=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=B0=D0=BA=D1=82=D0=BD=D0=B0=D1=8F=20=D0=BA=D0=B0=D1=80?= =?UTF-8?q?=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SelfPropelledArtilleryUnit/AbstractMap.cs | 84 +++++++++++++++++++ SelfPropelledArtilleryUnit/FormArtillery.cs | 6 +- .../{IDrawningObject.cs => IDrawingObject.cs} | 2 +- 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 SelfPropelledArtilleryUnit/AbstractMap.cs rename SelfPropelledArtilleryUnit/{IDrawningObject.cs => IDrawingObject.cs} (91%) diff --git a/SelfPropelledArtilleryUnit/AbstractMap.cs b/SelfPropelledArtilleryUnit/AbstractMap.cs new file mode 100644 index 0000000..4d24792 --- /dev/null +++ b/SelfPropelledArtilleryUnit/AbstractMap.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Artilleries +{ + 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; + do + { + GenerateMap(); + } + while (!SetObjectOnMap()); + return DrawMapWithObject(); + } + + public Bitmap MoveObject(Direction direction) + { + // TODO доделать коллизию + _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 проверка на налажение + return true; + } + + private Bitmap DrawMapWithObject() + { + Bitmap bmp = new Bitmap(_width, _height); + if (_drawingObject == null || _map == null) + { + return bmp; + } + Graphics g = 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(g, i, j); + } else if (_map[i, j] == _barrier) + { + DrawBarrierPart(g, i, j); + } + } + } + _drawingObject.DrawingObject(g); + 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/SelfPropelledArtilleryUnit/FormArtillery.cs b/SelfPropelledArtilleryUnit/FormArtillery.cs index 785ba5f..dbdbc43 100644 --- a/SelfPropelledArtilleryUnit/FormArtillery.cs +++ b/SelfPropelledArtilleryUnit/FormArtillery.cs @@ -17,7 +17,7 @@ namespace Artilleries pictureBoxArtilleries.Image = bmp; } - private void Prepair() + private void SetData() { Random rnd = new(); _artillery.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxArtilleries.Width, pictureBoxArtilleries.Height); @@ -30,7 +30,7 @@ namespace Artilleries { Random rnd = new(); _artillery = new DrawingArtillery(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - Prepair(); + SetData(); Draw(); } @@ -44,7 +44,7 @@ namespace Artilleries Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), rnd.Next(0, 2) == 1, rnd.Next(0, 2) == 1 ); - Prepair(); + SetData(); Draw(); } diff --git a/SelfPropelledArtilleryUnit/IDrawningObject.cs b/SelfPropelledArtilleryUnit/IDrawingObject.cs similarity index 91% rename from SelfPropelledArtilleryUnit/IDrawningObject.cs rename to SelfPropelledArtilleryUnit/IDrawingObject.cs index 67cae91..4f7acc3 100644 --- a/SelfPropelledArtilleryUnit/IDrawningObject.cs +++ b/SelfPropelledArtilleryUnit/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Artilleries { - internal interface IDrawningObject + internal interface IDrawingObject { public float Step { get; } void SetObject(int x, int y, int width, int height);