-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulationview.cpp
More file actions
74 lines (66 loc) · 2.73 KB
/
simulationview.cpp
File metadata and controls
74 lines (66 loc) · 2.73 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
#include "simulationview.h"
#include <QPainter>
#include "rabbit.h"
#include <QMouseEvent>
SimulationView::SimulationView(QWidget * parent) : QOpenGLWidget(parent)
{
lastClickedAnimal = std::make_shared<Rabbit>(Vector2(-10,-10),nullptr,15);
lastClickedAnimal->dead = true;
}
void SimulationView::initializeGL(){
glClearColor(1,1,1,1);
}
const std::map<EntityType,QColor> colorTable = {
{EntityType::Plant , Qt::green},
{EntityType::Water ,Qt::blue},
{EntityType::Rabbit ,QColor(139,69,19)}, // Brown
{EntityType::Fox ,QColor(255,165,0)}, // Orange
{EntityType::Entity ,Qt::gray},
{EntityType::Animal ,Qt::black}
};
void SimulationView::paintGL(){
QPainter p(this);
const float XScale = this->width() / enviroment->maxCoordinates.x;
const float YScale = this->height() / enviroment->maxCoordinates.y;
const float minScale = XScale < YScale ? XScale : YScale;
const int squareSize = 2 * minScale;
//const int borderSize = 1.3 * squareSize;
//const int borderdistance = (borderSize - squareSize)/2;
for(const std::shared_ptr<Entity> entity : enviroment->Entitys){
p.fillRect(entity->pos.x*XScale-(squareSize/2),entity->pos.y*YScale-(squareSize/2),
squareSize,squareSize,colorTable.at(entity->eType));
}
p.setPen(Qt::red);
if(lastClickedAnimal->dead){
lastClickedAnimal = enviroment->getAnimal(lastClickedAnimal);
}
p.drawEllipse(lastClickedAnimal->pos.x*XScale-lastClickedAnimal->gens.senseRadius*XScale
,lastClickedAnimal->pos.y*YScale-lastClickedAnimal->gens.senseRadius*YScale,
lastClickedAnimal->gens.senseRadius*XScale*2,lastClickedAnimal->gens.senseRadius*YScale*2);
}
void SimulationView::resizeGL(int w, int h){
glViewport(0,0,w,h);
}
void SimulationView::updateEnviroment(const Enviroment *env){
enviroment = env;
}
void SimulationView::mousePressEvent(QMouseEvent * event){
Vector2 min(event->x(),event->y());
const float XScale = this->width() / enviroment->maxCoordinates.x;
const float YScale = this->height() / enviroment->maxCoordinates.y;
const float minScale = XScale < YScale ? XScale : YScale;
const int squareSize = 2 * minScale;
min.x /= XScale;
min.y /= YScale;
Vector2 max(min.x+squareSize/2,min.y+squareSize/2);
min.x -= squareSize/2;
min.y -= squareSize/2;
for(const auto entity : enviroment->Entitys){
if(entity->pos.x > min.x && entity->pos.y > min.y &&entity->pos.x < max.x && entity->pos.y < max.y &&
(entity->eType == EntityType::Fox ||
entity->eType == EntityType::Rabbit)){
lastClickedAnimal = std::dynamic_pointer_cast<Animal>(entity);
update();
}
}
}