161 lines
5.9 KiB
C++
161 lines
5.9 KiB
C++
#include <iostream>
|
|
#include <cmath>
|
|
#include <QGraphicsScene>
|
|
#include <QPen>
|
|
#include "manageritem.h"
|
|
|
|
static const qreal pi = 3.14159265358979323846264338327950288419717;
|
|
|
|
/*
|
|
* Конструктор
|
|
*/
|
|
ManagerItem::ManagerItem(QList<Agent*> agents, QGraphicsScene *sceneParameter, int CM, int CN, int PM/*, int PN*/):
|
|
agent(agents), scene(sceneParameter), link(0)
|
|
{
|
|
fillingScene(CM, CN, PM);
|
|
}
|
|
|
|
/*
|
|
* Отрисовка связей между агентами и их агрессорами, защитниками/защищаемыми
|
|
*/
|
|
void ManagerItem::drawLinks(){
|
|
for(int i = 0; i < agent.size(); i++){
|
|
Agent *gamer = agent[i];
|
|
QGraphicsLineItem *item1 = new QGraphicsLineItem(QLineF(gamer->getCoordX(), gamer->getCoordY(),
|
|
agent[gamer->getAgressor()]->getCoordX(), agent[gamer->getAgressor()]->getCoordY()));
|
|
QGraphicsLineItem *item2 = new QGraphicsLineItem(QLineF(gamer->getCoordX(), gamer->getCoordY(),
|
|
agent[gamer->getProtector()]->getCoordX(), agent[gamer->getProtector()]->getCoordY()));
|
|
item1->setPen(QPen(Qt::blue));
|
|
item2->setPen(QPen(Qt::green));
|
|
links.push_back(item1);
|
|
links.push_back(item2);
|
|
scene->addItem(item1);
|
|
scene->addItem(item2);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Отрисовка направлений связей
|
|
*/
|
|
void ManagerItem::drawArrow(){
|
|
for(int i = 0; i < agent.size(); i++){
|
|
Agent *gamer = agent[i];
|
|
QLineF *line1 = new QLineF(gamer->getCoordX(), gamer->getCoordY(),
|
|
agent[gamer->getAgressor()]->getCoordX(), agent[gamer->getAgressor()]->getCoordY());
|
|
QLineF *line2 = new QLineF(gamer->getCoordX(), gamer->getCoordY(),
|
|
agent[gamer->getProtector()]->getCoordX(), agent[gamer->getProtector()]->getCoordY());
|
|
qreal angel1 = ::acos(line1->dx()/line1->length());
|
|
qreal angel2 = ::acos(line2->dx()/line2->length());
|
|
if (line1->dy() >= 0){
|
|
angel1 = 2*pi - angel1;
|
|
}
|
|
if (line2->dy() >= 0){
|
|
angel2 = 2*pi - angel2;
|
|
}
|
|
if (line1->length() > 20){
|
|
line1->setLength(20);
|
|
QPointF point1 = line1->p2();
|
|
QPointF point2 = point1 + QPointF(sin(angel1 - pi/3)*10, cos(angel1 - pi/3)*10);
|
|
QPointF point3 = point1 + QPointF(sin(angel1 - 2*pi/3)*10, cos(angel1 - 2*pi/3)*10);
|
|
QGraphicsPolygonItem *poly1 = new QGraphicsPolygonItem(QPolygonF() << point1 << point2 << point3);
|
|
poly1->setBrush(QBrush(Qt::blue));
|
|
poly1->setPen(QPen(Qt::blue));
|
|
arrows.push_back(poly1);
|
|
scene->addItem(poly1);
|
|
}
|
|
else {}
|
|
if (line2->length() > 20){
|
|
line2->setLength(20);
|
|
QPointF point4 = line2->p2();
|
|
QPointF point5 = point4 + QPointF(sin(angel2 - pi/3)*10, cos(angel2 - pi/3)*10);
|
|
QPointF point6 = point4 + QPointF(sin(angel2 - 2*pi/3)*10, cos(angel2 - 2*pi/3)*10);
|
|
QGraphicsPolygonItem *poly2 = new QGraphicsPolygonItem(QPolygonF() << point4 << point5 << point6);
|
|
poly2->setBrush(QBrush(Qt::green));
|
|
poly2->setPen(QPen(Qt::green));
|
|
arrows.push_back(poly2);
|
|
scene->addItem(poly2);
|
|
}
|
|
else {}
|
|
delete line1;
|
|
delete line2;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Заполнение сцены
|
|
*/
|
|
void ManagerItem:: fillingScene(int CM, int CN, int PM/*, int PN*/) {
|
|
|
|
clearScene();
|
|
|
|
QGraphicsRectItem *rect = new QGraphicsRectItem(-300, -300, 600, 600);
|
|
rect->setPen(QPen(Qt::gray));
|
|
scene->addItem(rect);
|
|
|
|
//std::cout << "start filling scene with number of gamers =" << agent.size() << std::endl;
|
|
for(int i = 0; i < agent.size(); i++){
|
|
AgentItem *agentIt;
|
|
if (i < CM){
|
|
agentIt = new AgentItem(agent[i], Qt::red);
|
|
}
|
|
else if (i < CM + CN){
|
|
agentIt = new AgentItem(agent[i], Qt::darkBlue);
|
|
}
|
|
else if (i < CM + CN + PM){
|
|
agentIt = new AgentItem(agent[i], Qt::yellow);
|
|
}
|
|
else {
|
|
agentIt = new AgentItem(agent[i], Qt::cyan);
|
|
}
|
|
agentItems.push_back(agentIt);
|
|
agentIt->setPos(agent[i]->getCoordX(), agent[i]->getCoordY());
|
|
scene->addItem(agentIt);
|
|
}
|
|
//std::cout << "INFO: ManagerItem::fillingScene finished, number of items =" << agentItems.size() << std::endl;
|
|
}
|
|
|
|
/*
|
|
* Очистка сцены
|
|
*/
|
|
void ManagerItem:: clearScene(){
|
|
scene->clear();
|
|
qDeleteAll(agentItems.begin(), agentItems.end());
|
|
agentItems.clear();
|
|
qDeleteAll(links.begin(), links.end());
|
|
links.clear();
|
|
qDeleteAll(arrows.begin(), arrows.end());
|
|
arrows.clear();
|
|
}
|
|
|
|
void ManagerItem::setLink(bool val){
|
|
this->link = val;
|
|
}
|
|
|
|
/*
|
|
* Событие игры: перерисовка всех элементов сцены, графика
|
|
*/
|
|
void ManagerItem::gameEvent(){
|
|
//std::cout << "INFO: ManagerItem::gameEvent start, agentItemsSize: " << agentItems.size() << std::endl;
|
|
// Перерисовка агентов
|
|
for(int i = 0; i < agentItems.size(); i++){
|
|
agentItems[i]->agentEvent();
|
|
}
|
|
//Очистка сцены от связей
|
|
for (int i = 0; i < links.size(); i++){
|
|
scene->removeItem(links[i]);
|
|
delete links[i];
|
|
}
|
|
for (int i = 0; i < arrows.size(); i++){
|
|
scene->removeItem(arrows[i]);
|
|
delete arrows[i];
|
|
}
|
|
links.clear();
|
|
arrows.clear();
|
|
|
|
// Отрисовка актуальных связей (если необходимо)
|
|
if(link == true){
|
|
drawLinks();
|
|
drawArrow();
|
|
}
|
|
}
|