File indexing completed on 2023-10-01 08:02:06

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Nathalie Liesse <nathalie.liesse@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kapmanparser.h"
0008 #include "element.h"
0009 #include "energizer.h"
0010 #include "pill.h"
0011 
0012 KapmanParser::KapmanParser(Game *p_game)
0013 {
0014     m_game = p_game;
0015     m_counterRows = 0;
0016 }
0017 
0018 KapmanParser::~KapmanParser() = default;
0019 
0020 bool KapmanParser::parse(QIODevice *input)
0021 {
0022     QXmlStreamReader reader(input);
0023 
0024     while (!reader.atEnd()) {
0025         reader.readNext();
0026         if (reader.hasError())
0027             return false;
0028 
0029         switch (reader.tokenType()) {
0030         case QXmlStreamReader::StartElement:
0031             if (!startElement(reader.namespaceUri(), reader.name(), reader.qualifiedName(), reader.attributes())) {
0032                 return false;
0033             }
0034             break;
0035         case QXmlStreamReader::EndElement:
0036             if (!endElement(reader.namespaceUri(), reader.name(), reader.qualifiedName())) {
0037                 return false;
0038             }
0039             break;
0040         case QXmlStreamReader::Characters:
0041             if (!reader.isWhitespace() && !reader.text().toString().trimmed().isEmpty()) {
0042                 if (!characters(reader.text()))
0043                     return false;
0044             }
0045             break;
0046         default:
0047             break;
0048         }
0049     }
0050 
0051     if (!reader.isEndDocument())
0052         return false;
0053 
0054     return true;
0055 }
0056 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0057 bool KapmanParser::characters(const QStringRef &ch)
0058 #else
0059 bool KapmanParser::characters(const QStringView &ch)
0060 #endif
0061 {
0062     m_buffer = ch.toString();
0063     return true;
0064 }
0065 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0066 bool KapmanParser::startElement(const QStringRef &namespaceURI, const QStringRef &localName, const QStringRef &qName, const QXmlStreamAttributes &atts)
0067 #else
0068 bool KapmanParser::startElement(const QStringView &namespaceURI, const QStringView &localName, const QStringView &qName, const QXmlStreamAttributes &atts)
0069 #endif
0070 {
0071     Q_UNUSED(namespaceURI)
0072     Q_UNUSED(localName)
0073     qreal x_position = 0.0;
0074     qreal y_position = 0.0;
0075 
0076     if (qName == QLatin1String("Maze")) {
0077         int nbRows = 0;
0078         int nbColumns = 0;
0079         // Initialize the number of rows and columns
0080         if (atts.hasAttribute(QLatin1String("rowCount"))) {
0081             nbRows = atts.value(QLatin1String("rowCount")).toInt();
0082         }
0083         if (atts.hasAttribute(QLatin1String("colCount"))) {
0084             nbColumns = atts.value(QLatin1String("colCount")).toInt();
0085         }
0086         // Create the Maze matrix
0087         m_game->getMaze()->init(nbRows, nbColumns);
0088     } else if (qName == QLatin1String("Bonus")) {
0089         // Initialize the number of rows and columns
0090         if (atts.hasAttribute(QLatin1String("rowIndex"))) {
0091             y_position = atts.value(QLatin1String("rowIndex")).toInt();
0092         }
0093         if (atts.hasAttribute(QLatin1String("colIndex"))) {
0094             x_position = atts.value(QLatin1String("colIndex")).toInt();
0095         }
0096         if (atts.hasAttribute(QLatin1String("x-align"))) {
0097             if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
0098                 x_position += 0.5;
0099             }
0100         }
0101         if (atts.hasAttribute(QLatin1String("y-align"))) {
0102             if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
0103                 y_position += 0.5;
0104             }
0105         }
0106         m_game->createBonus(QPointF(x_position, y_position));
0107     } else if (qName == QLatin1String("Kapman")) {
0108         if (atts.hasAttribute(QLatin1String("rowIndex"))) {
0109             y_position = atts.value(QLatin1String("rowIndex")).toInt();
0110         }
0111         if (atts.hasAttribute(QLatin1String("colIndex"))) {
0112             x_position = atts.value(QLatin1String("colIndex")).toInt();
0113         }
0114         if (atts.hasAttribute(QLatin1String("x-align"))) {
0115             if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
0116                 x_position += 0.5;
0117             }
0118         }
0119         if (atts.hasAttribute(QLatin1String("y-align"))) {
0120             if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
0121                 y_position += 0.5;
0122             }
0123         }
0124 
0125         m_game->createKapman(QPointF(x_position, y_position));
0126     } else if (qName == QLatin1String("Ghost")) {
0127         QString imageId;
0128         // Initialize the number of rows and columns
0129         if (atts.hasAttribute(QLatin1String("rowIndex"))) {
0130             y_position = atts.value(QLatin1String("rowIndex")).toInt();
0131         }
0132         if (atts.hasAttribute(QLatin1String("colIndex"))) {
0133             x_position = atts.value(QLatin1String("colIndex")).toInt();
0134         }
0135         if (atts.hasAttribute(QLatin1String("x-align"))) {
0136             if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
0137                 x_position += 0.5;
0138             }
0139         }
0140         if (atts.hasAttribute(QLatin1String("y-align"))) {
0141             if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
0142                 y_position += 0.5;
0143             }
0144         }
0145         if (atts.hasAttribute(QLatin1String("imageId"))) {
0146             imageId = atts.value(QLatin1String("imageId")).toString();
0147         }
0148         m_game->createGhost(QPointF(x_position, y_position), imageId);
0149     }
0150 
0151     return true;
0152 }
0153 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0154 bool KapmanParser::endElement(const QStringRef &namespaceURI, const QStringRef &localName, const QStringRef &qName)
0155 #else
0156 bool KapmanParser::endElement(const QStringView &namespaceURI, const QStringView &localName, const QStringView &qName)
0157 #endif
0158 {
0159     Q_UNUSED(namespaceURI)
0160     Q_UNUSED(localName)
0161 
0162     if (qName.toString() == QLatin1String("Row")) {
0163         for (int i = 0, total = m_buffer.length(); i < total; ++i) {
0164             switch (m_buffer.at(i).toLatin1()) {
0165             case '|':
0166             case '=':
0167                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::WALL);
0168                 break;
0169             case ' ':
0170                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
0171                 break;
0172             case '.':
0173                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
0174                 m_game->getMaze()->setCellElement(m_counterRows, i, new Pill(m_counterRows, i, m_game->getMaze(), QStringLiteral("pill")));
0175                 break;
0176             case 'o':
0177                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
0178                 m_game->getMaze()->setCellElement(m_counterRows, i, new Energizer(m_counterRows, i, m_game->getMaze(), QStringLiteral("energizer")));
0179                 break;
0180             case 'x':
0181                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::GHOSTCAMP);
0182                 break;
0183             case 'X':
0184                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::GHOSTCAMP);
0185                 m_game->getMaze()->setResurrectionCell(QPoint(m_counterRows, i));
0186                 break;
0187             }
0188         }
0189         ++m_counterRows;
0190     }
0191     return true;
0192 }