Skip to content
Snippets Groups Projects
Commit 21dbb42e authored by Grégoire Payen de La Garanderie's avatar Grégoire Payen de La Garanderie
Browse files

Simu: Ajout du placement des éléments de jeu, fix du repère dans Qt.

parent aaf2b7c4
No related branches found
No related tags found
No related merge requests found
......@@ -4,11 +4,16 @@
#include "distance.h"
#include "Angle.h"
#ifndef ROBOTHW
#include <QPoint>
#endif
class Position{
//private:
//Distance x,y;
public:
#ifndef ROBOTHW
operator QPoint() { return QPoint(x,-y); }
#endif
public:
Distance x,y;
Position() { x=0; y=0; }
Position(Distance x, Distance y);
......
File moved
......@@ -11,25 +11,19 @@ public:
Position p;
enum Type
{
Pion = 0,
Dame = 1,
Roi = 2
Pawn = 0,
Queen = 1,
King = 2
};
Type type;
unsigned int multiplier;
b2Body* body;
Element(b2World & world, Position p, Type t);
void paint(QPainter & pa)
{
pa.setBrush(QBrush(QColor("yellow")));
pa.setPen(QBrush(QColor("yellow")));
pa.drawEllipse(QPoint(p.x,p.y),100,100);
}
void paint(QPainter & pa);
void updatePos();
};
......
......@@ -14,6 +14,8 @@ private:
b2World world;
b2Body* tableBody;
void addCard(unsigned int n, int column);
public:
static const int tableWidth = 3000;
......
......@@ -31,7 +31,9 @@ float posx[DBG_SIZE];
float posy[DBG_SIZE];
float angle[DBG_SIZE];
#ifndef NULL
#define NULL 0
#endif
Asservissement * Asservissement::asservissement = NULL;
const uint16_t Asservissement::nb_ms_between_updates = 10;
......
......@@ -4,6 +4,7 @@ Element::Element(b2World & world, Position p, Type t)
{
this->p = p;
type = t;
multiplier = 0;
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
......@@ -56,3 +57,36 @@ void Element::updatePos()
}
body->SetAngularVelocity(angular);
}
void Element::paint(QPainter & pa)
{
pa.setBrush(QBrush(QColor("yellow")));
pa.setPen(QBrush(QColor("yellow")));
pa.drawEllipse(QPoint(p.x,-p.y),100,-100);
pa.setBrush(QBrush(QColor("black")));
pa.setPen(QBrush(QColor("black")));
QFont font;
font.setPointSize(30);
pa.setFont(font);
QString text;
if(type == Pawn)
{
if(multiplier > 0)
text = QString::number(multiplier);
}
else
{
if(type == Queen)
text = "Q";
else
text = "K";
if(multiplier > 0)
text = QString(" ") + QString::number(multiplier);
}
pa.drawText(p.x-50, -p.y+50, 100, -100, Qt::AlignCenter, text);
}
#include <QApplication>
#include "simul/main.h"
#include "simul/main_window.h"
#include <ctime>
int main(int argc, char** argv)
{
srand(time(NULL));
QApplication app(argc, argv);
MainWindow mw;
mw.show();
......
......@@ -130,7 +130,7 @@ void Robot::paint(QPainter &p, int dt)
}
}
p.setWorldTransform(QTransform().translate(pos.position.x,pos.position.y).rotateRadians(pos.angle.getValueInRadian()));
p.setWorldTransform(QTransform().translate(pos.position.x,-pos.position.y).rotateRadians(-pos.angle.getValueInRadian()));
p.setPen(QColor(Qt::black));
......@@ -147,7 +147,7 @@ void Robot::paint(QPainter &p, int dt)
p.setPen(QColor(Qt::green));
for(unsigned int i=0; i+1 < olds.size(); i++)
p.drawLine(olds[i].position.x, olds[i].position.y, olds[i+1].position.x, olds[i+1].position.y);
p.drawLine(olds[i].position.x, -olds[i].position.y, olds[i+1].position.x, -olds[i+1].position.y);
}
#define IF_KEYSWITCH(n,a) \
......@@ -169,9 +169,9 @@ void Robot::keyPressEvent(QKeyEvent* evt, bool press)
IF_KEYSWITCH(arriere,evt->key() == Qt::Key_Down)
deriv.position.x -= dinc;
IF_KEYSWITCH(gauche,evt->key() == Qt::Key_Right)
deriv.angle += ainc;
IF_KEYSWITCH(droite,evt->key() == Qt::Key_Left)
deriv.angle -= ainc;
IF_KEYSWITCH(droite,evt->key() == Qt::Key_Left)
deriv.angle += ainc;
}
}
......@@ -3,6 +3,16 @@
#include <iostream>
#include <QPainter>
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);
}
Table::Table(QWidget* parent) : QWidget(parent), world(b2Vec2(0.f,0.f), false)
{
dt=0;
......@@ -14,8 +24,6 @@ Table::Table(QWidget* parent) : QWidget(parent), world(b2Vec2(0.f,0.f), false)
robots.push_back(new Robot(world));
elements.push_back(new Element(world,Position(900,900),Element::Pion));
//Geometry
b2BodyDef bodyDef;
bodyDef.position.Set(0., 0.);
......@@ -62,6 +70,20 @@ Table::Table(QWidget* parent) : QWidget(parent), world(b2Vec2(0.f,0.f), false)
tableBody->CreateFixture(&fixtureDef);
box.SetAsBox(.11,0.64, b2Vec2(25.39,19.15), 0.);
tableBody->CreateFixture(&fixtureDef);
//Init position of elements
int l1 = rand() % 20;
int l2 = rand() % 20;
int r1 = rand() % 20;
int r2 = rand() % 20;
addCard(l1, 1);
addCard(l2, 2);
addCard(l2, 4);
addCard(l1, 5);
addCard(r1, -1);
addCard(r2, -2);
elements.push_back(new Element(world,getCase(3,3),Element::Pawn)); //Central element
}
Table::~Table()
......@@ -81,49 +103,36 @@ void Table::update(int dt)
repaint();
}
QPoint getCase(unsigned int i, unsigned int j)
{
return QPoint(450 + i*350, j*350);
}
QPoint getCaseCenter(unsigned int i, unsigned int j)
{
return QPoint(625 + i*350, 175 + j*350);
}
void Table::paintEvent(QPaintEvent* evt)
{
static float l = 0;
l+= dt*0.005;
QPainter p(this);
p.setRenderHints(QPainter::Antialiasing,true);
p.setWindow(QRect(0,0,tableWidth,tableHeight));
p.setWindow(QRect(0,-tableHeight,tableWidth,tableHeight));
p.setWorldMatrixEnabled(true);
for(unsigned int i=0; i<6; i++)
for(unsigned int j=0; j<6; j++)
p.fillRect(450+i*350,j*350,350,350, ((i+j) & 1) ? Qt::red : Qt::blue);
p.drawEllipse(QRectF(50.0,50.0f,100,l));
p.fillRect(450+i*350,-j*350,350,-350, ((i+j) & 1) ? Qt::red : Qt::blue);
//Starting zones
p.fillRect(0,400,400,1700,QColor(0,146,54));
p.fillRect(2600,400,400,1700,QColor(0,146,54));
p.fillRect(0,0,400,-400,Qt::blue);
p.fillRect(2600,0,400,-400,Qt::red);
//Green zones
p.fillRect(0,0,400,400,Qt::red);
p.fillRect(2600,0,400,400,Qt::blue);
p.fillRect(0,-400,400,-1700,QColor(0,146,54));
p.fillRect(2600,-400,400,-1700,QColor(0,146,54));
//Starting zones borders
p.fillRect(0,389,400,22,Qt::black);
p.fillRect(2600,389,400,22,Qt::black);
p.fillRect(0,-389,400,-22,Qt::black);
p.fillRect(2600,-389,400,-22,Qt::black);
//Blocked zones
p.fillRect(450,1980,700,120,Qt::black);
p.fillRect(1850,1980,700,120,Qt::black);
p.fillRect(450,-1980,700,-120,Qt::black);
p.fillRect(1850,-1980,700,-120,Qt::black);
p.fillRect(450,1850,22,130,Qt::black);
p.fillRect(1850,1850,22,130,Qt::black);
p.fillRect(450,-1850,22,-130,Qt::black);
p.fillRect(1850,-1850,22,-130,Qt::black);
p.fillRect(1128,1850,22,130,Qt::black);
p.fillRect(2528,1850,22,130,Qt::black);
p.fillRect(1128,-1850,22,-130,Qt::black);
p.fillRect(2528,-1850,22,-130,Qt::black);
//Extra-points
p.setBrush(QBrush(QColor(30,30,30)));
......@@ -155,3 +164,35 @@ void Table::keyPressEvent(QKeyEvent* evt, bool press)
robots[i]->keyPressEvent(evt, press);
}
Position getSideElemCenter(bool right, unsigned int elem)
{
return Position(right ? 200 : 2800, 1810 - elem*280);
}
void Table::addCard(unsigned int n, int column)
{
unsigned int k = n % 4;
unsigned int q = n % 3;
if(q >= k)
q++;
if(column > 0)
{
elements.push_back(new Element(world,getCase(column,k+1),Element::Pawn));
elements.push_back(new Element(world,getCase(column,q+1),Element::Pawn));
}
else
{
for(unsigned int i=0; i < 5; i++)
{
Element::Type t = Element::Pawn;
if(i == q)
t = Element::Queen;
else if(i == k)
t = Element::King;
elements.push_back(new Element(world,getSideElemCenter(-column-1, i),t));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment