~mzanetti/+junk/ubuntudefence

« back to all changes in this revision

Viewing changes to levelpack.cpp

  • Committer: Michael Zanetti
  • Date: 2014-07-06 01:09:39 UTC
  • Revision ID: michael.zanetti@canonical.com-20140706010939-x2m8q24bgwuj867b
add support for locking levels and properly collect level reward points

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "levelpack.h"
2
2
 
 
3
#include "level.h"
 
4
 
 
5
 
3
6
LevelPack::LevelPack(Engine *engine, const QString &id, const QString &name, const QString &levelSelectBackground, QObject *parent) :
4
 
    QObject(parent),
 
7
    QAbstractListModel(parent),
5
8
    m_engine(engine),
6
9
    m_id(id),
7
10
    m_name(name),
25
28
    return m_levelSelectBackground;
26
29
}
27
30
 
 
31
int LevelPack::rowCount(const QModelIndex &parent) const
 
32
{
 
33
    return m_levels.count();
 
34
}
 
35
 
 
36
QVariant LevelPack::data(const QModelIndex &index, int role) const
 
37
{
 
38
    switch (role) {
 
39
    case RoleHighscore:
 
40
        return m_levels.at(index.row())->highscore();
 
41
    }
 
42
    return QVariant();
 
43
}
 
44
 
 
45
QHash<int, QByteArray> LevelPack::roleNames() const
 
46
{
 
47
    QHash<int, QByteArray> roles;
 
48
    roles.insert(RoleHighscore, "highscore");
 
49
    return roles;
 
50
}
 
51
 
 
52
Level *LevelPack::get(int index) const
 
53
{
 
54
    qDebug() << "requesting level" << index << "got" << m_levels.count();
 
55
    if (index >= m_levels.count()) {
 
56
        return nullptr;
 
57
    }
 
58
    return m_levels.at(index);
 
59
}
 
60
 
28
61
Enemies *LevelPack::enemies() const
29
62
{
30
63
    return m_enemies;
35
68
    return m_engine->towerFactory();
36
69
}
37
70
 
 
71
Level* LevelPack::addLevel(const QVariantMap &levelMap)
 
72
{
 
73
    beginInsertRows(QModelIndex(), m_levels.count(), m_levels.count());
 
74
    Level *level = new Level(levelMap, this);
 
75
    m_levels.append(level);
 
76
    endInsertRows();
 
77
    connect(level, &Level::highscoreChanged, this, &LevelPack::levelHighscoreChanged);
 
78
    return level;
 
79
}
 
80
 
38
81
void LevelPack::init()
39
82
{
40
83
    m_enemies->clear();
43
86
        m_enemies->addEnemy(m_engine->enemyFactory()->createEnemy(id));
44
87
    }
45
88
}
 
89
 
 
90
void LevelPack::levelHighscoreChanged()
 
91
{
 
92
    Level *level = static_cast<Level*>(sender());
 
93
    int idx = m_levels.indexOf(level);
 
94
    emit dataChanged(index(idx), index(idx));
 
95
}