PIbd_23_Kislitsa_E.D_AirFig.../AirFighter/FormAirFighter.cs

141 lines
4.8 KiB
C#
Raw Permalink Normal View History

2023-10-15 17:11:27 +04:00
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;
2023-10-02 21:42:00 +04:00
using ProjectAirFighter.DrawningObjects;
2023-10-15 17:11:27 +04:00
using ProjectAirFighter.MovementStrategy;
2023-10-02 21:42:00 +04:00
2023-09-21 18:40:56 +04:00
namespace ProjectAirFighter
{
public partial class FormAirFighter : Form
{
2023-10-15 17:11:27 +04:00
2023-10-02 21:42:00 +04:00
private DrawningAirplane? _drawningAirplane;
2023-10-15 17:11:27 +04:00
2023-10-02 21:42:00 +04:00
private AbstractStrategy? _abstractStrategy;
2023-09-21 18:40:56 +04:00
2023-10-15 17:11:27 +04:00
public DrawningAirplane? SelectedAirplane { get; private set; }
2023-09-21 18:40:56 +04:00
public FormAirFighter()
{
InitializeComponent();
2023-10-15 17:11:27 +04:00
_abstractStrategy = null;
SelectedAirplane = null;
2023-09-21 18:40:56 +04:00
}
2023-10-02 21:42:00 +04:00
2023-09-21 18:40:56 +04:00
private void Draw()
{
2023-10-02 21:42:00 +04:00
if (_drawningAirplane == null)
2023-09-21 18:40:56 +04:00
return;
2023-10-15 17:11:27 +04:00
Bitmap bmp = new(pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
2023-09-21 18:40:56 +04:00
Graphics gr = Graphics.FromImage(bmp);
2023-10-02 21:42:00 +04:00
_drawningAirplane.DrawTransport(gr);
2023-09-21 18:40:56 +04:00
pictureBoxAirFighter.Image = bmp;
}
2023-10-02 21:42:00 +04:00
private void ButtonCreateAirplane_Click(object sender, EventArgs e)
2023-09-21 18:40:56 +04:00
{
Random random = new();
2023-10-15 17:11:27 +04:00
Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
2023-09-21 18:40:56 +04:00
2023-10-15 17:11:27 +04:00
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
bodyColor = dialog.Color;
}
_drawningAirplane = new DrawningAirplane(random.Next(100, 300), random.Next(1000, 3000),
bodyColor,pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
_drawningAirplane.SetPosition(random.Next(10, 100), random.Next(70, 100));
2023-09-21 18:40:56 +04:00
Draw();
}
2023-10-15 17:11:27 +04:00
private void ButtonMoveClick(object sender, EventArgs e)
2023-09-21 18:40:56 +04:00
{
2023-10-02 21:42:00 +04:00
if (_drawningAirplane == null)
return;
2023-10-15 17:11:27 +04:00
2023-10-02 21:42:00 +04:00
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningAirplane.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningAirplane.MoveTransport(DirectionType.Down);
break;
case "buttonRight":
_drawningAirplane.MoveTransport(DirectionType.Right);
break;
2023-10-15 17:11:27 +04:00
case "buttonLeft":
_drawningAirplane.MoveTransport(DirectionType.Left);
break;
2023-10-02 21:42:00 +04:00
}
Draw();
2023-09-21 18:40:56 +04:00
}
2023-10-02 21:42:00 +04:00
2023-10-15 17:11:27 +04:00
private void ButtonCreateAirFighter_Click(object sender, EventArgs e)
{
Random random = new();
Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
Color additionalColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
bodyColor = dialog.Color;
if (dialog.ShowDialog() == DialogResult.OK)
additionalColor = dialog.Color;
_drawningAirplane = new DrawningAirFighter(random.Next(100, 300), random.Next(1000, 3000),
bodyColor,additionalColor, Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)), pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
_drawningAirplane.SetPosition(random.Next(10, 100), random.Next(70, 100));
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
2023-09-21 18:40:56 +04:00
{
2023-10-02 21:42:00 +04:00
if (_drawningAirplane == null)
return;
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectAirplane(_drawningAirplane), pictureBoxAirFighter.Width,
pictureBoxAirFighter.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
2023-09-21 18:40:56 +04:00
}
2023-10-15 17:11:27 +04:00
private void buttonSelectAirplane_Click(object sender, EventArgs e)
{
SelectedAirplane = _drawningAirplane;
DialogResult = DialogResult.OK;
}
2023-09-21 18:40:56 +04:00
}
}