-
Notifications
You must be signed in to change notification settings - Fork 1
/
dungeon.h
230 lines (203 loc) · 9.83 KB
/
dungeon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* box2dworld.h
* Copyright (c) 2010 Thorbjørn Lindeijer <[email protected]>
* Copyright (c) 2011 Joonas Erkinheimo <[email protected]>
* Copyright (c) 2012 Adriano Rezende <[email protected]>
*
* This file is part of the Box2D QML plugin.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in
* a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef DUNGEON_H
#define DUNGEON_H
#include <QQuickItem>
#include <QApplication>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDesktopWidget>
#include <QTime>
#include <utils.h>
#include <DungeonGenerator.h>
class Dungeon : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(int cellSize READ cellSize WRITE setCellSize NOTIFY cellSizeChanged) // cell size
Q_PROPERTY(int levelWidth READ levelWidth WRITE setLevelWidth) // level size
Q_PROPERTY(int levelHeight READ levelHeight WRITE setLevelHeight) // level size
public:
explicit Dungeon(QQuickItem *parent = 0){
//--------Create Debug Console
QQmlComponent component(&engine);
//QString s("import QtQuick 1.1; Flickable {x:0; y:0; width:"+QString::number(QApplication::focusWidget()->geometry().width(),10) + "; height: 300; Text{x:0; y:0; text: \"Hello world!\"} }");
//-----------ADD GENERATION FUNCTION TO ADD CONTENT + HERO AND OTHER CPP PLUGINS AS ITEMS USE
};
~Dungeon(){};
// TODO: придумать либо генерацию имен с одинаковыми началами для objectName либо таки как свойство обектов для определения колизий но что бы обьекты можна было найти и по ojectname каждый
Q_INVOKABLE void generateMapNew(int width, int height){
// MAP GENERATION
QList<QList<int> > t;
DungeonGenerator mydungeon( QString::number(QTime::currentTime().msec()).toStdString(), 30, 30); // for repeat level set the same string name
mydungeon.Generate();
Grid griddata = mydungeon.GetGrid();
m_MapNew = griddata;
}
//TODO: load from file, save to file, now it just fill level from generated map
Q_INVOKABLE void loadMap(){
QQmlEngine engine;
QQmlComponent component(&engine);
for (int i=0; i<mLevelWidth; i++){
for (int k=0; k<mLevelHeight; k++){
switch(m_MapNew[i][k]){
// empty
case Empty:{
component.loadUrl(QUrl("qrc:/qml/ANGSC1/tile-wall.qml"));
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
QObject *ground = this->findChild<QObject*>("ground");
item->setParentItem(qobject_cast<QQuickItem *>(ground));
item->setProperty("x", i*mCellSize);
item->setProperty("y", k*mCellSize);
mapItems.append(item);
break;
}
// floor
case Floor:{
//IMPORTANT !!! here can load - file.qml too, not only from string
// !!! Приведения Обьекта к типу QML item из созданого компонента загруженог ос QML файла
component.loadUrl(QUrl("qrc:/qml/ANGSC1/tile-floor.qml"));
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
// !!! Ищем дочерний елемент с именем обьекта ground и делаем базовым для нашего созданого компонента
QObject *ground = this->findChild<QObject*>("ground");
item->setParentItem(qobject_cast<QQuickItem *>(ground));
// преобразовываем свойства загруженого QML компонента
item->setProperty("x", i*mCellSize);
item->setProperty("y", k*mCellSize);
// добавляем его в список наш на всякий случай для доступа
mapItems.append(item);
break;
}
// Corridor
case Corridor:{
component.loadUrl(QUrl("qrc:/qml/ANGSC1/tile-corridor.qml"));
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
QObject *ground = this->findChild<QObject*>("ground");
item->setParentItem(qobject_cast<QQuickItem *>(ground));
item->setProperty("x", i*mCellSize);
item->setProperty("y", k*mCellSize);
mapItems.append(item);
break;
}
// Door
case Door:{
component.loadUrl(QUrl("qrc:/qml/ANGSC1/tile-door.qml"));
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
QObject *ground = this->findChild<QObject*>("ground");
item->setParentItem(qobject_cast<QQuickItem *>(ground));
item->setProperty("x", i*mCellSize);
item->setProperty("y", k*mCellSize);
mapItems.append(item);
break;
}
// Trap
case Trap:{
component.loadUrl(QUrl("qrc:/qml/ANGSC1/tile-trap.qml"));
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
QObject *ground = this->findChild<QObject*>("ground");
item->setParentItem(qobject_cast<QQuickItem *>(ground));
item->setProperty("x", i*mCellSize);
item->setProperty("y", k*mCellSize);
mapItems.append(item);
break;
}
// Treasure
case Treasure:{
component.loadUrl(QUrl("qrc:/qml/ANGSC1/tile-treasure.qml"));
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
QObject *ground = this->findChild<QObject*>("ground");
item->setParentItem(qobject_cast<QQuickItem *>(ground));
item->setProperty("x", i*mCellSize);
item->setProperty("y", k*mCellSize);
mapItems.append(item);
break;
}
// Monster
case Monster:{
component.loadUrl(QUrl("qrc:/qml/ANGSC1/tile-monster.qml"));
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
QObject *ground = this->findChild<QObject*>("ground");
item->setParentItem(qobject_cast<QQuickItem *>(ground));
item->setProperty("x", i*mCellSize);
item->setProperty("y", k*mCellSize);
mapItems.append(item);
break;
}
// Entrance
case Entrance:{
component.loadUrl(QUrl("qrc:/qml/ANGSC1/tile-entrance.qml"));
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
QObject *ground = this->findChild<QObject*>("ground");
item->setParentItem(qobject_cast<QQuickItem *>(ground));
item->setProperty("x", i*mCellSize);
item->setProperty("y", k*mCellSize);
mapItems.append(item);
break;
}
// Exit
case Exit:{
component.loadUrl(QUrl("qrc:/qml/ANGSC1/tile-exit.qml"));
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
QObject *ground = this->findChild<QObject*>("ground");
item->setParentItem(qobject_cast<QQuickItem *>(ground));
item->setProperty("x", i*mCellSize);
item->setProperty("y", k*mCellSize);
mapItems.append(item);
break;
}
}
}
}
}
// cell size
int cellSize() const { return mCellSize; }
void setCellSize(int cellSize){mCellSize = cellSize;}
// level width
int levelWidth() const { return mLevelWidth; }
void setLevelWidth(int levelWidth){mLevelWidth = levelWidth;}
// level height
int levelHeight() const { return mLevelHeight; }
void setLevelHeight(int levelHeight){mLevelHeight = levelHeight;}
signals:
void cellSizeChanged();
private:
//- access to context
QQmlEngine engine;
// List of loaded QML items may be usable
QList<QQuickItem*> mapItems;
int mCellSize; // cell size
int mLevelWidth; // level width
int mLevelHeight; // level height
struct Map{
QList<QList<int> > ground;
QList<QList<int> > enivorment;
QList<QList<int> > creatures;
} map;
//QList<QList<int> > m_MapNew;
std::vector<std::vector<uint> > m_MapNew;
};
QML_DECLARE_TYPE(Dungeon)
#endif // DUNGEON_H