Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Upd: создан класс Корабль и Маршрутный лист, перенесено всё в классы
- #pragma once
- #include "windows.h"
- #include <list>
- namespace ships {
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- using namespace System::Collections::Generic;
- /// <summary>
- /// Сводка для MyForm
- /// </summary>
- enum class Direction
- {
- EAST,
- SOUTH,
- WEST,
- NORTH
- };
- enum class ShipMode
- {
- ROADS_MODE, //0-на рейде
- FULL_SPEED_MODE, //1-полный ход
- SLOW_SPEED_MODE, //2-малый ход
- UNLOAD_MODE, //3-разгрузка
- LOAD_MODE //4-загрузка
- };
- private ref class RoutePoint
- {
- public:
- const int x;
- const int y;
- const Direction dir;
- const ShipMode mode;
- RoutePoint(int _x, int _y, Direction _dir, ShipMode _mode): x(_x), y(_y),dir(_dir),mode(_mode) {}
- };
- private ref class Ship
- {
- private:
- String^ name;
- int passengers; //кол-во пассажирова на судне
- int capacity; //максимальная вместимсть пассажиров
- int routeTargetIndex; //номер целевой точки в маршрутном листе
- double x;
- double y;
- double v; //м/c
- Direction direction;
- ShipMode mode; //режим судна
- List<RoutePoint^>^ route = nullptr;
- public:
- Ship(String^ n, int p, int c)
- {
- name = n;
- passengers = p;
- capacity = c;
- routeTargetIndex = -1;
- x = 0;
- y = 0;
- v = 0;
- direction = Direction::EAST;
- mode = ShipMode::ROADS_MODE;
- }
- double get_x()
- {
- return x;
- }
- double get_y()
- {
- return y;
- }
- int get_p()
- {
- return passengers;
- }
- int get_c()
- {
- return capacity;
- }
- Direction get_dir()
- {
- return direction;
- }
- ShipMode get_mode()
- {
- return mode;
- }
- String^ get_name()
- {
- return name;
- }
- void load(int dp) //dp - кол-во входящих/выходящих пассажиров в секунду
- {
- passengers += dp;
- if (passengers > capacity) passengers = capacity;
- }
- void unload(int dp)
- {
- passengers -= dp;
- if (passengers < 0) passengers = 0;
- }
- void setRoute(List<RoutePoint^>^ r) //функция установки маршрутного листа
- {
- route = r;
- routeTargetIndex = 0;
- x = route[0]->x;
- y = route[0]->y;
- direction = route[0]->dir;
- mode = route[0]->mode;
- v = vFromMode(mode);
- }
- double vFromMode(ShipMode m) //функция расчета скорости в зависимости от режима
- {
- switch (m)
- {
- case ships::ShipMode::ROADS_MODE:
- return 0;
- case ships::ShipMode::FULL_SPEED_MODE:
- return 2.0;
- case ships::ShipMode::SLOW_SPEED_MODE:
- return 0.5;
- case ships::ShipMode::UNLOAD_MODE:
- return 0;
- case ships::ShipMode::LOAD_MODE:
- return 0;
- default:
- return 0;
- }
- }
- static String^ modeStr(ShipMode m) //функция для вывода информационных сообщений
- {
- switch (m)
- {
- case ships::ShipMode::ROADS_MODE:
- return "Ожидаю на рейде";
- case ships::ShipMode::FULL_SPEED_MODE:
- return "Режим полный ход";
- case ships::ShipMode::SLOW_SPEED_MODE:
- return "Режим малый ход";
- case ships::ShipMode::UNLOAD_MODE:
- return "Режим разгрузки";
- case ships::ShipMode::LOAD_MODE:
- return "Режим загрузки";
- default:
- return "Неизвестный режим";
- }
- }
- void nextSecond() //функция моделирования движения судна
- {
- if (route == nullptr) return;
- if (routeTargetIndex < 0 || routeTargetIndex >= route->Count) return;
- if (v > 0)
- {
- int tx = route[routeTargetIndex]->x; //tx,ty - координаты целевой точки на маршруте
- int ty = route[routeTargetIndex]->y;
- double d = Math::Sqrt((x - tx)*(x - tx) + (y - ty)*(y - ty)); //расстояние от текущей точ
- if (d < v)
- {
- if (routeTargetIndex == route->Count - 1)
- {
- return;
- }
- direction = route[routeTargetIndex]->dir;
- mode = route[routeTargetIndex]->mode;
- v = vFromMode(mode);
- if (v > 0)
- {
- routeTargetIndex++;
- }
- x = tx;
- y = ty;
- return;
- }
- else
- {
- switch (direction)
- {
- case ships::Direction::EAST:
- x += v;
- break;
- case ships::Direction::SOUTH:
- y += v;
- break;
- case ships::Direction::WEST:
- x -= v;
- break;
- case ships::Direction::NORTH:
- y -= v;
- break;
- default:
- break;
- }
- }
- return;
- }
- else
- {
- if (mode == ShipMode::UNLOAD_MODE)
- {
- unload(5);
- if (passengers == 0)
- {
- routeTargetIndex++;
- if (routeTargetIndex < route->Count)
- {
- mode = route[routeTargetIndex]->mode;
- v = vFromMode(mode);
- }
- }
- return;
- }
- if (mode == ShipMode::LOAD_MODE)
- {
- load(5);
- if (passengers == capacity)
- {
- routeTargetIndex++;
- if (routeTargetIndex < route->Count)
- {
- mode = route[routeTargetIndex]->mode;
- v = vFromMode(mode);
- }
- }
- return;
- }
- }
- }
- };
- public ref class MyForm : public System::Windows::Forms::Form
- {
- Image^ ship1 = Image::FromFile("C:/Users/golubova/Desktop/ship1.png");
- Image^ ship1_2 = Image::FromFile("C:/Users/golubova/Desktop/ship_135.png");
- Image^ ship2 = Image::FromFile("C:/Users/golubova/Desktop/ship2.png");
- Image^ ship2_3 = Image::FromFile("C:/Users/golubova/Desktop/ship_225.png");
- Image^ ship3 = Image::FromFile("C:/Users/golubova/Desktop/ship3.png");
- Image^ ship3_4 = Image::FromFile("C:/Users/golubova/Desktop/ship_315.png");
- Image^ ship4 = Image::FromFile("C:/Users/golubova/Desktop/ship4.png");
- Image^ ship4_1 = Image::FromFile("C:/Users/golubova/Desktop/ship_45.png");
- int height, width;
- const double metrPerPixels = 2;
- int modelSecondPerRealSecond = 1; //1 секунда на 1 шаг моделирования (тик)
- List<RoutePoint^>^ route = gcnew List<RoutePoint^>();
- Ship^ pShip = gcnew Ship("Беринг", 100, 100);
- void modelStep()
- {
- for (int k = 0; k < modelSecondPerRealSecond; k++)
- {
- ShipMode oldMode = pShip->get_mode();
- pShip->nextSecond();
- ShipMode newMode = pShip->get_mode();
- if (oldMode != newMode)
- {
- String^ msg = "'" + pShip->get_name() + "' : " + Ship::modeStr(newMode);
- listBox1->Items->Add(msg);
- }
- }
- }
- public:
- MyForm(void)
- {
- InitializeComponent();
- //
- //TODO: добавьте код конструктора
- //
- }
- protected:
- /// <summary>
- /// Освободить все используемые ресурсы.
- /// </summary>
- ~MyForm()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Windows::Forms::Timer^ timer1;
- private: System::Windows::Forms::TrackBar^ trackBar1;
- private: System::Windows::Forms::ListBox^ listBox1;
- private: System::Windows::Forms::PictureBox^ picPort;
- //private: System::Windows::Forms::PictureBox^ picPortKors;
- protected:
- private: System::Windows::Forms::PictureBox^ picShip;
- private: System::ComponentModel::IContainer^ components;
- protected:
- protected:
- protected:
- private:
- /// <summary>
- /// Обязательная переменная конструктора.
- /// </summary>
- #pragma region Windows Form Designer generated code
- /// <summary>
- /// Требуемый метод для поддержки конструктора — не изменяйте
- /// содержимое этого метода с помощью редактора кода.
- /// </summary>
- void InitializeComponent(void)
- {
- this->components = (gcnew System::ComponentModel::Container());
- System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(MyForm::typeid));
- this->picPort = (gcnew System::Windows::Forms::PictureBox());
- this->picShip = (gcnew System::Windows::Forms::PictureBox());
- this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
- this->trackBar1 = (gcnew System::Windows::Forms::TrackBar());
- this->listBox1 = (gcnew System::Windows::Forms::ListBox());
- //this->picPortKors = (gcnew System::Windows::Forms::PictureBox());
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPort))->BeginInit();
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picShip))->BeginInit();
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->trackBar1))->BeginInit();
- //(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPortKors))->BeginInit();
- this->SuspendLayout();
- //
- // picPort
- //
- this->picPort->BackColor = System::Drawing::SystemColors::ActiveCaption;
- this->picPort->Dock = System::Windows::Forms::DockStyle::Fill;
- this->picPort->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"picPort.Image")));
- this->picPort->Location = System::Drawing::Point(0, 0);
- this->picPort->Name = L"picPort";
- this->picPort->Size = System::Drawing::Size(871, 613);
- this->picPort->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
- this->picPort->TabIndex = 0;
- this->picPort->TabStop = false;
- //
- // picShip
- //
- this->picShip->BackColor = System::Drawing::Color::LightSkyBlue;
- this->picShip->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"picShip.Image")));
- this->picShip->Location = System::Drawing::Point(0, 12);
- this->picShip->Name = L"picShip";
- this->picShip->Size = System::Drawing::Size(51, 20);
- this->picShip->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
- this->picShip->TabIndex = 1;
- this->picShip->TabStop = false;
- this->picShip->Visible = false;
- this->picShip->Click += gcnew System::EventHandler(this, &MyForm::pictureBox2_Click);
- //
- // timer1
- //
- this->timer1->Enabled = true;
- this->timer1->Interval = 20;
- this->timer1->Tick += gcnew System::EventHandler(this, &MyForm::timer1_Tick);
- //
- // trackBar1
- //
- this->trackBar1->Location = System::Drawing::Point(660, 520);
- this->trackBar1->Maximum = 100;
- this->trackBar1->Minimum = 1;
- this->trackBar1->Name = L"trackBar1";
- this->trackBar1->Size = System::Drawing::Size(189, 69);
- this->trackBar1->TabIndex = 2;
- this->trackBar1->TickFrequency = 75;
- this->trackBar1->Value = 1;
- this->trackBar1->Scroll += gcnew System::EventHandler(this, &MyForm::trackBar1_Scroll);
- //
- // listBox1
- //
- this->listBox1->FormattingEnabled = true;
- this->listBox1->ItemHeight = 20;
- this->listBox1->Location = System::Drawing::Point(604, 50);
- this->listBox1->Name = L"listBox1";
- this->listBox1->Size = System::Drawing::Size(245, 84);
- this->listBox1->TabIndex = 3;
- /*
- // picPortKors
- //
- this->picPortKors->BackColor = System::Drawing::Color::Transparent;
- this->picPortKors->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"picPortKors.Image")));
- this->picPortKors->Location = System::Drawing::Point(190, 499);
- this->picPortKors->Name = L"picPortKors";
- this->picPortKors->Size = System::Drawing::Size(81, 56);
- this->picPortKors->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
- this->picPortKors->TabIndex = 5;
- this->picPortKors->TabStop = false;
- */
- //
- // MyForm
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(9, 20);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(871, 613);
- //this->Controls->Add(this->picPortKors);
- this->Controls->Add(this->listBox1);
- this->Controls->Add(this->trackBar1);
- this->Controls->Add(this->picShip);
- this->Controls->Add(this->picPort);
- this->Name = L"MyForm";
- this->Text = L"Модель захода морских судов в порт";
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPort))->EndInit();
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picShip))->EndInit();
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->trackBar1))->EndInit();
- //(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPortKors))->EndInit();
- this->ResumeLayout(false);
- this->PerformLayout();
- /*Маршрутная карта*/
- route->Add(gcnew RoutePoint(0, 250, Direction::EAST, ShipMode::FULL_SPEED_MODE)); //старт
- route->Add(gcnew RoutePoint(375, 250, Direction::SOUTH, ShipMode::SLOW_SPEED_MODE));
- route->Add(gcnew RoutePoint(375, 400, Direction::SOUTH, ShipMode::UNLOAD_MODE));
- route->Add(gcnew RoutePoint(375, 400, Direction::SOUTH, ShipMode::LOAD_MODE));
- route->Add(gcnew RoutePoint(375, 400, Direction::SOUTH, ShipMode::SLOW_SPEED_MODE));
- route->Add(gcnew RoutePoint(375, 520, Direction::EAST, ShipMode::SLOW_SPEED_MODE));
- route->Add(gcnew RoutePoint(700, 520, Direction::NORTH, ShipMode::SLOW_SPEED_MODE));
- route->Add(gcnew RoutePoint(700, 200, Direction::EAST, ShipMode::SLOW_SPEED_MODE));
- route->Add(gcnew RoutePoint(800, 200, Direction::EAST, ShipMode::FULL_SPEED_MODE));
- route->Add(gcnew RoutePoint(1210, 200, Direction::EAST, ShipMode::ROADS_MODE));
- pShip->setRoute(route);
- }
- #pragma endregion
- private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) {
- }
- private: System::Void pictureBox2_Click(System::Object^ sender, System::EventArgs^ e) {
- }
- private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
- modelStep();
- if (pShip->get_dir() == Direction::EAST)
- {
- picShip->Image = ship1;
- picShip->Width = ship1->Width;
- picShip->Height = ship1->Height;
- }
- else if (pShip->get_dir() == Direction::SOUTH)
- {
- picShip->Image = ship2;
- picShip->Width = ship2->Width;
- picShip->Height = ship2->Height;
- }
- else if (pShip->get_dir() == Direction::WEST)
- {
- picShip->Image = ship3;
- picShip->Width = ship3->Width;
- picShip->Height = ship3->Height;
- }
- else
- {
- picShip->Image = ship4;
- picShip->Width = ship4->Width;
- picShip->Height = ship4->Height;
- }
- int leftUpCornerY = pShip->get_y()/ metrPerPixels - picShip->Height / 2;
- int leftUpCornerX = pShip->get_x() / metrPerPixels - picShip->Width / 2;
- picShip->Top = leftUpCornerY;
- picShip->Left = leftUpCornerX;
- picShip->Visible = true;
- }
- private: System::Void trackBar1_Scroll(System::Object^ sender, System::EventArgs^ e)
- {
- modelSecondPerRealSecond = trackBar1->Value;
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement