-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
103 lines (79 loc) · 2.14 KB
/
Game.cpp
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
#include "Game.hpp"
Game::Game() {
window.create(sf::VideoMode(width, height), "SHOOTER");
window.setFramerateLimit(60);
if (loadTextures()) {
loop();
}
}
bool Game::loadTextures() {
return map.load("art/tileset.png", sf::Vector2u(64, 64), map_pattern, 20, 12) &&
player.load("art/player.png") && enemy.load("art/enemy.png") &&
projectile.load("art/projectile.png");
}
void Game::loop() {
clock.restart();
enemies.push_back(enemy);
while (window.isOpen()) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
else if (event.type == event.MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left) {
projectile.setDirection(window, player.get_pos());
isShot = true;
}
}
player.move();
player.setRotation(player.getAngle(window));
player.checkWallCollision(width, height);
render();
}
}
void Game::render() {
window.clear();
window.draw(map);
window.draw(player);
if (isShot) {
Projectile bullet = projectile;
projectiles.push_back(bullet);
isShot = false;
}
for (unsigned int i = 0; i < projectiles.size(); i++) {
if (projectiles.at(i).isShot()) {
projectiles.at(i).shoot();
}
window.draw(projectiles.at(i));
i_counter = i;
}
for (unsigned int j = 0; j < enemies.size(); j++) {
if (enemies.at(j).isAlive()) {
enemies.at(j).updatePos(player.get_pos());
window.draw(enemies.at(j));
if (player.isColliding(enemies.at(j).get_pos())) {
window.close();
}
if (enemies.at(j).isColliding(projectiles.at(i_counter).get_pos())) {
enemies.at(j).setAlive(false);
isAlive = false;
}
elapsed = clock.getElapsedTime();
if (!isAlive) {
enemies.at(j) = enemy;
enemies.at(j).setAlive(true);
enemies.at(j).setRandomPos(window);
isAlive = true;
}
if (static_cast<int>(elapsed.asSeconds()) % 4 == 0 && static_cast<int>(elapsed.asSeconds()) != 0) {
if (isAlive) {
clock.restart();
}
Enemy new_enemy = enemy;
new_enemy.setAlive(true);
new_enemy.setRandomPos(window);
enemies.push_back(new_enemy);
}
}
}
window.display();
}