mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-13 01:24:46 -07:00
improved counters
This commit is contained in:
parent
939ab62273
commit
3388804e8f
18 changed files with 158 additions and 80 deletions
|
|
@ -291,6 +291,11 @@ int Client::incCounter(const QString &counter, int delta)
|
|||
return cmd(QString("inc_counter|%1|%2").arg(counter).arg(delta));
|
||||
}
|
||||
|
||||
int Client::addCounter(const QString &counter, QColor color, int value)
|
||||
{
|
||||
return cmd(QString("add_counter|%1|%2|%3").arg(counter).arg(color.red() * 65536 + color.green() * 256 + color.blue()).arg(value));
|
||||
}
|
||||
|
||||
int Client::setCounter(const QString &counter, int value)
|
||||
{
|
||||
return cmd(QString("set_counter|%1|%2").arg(counter).arg(value));
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#include "serverzonecard.h"
|
||||
#include "pendingcommand.h"
|
||||
#include <QTcpSocket>
|
||||
#include <QColor>
|
||||
|
||||
class QTimer;
|
||||
|
||||
|
|
@ -76,6 +77,7 @@ public:
|
|||
int setCardAttr(const QString &zone, int cardid, const QString &aname, const QString &avalue);
|
||||
int readyStart();
|
||||
int incCounter(const QString &counter, int delta);
|
||||
int addCounter(const QString &counter, QColor color, int value);
|
||||
int setCounter(const QString &counter, int value);
|
||||
int delCounter(const QString &counter);
|
||||
int setActivePlayer(int player);
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
#include "counter.h"
|
||||
#include "player.h"
|
||||
#include "client.h"
|
||||
#include <QtGui>
|
||||
|
||||
Counter::Counter(Player *_player, const QString &_name, QGraphicsItem *parent)
|
||||
: QGraphicsItem(parent), name(_name), value(0), player(_player)
|
||||
Counter::Counter(Player *_player, const QString &_name, QColor _color, int _value, QGraphicsItem *parent)
|
||||
: QGraphicsItem(parent), name(_name), color(_color), value(_value), player(_player)
|
||||
{
|
||||
player->addCounter(this);
|
||||
}
|
||||
|
||||
QRectF Counter::boundingRect() const
|
||||
{
|
||||
return QRectF(0, 0, 50, 30);
|
||||
return QRectF(0, 0, 40, 40);
|
||||
}
|
||||
|
||||
void Counter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
||||
|
|
@ -18,8 +18,12 @@ void Counter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
|
|||
Q_UNUSED(option);
|
||||
Q_UNUSED(widget);
|
||||
painter->save();
|
||||
painter->fillRect(boundingRect(), QBrush(QColor("gray")));
|
||||
painter->drawText(boundingRect(), Qt::AlignCenter, QString("%1").arg(value));
|
||||
painter->setBrush(QBrush(color));
|
||||
painter->drawEllipse(boundingRect());
|
||||
if (value) {
|
||||
painter->setFont(QFont("Times", 16, QFont::Bold));
|
||||
painter->drawText(boundingRect(), Qt::AlignCenter, QString("%1").arg(value));
|
||||
}
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
|
|
@ -28,3 +32,11 @@ void Counter::setValue(int _value)
|
|||
value = _value;
|
||||
update(boundingRect());
|
||||
}
|
||||
|
||||
void Counter::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton)
|
||||
player->client->incCounter(name, 1);
|
||||
else if (event->button() == Qt::RightButton)
|
||||
player->client->incCounter(name, -1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,11 +8,13 @@ class Player;
|
|||
class Counter : public QGraphicsItem {
|
||||
private:
|
||||
QString name;
|
||||
QColor color;
|
||||
int value;
|
||||
protected:
|
||||
Player *player;
|
||||
void mousePressEvent(QGraphicsSceneMouseEvent *event);
|
||||
public:
|
||||
Counter(Player *_player, const QString &_name, QGraphicsItem *parent = 0);
|
||||
Counter(Player *_player, const QString &_name, QColor _color, int _value, QGraphicsItem *parent = 0);
|
||||
QRectF boundingRect() const;
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
#include "counterlist.h"
|
||||
|
||||
Counter *CounterList::findCounter(const QString &name) const
|
||||
{
|
||||
for (int i = 0; i < size(); i++) {
|
||||
Counter *temp = at(i);
|
||||
if (!temp->getName().compare(name))
|
||||
return temp;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
#ifndef COUNTERLIST_H
|
||||
#define COUNTERLIST_H
|
||||
|
||||
#include "counter.h"
|
||||
#include <QList>
|
||||
|
||||
class CounterList : public QList<Counter *> {
|
||||
public:
|
||||
Counter *findCounter(const QString &name) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -9,6 +9,8 @@
|
|||
#include "handzone.h"
|
||||
#include "carddatabase.h"
|
||||
#include "dlg_startgame.h"
|
||||
#include "playerarea.h"
|
||||
#include "counter.h"
|
||||
|
||||
Game::Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_actionsMenu, QMenu *_cardMenu, int playerId, const QString &playerName)
|
||||
: QObject(), actionsMenu(_actionsMenu), cardMenu(_cardMenu), db(_db), client(_client), scene(_scene), started(false)
|
||||
|
|
@ -209,6 +211,7 @@ void Game::gameEvent(ServerEventData *msg)
|
|||
case eventCreateToken:
|
||||
case eventSetupZones:
|
||||
case eventSetCardAttr:
|
||||
case eventAddCounter:
|
||||
case eventSetCounter:
|
||||
case eventDelCounter:
|
||||
case eventPlayerId: {
|
||||
|
|
@ -257,7 +260,7 @@ void Game::actDecLife()
|
|||
void Game::actSetLife()
|
||||
{
|
||||
bool ok;
|
||||
int life = QInputDialog::getInteger(0, tr("Set life"), tr("New life total:"), localPlayer->getCounters()->findCounter("life")->getValue(), 0, 2000000000, 1, &ok);
|
||||
int life = QInputDialog::getInteger(0, tr("Set life"), tr("New life total:"), localPlayer->area->getCounter("life")->getValue(), 0, 2000000000, 1, &ok);
|
||||
if (ok)
|
||||
client->setCounter("life", life);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include "client.h"
|
||||
#include "cardzone.h"
|
||||
#include "playerarea.h"
|
||||
#include "counter.h"
|
||||
#include <QGraphicsScene>
|
||||
#include <QMenu>
|
||||
|
||||
|
|
@ -12,9 +13,6 @@ Player::Player(const QString &_name, int _id, QPointF _base, bool _local, CardDa
|
|||
area->setPos(_base);
|
||||
_scene->addItem(area);
|
||||
|
||||
Counter *life = new Counter(this, "life");
|
||||
life->setPos(_base + QPointF(-50, 500));
|
||||
|
||||
aMoveHandToTopLibrary = new QAction(tr("Move to &top of library"), this);
|
||||
connect(aMoveHandToTopLibrary, SIGNAL(triggered()), this, SLOT(actMoveHandToTopLibrary()));
|
||||
aMoveHandToBottomLibrary = new QAction(tr("Move to &bottom of library"), this);
|
||||
|
|
@ -70,9 +68,6 @@ Player::~Player()
|
|||
for (int i = 0; i < zones.size(); i++)
|
||||
delete zones.at(i);
|
||||
|
||||
for (int i = 0; i < counters.size(); i++)
|
||||
delete counters.at(i);
|
||||
|
||||
delete area;
|
||||
}
|
||||
|
||||
|
|
@ -123,11 +118,6 @@ void Player::addZone(CardZone *z)
|
|||
zones << z;
|
||||
}
|
||||
|
||||
void Player::addCounter(Counter *c)
|
||||
{
|
||||
counters << c;
|
||||
}
|
||||
|
||||
void Player::setCardAttrHelper(CardItem *card, const QString &aname, const QString &avalue, bool allCards)
|
||||
{
|
||||
if (aname == "tapped") {
|
||||
|
|
@ -159,17 +149,15 @@ void Player::gameEvent(ServerEventData *event)
|
|||
switch (event->getEventType()) {
|
||||
case eventSetupZones: {
|
||||
// XXX Life counter
|
||||
int life = data[0].toInt();
|
||||
int deck_cards = data[1].toInt();
|
||||
int sb_cards = data[2].toInt();
|
||||
int deck_cards = data[0].toInt();
|
||||
int sb_cards = data[1].toInt();
|
||||
// XXX Fehlerbehandlung
|
||||
|
||||
// Clean up existing zones first
|
||||
for (int i = 0; i < zones.size(); i++)
|
||||
zones.at(i)->clearContents();
|
||||
|
||||
Counter *lifeCounter = counters.findCounter("life");
|
||||
lifeCounter->setValue(life);
|
||||
|
||||
area->clearCounters();
|
||||
|
||||
CardZone *deck = zones.findZone("deck");
|
||||
for (; deck_cards; deck_cards--)
|
||||
|
|
@ -181,6 +169,17 @@ void Player::gameEvent(ServerEventData *event)
|
|||
sb->addCard(new CardItem(db, QString(), -1));
|
||||
sb->reorganizeCards();
|
||||
|
||||
if (local) {
|
||||
client->addCounter("life", QColor("white"), 20);
|
||||
client->addCounter("w", QColor(200, 200, 200), 0);
|
||||
client->addCounter("u", QColor(0, 0, 200), 0);
|
||||
client->addCounter("b", QColor(100, 100, 100), 0);
|
||||
client->addCounter("r", QColor(200, 0, 0), 0);
|
||||
client->addCounter("g", QColor(0, 200, 0), 0);
|
||||
client->addCounter("x", QColor(255, 255, 255), 0);
|
||||
client->addCounter("storm", QColor(255, 255, 255), 0);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case eventDraw: {
|
||||
|
|
@ -269,12 +268,25 @@ void Player::gameEvent(ServerEventData *event)
|
|||
}
|
||||
break;
|
||||
}
|
||||
case eventAddCounter: {
|
||||
if (data.size() != 3) {
|
||||
// XXX
|
||||
}
|
||||
QString counterName = data[0];
|
||||
int colorValue = data[1].toInt();
|
||||
int value = data[2].toInt();
|
||||
QColor color(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256);
|
||||
qDebug(QString("%1 / %2 / %3").arg(color.red()).arg(color.green()).arg(color.blue()).toLatin1());
|
||||
area->addCounter(counterName, color, value);
|
||||
break;
|
||||
}
|
||||
case eventSetCounter: {
|
||||
if (data.size() != 2) {
|
||||
// XXX
|
||||
}
|
||||
int value = data[1].toInt();
|
||||
Counter *c = counters.findCounter(data[0]);
|
||||
QString counterName = data[0];
|
||||
Counter *c = area->getCounter(counterName);
|
||||
int oldValue = c->getValue();
|
||||
c->setValue(value);
|
||||
emit logSetCounter(name, c->getName(), value, oldValue);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
#include <QInputDialog>
|
||||
#include <QPoint>
|
||||
#include "zonelist.h"
|
||||
#include "counterlist.h"
|
||||
#include "servereventdata.h"
|
||||
|
||||
class Client;
|
||||
|
|
@ -44,21 +43,18 @@ private:
|
|||
QAction *aMoveHandToTopLibrary, *aMoveHandToBottomLibrary,
|
||||
*aViewLibrary, *aViewTopCards, *aViewGraveyard, *aViewRfg, *aViewSideboard;
|
||||
|
||||
PlayerArea *area;
|
||||
|
||||
int defaultNumberTopCards;
|
||||
QString name;
|
||||
int id;
|
||||
QPointF base;
|
||||
bool local;
|
||||
ZoneList zones;
|
||||
CounterList counters;
|
||||
CardDatabase *db;
|
||||
void setCardAttrHelper(CardItem *card, const QString &aname, const QString &avalue, bool allCards);
|
||||
public:
|
||||
PlayerArea *area;
|
||||
Client *client;
|
||||
void addZone(CardZone *z);
|
||||
void addCounter(Counter *c);
|
||||
Player(const QString &_name, int _id, QPointF _base, bool _local, CardDatabase *_db, Client *_client, QGraphicsScene *_scene);
|
||||
~Player();
|
||||
QMenu *getPlayerMenu() const { return playerMenu; }
|
||||
|
|
@ -66,7 +62,6 @@ public:
|
|||
QString getName() const { return name; }
|
||||
bool getLocal() const { return local; }
|
||||
const ZoneList *const getZones() const { return &zones; }
|
||||
const CounterList *const getCounters() const { return &counters; }
|
||||
void gameEvent(ServerEventData *event);
|
||||
void hoverCardEvent(CardItem *card);
|
||||
CardDatabase *getDb() const { return db; }
|
||||
|
|
|
|||
|
|
@ -6,12 +6,13 @@
|
|||
#include "gravezone.h"
|
||||
#include "rfgzone.h"
|
||||
#include "sideboardzone.h"
|
||||
#include "counter.h"
|
||||
#include <QPainter>
|
||||
|
||||
PlayerArea::PlayerArea(Player *_player, QGraphicsItem *parent)
|
||||
: QGraphicsItem(parent), player(_player)
|
||||
{
|
||||
QPointF base = QPointF(20, 50);
|
||||
QPointF base = QPointF(55, 50);
|
||||
|
||||
LibraryZone *deck = new LibraryZone(_player, this);
|
||||
deck->setPos(base);
|
||||
|
|
@ -27,7 +28,7 @@ PlayerArea::PlayerArea(Player *_player, QGraphicsItem *parent)
|
|||
SideboardZone *sb = new SideboardZone(_player, this);
|
||||
sb->setVisible(false);
|
||||
|
||||
base = QPointF(deck->boundingRect().width() + 40, 0);
|
||||
base = QPointF(deck->boundingRect().width() + 60, 0);
|
||||
|
||||
CardZone *hand = new HandZone(_player, this);
|
||||
hand->setPos(base);
|
||||
|
|
@ -42,7 +43,7 @@ PlayerArea::PlayerArea(Player *_player, QGraphicsItem *parent)
|
|||
|
||||
PlayerArea::~PlayerArea()
|
||||
{
|
||||
|
||||
clearCounters();
|
||||
}
|
||||
|
||||
QRectF PlayerArea::boundingRect() const
|
||||
|
|
@ -58,7 +59,51 @@ void PlayerArea::paint(QPainter *painter, const QStyleOptionGraphicsItem *option
|
|||
|
||||
painter->setFont(QFont("Times", 16, QFont::Bold));
|
||||
painter->setPen(QPen(QColor("black")));
|
||||
painter->drawText(QRectF(0, 0, CARD_WIDTH + 40, 40), Qt::AlignCenter, player->getName());
|
||||
painter->drawText(QRectF(0, 0, CARD_WIDTH + 60, 40), Qt::AlignCenter, player->getName());
|
||||
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
Counter *PlayerArea::getCounter(const QString &name, bool remove)
|
||||
{
|
||||
for (int i = 0; i < counterList.size(); i++) {
|
||||
Counter *temp = counterList.at(i);
|
||||
if (temp->getName() == name) {
|
||||
if (remove)
|
||||
counterList.removeAt(i);
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void PlayerArea::addCounter(const QString &name, QColor color, int value)
|
||||
{
|
||||
counterList.append(new Counter(player, name, color, value, this));
|
||||
rearrangeCounters();
|
||||
}
|
||||
|
||||
void PlayerArea::delCounter(const QString &name)
|
||||
{
|
||||
delete getCounter(name, true);
|
||||
rearrangeCounters();
|
||||
}
|
||||
|
||||
void PlayerArea::clearCounters()
|
||||
{
|
||||
for (int i = 0; i < counterList.size(); i++)
|
||||
delete counterList.at(i);
|
||||
counterList.clear();
|
||||
}
|
||||
|
||||
void PlayerArea::rearrangeCounters()
|
||||
{
|
||||
const int counterAreaWidth = 55;
|
||||
int y = 50;
|
||||
for (int i = 0; i < counterList.size(); i++) {
|
||||
Counter *temp = counterList.at(i);
|
||||
QRectF br = temp->boundingRect();
|
||||
temp->setPos((counterAreaWidth - br.width()) / 2, y);
|
||||
y += br.height() + 10;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,19 @@
|
|||
#ifndef PLAYERAREA_H
|
||||
#define PLAYERAREA_H
|
||||
|
||||
#include <QList>
|
||||
#include "carditem.h"
|
||||
|
||||
class Player;
|
||||
class Counter;
|
||||
|
||||
class PlayerArea : public QGraphicsItem {
|
||||
private:
|
||||
QRectF bRect;
|
||||
Player *player;
|
||||
QList<Counter *> counterList;
|
||||
|
||||
void rearrangeCounters();
|
||||
public:
|
||||
enum { Type = typeOther };
|
||||
int type() const { return Type; }
|
||||
|
|
@ -16,6 +21,11 @@ public:
|
|||
~PlayerArea();
|
||||
QRectF boundingRect() const;
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
|
||||
|
||||
Counter *getCounter(const QString &name, bool remove = false);
|
||||
void addCounter(const QString &name, QColor color, int value);
|
||||
void delCounter(const QString &name);
|
||||
void clearCounters();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
// Message structure for server events:
|
||||
// {"private","public"}|PlayerId|PlayerName|EventType|EventData
|
||||
|
||||
const int event_count = 19;
|
||||
const int event_count = 20;
|
||||
const event_string event_strings[event_count] = {
|
||||
{eventPlayerId, "player_id"},
|
||||
{eventSay, "say"},
|
||||
|
|
@ -19,6 +19,7 @@ const event_string event_strings[event_count] = {
|
|||
{eventMoveCard, "move_card"},
|
||||
{eventCreateToken, "create_token"},
|
||||
{eventSetCardAttr, "set_card_attr"},
|
||||
{eventAddCounter, "add_counter"},
|
||||
{eventSetCounter, "set_counter"},
|
||||
{eventDelCounter, "del_counter"},
|
||||
{eventSetActivePlayer, "set_active_player"},
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ enum ServerEventType {
|
|||
eventMoveCard,
|
||||
eventCreateToken,
|
||||
eventSetCardAttr,
|
||||
eventAddCounter,
|
||||
eventSetCounter,
|
||||
eventDelCounter,
|
||||
eventSetActivePlayer,
|
||||
|
|
|
|||
|
|
@ -121,8 +121,8 @@ void MainWindow::updateSceneSize()
|
|||
{
|
||||
QRectF sr = scene->sceneRect();
|
||||
QSizeF zoneSize = zoneLayout->size();
|
||||
qDebug(QString("updateSceneSize: width=%1").arg(932 + zoneSize.width()).toLatin1());
|
||||
scene->setSceneRect(sr.x(), sr.y(), 932 + zoneSize.width(), sr.height());
|
||||
qDebug(QString("updateSceneSize: width=%1").arg(952 + zoneSize.width()).toLatin1());
|
||||
scene->setSceneRect(sr.x(), sr.y(), 952 + zoneSize.width(), sr.height());
|
||||
view->scaleToScene();
|
||||
}
|
||||
|
||||
|
|
@ -218,13 +218,13 @@ MainWindow::MainWindow(QWidget *parent)
|
|||
int cardCount = db->loadFromFile("../cards.dat");
|
||||
qDebug(QString("%1 cards loaded").arg(cardCount).toLatin1());
|
||||
|
||||
scene = new QGraphicsScene(0, 0, 932, 1020, this);
|
||||
scene = new QGraphicsScene(0, 0, 952, 1020, this);
|
||||
view = new GameView(scene);
|
||||
|
||||
// view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
|
||||
|
||||
zoneLayout = new ZoneViewLayout(db);
|
||||
zoneLayout->setPos(932, 0);
|
||||
zoneLayout->setPos(952, 0);
|
||||
scene->addItem(zoneLayout);
|
||||
connect(zoneLayout, SIGNAL(sizeChanged()), this, SLOT(updateSceneSize()));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue