|
| 1 | +#include "Hex.h" |
| 2 | +#include <QPointF> |
| 3 | +#include <QPolygonF> |
| 4 | +#include <QVector> |
| 5 | +#include <QBrush> |
| 6 | +#include "Game.h" |
| 7 | + |
| 8 | +extern Game* game; |
| 9 | + |
| 10 | +#include <QDebug> // TODO remove |
| 11 | +Hex::Hex(QGraphicsItem *parent){ |
| 12 | + // draw the polygon |
| 13 | + |
| 14 | + // points needed to draw hexagon: (1,0), (2,0), (3,1), (2,2), (1,2), (0,1) |
| 15 | + QVector<QPointF> hexPoints; |
| 16 | + hexPoints << QPointF(1,0) << QPointF(2,0) << QPointF(3,1) << QPointF(2,2) |
| 17 | + << QPointF(1,2) << QPointF(0,1); |
| 18 | + |
| 19 | + // scale the points |
| 20 | + int SCALE_BY = 40; |
| 21 | + for (size_t i = 0, n = hexPoints.size(); i < n; ++i){ |
| 22 | + hexPoints[i] = hexPoints[i] * SCALE_BY; |
| 23 | + } |
| 24 | + |
| 25 | + // create a polygon with the scaled points |
| 26 | + QPolygonF hexagon(hexPoints); |
| 27 | + |
| 28 | + // draw the poly |
| 29 | + setPolygon(hexagon); |
| 30 | + |
| 31 | + // initialize |
| 32 | + isPlaced = false; |
| 33 | +} |
| 34 | + |
| 35 | +bool Hex::getIsPlaced(){ |
| 36 | + return isPlaced; |
| 37 | +} |
| 38 | + |
| 39 | +QString Hex::getOwner(){ |
| 40 | + return owner; |
| 41 | +} |
| 42 | + |
| 43 | +void Hex::mousePressEvent(QGraphicsSceneMouseEvent *event){ |
| 44 | + // if this hex is NOT placed (so it is a card) then pick it up |
| 45 | + if (getIsPlaced() == false){ |
| 46 | + game->pickUpCard(this); |
| 47 | + } |
| 48 | + |
| 49 | + // if this hex IS placed, then replace it |
| 50 | + else { |
| 51 | + game->placeCard(this); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void Hex::setOwner(QString player){ |
| 56 | + // set the owner |
| 57 | + owner = player; |
| 58 | + |
| 59 | + // change the color |
| 60 | + if (player == QString("NOONE")){ |
| 61 | + QBrush brush; |
| 62 | + brush.setStyle(Qt::SolidPattern); |
| 63 | + brush.setColor(Qt::lightGray); |
| 64 | + setBrush(brush); |
| 65 | + } |
| 66 | + |
| 67 | + if (player == QString("PLAYER1")){ |
| 68 | + QBrush brush; |
| 69 | + brush.setStyle(Qt::SolidPattern); |
| 70 | + brush.setColor(Qt::blue); |
| 71 | + setBrush(brush); |
| 72 | + } |
| 73 | + |
| 74 | + if (player == QString("PLAYER2")){ |
| 75 | + QBrush brush; |
| 76 | + brush.setStyle(Qt::SolidPattern); |
| 77 | + brush.setColor(Qt::red); |
| 78 | + setBrush(brush); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void Hex::setIsPlaced(bool b){ |
| 83 | + isPlaced = b; |
| 84 | +} |
0 commit comments