-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorldWidget.cpp
More file actions
259 lines (229 loc) · 7.56 KB
/
WorldWidget.cpp
File metadata and controls
259 lines (229 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <cmath>
#include <QWheelEvent>
#include <QFileDialog>
#include <iostream>
#include <QPainter>
#include <QImage>
#include <QGraphicsPixmapItem>
#include "WorldWidget.h"
#include "Animal.h"
#include "Vegetal.h"
#include "Water.h"
#include "Meat.h"
#include "SaveManager.h"
WorldWidget::WorldWidget(World *world) : QGraphicsView(), m_world(world)
{
//setup default selected animal
selectedAnimal = std::weak_ptr<Animal>();
//setup a new scene
m_scene = new QGraphicsScene();
this->setScene(m_scene);
//eneable map style navigation
this->setDragMode(QGraphicsView::ScrollHandDrag);
//disable scroll bar as counter intuitive
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
//set timer:
updateTimerInterval();
//set last scene update value
ftime(&lastSceneUpdate);
//signal and slot connection
QObject::connect(&timer,SIGNAL(timeout()),this,SLOT(tick()));
}
WorldWidget::~WorldWidget()
{
delete m_scene;
}
void WorldWidget::updateTimerInterval()
{
timer.setInterval(config::UPDATE_TIMER_INTERVALE);
}
void WorldWidget::setWorld(World *world)
{
m_world = world;
this->updateScene();
//fit the scene in the view
this->fitInView(0,0,2*m_world->getSizeX(),2*m_world->getSizeY(),Qt::KeepAspectRatio); //why 2* ??? but it work more or less
}
void WorldWidget::updateScene()
{
struct timeb tp;
ftime(&tp);
//update scene only every 20 ms (50 fps max)
if(tp.time > lastSceneUpdate.time || tp.millitm > lastSceneUpdate.millitm+(1000.0/config::FPS))//30fps
{
forcedSceneUpdate();
lastSceneUpdate = tp;
}
}
void WorldWidget::forcedSceneUpdate()
{
//clear the scene:
m_scene->clear();
m_scene->setSceneRect(0,0,m_world->getSizeX(),m_world->getSizeY());
m_scene->setBackgroundBrush(QBrush(Qt::gray));
m_scene->addRect(0,0,m_world->getSizeX(),m_world->getSizeY(),QPen(Qt::gray),colors.getBackgroundBrush());
//add each entity to the scene one by one:
const std::list<std::shared_ptr<Entity>> & entities = m_world->getEntities();
for(std::shared_ptr<Entity> ite : entities)
{
drawEntity(ite);
}
emit sceneUpdated();
}
void WorldWidget::resizeEvent(QResizeEvent *e)
{
this->fitInView(0,0,config::WORLD_SIZE_X,config::WORLD_SIZE_Y,Qt::KeepAspectRatio);
QGraphicsView::resizeEvent(e);
}
void WorldWidget::wheelEvent(QWheelEvent* e)
{
QPoint numDegree = e->angleDelta() /8;
qreal factor = std::pow(1.01, numDegree.ry());
this->scale(factor, factor);
}
void WorldWidget::selectAnimal(std::weak_ptr<Animal> a)
{
selectedAnimal = a;
emit animalSelected(a);
updateScene();
}
void WorldWidget::tick()
{
m_world->tick();
updateScene();
}
//timer managment
void WorldWidget::startSimulation()
{
timer.start();
}
void WorldWidget::suspendSimulation()
{
timer.stop();
}
bool WorldWidget::isSimulationRunning() const
{
return timer.isActive();
}
void WorldWidget::drawEntity(const std::shared_ptr<Entity> e)
{
///TODO implemente toric view on diagonals
double x=e->getCoordinate().getX(), y=e->getCoordinate().getY();
double radius = e->getRadius();
std::vector<QPoint> positions;
positions.push_back(QPoint(x,y));
if(x<radius)
{
positions.push_back(QPoint(x+config::WORLD_SIZE_X,y));
}
else if(x>config::WORLD_SIZE_X-radius)
{
positions.push_back(QPoint(x-config::WORLD_SIZE_X,y));
}
if(y<radius)
{
positions.push_back(QPoint(x,y+config::WORLD_SIZE_Y));
}
else if(y>config::WORLD_SIZE_Y-radius)
{
positions.push_back(QPoint(x,y-config::WORLD_SIZE_Y));
}
if(const std::shared_ptr<Animal> living = std::dynamic_pointer_cast<Animal>(e))
{
for(QPoint pos:positions)
{
drawAnimal(living,pos);
}
if(living == selectedAnimal.lock())
{
std::vector<std::shared_ptr<Percepted>> percepted = living->getVision()->getPercepted();
for(std::shared_ptr<Percepted> p:percepted)
{
const std::shared_ptr<Entity> e = p->getEntity();
if(e != nullptr)
{
Coordinate c = e->getCoordinate();
m_scene->addEllipse(c.getX()-5,c.getY()-5,10,10,QPen(Qt::red));
}
}
}
}
else
{
for(QPoint pos:positions)
{
QBrush brush = colors.getEntityBrush(e);
QPen pen = colors.getEntityPen(e);
if(const std::shared_ptr<Resource> resource = std::dynamic_pointer_cast<Resource>(e))
{
// Add transparency
double transparency = ((double)resource->getQuantity()) / ((double)resource->getMaxQuantity());
brush.setColor(QColor(brush.color().red(), brush.color().green(), brush.color().blue(), transparency * 255.0));
pen.setColor(QColor(pen.color().red(), pen.color().green(), pen.color().blue(), transparency * 255.0));
}
drawBasicEntity(pos,radius,colors.getEntityBrush(e),colors.getEntityPen(e));
}
}
}
void WorldWidget::drawAnimal(QPoint pos, double radius, double angle, QBrush brush, QPen pen)
{
drawBasicEntity(pos,radius,brush,pen);
//add an eye to show the looking direction
double eyeRadius = radius/3;
double eyeXCenter = pos.x()+cos(angle)*(radius-eyeRadius);
double eyeYCenter = pos.y()+sin(angle)*(radius-eyeRadius);
QRect eyeSquare(eyeXCenter-eyeRadius,eyeYCenter-eyeRadius,eyeRadius*2,eyeRadius*2);
QBrush eyeBrush = colors.getTeamsEyeBrush();
m_scene->addEllipse(eyeSquare,pen,eyeBrush);
}
void WorldWidget::drawAnimal(const std::shared_ptr<Animal> animal)
{
QPoint pos(animal->getX(),animal->getY());
drawAnimal(animal,pos);
}
void WorldWidget::drawAnimal(const std::shared_ptr<Animal> animal, QPoint pos)
{
QPen animalPen;
if(animal == selectedAnimal.lock())
animalPen = colors.getTeamsSelectedPen();
else
animalPen = colors.getEntityPen(animal);
drawAnimal(pos,animal->getRadius(),animal->getAngle(),colors.getEntityBrush(animal),animalPen);
/*QImage image("../Fil-Rouge/sprites/pig30.png");
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
double scale = ((double)animal->getRadius()) / ((double)image.width()) * 3;
item->setScale(scale);
item->setTransformOriginPoint(((double)image.width()) / 2, ((double)image.height()) / 2);
item->setRotation(animal->getAngle() * (180.0 / 3.141592));
//item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
m_scene->addItem(item);
item->setPos(pos.x() - ((double)image.width()) / 2, pos.y() - ((double)image.height()) / 2);*/
}
void WorldWidget::drawBasicEntity(QPoint pos, double radius, QBrush brush, QPen pen)
{
QRect baseSquare(pos.x()-radius,pos.y()-radius,2*radius,2*radius);
m_scene->addEllipse(baseSquare,pen,brush);
}
void WorldWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
QPointF point = this->mapToScene(event->pos());
for(std::shared_ptr<Entity> e:m_world->getEntities())
{
if(std::shared_ptr<Animal> living = std::dynamic_pointer_cast<Animal>(e))
{
if(point.x()<e->getX()+e->getRadius() && point.x()>e->getX()-e->getRadius() &&
point.y()<e->getY()+e->getRadius() && point.y()>e->getY()-e->getRadius())
{
selectAnimal(living);
return;
}
}
}
//if nothing found send nullptr
selectAnimal(std::weak_ptr<Animal>());
}
WorldColors & WorldWidget::getColors()
{
return colors;
}