Commit 39795399 authored by Ian Wadham's avatar Ian Wadham

Make KGrSprites inherit from KGameRenderedItem and become items in the scene.

Add the methods to show hidden ladders and display the collection and dropping of gold.

CCMAIL: roney477@gmail.com

svn path=/branches/work/kgoldrunner-qgv/; revision=1312119
parent 72e43fea
......@@ -707,7 +707,7 @@ void KGrCanvas::gotGold (const int spriteId, const int i, const int j,
// If the rules allow, show whether or not an enemy sprite is carrying gold.
if (enemiesShowGold && (sprites->at(spriteId)->spriteType() == ENEMY)) {
sprites->at(spriteId)->setFrameOffset (spriteHasGold ? goldEnemy : 0);
//sprites->at(spriteId)->setFrameOffset (spriteHasGold ? goldEnemy : 0);
}
}
......
......@@ -146,7 +146,7 @@ void KGrLevelPlayer::init (KGrView * view,
randIndex = 0;
T = 0;
// view->setGoldEnemiesRule (rules->enemiesShowGold());
view->gameScene()->setGoldEnemiesRule (rules->enemiesShowGold());
// Determine the access for hero and enemies to and from each grid-cell.
grid->calculateAccess (rules->runThruHole());
......@@ -240,8 +240,8 @@ void KGrLevelPlayer::init (KGrView * view,
}
// Connect the hero's and enemies' efforts to the graphics.
/* connect (this, SIGNAL (gotGold(int,int,int,bool,bool)),
view, SLOT (gotGold(int,int,int,bool,bool))); */
connect (this, SIGNAL (gotGold(int,int,int,bool,bool)),
view->gameScene(), SLOT (gotGold(int,int,int,bool,bool)));
// Connect mouse-clicks from KGrCanvas to digging slot.
connect (view, SIGNAL (mouseClick(int)), SLOT (doDig(int)));
......@@ -281,8 +281,8 @@ void KGrLevelPlayer::init (KGrView * view,
view->gameScene(), SLOT (deleteSprite(int)));
// Connect the grid to the view, to show hidden ladders when the time comes.
/* connect (grid, SIGNAL (showHiddenLadders(QList<int>,int)),
view, SLOT (showHiddenLadders(QList<int>,int))); */
connect (grid, SIGNAL (showHiddenLadders(QList<int>,int)),
view->gameScene(), SLOT (showHiddenLadders(QList<int>,int)));
// Connect and start the timer. The tick() slot emits signal animation(),
// so there is just one time-source for the model and the view.
......
......@@ -158,6 +158,21 @@ KGameRenderedItem * KGrRenderer::getTileItem
return tile;
}
KGrSprite * KGrRenderer::getSpriteItem (const char picType, const int tickTime)
{
int index = findKeyTableIndex (picType);
if (index < 0) {
return 0; // Missing type, so no KGrSprite item.
}
QString key = (picType == HERO) ? "hero" :
((picType == ENEMY) ? "enemy" : "brick");
KGrSprite * sprite = new KGrSprite ((keyTable[index].picSource == Set) ?
m_setRenderer : m_actorsRenderer,
key, picType, tickTime);
m_scene->addItem (sprite);
return sprite;
}
KGameRenderedItem * KGrRenderer::getBackground
(const int level, KGameRenderedItem * currentBackground)
{
......@@ -194,7 +209,7 @@ bool KGrRenderer::hasBorder() const
{
QString s = m_setRenderer->theme()->customData("DrawCanvasBorder", "0");
if (s == "1")
if (s == QString ("1"))
return true;
else
return false;
......
......@@ -23,6 +23,8 @@
#include <QString>
#include <KGameRenderer>
#include "kgrsprite.h"
class KGrScene;
class KgThemeProvider;
class KgThemeSelector;
......@@ -105,6 +107,12 @@ public:
*/
KGameRenderedItem * getBorderItem (QString spriteKey,
KGameRenderedItem * currentItem);
/*
* TODO - Document this.
*/
KGrSprite * getSpriteItem (const char picType, const int tickTime);
/*
* Returns true case the current theme has a border around its background
* and false otherwise.
......
......@@ -83,6 +83,9 @@ void KGrScene::redrawScene ()
setTileSize (tile, tileSize);
}
}
foreach (KGrSprite * sprite, m_sprites) {
setTileSize (sprite, tileSize);
}
m_sizeChanged = false;
m_tileSize = tileSize;
......@@ -198,7 +201,7 @@ void KGrScene::paintCell (const int i, const int j, const char type)
int KGrScene::makeSprite (const char type, int i, int j)
{
int spriteId;
KGrSprite * sprite = new KGrSprite (m_renderer, type, TickTime);
KGrSprite * sprite = m_renderer->getSpriteItem (type, TickTime);
if (m_sprites.count(0) > 0 &&
((spriteId = m_sprites.lastIndexOf (0)) >= 0)) {
......@@ -230,7 +233,7 @@ int KGrScene::makeSprite (const char type, int i, int j)
frame1 = 1;
// The hero and enemies must be painted in front of dug bricks.
// sprite->stackUnder (sprites->at (heroId));
sprite->setZ (0);
// Erase the brick-image so that animations are visible in all themes.
paintCell (i, j, FREE);
......@@ -239,12 +242,8 @@ int KGrScene::makeSprite (const char type, int i, int j)
break;
}
// In KGoldrunner, the top-left visible cell is [1,1]: in KGrSprite [0,0].
sprite->move (i, j, frame1);
// sprite->show();
// kDebug() << "Sprite ID" << spriteId << "sprite type" << type
// << "at" << i << j;
return spriteId;
}
......@@ -315,6 +314,30 @@ void KGrScene::startAnimation (const int id, const bool repeating,
time, nFrameChanges);
}
void KGrScene::gotGold (const int spriteId, const int i, const int j,
const bool spriteHasGold, const bool lost)
{
// Hide collected gold or show dropped gold, but not if the gold was lost.
if (! lost) {
paintCell (i, j, (spriteHasGold) ? FREE : NUGGET);
}
// If the rules allow, show whether or not an enemy sprite is carrying gold.
if (enemiesShowGold && (m_sprites.at(spriteId)->spriteType() == ENEMY)) {
m_sprites.at(spriteId)->setSpriteKey (spriteHasGold ? "gold_enemy"
: "enemy");
}
}
void KGrScene::showHiddenLadders (const QList<int> & ladders, const int width)
{
foreach (int offset, ladders) {
int i = offset % width;
int j = offset / width;
paintCell (i, j, LADDER);
}
}
void KGrScene::deleteSprite (const int spriteId)
{
QPointF loc = m_sprites.at(spriteId)->currentLoc();
......
......@@ -100,10 +100,15 @@ public:
*/
KGrRenderer * renderer () const { return m_renderer; }
inline void setGoldEnemiesRule (bool showIt) { enemiesShowGold = showIt; }
public slots:
int makeSprite (const char type, int i, int j);
void animate (bool missed);
void gotGold (const int spriteId, const int i, const int j,
const bool spriteHasGold, const bool lost = false);
void showHiddenLadders (const QList<int> & ladders, const int width);
void deleteSprite (const int id);
void deleteAllSprites ();
......@@ -200,6 +205,8 @@ private:
// The type of each tile stored in m_tiles.
QByteArray m_tileTypes;
bool enemiesShowGold; // Show or conceal if enemies have gold.
};
#endif // KGRSCENE_H
......@@ -16,15 +16,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
// #include "kgrglobals.h" // IDW test.
#include "kgrsprite.h"
#include "kgrrenderer.h"
#include <KDebug>
KGrSprite::KGrSprite (KGrRenderer * renderer, const char type,
const int tickTime)
KGrSprite::KGrSprite (KGameRenderer * renderer, QString & key,
const char type, const int tickTime)
:
m_frameOffset (0), // No offset at first (e.g. carrying no gold).
KGameRenderedItem (renderer, key),
m_type (type),
m_tickTime (tickTime),
m_stationary (true), // Animation is OFF at first.
......@@ -35,10 +37,8 @@ KGrSprite::KGrSprite (KGrRenderer * renderer, const char type,
m_frameCtr (0),
m_dx (0),
m_dy (0),
m_dt (0),
m_renderer (renderer)
m_dt (0)
{
m_item = m_renderer->getTileItem (type, 0);
}
KGrSprite::~KGrSprite()
......@@ -47,19 +47,9 @@ KGrSprite::~KGrSprite()
void KGrSprite::move (double x, double y, int frame)
{
// Adjust the frame-number if the sprite is an enemy carrying gold and the
// caller is not already using an adjusted frame number. The value of
// m_frameOffset is either 0 or the number of the first gold-carrying frame.
int adjustedFrame = (frame < m_frameOffset) ? frame + m_frameOffset : frame;
if (m_item->frame() != adjustedFrame) {
m_item->setFrame(adjustedFrame);
}
if ((m_item->x() != x) || (m_item->y() != y)) {
m_item->setPos(x, y);
}
setFrame (frame); // Set the frame in KGameRenderedItem.
setPos (x, y); // Set the position in the scene.
return;
}
void KGrSprite::setAnimation (bool repeating, int x, int y, int startFrame,
......
......@@ -26,19 +26,17 @@
class KGrRenderer;
class KGrSprite
class KGrSprite : public KGameRenderedItem
{
public:
explicit KGrSprite (KGrRenderer * renderer,
const char type = ' ',
const int tickTime = 20);
explicit KGrSprite (KGameRenderer * renderer, QString & key,
const char type, const int tickTime = 20);
~KGrSprite();
inline char spriteType () { return m_type; }
inline QPointF currentLoc () { return m_item->pos(); }
inline int currentFrame () { return m_item->frame(); }
inline void setZ (qreal z) { m_item->setZValue(z); }
inline void setFrameOffset (int offset) { m_frameOffset = offset; }
inline QPointF currentLoc () { return pos(); }
inline int currentFrame () { return frame(); }
inline void setZ (qreal z) { setZValue(z); }
void move (double x, double y, int frame);
void animate (bool missed);
......@@ -47,8 +45,6 @@ public:
int nFrameChanges);
private:
int m_frameOffset; // For extra animation frames (e.g. gold enemy).
char m_type;
char m_tickTime;
bool m_stationary;
......@@ -65,9 +61,6 @@ private:
int m_ticks;
double m_frameTicks;
double m_frameChange;
KGrRenderer * m_renderer;
KGameRenderedItem * m_item;
};
#endif // KGRSPRITE_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment