Newer
Older
Grégoire Payen de La Garanderie
committed
#include "simul/table.h"
#include "simul/robot.h"
Xavier CORBILLON
committed
#include "simul/objet.h"
Grégoire Payen de La Garanderie
committed
#include "element.h"
Xavier CORBILLON
committed
#include "cstdlib"
#include "time.h"
Grégoire Payen de La Garanderie
committed
Xavier CORBILLON
committed
Grégoire Payen de La Garanderie
committed
#include <iostream>
#include <QPainter>
Xavier CORBILLON
committed
#include "CommandGoTo.h"
Grégoire Payen de La Garanderie
committed
Grégoire Payen de La Garanderie
committed
Position getCase(unsigned int i, unsigned int j)
{
return Position(450 + i*350, j*350);
}
Position getCaseCenter(unsigned int i, unsigned int j)
{
return Position(625 + i*350, 175 + j*350);
}
b2AABB Table::getWorldAABB()
{
b2AABB a;
a.lowerBound.Set(-100,-100);
a.upperBound.Set(tableWidth+100, tableHeight+100);
return a;
}
Grégoire Payen de La Garanderie
committed
Table::Table(QWidget* parent) :
QWidget(parent),
#ifdef BOX2D_2_0_1
world(getWorldAABB(),b2Vec2(0.f,0.f), false)
#else
Xavier CORBILLON
committed
// world(b2Vec2(0.f,0.f), false)
world(b2Vec2(0.f,0.f))
Grégoire Payen de La Garanderie
committed
#endif
Grégoire Payen de La Garanderie
committed
{
dt=0;
setAutoFillBackground(true);
Xavier CORBILLON
committed
Grégoire Payen de La Garanderie
committed
QPalette p = palette();
p.setColor(QPalette::Window,QColor(Qt::darkGray));
setPalette(p);
Xavier CORBILLON
committed
//création des robots
robots.push_back(new Robot(world));
hilnius
committed
//création des verres
// côté bleu
objets.push_back(new Objet(world, Position(900.,650.), Objet::glass, 0, QColor(170, 170, 170)));
objets.push_back(new Objet(world, Position(1200.,650.), Objet::glass, 0, QColor(170, 170, 170)));
objets.push_back(new Objet(world, Position(1050.,900.), Objet::glass, 0, QColor(170, 170, 170)));
objets.push_back(new Objet(world, Position(1350.,900.), Objet::glass, 0, QColor(170, 170, 170)));
objets.push_back(new Objet(world, Position(900.,1150.), Objet::glass, 0, QColor(170, 170, 170)));
objets.push_back(new Objet(world, Position(1200.,1150.), Objet::glass, 0, QColor(170, 170, 170)));
// côté rouge
objets.push_back(new Objet(world, Position(2100.,650.), Objet::glass, 0, QColor(170, 170, 170)));
objets.push_back(new Objet(world, Position(1800.,650.), Objet::glass, 0, QColor(170, 170, 170)));
objets.push_back(new Objet(world, Position(1950.,900.), Objet::glass, 0, QColor(170, 170, 170)));
objets.push_back(new Objet(world, Position(1650.,900.), Objet::glass, 0, QColor(170, 170, 170)));
objets.push_back(new Objet(world, Position(2100.,1150.), Objet::glass, 0, QColor(170, 170, 170)));
objets.push_back(new Objet(world, Position(1800.,1150.), Objet::glass, 0, QColor(170, 170, 170)));
Xavier CORBILLON
committed
//Geometry
b2BodyDef bodyDef;
bodyDef.position.Set(0., 0.);
Xavier CORBILLON
committed
tableBody = world.CreateBody(&bodyDef);
Xavier CORBILLON
committed
Grégoire Payen de La Garanderie
committed
#ifdef BOX2D_2_0_1
Grégoire Payen de La Garanderie
committed
b2PolygonDef &fixture = box;
#else
b2PolygonShape box;
b2FixtureDef fixture;
fixture.shape = &box;
#endif
fixture.friction = 0.5;
fixture.density = 0;
#ifdef BOX2D_2_0_1
#define CreateFixture CreateShape
#endif
hilnius
committed
// bordures
box.SetAsBox(30,1, b2Vec2(0,0),0);
Grégoire Payen de La Garanderie
committed
tableBody->CreateFixture(&fixture);
Xavier CORBILLON
committed
box.SetAsBox(1,20, b2Vec2(-1,0),0);
Grégoire Payen de La Garanderie
committed
tableBody->CreateFixture(&fixture);
Xavier CORBILLON
committed
box.SetAsBox(1,20, b2Vec2(31,0),0);
Grégoire Payen de La Garanderie
committed
tableBody->CreateFixture(&fixture);
hilnius
committed
box.SetAsBox(30,1, b2Vec2(0,22),0);
Xavier CORBILLON
committed
tableBody->CreateFixture(&fixture);
hilnius
committed
// fin bordures
Xavier CORBILLON
committed
hilnius
committed
// gateau
b2CircleShape circle;
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
circle.m_radius = 5.f;
circle.m_p.Set(15.,21.);
tableBody->CreateFixture(&fixtureDef);
Xavier CORBILLON
committed
hilnius
committed
// load the graphics of the game.
tableGraphics.load("tabledata.xml");
tableGraphics.addYOffset(-tableHeight); // because we want the (0,0) point to be at the bottom left.
Xavier Corbillon
committed
Grégoire Payen de La Garanderie
committed
}
Table::~Table()
{
for (unsigned int i = 0; i<robots.size();i++)
delete robots[i];
for (unsigned int i = 0; i<objets.size();i++)
delete objets[i];
Grégoire Payen de La Garanderie
committed
}
void Table::update(int dt)
{
this->dt = dt;
for(unsigned int i=0; i < robots.size(); i++)
robots[i]->updateForces(dt);
Xavier CORBILLON
committed
// robots[i]->interact(elements);
Grégoire Payen de La Garanderie
committed
#ifdef BOX2D_2_0_1
Grégoire Payen de La Garanderie
committed
#else
world.Step((float)dt/1000., 10, 10);
world.ClearForces();
#endif
Xavier CORBILLON
committed
for(unsigned int i=0; i < objets.size(); i++)
objets[i]->updatePos();
Grégoire Payen de La Garanderie
committed
repaint();
}
void Table::paintEvent(QPaintEvent* evt)
{
QPainter p(this);
p.setRenderHints(QPainter::Antialiasing,true);
Grégoire Payen de La Garanderie
committed
p.setWindow(QRect(0,-tableHeight,tableWidth,tableHeight));
Grégoire Payen de La Garanderie
committed
p.setWorldMatrixEnabled(true);
hilnius
committed
// dessine la table
tableGraphics.draw(&p);
hilnius
committed
Xavier CORBILLON
committed
for (unsigned int i=0; i < objets.size(); i++)
objets[i]->paint(p);
Grégoire Payen de La Garanderie
committed
for(unsigned int i=0; i < robots.size(); i++)
hilnius
committed
robots[i]->paint(p,dt);
Grégoire Payen de La Garanderie
committed
dt = 0;
Xavier CORBILLON
committed
// Dessin d'un trajet
Xavier Corbillon
committed
/* PositionPlusAngle** path = CommandGoTo::path();
Xavier Corbillon
committed
p.setPen(QColor(Qt::black));
Xavier Corbillon
committed
if (path)
{
for(unsigned int i=0; i+1 < PATH_LENGTH; i++)
Xavier CORBILLON
committed
p.drawLine(path[i]->position.x, -path[i]->position.y, path[i+1]->position.x, -path[i+1]->position.y);
Xavier Corbillon
committed
CommandGoTo::deletePath(path);
Xavier Corbillon
committed
}
Xavier Corbillon
committed
*/
Grégoire Payen de La Garanderie
committed
}
void Table::keyPressEvent(QKeyEvent* evt, bool press)
{
for(unsigned int i=0; i < robots.size(); i++)
robots[i]->keyPressEvent(evt, press);
Xavier Corbillon
committed
Sensors::getSensors()->keyPressEvent(evt,press);
Grégoire Payen de La Garanderie
committed
Position getSideElemCenter(bool right, unsigned int elem)
{
return Position(right ? 200 : 2800, 1810 - elem*280);
}