[kde-doc-english] [knavalbattle] src: Do not hide ships when they cannot be placed.
Roney Gomes
roney477 at gmail.com
Tue Jan 14 01:45:26 UTC 2014
Git commit a80288ebf8759e0a74b3dd7b5f3c50114b340a4b by Roney Gomes.
Committed on 14/01/2014 at 01:15.
Pushed by roneygomes into branch 'master'.
Do not hide ships when they cannot be placed.
Some methods in the Delegate classes were too much procedural to my
like. I don't see with good eyes a method that should return a bool
returning a pointer instead.
To get rid of this, Delegate::canAddShip() now returns bool and when we
need to get a pointer to the next ship to be placed, nextShip() -- which
became public -- does its job.
This is not everything, the coolest part of this commit is that now
sprites will turn red when a ship can't be placed and green when this is
not the case. Just like some RTS games do. :-)
Just between us, having no sprites at all wasn't very user friendly.
FEATURE: 308215
FIXED-IN: 4.13.1
GUI:
DIGEST:
M +32 -20 src/battlefieldview.cpp
M +5 -1 src/battlefieldview.h
M +2 -1 src/delegate.h
M +9 -9 src/playerentity.cpp
M +2 -2 src/playerentity.h
M +3 -8 src/seaview.cpp
M +15 -0 src/sprite.cpp
M +2 -0 src/sprite.h
http://commits.kde.org/knavalbattle/a80288ebf8759e0a74b3dd7b5f3c50114b340a4b
diff --git a/src/battlefieldview.cpp b/src/battlefieldview.cpp
index 0fc2a35..6e50c88 100644
--- a/src/battlefieldview.cpp
+++ b/src/battlefieldview.cpp
@@ -149,21 +149,40 @@ void BattleFieldView::refresh()
}
}
-void BattleFieldView::setPreview(const QPointF& pos, Ship* ship)
+void BattleFieldView::setPreview(const QPoint & pos)
{
- if (!m_preview.sprite) {
- m_preview.ship = ship;
- m_preview.sprite = m_factory.createShip(ship);
- kDebug() << "created preview: dir =" << ship->direction();
- m_preview.sprite->setOpacity(PREVIEW_OPACITY);
- scene()->addItem(m_preview.sprite);
- m_preview.sprite->stackBefore(m_screen);
+ Ship * ship = m_delegate->nextShip();
+
+ if (!ship) {
+ return;
}
-
- m_preview.pos = m_renderer->toLogical(pos);
+
+ loadPreviewSprite(ship);
+ Coord coordinate = m_renderer->toLogical(pos);
+
+ if (m_delegate->canAddShip(m_player, coordinate)) {
+ m_preview.sprite->turnGreen();
+ } else {
+ m_preview.sprite->turnRed();
+ }
+
+ QPointF scenePos = mapToScene(pos);
+ m_preview.pos = m_renderer->toLogical(scenePos);
m_preview.sprite->setPos(m_renderer->toReal(m_preview.pos));
}
+void BattleFieldView::loadPreviewSprite(Ship * ship)
+{
+ if (m_preview.ship) {
+ return;
+ }
+
+ m_preview.ship = ship;
+ m_preview.sprite = m_factory.createShip(ship);
+
+ m_preview.sprite->setOpacity(PREVIEW_OPACITY);
+ scene()->addItem(m_preview.sprite);
+}
void BattleFieldView::cancelPreview()
{
delete m_preview.sprite;
@@ -281,11 +300,7 @@ void BattleFieldView::mousePressEvent(QMouseEvent *ev)
else if (ev->button() == Qt::RightButton && m_delegate)
{
m_delegate->changeDirection(m_player);
-
- Coord c = m_renderer->toLogical(ev->pos());
-
- if(Ship *ship = m_delegate->canAddShip(m_player, c))
- setPreview(mapToScene(ev->pos()), ship);
+ setPreview(ev->pos());
}
}
@@ -313,10 +328,7 @@ void BattleFieldView::mouseMoveEvent(QMouseEvent *ev)
else
{
cancelPreview();
- Coord c = m_renderer->toLogical(ev->pos());
-
- if(Ship *ship = m_delegate->canAddShip(m_player, c))
- setPreview(mapToScene(ev->pos()), ship);
+ setPreview(ev->pos());
}
}
@@ -344,4 +356,4 @@ void BattleFieldView::setPlayer(Sea::Player player)
m_player = player;
}
-const qreal BattleFieldView::PREVIEW_OPACITY = 0.5;
+const qreal BattleFieldView::PREVIEW_OPACITY = 0.7;
diff --git a/src/battlefieldview.h b/src/battlefieldview.h
index 73335d7..2810cb6 100644
--- a/src/battlefieldview.h
+++ b/src/battlefieldview.h
@@ -70,14 +70,18 @@ class BattleFieldView : public QGraphicsView
typedef QMultiHash<Coord, Sprite*> Sprites;
Sprites m_sprites;
+
+private:
void addSprite(const Coord& c, Sprite* ship);
+ void loadPreviewSprite(Ship * ship);
+
public:
BattleFieldView(QWidget* parent, KBSRenderer* renderer, const QString& bgID, int gridSize);
void toggleGrid(bool show);
void refresh();
- void setPreview(const QPointF& pos, Ship* ship);
+ void setPreview(const QPoint &pos);
void cancelPreview();
void add(Ship* ship);
void hit(const Coord& c);
diff --git a/src/delegate.h b/src/delegate.h
index 3e10b42..299e567 100644
--- a/src/delegate.h
+++ b/src/delegate.h
@@ -16,7 +16,8 @@ public:
virtual ~Delegate() { }
virtual void action(Sea::Player player, const Coord& c) = 0;
virtual void changeDirection(Sea::Player player) = 0;
- virtual Ship* canAddShip(Sea::Player player, const Coord& c) = 0;
+ virtual bool canAddShip(Sea::Player player, const Coord& c) = 0;
+ virtual Ship * nextShip() = 0;
};
#endif // DELEGATE_H
diff --git a/src/playerentity.cpp b/src/playerentity.cpp
index 230f1ec..f447b2d 100644
--- a/src/playerentity.cpp
+++ b/src/playerentity.cpp
@@ -122,19 +122,19 @@ void PlayerEntity::changeDirection(Sea::Player player)
}
}
-Ship* PlayerEntity::canAddShip(Sea::Player player, const Coord& c)
+bool PlayerEntity::canAddShip(Sea::Player player, const Coord& c)
{
- Ship* next = nextShip();
- if (next == 0 || player != m_player) {
- return 0;
+ if (m_ships.isEmpty() || player != m_player) {
+ return false;
}
+
+ Ship * next = m_ships.at(0);
+
if (m_sea->canAddShip(player, c, next->size(), next->direction())) {
- next->setPosition(c);
- return next;
- }
- else {
- return 0;
+ return true;
}
+
+ return false;
}
void PlayerEntity::registerHit(Sea::Player player, const Coord&)
diff --git a/src/playerentity.h b/src/playerentity.h
index aae134e..1c2efda 100644
--- a/src/playerentity.h
+++ b/src/playerentity.h
@@ -23,7 +23,6 @@ class PlayerEntity : public UIEntity, private Delegate
Q_OBJECT
QList<Ship*> m_ships;
- Ship* nextShip();
Ship* canAddShip(const Coord& c);
ChatWidget* m_chat;
@@ -40,7 +39,8 @@ public:
// delegate interface
virtual void action(Sea::Player player, const Coord& c);
virtual void changeDirection(Sea::Player player);
- virtual Ship* canAddShip(Sea::Player player, const Coord& c);
+ virtual bool canAddShip(Sea::Player player, const Coord& c);
+ virtual Ship * nextShip();
virtual void setNick(const QString& nick);
protected:
diff --git a/src/seaview.cpp b/src/seaview.cpp
index 34d44c3..595a5ca 100644
--- a/src/seaview.cpp
+++ b/src/seaview.cpp
@@ -162,14 +162,9 @@ bool SeaView::setPreview(Sea::Player player, const QPoint& pos)
{
QPoint p = pos - m_fields[player]->pos();
Coord c = m_renderer->toLogical(p);
- if (Ship* ship = m_delegate->canAddShip(player, c)) {
- m_fields[player]->setPreview(p, ship);
- return true;
- }
- else {
- m_fields[player]->cancelPreview();
- return false;
- }
+
+ m_fields[player]->setPreview(p);
+ return m_delegate->canAddShip(player, c);
}
void SeaView::cancelPreview()
diff --git a/src/sprite.cpp b/src/sprite.cpp
index a368ae8..e9ee4a1 100644
--- a/src/sprite.cpp
+++ b/src/sprite.cpp
@@ -10,6 +10,7 @@
#include "sprite.h"
#include <kdebug.h>
+#include <QGraphicsEffect>
#include "kbsrenderer.h"
@@ -26,6 +27,20 @@ Sprite::~Sprite()
{
}
+void Sprite::turnGreen()
+{
+ QGraphicsColorizeEffect * fx = new QGraphicsColorizeEffect();
+ fx->setColor(QColor(Qt::green));
+ setGraphicsEffect(fx);
+}
+
+void Sprite::turnRed()
+{
+ QGraphicsColorizeEffect * fx = new QGraphicsColorizeEffect();
+ fx->setColor(QColor(Qt::red));
+ setGraphicsEffect(fx);
+}
+
void Sprite::refresh(KBSRenderer* renderer)
{
if (m_rotated)
diff --git a/src/sprite.h b/src/sprite.h
index 09f0073..4cef6fc 100644
--- a/src/sprite.h
+++ b/src/sprite.h
@@ -26,6 +26,8 @@ public:
~Sprite();
void refresh(KBSRenderer* renderer);
+ void turnRed();
+ void turnGreen();
};
#endif // SPRITE_H
More information about the kde-doc-english
mailing list