166 lines
5.7 KiB
C#

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;
using WarmlyLocomotive.Drawnings;
using WarmlyLocomotive.MovementStrategy;
namespace WarmlyLocomotive
{
public partial class FormWarmlyLocomotive : Form
{
private DrawningLocomotive? _drawningLocomotive;
private AbstractStrategy _strategy;
public FormWarmlyLocomotive()
{
InitializeComponent();
_strategy = null;
}
private void Draw()
{
if (_drawningLocomotive == null)
{
return;
}
Bitmap bmp = new(pictureBoxWarmlyLocomotive.Width,
pictureBoxWarmlyLocomotive.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningLocomotive.DrawTransport(gr);
pictureBoxWarmlyLocomotive.Image = bmp;
}
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningLocomotive):
{
_drawningLocomotive = new DrawningLocomotive(
random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
}
case nameof(DrawningWarmlyLocomotive):
{
_drawningLocomotive = new DrawningWarmlyLocomotive(
random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)));
break;
}
default:
return;
}
_drawningLocomotive.SetPictureSize(pictureBoxWarmlyLocomotive.Width, pictureBoxWarmlyLocomotive.Height);
_drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
private void createButton_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningWarmlyLocomotive));
private void createBaseButton_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLocomotive));
//{
//Random random = new();
//_drawningWarmlyLocomotive = new DrawningWarmlyLocomotive();
//_drawningWarmlyLocomotive.Init(
// random.Next(100, 300),
// random.Next(1000,3000),
// Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
// Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
// Convert.ToBoolean(random.Next(0, 2)),
// Convert.ToBoolean(random.Next(0, 2)));
//_drawningWarmlyLocomotive.SetPictureSize(
// pictureBoxWarmlyLocomotive.Width,
// pictureBoxWarmlyLocomotive.Height);
//_drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
//Draw();
//}
private void moveButton_Click(object sender, EventArgs e)
{
if (_drawningLocomotive == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result =
_drawningLocomotive.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawningLocomotive.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawningLocomotive.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawningLocomotive.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
private void strategyMoveButton_Click(object sender, EventArgs e)
{
if (_drawningLocomotive == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableLocomotive(_drawningLocomotive),
pictureBoxWarmlyLocomotive.Width, pictureBoxWarmlyLocomotive.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}
}