-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added forceLayout() to grid to make it more responsive.
Also work on a new C++-based game model.
- Loading branch information
Showing
9 changed files
with
206 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
*.android | ||
*.keystore | ||
|
||
KeystoreInfo.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "gamemodel.h" | ||
|
||
GameModel::GameModel(QObject *parent) : | ||
QAbstractListModel(parent) | ||
{ | ||
int x, y, z; | ||
int randItem [2]; | ||
QModelIndex ind; | ||
QMap<int,QVariant> obj_roles; | ||
|
||
x = 0; | ||
y = 0; | ||
|
||
for (z = 0; z < rowCount; z++) | ||
{ | ||
obj_roles[ValueRole] = 0; | ||
obj_roles[XRole] = x; | ||
obj_roles[YRole] = y; | ||
|
||
setItemData(index(z),obj_roles); | ||
|
||
if (x == 3) | ||
x = 0; | ||
else | ||
x++; | ||
|
||
if (y == 3) | ||
y = 0; | ||
else | ||
y++; | ||
} | ||
|
||
randItem[0] = rand() % 16; | ||
randItem[1] = rand() % 16; | ||
|
||
while (randItem[1] == randItem[0]) | ||
randItem[1] = rand() % 16; | ||
|
||
for (z = 0; z < 2; z++) | ||
{ | ||
ind = index(randItem[z]); | ||
|
||
setData(ind,2*(rand() % 2 + 1),ValueRole); | ||
} | ||
} | ||
|
||
QHash<int,QByteArray> GameModel::roleNames() const | ||
{ | ||
QHash<int, QByteArray> roles; | ||
|
||
roles[ValueRole] = "value"; | ||
roles[XRole] = "xpos"; | ||
roles[YRole] = "ypos"; | ||
|
||
return roles; | ||
} | ||
|
||
QVariant GameModel::data(const QModelIndex &index, int role) const | ||
{ | ||
if (!index.isValid()) | ||
return QVariant(); | ||
|
||
GameTile* tile = static_cast<GameTile*>(index.internalPointer()); | ||
|
||
return tile->data(role); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#ifndef GAMEMODEL_H | ||
#define GAMEMODEL_H | ||
|
||
#include <QAbstractListModel> | ||
#include <QModelIndex> | ||
#include <QVariant> | ||
|
||
#include <cstdlib> | ||
|
||
#include "gametile.h" | ||
|
||
class GameModel : public QAbstractListModel | ||
{ | ||
Q_OBJECT | ||
public: | ||
enum GameRoles { | ||
ValueRole = Qt::UserRole + 1, | ||
XRole, | ||
YRole | ||
}; | ||
|
||
enum MoveDirection { | ||
Up, | ||
Right, | ||
Down, | ||
Left | ||
}; | ||
|
||
explicit GameModel(QObject *parent = 0); | ||
|
||
QHash<int,QByteArray> roleNames() const; | ||
int rowCount (const QModelIndex &parent) const; | ||
QVariant data (const QModelIndex &index, int role) const; | ||
|
||
/** INTERNAL DATA MANAGEMENT METHODS **/ | ||
|
||
void swap (int index1, int index2); | ||
void merge (int from, int to); | ||
|
||
/** QML INVOKABLE METHODS **/ | ||
Q_INVOKABLE void createNew(); | ||
Q_INVOKABLE void generate(); | ||
|
||
Q_INVOKABLE bool doesTileExist (int value); | ||
Q_INVOKABLE void makeMove (MoveDirection dir); | ||
|
||
Q_INVOKABLE int indexAtCoord (int x, int y); | ||
Q_INVOKABLE QVariant getTileAtCoord (int x, int y); | ||
signals: | ||
void addScore (int score); | ||
|
||
public slots: | ||
|
||
}; | ||
|
||
#endif // GAMEMODEL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "gametile.h" | ||
|
||
GameTile::GameTile(const QList<QVariant> &data) | ||
{ | ||
itemData = data; | ||
} | ||
|
||
QVariant GameTile::data(int index) | ||
{ | ||
return itemData.value(index); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef GAMETILE_H | ||
#define GAMETILE_H | ||
|
||
#include <QList> | ||
#include <QVariant> | ||
|
||
class GameTile | ||
{ | ||
public: | ||
GameTile(QList<QVariant> data); | ||
|
||
QVariant data (int index); | ||
private: | ||
QList<QVariant> itemData; | ||
}; | ||
|
||
#endif // GAMETILE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters