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
|
/*
* Copyright 2007-2008 Nathalie Liesse <nathalie.liesse@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "kapmanparser.h"
#include "element.h"
#include "pill.h"
#include "energizer.h"
#include <QDebug>
KapmanParser::KapmanParser(Game *p_game)
{
m_game = p_game;
m_counterRows = 0;
}
KapmanParser::~KapmanParser()
{
}
bool KapmanParser::parse(QIODevice *input)
{
QXmlStreamReader reader(input);
while (!reader.atEnd()) {
reader.readNext();
if (reader.hasError())
return false;
switch (reader.tokenType()) {
case QXmlStreamReader::StartElement:
if (!startElement(reader.namespaceUri(), reader.name(),
reader.qualifiedName(), reader.attributes())) {
return false;
}
break;
case QXmlStreamReader::EndElement:
if (!endElement(reader.namespaceUri(), reader.name(),
reader.qualifiedName())) {
return false;
}
break;
case QXmlStreamReader::Characters:
if (!reader.isWhitespace() && !reader.text().toString().trimmed().isEmpty()) {
if (!characters(reader.text()))
return false;
}
break;
default:
break;
}
}
if (!reader.isEndDocument())
return false;
return true;
}
bool KapmanParser::characters(const QStringRef &ch)
{
m_buffer = ch.toString();
return true;
}
bool KapmanParser::startElement(const QStringRef &namespaceURI, const QStringRef &localName, const QStringRef &qName, const QXmlStreamAttributes &atts)
{
Q_UNUSED(namespaceURI)
Q_UNUSED(localName)
qreal x_position = 0.0;
qreal y_position = 0.0;
if (qName == QLatin1String("Maze")) {
int nbRows = 0;
int nbColumns = 0;
// Initialize the number of rows and columns
if (atts.hasAttribute(QLatin1String("rowCount"))) {
nbRows = atts.value(QLatin1String("rowCount")).toInt();
}
if (atts.hasAttribute(QLatin1String("colCount"))) {
nbColumns = atts.value(QLatin1String("colCount")).toInt();
}
// Create the Maze matrix
m_game->getMaze()->init(nbRows, nbColumns);
} else if (qName == QLatin1String("Bonus")) {
// Initialize the number of rows and columns
if (atts.hasAttribute(QLatin1String("rowIndex"))) {
y_position = atts.value(QLatin1String("rowIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("colIndex"))) {
x_position = atts.value(QLatin1String("colIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("x-align"))) {
if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
x_position += 0.5;
}
}
if (atts.hasAttribute(QLatin1String("y-align"))) {
if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
y_position += 0.5;
}
}
m_game->createBonus(QPointF(x_position, y_position));
} else if (qName == QLatin1String("Kapman")) {
if (atts.hasAttribute(QLatin1String("rowIndex"))) {
y_position = atts.value(QLatin1String("rowIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("colIndex"))) {
x_position = atts.value(QLatin1String("colIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("x-align"))) {
if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
x_position += 0.5;
}
}
if (atts.hasAttribute(QLatin1String("y-align"))) {
if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
y_position += 0.5;
}
}
m_game->createKapman(QPointF(x_position, y_position));
} else if (qName == QLatin1String("Ghost")) {
QString imageId;
// Initialize the number of rows and columns
if (atts.hasAttribute(QLatin1String("rowIndex"))) {
y_position = atts.value(QLatin1String("rowIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("colIndex"))) {
x_position = atts.value(QLatin1String("colIndex")).toInt();
}
if (atts.hasAttribute(QLatin1String("x-align"))) {
if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
x_position += 0.5;
}
}
if (atts.hasAttribute(QLatin1String("y-align"))) {
if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
y_position += 0.5;
}
}
if (atts.hasAttribute(QLatin1String("imageId"))) {
imageId = atts.value(QLatin1String("imageId")).toString();
}
m_game->createGhost(QPointF(x_position, y_position), imageId);
}
return true;
}
bool KapmanParser::endElement(const QStringRef &namespaceURI, const QStringRef &localName, const QStringRef &qName)
{
Q_UNUSED(namespaceURI)
Q_UNUSED(localName)
if (qName.toString() == QLatin1String("Row")) {
for (int i = 0, total = m_buffer.length(); i < total; ++i) {
switch (m_buffer.at(i).toLatin1()) {
case '|':
case '=':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::WALL);
break;
case ' ':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
break;
case '.':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
m_game->getMaze()->setCellElement(m_counterRows, i,
new Pill(m_counterRows, i, m_game->getMaze(), QStringLiteral("pill")));
break;
case 'o':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
m_game->getMaze()->setCellElement(m_counterRows, i,
new Energizer(m_counterRows, i, m_game->getMaze(), QStringLiteral("energizer")));
break;
case 'x':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::GHOSTCAMP);
break;
case 'X':
m_game->getMaze()->setCellType(m_counterRows, i, Cell::GHOSTCAMP);
m_game->getMaze()->setResurrectionCell(QPoint(m_counterRows, i));
break;
}
}
++m_counterRows;
}
return true;
}
|