• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

granatier

  • sources
  • kde-4.14
  • kdegames
  • granatier
  • src
mapparser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Mathias Kraus <k.hias@gmx.de>
3  * Copyright 2007-2008 Nathalie Liesse <nathalie.liesse@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "mapparser.h"
20 #include "arena.h"
21 
22 #include <cstdlib>
23 #include <QPointF>
24 #include <QDateTime>
25 
26 MapParser::MapParser(Arena* p_arena)
27 {
28  m_arena = p_arena;
29  m_counterRows = 0;
30 }
31 
32 MapParser::~MapParser()
33 {
34 }
35 
36 bool MapParser::characters(const QString & ch)
37 {
38  m_buffer = ch;
39  return true;
40 }
41 
42 bool MapParser::startElement(const QString&, const QString&, const QString& p_qName, const QXmlAttributes& p_atts)
43 {
44  if (p_qName == "Arena")
45  {
46  int nbRows = 0;
47  int nbColumns = 0;
48  // Initialize the number of rows and columns
49  for (int i = 0; i < p_atts.count(); ++i)
50  {
51  if (p_atts.qName(i) == "rowCount")
52  {
53  nbRows = p_atts.value(i).toInt();
54  }
55  if (p_atts.qName(i) == "colCount")
56  {
57  nbColumns = p_atts.value(i).toInt();
58  }
59  //TODO:check for the right arenaFileVersion
60  //if (p_atts.qName(i) == "arenaFileVersion")
61  //{
62  // m_arenaFileVersion = p_atts.value(i).toInt();
63  //}
64  }
65  // Create the Arena matrix
66  m_arena->init(nbRows, nbColumns);
67  }
68 
69  return true;
70 }
71 
72 bool MapParser::endElement(const QString &, const QString &, const QString & p_qName)
73 {
74  if(p_qName == "Row")
75  {
76  for (int i=0; i<m_buffer.length();++i)
77  {
78  switch(m_buffer.at(i).toAscii())
79  {
80  case ' ':
81  m_arena->setCellType(m_counterRows,i,Granatier::Cell::HOLE);
82  break;
83  case '=':
84  m_arena->setCellType(m_counterRows,i,Granatier::Cell::WALL);
85  break;
86  case '_':
87  m_arena->setCellType(m_counterRows,i,Granatier::Cell::GROUND);
88  break;
89  case '+':
90  m_arena->setCellType(m_counterRows,i,Granatier::Cell::BLOCK);
91  break;
92  case 'x':
93  // create a random block
94  if((qrand()/1.0)/RAND_MAX > 0.25)
95  {
96  m_arena->setCellType(m_counterRows,i,Granatier::Cell::BLOCK);
97  }
98  else
99  {
100  m_arena->setCellType(m_counterRows,i,Granatier::Cell::GROUND);
101  }
102  break;
103  case 'o':
104  m_arena->setCellType(m_counterRows,i,Granatier::Cell::BOMBMORTAR);
105  break;
106  case '-':
107  m_arena->setCellType(m_counterRows,i,Granatier::Cell::ICE);
108  break;
109  case 'u':
110  m_arena->setCellType(m_counterRows,i,Granatier::Cell::ARROWUP);
111  break;
112  case 'r':
113  m_arena->setCellType(m_counterRows,i,Granatier::Cell::ARROWRIGHT);
114  break;
115  case 'd':
116  m_arena->setCellType(m_counterRows,i,Granatier::Cell::ARROWDOWN);
117  break;
118  case 'l':
119  m_arena->setCellType(m_counterRows,i,Granatier::Cell::ARROWLEFT);
120  break;
121  case 'p':
122  m_arena->setCellType(m_counterRows,i,Granatier::Cell::GROUND);
123  m_arena->addPlayerPosition(QPointF(i+0.5, m_counterRows+0.5));
124  break;
125  default:
126  m_arena->setCellType(m_counterRows,i,Granatier::Cell::GROUND);
127  }
128  }
129  m_counterRows ++;
130  }
131  return true;
132 }
Granatier::Cell::GROUND
Definition: granatierglobals.h:74
Granatier::Cell::ICE
Definition: granatierglobals.h:77
QChar::toAscii
char toAscii() const
Granatier::Cell::ARROWDOWN
Definition: granatierglobals.h:80
MapParser::characters
bool characters(const QString &ch)
Implement QXmlDefaultHandler::characters.
Definition: mapparser.cpp:36
QPointF
QXmlAttributes::count
int count() const
Granatier::Cell::ARROWRIGHT
Definition: granatierglobals.h:82
Arena::init
void init(const int p_nbRows, const int p_nbColumns)
Creates the Arena matrix.
Definition: arena.cpp:40
MapParser::~MapParser
~MapParser()
Deletes the GameParser instance.
Definition: mapparser.cpp:32
QString::toInt
int toInt(bool *ok, int base) const
mapparser.h
Arena
This class represents the Arena of the game.
Definition: arena.h:36
Granatier::Cell::WALL
Definition: granatierglobals.h:75
QString
QXmlAttributes::qName
QString qName(int index) const
Arena::addPlayerPosition
void addPlayerPosition(const QPointF &p_position)
Sets a player position on the arena.
Definition: arena.cpp:90
QXmlAttributes
MapParser::MapParser
MapParser(Arena *p_arena)
Creates a new GameParser.
Definition: mapparser.cpp:26
QString::at
const QChar at(int position) const
Granatier::Cell::ARROWUP
Definition: granatierglobals.h:79
QString::length
int length() const
arena.h
MapParser::startElement
bool startElement(const QString &, const QString &, const QString &p_qName, const QXmlAttributes &p_atts)
Implements QXmlDefaultHandler::startElement()
Definition: mapparser.cpp:42
MapParser::endElement
bool endElement(const QString &, const QString &, const QString &p_qName)
Implements QXmlDefaultHandler::endElement()
Definition: mapparser.cpp:72
QXmlAttributes::value
QString value(int index) const
Granatier::Cell::BOMBMORTAR
Definition: granatierglobals.h:78
Granatier::Cell::ARROWLEFT
Definition: granatierglobals.h:81
Granatier::Cell::HOLE
Definition: granatierglobals.h:73
Arena::setCellType
void setCellType(const int p_row, const int p_column, const Granatier::Cell::Type p_type)
Sets the CellType of the Cell whose coordinates are given in parameters.
Definition: arena.cpp:63
Granatier::Cell::BLOCK
Definition: granatierglobals.h:76
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

granatier

Skip menu "granatier"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal