From c4dc78fc098c3dadff0b44cd3752ae433281aa6c Mon Sep 17 00:00:00 2001 From: rozenkranzz Date: Thu, 6 Feb 2025 00:00:42 +0400 Subject: [PATCH] Added parent classes and constructors --- .../{ => Drawnings}/DirectionType.cs | 2 +- .../DrawingBaseLiner.cs} | 94 +++++++++---------- .../ProjectLiner/Drawnings/DrawingLiner.cs | 47 ++++++++++ .../ProjectLiner/Entities/BaseLinerEntity.cs | 15 +++ .../{ => Entities}/LinerEntity.cs | 14 +-- .../ProjectLiner/FormLiner.Designer.cs | 41 +++++--- ProjectLiner/ProjectLiner/FormLiner.cs | 43 +++++++-- 7 files changed, 172 insertions(+), 84 deletions(-) rename ProjectLiner/ProjectLiner/{ => Drawnings}/DirectionType.cs (69%) rename ProjectLiner/ProjectLiner/{DrawingLiner.cs => Drawnings/DrawingBaseLiner.cs} (54%) create mode 100644 ProjectLiner/ProjectLiner/Drawnings/DrawingLiner.cs create mode 100644 ProjectLiner/ProjectLiner/Entities/BaseLinerEntity.cs rename ProjectLiner/ProjectLiner/{ => Entities}/LinerEntity.cs (72%) diff --git a/ProjectLiner/ProjectLiner/DirectionType.cs b/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs similarity index 69% rename from ProjectLiner/ProjectLiner/DirectionType.cs rename to ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs index 8d54037..cacc39a 100644 --- a/ProjectLiner/ProjectLiner/DirectionType.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs @@ -1,4 +1,4 @@ -namespace ProjectLiner; +namespace ProjectLiner.Drawnings; public enum DirectionType { diff --git a/ProjectLiner/ProjectLiner/DrawingLiner.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawingBaseLiner.cs similarity index 54% rename from ProjectLiner/ProjectLiner/DrawingLiner.cs rename to ProjectLiner/ProjectLiner/Drawnings/DrawingBaseLiner.cs index f472d0e..d9d175a 100644 --- a/ProjectLiner/ProjectLiner/DrawingLiner.cs +++ b/ProjectLiner/ProjectLiner/Drawnings/DrawingBaseLiner.cs @@ -1,33 +1,40 @@ -namespace ProjectLiner; -public class DrawingLiner +using ProjectLiner.Entities; + +namespace ProjectLiner.Drawnings; + +public class DrawingBaseLiner { - public LinerEntity? LinerEntity { get; set; } - + public BaseLinerEntity? BaseLiner { get; protected set; } + private int? _pictureWidth; private int? _pictureHeight; - private int? _startPosX; + protected int? _startPosX; - private int? _startPosY; + protected int? _startPosY; private readonly int _drawingLinerWidth = 140; - private readonly int _drawingLinerHeight = 50; - public void Init( - int speed, double weight, Color primaryColor, Color secondaryColor, - LinerEntityType type, int capacity, int maxPassengers, - bool hasExtraDeck, bool hasPool - ) + private readonly int _drawingLinerHeight = 40; + + private DrawingBaseLiner() { - LinerEntity = new LinerEntity(); - LinerEntity.Init(speed, weight, primaryColor, secondaryColor, - type, capacity, maxPassengers, hasExtraDeck, hasPool); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + public DrawingBaseLiner(int speed, double weight, Color primaryColor) : this() + { + BaseLiner = new BaseLinerEntity(speed, weight, primaryColor); + } + + public DrawingBaseLiner(int drawingBaseLinerWidth, int drawingBaseLinerHeight) + { + _drawingLinerHeight = drawingBaseLinerHeight; + _drawingLinerWidth = drawingBaseLinerWidth; + } public bool SetPictureSize(int width, int height) { if (_drawingLinerWidth <= width && _drawingLinerHeight <= height) @@ -35,7 +42,8 @@ public class DrawingLiner _pictureWidth = width; _pictureHeight = height; return true; - } else + } + else { return false; } @@ -46,7 +54,7 @@ public class DrawingLiner { return; } - if (x < 0) + if (x < 0) { x = 0; } @@ -67,7 +75,7 @@ public class DrawingLiner } public bool MoveTransport(DirectionType direction) { - if (LinerEntity == null || + if (BaseLiner == null || !_startPosX.HasValue || !_startPosY.HasValue || !_pictureWidth.HasValue || !_pictureHeight.HasValue) { @@ -77,32 +85,32 @@ public class DrawingLiner { //left case DirectionType.Left: - if (_startPosX.Value - LinerEntity.Step > 0) + if (_startPosX.Value - BaseLiner.Step > 0) { - _startPosX -= (int)LinerEntity.Step; + _startPosX -= (int)BaseLiner.Step; } break; //up case DirectionType.Up: - if (_startPosY.Value - LinerEntity.Step > 0) + if (_startPosY.Value - BaseLiner.Step > 0) { - _startPosY -= (int)LinerEntity.Step; + _startPosY -= (int)BaseLiner.Step; } break; //right case DirectionType.Right: - if (_startPosX.Value + LinerEntity.Step < + if (_startPosX.Value + BaseLiner.Step < _pictureWidth.Value - _drawingLinerWidth) { - _startPosX += (int)LinerEntity.Step; + _startPosX += (int)BaseLiner.Step; } break; //down case DirectionType.Down: - if (_startPosY.Value + LinerEntity.Step < + if (_startPosY.Value + BaseLiner.Step < _pictureHeight.Value - _drawingLinerHeight) { - _startPosY += (int)LinerEntity.Step; + _startPosY += (int)BaseLiner.Step; } break; default: @@ -110,9 +118,9 @@ public class DrawingLiner } return true; } - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (LinerEntity == null || + if (BaseLiner == null || !_startPosX.HasValue || !_startPosY.HasValue) return; @@ -120,38 +128,22 @@ public class DrawingLiner int y = _startPosY.Value; Pen borderPen = new(Color.Black); - Brush bodyBrush = new SolidBrush(LinerEntity.PrimaryColor); // Hull - Brush additionalBrush = new SolidBrush(LinerEntity.SecondaryColor); + Brush bodyBrush = new SolidBrush(BaseLiner.PrimaryColor); // Hull Brush deckBrush = new SolidBrush(Color.White); // Deck - Brush poolBrush = new SolidBrush(Color.Cyan); // Pool // body (hull) Point[] hullPoints = { - new Point(x + 20, y + 50), // bottom Left - new Point(x + 120, y + 50), // bottom right - new Point(x + 140, y + 20), // Top left - new Point(x , y + 20) // Top right + new Point(x + 20, y + 40), // bottom Left + new Point(x + 120, y + 40), // bottom right + new Point(x + 140, y + 10), // Top left + new Point(x , y + 10) // Top right }; g.FillPolygon(bodyBrush, hullPoints); g.DrawPolygon(borderPen, hullPoints); // first deck - g.FillRectangle(deckBrush, x + 30, y + 10, 100, 10); - g.DrawRectangle(borderPen, x + 30, y + 10, 100, 10); - - if (LinerEntity.HasPool) - { - // pool on the deck - g.FillEllipse(poolBrush, x + 35, y + 5, 30, 10); - g.DrawEllipse(borderPen, x + 35, y + 5, 30, 10); - } - - if (LinerEntity.HasExtraDeck) - { - // optional deck - g.FillRectangle(additionalBrush, x + 70, y, 50, 10); - g.DrawRectangle(borderPen, x + 70, y, 50, 10); - } + g.FillRectangle(deckBrush, x + 30, y, 100, 10); + g.DrawRectangle(borderPen, x + 30, y, 100, 10); } } diff --git a/ProjectLiner/ProjectLiner/Drawnings/DrawingLiner.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawingLiner.cs new file mode 100644 index 0000000..c7d85cb --- /dev/null +++ b/ProjectLiner/ProjectLiner/Drawnings/DrawingLiner.cs @@ -0,0 +1,47 @@ +using ProjectLiner.Entities; + +namespace ProjectLiner.Drawnings; +public class DrawingLiner : DrawingBaseLiner +{ + public DrawingLiner( + int speed, double weight, Color primaryColor, Color secondaryColor, + LinerEntityType type, int capacity, int maxPassengers, + bool hasExtraDeck, bool hasPool + ) : base(140, 50) + { + BaseLiner = new LinerEntity(speed, weight, primaryColor, secondaryColor, + type, capacity, maxPassengers, hasExtraDeck, hasPool); + } + + public override void DrawTransport(Graphics g) + { + if (BaseLiner == null || BaseLiner is not LinerEntity linerEntity || + !_startPosX.HasValue || !_startPosY.HasValue) + return; + + _startPosY += 10; + base.DrawTransport(g); + _startPosY -= 10; + + int x = _startPosX.Value; + int y = _startPosY.Value; + + Pen borderPen = new(Color.Black); + Brush additionalBrush = new SolidBrush(linerEntity.SecondaryColor); + Brush poolBrush = new SolidBrush(Color.Cyan); // Pool + + if (linerEntity.HasPool) + { + // pool on the deck + g.FillEllipse(poolBrush, x + 35, y + 5, 30, 10); + g.DrawEllipse(borderPen, x + 35, y + 5, 30, 10); + } + + if (linerEntity.HasExtraDeck) + { + // optional deck + g.FillRectangle(additionalBrush, x + 70, y, 50, 10); + g.DrawRectangle(borderPen, x + 70, y, 50, 10); + } + } +} diff --git a/ProjectLiner/ProjectLiner/Entities/BaseLinerEntity.cs b/ProjectLiner/ProjectLiner/Entities/BaseLinerEntity.cs new file mode 100644 index 0000000..5e029a8 --- /dev/null +++ b/ProjectLiner/ProjectLiner/Entities/BaseLinerEntity.cs @@ -0,0 +1,15 @@ +namespace ProjectLiner.Entities; +public class BaseLinerEntity +{ + public int Speed { get; private set; } + public double Weight { get; private set; } + public Color PrimaryColor { get; private set; } + public double Step => Speed / (Weight / 100); + + public BaseLinerEntity(int speed, double weight, Color primaryColor) + { + Speed = speed; + Weight = weight; + PrimaryColor = primaryColor; + } +} diff --git a/ProjectLiner/ProjectLiner/LinerEntity.cs b/ProjectLiner/ProjectLiner/Entities/LinerEntity.cs similarity index 72% rename from ProjectLiner/ProjectLiner/LinerEntity.cs rename to ProjectLiner/ProjectLiner/Entities/LinerEntity.cs index e7a740a..7d0a215 100644 --- a/ProjectLiner/ProjectLiner/LinerEntity.cs +++ b/ProjectLiner/ProjectLiner/Entities/LinerEntity.cs @@ -1,4 +1,4 @@ -namespace ProjectLiner; +namespace ProjectLiner.Entities; public enum LinerEntityType { Passenger, @@ -7,11 +7,8 @@ public enum LinerEntityType Mixed } -public class LinerEntity +public class LinerEntity : BaseLinerEntity { - public int Speed { get; private set; } - public double Weight { get; private set; } - public Color PrimaryColor { get; private set; } public Color SecondaryColor { get; private set; } public LinerEntityType Type { get; private set; } public int Capacity { get; private set; } @@ -20,15 +17,12 @@ public class LinerEntity public bool HasPool { get; private set; } public double Step => Speed / (Weight / 100); - public void Init( + public LinerEntity( int speed, double weight, Color primaryColor, Color secondaryColor, LinerEntityType type, int capacity, int maxPassengers, bool hasExtraDeck, bool hasPool - ) + ) : base(speed, weight, primaryColor) { - Speed = speed; - Weight = weight; - PrimaryColor = primaryColor; SecondaryColor = secondaryColor; Type = type; Capacity = capacity; diff --git a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs index c7948ba..7249588 100644 --- a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs +++ b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs @@ -30,11 +30,12 @@ { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormLiner)); pictureBoxLiner = new PictureBox(); - buttonCreateLiner = new Button(); + buttonCreateFullLiner = new Button(); buttonMoveLeft = new Button(); buttonMoveUp = new Button(); buttonMoveDown = new Button(); buttonMoveRight = new Button(); + buttonCreateBaseLiner = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxLiner).BeginInit(); SuspendLayout(); // @@ -48,17 +49,17 @@ pictureBoxLiner.TabIndex = 0; pictureBoxLiner.TabStop = false; // - // buttonCreateLiner + // buttonCreateFullLiner // - buttonCreateLiner.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateLiner.Location = new Point(10, 589); - buttonCreateLiner.Margin = new Padding(3, 2, 3, 2); - buttonCreateLiner.Name = "buttonCreateLiner"; - buttonCreateLiner.Size = new Size(82, 22); - buttonCreateLiner.TabIndex = 1; - buttonCreateLiner.Text = "Create"; - buttonCreateLiner.UseVisualStyleBackColor = true; - buttonCreateLiner.Click += ButtonCreateLiner_Click; + buttonCreateFullLiner.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateFullLiner.Location = new Point(10, 589); + buttonCreateFullLiner.Margin = new Padding(3, 2, 3, 2); + buttonCreateFullLiner.Name = "buttonCreateFullLiner"; + buttonCreateFullLiner.Size = new Size(114, 22); + buttonCreateFullLiner.TabIndex = 1; + buttonCreateFullLiner.Text = "Create Full Liner"; + buttonCreateFullLiner.UseVisualStyleBackColor = true; + buttonCreateFullLiner.Click += ButtonCreateLiner_Click; // // buttonMoveLeft // @@ -112,16 +113,29 @@ buttonMoveRight.UseVisualStyleBackColor = true; buttonMoveRight.Click += ButtonMove_Click; // + // buttonCreateBaseLiner + // + buttonCreateBaseLiner.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBaseLiner.Location = new Point(130, 589); + buttonCreateBaseLiner.Margin = new Padding(3, 2, 3, 2); + buttonCreateBaseLiner.Name = "buttonCreateBaseLiner"; + buttonCreateBaseLiner.Size = new Size(114, 22); + buttonCreateBaseLiner.TabIndex = 6; + buttonCreateBaseLiner.Text = "Create Base Liner"; + buttonCreateBaseLiner.UseVisualStyleBackColor = true; + buttonCreateBaseLiner.Click += buttonCreateBaseLiner_Click; + // // FormLiner // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(995, 620); + Controls.Add(buttonCreateBaseLiner); Controls.Add(buttonMoveRight); Controls.Add(buttonMoveDown); Controls.Add(buttonMoveUp); Controls.Add(buttonMoveLeft); - Controls.Add(buttonCreateLiner); + Controls.Add(buttonCreateFullLiner); Controls.Add(pictureBoxLiner); Margin = new Padding(3, 2, 3, 2); Name = "FormLiner"; @@ -134,10 +148,11 @@ #endregion private PictureBox pictureBoxLiner; - private Button buttonCreateLiner; + private Button buttonCreateFullLiner; private Button buttonMoveLeft; private Button buttonMoveUp; private Button buttonMoveDown; private Button buttonMoveRight; + private Button buttonCreateBaseLiner; } } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/FormLiner.cs b/ProjectLiner/ProjectLiner/FormLiner.cs index 9b438a5..2fd2351 100644 --- a/ProjectLiner/ProjectLiner/FormLiner.cs +++ b/ProjectLiner/ProjectLiner/FormLiner.cs @@ -1,8 +1,11 @@ -namespace ProjectLiner +using ProjectLiner.Drawnings; +using ProjectLiner.Entities; + +namespace ProjectLiner { public partial class FormLiner : Form { - public DrawingLiner? _drawingLiner; + public DrawingBaseLiner? _drawingLiner; public FormLiner() { InitializeComponent(); @@ -20,21 +23,41 @@ pictureBoxLiner.Image = bitmap; } - private void ButtonCreateLiner_Click(object sender, EventArgs e) + private void CreateObject(string type) { Random random = new(); - _drawingLiner = new DrawingLiner(); - _drawingLiner.Init(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), - Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), - LinerEntityType.Cargo, random.Next(1000, 10000), random.Next(10, 100), - random.Next(0, 2) == 1, random.Next(0, 2) == 1); + switch (type) + { + case nameof(DrawingLiner): + _drawingLiner = new DrawingLiner(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), + Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), + LinerEntityType.Cargo, random.Next(1000, 10000), random.Next(10, 100), + random.Next(0, 2) == 1, random.Next(0, 2) == 1); + break; + case nameof(DrawingBaseLiner): + _drawingLiner = new DrawingBaseLiner(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255))); + break; + default: + return; + } _drawingLiner.SetPictureSize(pictureBoxLiner.Width, pictureBoxLiner.Height); _drawingLiner.SetPosition(random.Next(0, pictureBoxLiner.Width), random.Next(0, pictureBoxLiner.Height)); DrawTransoprt(); } + private void ButtonCreateLiner_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawingLiner)); + } + + private void buttonCreateBaseLiner_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawingBaseLiner)); + } + private void ButtonMove_Click(object sender, EventArgs e) { if (_drawingLiner == null) @@ -65,5 +88,7 @@ DrawTransoprt(); } } + + } }