-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.cpp
More file actions
141 lines (134 loc) · 3.94 KB
/
Maze.cpp
File metadata and controls
141 lines (134 loc) · 3.94 KB
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
#include "Maze.h"
#include <QPainter>
#include <QTextStream>
#include <QFile>
#include <QMessageBox>
#include "Player.h"
#include "PowerPill.h"
#include "Game.h"
#include "Enemy.h"
QVector<QVector<int> > Maze::field;
Maze::Maze() : QObject(), timer(this)
{
this->size.height = 260;
this->size.width = 228;
startField.resize(size.height);
// read Map from file;
readFieldData();
// initialize current field and pills list
updateData();
// change pills list entry
initPills ();
// set connect on PowerPill effect
connect(&timer,&QTimer::timeout,this, &Maze::endAngryPacmanMode);
}
void Maze::readFieldData() {
for (int i = 0; i < size.height; ++i)
startField[i].resize (size.width);
QFile file(":/res/resources/maze.txt");
if (!file.open (QIODevice::ReadOnly)) {
QMessageBox msg;
msg.setText ("Error! Can't read the maze!");
msg.show ();
return;
}
QString value = "";
QTextStream out(&file);
value = out.read (1);
for (int i = 0; ! out.atEnd ();++i) {
value = out.read(1);
for (int j = 0; value != "\n" && ! out.atEnd ();++j) {
startField[i][j] = value.toInt ();
value = out.read (1);
}
}
}
void Maze::draw (QPainter *painter) {
QPixmap img(":/res/resources/maze2.png");
painter->drawPixmap (0, 0, size.width * 2, size.height * 2, img);
painter->setPen (QPen(Qt::darkYellow,3,Qt::SolidLine, Qt::FlatCap));
painter->setBrush (Qt::darkYellow);
// draw pills
for (auto it = pills.begin (); it != pills.end ();++it)
it->draw (painter);
}
// init pills
void Maze::initPills() {
for (int row = 0; row < size.height; ++row) {
for (int column = 0; column < size.width; ++column) {
if (field[row][column] == 3)
startPills.emplace_back(PowerPill(row, column));
else if (field[row][column] == 2)
startPills.emplace_back(Pill(row, column));
}
}
pills = startPills;
}
void Maze::removePill(const Position &position) {
for (auto it = pills.begin(); it != pills.end();++it) {
if (it->getPostion () == position) {
pills.erase(it);
break;
}
}
}
int Maze::getPillsCount () {
return pills.size ();
}
void Maze::endAngryPacmanMode () {
Game::setDelay (30);
emit angryPacmanModeOFF ();
}
void Maze::updateData() {
field = startField;
pills = startPills;
}
void Maze::update() {
const Position * playerPosition = Player::playerPosition ();
if (field[playerPosition->x][playerPosition->y] == 2) {
field[playerPosition->x][playerPosition->y] = 1;
Game::addMoney(10);
removePill(*playerPosition);
}
else if (field[playerPosition->x][playerPosition->y] == 3) {
Game::setDelay (20);
timer.start (5000);
emit angryPacmanModeON();
field[playerPosition->x][playerPosition->y] = 1;
Game::addMoney(20);
removePill(*playerPosition);
}
}
void Maze::initPlayerStartPosition (Position * pos) {
int height = field.size ();
int width = field[0].size ();
for (int row = 0; row < height;++row) {
for (int column = 0; column < width;++column) {
if (field[row][column] == 5) {
pos->x = row;
pos->y =column;
return;
}
}
}
}
void Maze::initEnemyStartPosition (Position *pos) {
int height = field.size ();
int width = field[0].size ();
for (int row = 0; row < height;++row) {
for (int column = 0; column < width; ++column) {
if (field[row][column] == 4) {
pos->x = row;
pos->y = column;
return;
}
}
}
}
const QVector<QVector<int> > & Maze::getField() {
return field;
}
Maze::~Maze() {
pills.clear ();
startPills.clear();
}