1 | /* |
2 | * Copyright 2007-2008 Nathalie Liesse <nathalie.liesse@gmail.com> |
3 | * |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License as |
6 | * published by the Free Software Foundation; either version 2 of |
7 | * the License, or (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #include "kapmanparser.h" |
19 | #include "element.h" |
20 | #include "pill.h" |
21 | #include "energizer.h" |
22 | |
23 | KapmanParser::KapmanParser(Game* p_game) { |
24 | m_game = p_game; |
25 | m_counterRows = 0; |
26 | } |
27 | |
28 | KapmanParser::~KapmanParser() { |
29 | |
30 | } |
31 | |
32 | bool KapmanParser::characters(const QString & ch ){ |
33 | m_buffer = ch; |
34 | return true; |
35 | } |
36 | |
37 | bool KapmanParser::startElement(const QString&, const QString&, const QString& p_qName, const QXmlAttributes& p_atts) { |
38 | qreal x_position = 0.0; |
39 | qreal y_position = 0.0; |
40 | |
41 | if (p_qName == "Maze" ) { |
42 | int nbRows = 0; |
43 | int nbColumns = 0; |
44 | // Initialize the number of rows and columns |
45 | for (int i = 0; i < p_atts.count(); ++i) { |
46 | if (p_atts.qName(i) == "rowCount" ) { |
47 | nbRows = p_atts.value(i).toInt(); |
48 | } |
49 | if (p_atts.qName(i) == "colCount" ) { |
50 | nbColumns = p_atts.value(i).toInt(); |
51 | } |
52 | } |
53 | // Create the Maze matrix |
54 | m_game->getMaze()->init(nbRows, nbColumns); |
55 | } |
56 | else if (p_qName == "Bonus" ) { |
57 | // Initialize the number of rows and columns |
58 | for (int i = 0; i < p_atts.count(); ++i) { |
59 | if (p_atts.qName(i) == "rowIndex" ) { |
60 | y_position = p_atts.value(i).toInt(); |
61 | } |
62 | if (p_atts.qName(i) == "colIndex" ) { |
63 | x_position = p_atts.value(i).toInt(); |
64 | } |
65 | if (p_atts.qName(i) == "x-align" ) { |
66 | if(p_atts.value(i) == "center" ){ |
67 | x_position += 0.5; |
68 | } |
69 | } |
70 | if (p_atts.qName(i) == "y-align" ) { |
71 | if(p_atts.value(i) == "center" ){ |
72 | y_position += 0.5; |
73 | } |
74 | } |
75 | } |
76 | m_game->createBonus(QPointF(x_position, y_position)); |
77 | } |
78 | else if (p_qName == "Kapman" ) { |
79 | // Initialize the number of rows and columns |
80 | for (int i = 0; i < p_atts.count(); ++i) { |
81 | if (p_atts.qName(i) == "rowIndex" ) { |
82 | y_position = p_atts.value(i).toInt(); |
83 | } |
84 | if (p_atts.qName(i) == "colIndex" ) { |
85 | x_position = p_atts.value(i).toInt(); |
86 | } |
87 | if (p_atts.qName(i) == "x-align" ) { |
88 | if(p_atts.value(i) == "center" ){ |
89 | x_position += 0.5; |
90 | } |
91 | } |
92 | if (p_atts.qName(i) == "y-align" ) { |
93 | if(p_atts.value(i) == "center" ){ |
94 | y_position += 0.5; |
95 | } |
96 | } |
97 | } |
98 | m_game->createKapman(QPointF(x_position, y_position)); |
99 | } |
100 | else if (p_qName == "Ghost" ) { |
101 | QString imageId = "" ; |
102 | // Initialize the number of rows and columns |
103 | for (int i = 0; i < p_atts.count(); ++i) { |
104 | if (p_atts.qName(i) == "rowIndex" ) { |
105 | y_position = p_atts.value(i).toInt(); |
106 | } |
107 | if (p_atts.qName(i) == "colIndex" ) { |
108 | x_position = p_atts.value(i).toInt(); |
109 | } |
110 | if (p_atts.qName(i) == "x-align" ) { |
111 | if(p_atts.value(i) == "center" ){ |
112 | x_position += 0.5; |
113 | } |
114 | } |
115 | if (p_atts.qName(i) == "y-align" ) { |
116 | if(p_atts.value(i) == "center" ){ |
117 | y_position += 0.5; |
118 | } |
119 | } |
120 | if (p_atts.qName(i) == "imageId" ) { |
121 | imageId = p_atts.value(i); |
122 | } |
123 | } |
124 | m_game->createGhost(QPointF(x_position, y_position),imageId); |
125 | } |
126 | |
127 | |
128 | return true; |
129 | } |
130 | |
131 | bool KapmanParser::endElement(const QString &, const QString &, const QString & p_qName ){ |
132 | if(p_qName == "Row" ) |
133 | { |
134 | for (int i=0; i<m_buffer.length();++i) |
135 | { |
136 | switch(m_buffer.at(i).toAscii()){ |
137 | case '|': |
138 | case '=': m_game->getMaze()->setCellType(m_counterRows,i,Cell::WALL); |
139 | break; |
140 | case ' ': m_game->getMaze()->setCellType(m_counterRows,i,Cell::CORRIDOR); |
141 | break; |
142 | case '.': m_game->getMaze()->setCellType(m_counterRows,i,Cell::CORRIDOR); |
143 | m_game->getMaze()->setCellElement(m_counterRows, i, |
144 | new Pill(m_counterRows, i, m_game->getMaze(), "pill" )); |
145 | break; |
146 | case 'o':m_game->getMaze()->setCellType(m_counterRows,i,Cell::CORRIDOR); |
147 | m_game->getMaze()->setCellElement(m_counterRows, i, |
148 | new Energizer(m_counterRows, i, m_game->getMaze(), "energizer" )); |
149 | break; |
150 | case 'x':m_game->getMaze()->setCellType(m_counterRows,i,Cell::GHOSTCAMP); |
151 | break; |
152 | case 'X':m_game->getMaze()->setCellType(m_counterRows,i,Cell::GHOSTCAMP); |
153 | m_game->getMaze()->setResurrectionCell(QPoint(m_counterRows, i)); |
154 | break; |
155 | } |
156 | } |
157 | m_counterRows ++; |
158 | } |
159 | return true; |
160 | } |
161 | |
162 | |
163 | |
164 | |