-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameController.cpp
More file actions
73 lines (63 loc) · 1.94 KB
/
Copy pathGameController.cpp
File metadata and controls
73 lines (63 loc) · 1.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
#include "GameController.h"
#include "Map.h"
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
// Inisialisasi vektor Ghost statik
std::vector<std::shared_ptr<Ghost>> GameController::ghosts;
GameController::GameController() : pac(10, 1) {
srand(time(0));
ghosts.push_back(GhostFactory::createGhost("Blinky", 5, 5));
}
// Fungsi untuk merender peta, Pacman, dan Ghost
void render(const Pacman& pacman, const std::vector<std::shared_ptr<Ghost>>& ghosts) {
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
bool printed = false;
if (i == pacman.getX() && j == pacman.getY()) {
cout << pacman.getSymbol() << " ";
printed = true;
} else {
for (const auto& g : ghosts) {
if (g->getX() == i && g->getY() == j) {
cout << g->getSymbol() << " ";
printed = true;
break;
}
}
}
if (!printed) cout << Map::map[i][j] << " ";
}
cout << endl;
}
for (const auto& g : ghosts) {
cout << "Ghost at (" << g->getX() << ", " << g->getY() << ") is in state: " << g->getStateName() << endl;
}
}
// Loop utama permainan
void GameController::run() {
char input;
while (true) {
system("cls");
render(pac, ghosts);
if (_kbhit()) {
input = _getch();
if (input == 'q') break;
pac.setDirection(input);
}
pac.move(*this);
for (auto& ghost : ghosts) {
ghost->move(pac.getX(), pac.getY());
}
Sleep(400);
}
}
// Trigger state frightened untuk semua ghost
void GameController::triggerFrightenedState() {
for (auto& ghost : ghosts) {
ghost->changeState(std::make_shared<FrightenedState>());
}
}