File indexing completed on 2024-04-21 04:01:58

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 
0057 bool KapmanParser::characters(const QStringView &ch)
0058 {
0059     m_buffer = ch.toString();
0060     return true;
0061 }
0062 
0063 bool KapmanParser::startElement(const QStringView &namespaceURI, const QStringView &localName, const QStringView &qName, const QXmlStreamAttributes &atts)
0064 {
0065     Q_UNUSED(namespaceURI)
0066     Q_UNUSED(localName)
0067     qreal x_position = 0.0;
0068     qreal y_position = 0.0;
0069 
0070     if (qName == QLatin1String("Maze")) {
0071         int nbRows = 0;
0072         int nbColumns = 0;
0073         // Initialize the number of rows and columns
0074         if (atts.hasAttribute(QLatin1String("rowCount"))) {
0075             nbRows = atts.value(QLatin1String("rowCount")).toInt();
0076         }
0077         if (atts.hasAttribute(QLatin1String("colCount"))) {
0078             nbColumns = atts.value(QLatin1String("colCount")).toInt();
0079         }
0080         // Create the Maze matrix
0081         m_game->getMaze()->init(nbRows, nbColumns);
0082     } else if (qName == QLatin1String("Bonus")) {
0083         // Initialize the number of rows and columns
0084         if (atts.hasAttribute(QLatin1String("rowIndex"))) {
0085             y_position = atts.value(QLatin1String("rowIndex")).toInt();
0086         }
0087         if (atts.hasAttribute(QLatin1String("colIndex"))) {
0088             x_position = atts.value(QLatin1String("colIndex")).toInt();
0089         }
0090         if (atts.hasAttribute(QLatin1String("x-align"))) {
0091             if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
0092                 x_position += 0.5;
0093             }
0094         }
0095         if (atts.hasAttribute(QLatin1String("y-align"))) {
0096             if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
0097                 y_position += 0.5;
0098             }
0099         }
0100         m_game->createBonus(QPointF(x_position, y_position));
0101     } else if (qName == QLatin1String("Kapman")) {
0102         if (atts.hasAttribute(QLatin1String("rowIndex"))) {
0103             y_position = atts.value(QLatin1String("rowIndex")).toInt();
0104         }
0105         if (atts.hasAttribute(QLatin1String("colIndex"))) {
0106             x_position = atts.value(QLatin1String("colIndex")).toInt();
0107         }
0108         if (atts.hasAttribute(QLatin1String("x-align"))) {
0109             if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
0110                 x_position += 0.5;
0111             }
0112         }
0113         if (atts.hasAttribute(QLatin1String("y-align"))) {
0114             if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
0115                 y_position += 0.5;
0116             }
0117         }
0118 
0119         m_game->createKapman(QPointF(x_position, y_position));
0120     } else if (qName == QLatin1String("Ghost")) {
0121         QString imageId;
0122         // Initialize the number of rows and columns
0123         if (atts.hasAttribute(QLatin1String("rowIndex"))) {
0124             y_position = atts.value(QLatin1String("rowIndex")).toInt();
0125         }
0126         if (atts.hasAttribute(QLatin1String("colIndex"))) {
0127             x_position = atts.value(QLatin1String("colIndex")).toInt();
0128         }
0129         if (atts.hasAttribute(QLatin1String("x-align"))) {
0130             if (atts.value(QLatin1String("x-align")).toString() == QLatin1String("center")) {
0131                 x_position += 0.5;
0132             }
0133         }
0134         if (atts.hasAttribute(QLatin1String("y-align"))) {
0135             if (atts.value(QLatin1String("y-align")).toString() == QLatin1String("center")) {
0136                 y_position += 0.5;
0137             }
0138         }
0139         if (atts.hasAttribute(QLatin1String("imageId"))) {
0140             imageId = atts.value(QLatin1String("imageId")).toString();
0141         }
0142         m_game->createGhost(QPointF(x_position, y_position), imageId);
0143     }
0144 
0145     return true;
0146 }
0147 
0148 bool KapmanParser::endElement(const QStringView &namespaceURI, const QStringView &localName, const QStringView &qName)
0149 {
0150     Q_UNUSED(namespaceURI)
0151     Q_UNUSED(localName)
0152 
0153     if (qName.toString() == QLatin1String("Row")) {
0154         for (int i = 0, total = m_buffer.length(); i < total; ++i) {
0155             switch (m_buffer.at(i).toLatin1()) {
0156             case '|':
0157             case '=':
0158                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::WALL);
0159                 break;
0160             case ' ':
0161                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
0162                 break;
0163             case '.':
0164                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
0165                 m_game->getMaze()->setCellElement(m_counterRows, i, new Pill(m_counterRows, i, m_game->getMaze(), QStringLiteral("pill")));
0166                 break;
0167             case 'o':
0168                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::CORRIDOR);
0169                 m_game->getMaze()->setCellElement(m_counterRows, i, new Energizer(m_counterRows, i, m_game->getMaze(), QStringLiteral("energizer")));
0170                 break;
0171             case 'x':
0172                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::GHOSTCAMP);
0173                 break;
0174             case 'X':
0175                 m_game->getMaze()->setCellType(m_counterRows, i, Cell::GHOSTCAMP);
0176                 m_game->getMaze()->setResurrectionCell(QPoint(m_counterRows, i));
0177                 break;
0178             }
0179         }
0180         ++m_counterRows;
0181     }
0182     return true;
0183 }