forked from MeLikeyCode/PacmanPathfindingDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
128 lines (111 loc) · 3.98 KB
/
Game.cpp
File metadata and controls
128 lines (111 loc) · 3.98 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
#include "Game.h"
#include <cassert>
#include <QDebug>
#include <QTimer>
Game::Game( int numCellsWide, int numCellsLong, int cellSize, QWidget *parent):
QGraphicsView(parent),
pathingMap_(numCellsWide,numCellsLong,cellSize),
scene_(new QGraphicsScene(this)),
player_(new Player(this,cellSize)),
enemies_(),
cellSize_(cellSize)
{
//set size of window/scene
setScene(scene_);
setSceneRect(0,0,numCellsWide*cellSize,numCellsLong*cellSize);
// add the player to the scene/give him keyboard focus
player_->setPos(3*cellSize,1*cellSize);
scene_->addItem(player_);
player_->setFocus();
// create some enemies
createEnemy(1,1);
createEnemy(14,1);
createEnemy(14,10);
// draw the map
std::vector<std::vector<int>> vec {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,1,0,1,1,1,1,1,1,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,0,1,1,1,1,1,1,1,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,0,1,1,1,1,1,1,1,1,0,1,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
drawMap(vec);
// make enemies follow player
QTimer* followTimer = new QTimer(this);
connect(followTimer,SIGNAL(timeout()),this,SLOT(setEnemyPathsToPlayer()));
followTimer->start(1000);
}
/// Creates an enemy at the specified cell.
void Game::createEnemy( int x, int y){
Enemy* enemy = new Enemy(cellSize_);
enemy->setPos(cellSize_*x,cellSize_*y);
scene_->addItem(enemy);
enemies_.push_back(enemy);
}
/// Fills the specified cell.
///
/// Marks it filled in the pathingMap_ and draws a gray square to visually
/// represent it.
void Game::fill( int x, int y){
pathingMap_.fillCell(x,y);
// draw rectangle
QGraphicsRectItem* rect = new QGraphicsRectItem(0,0,cellSize_,cellSize_);
rect->setPos(x*cellSize_,y*cellSize_);
QBrush brush;
brush.setColor(Qt::gray);
brush.setStyle(Qt::SolidPattern);
rect->setBrush(brush);
scene_->addItem(rect);
}
/// Returns true if the specified cell is filled.
///
/// Delegates to the PathingMap attribute.
bool Game::filled(int x, int y){
return pathingMap_.filledCell(x,y);
}
/// Sets all the paths of the enemies to the location of the player
void Game::setEnemyPathsToPlayer(){
for (Enemy* enemy:enemies_){
std::vector<Node> nodePath = pathingMap_.shortestPath(enemy->pos().x(),
enemy->pos().y(),
player_->pos().x(),
player_->pos().y());
std::vector<QPointF> path;
for (Node node:nodePath){
path.push_back(QPointF(node.x(),node.y()));
}
enemy->setPoints(path);
}
}
/// Draws the map based on the provided 2d vector of ints.
///
/// A value of 0 in the vector represents open ground. Anything else represents
/// a block of wall.
void Game::drawMap(const std::vector<std::vector<int> > &vec){
// make sure the vec is big enough to fill all the cell
assert(vec.size() == pathingMap_.numCellsLong());
assert(vec[0].size() == pathingMap_.numCellsWide());
// draw map based on 2d vector
for (int y = 0, n = pathingMap_.numCellsLong(); y < n; y++){
for (int x = 0, p = pathingMap_.numCellsWide(); x < p; x++){
if (vec[y][x] != 0){
fill(x,y);
}
}
}
}
/// Returns the cell (represented as a Node object) at the specified point.
Node Game::pointToNode(const QPointF &point){
return Node(point.x()/cellSize_,point.y()/cellSize_);
}
/// Returns a point representing the top left corner of the specified cell.
QPointF Game::nodeToPoint(const Node &node){
return QPointF(node.x()*cellSize_,node.y()*cellSize_);
}