blob: 581d2f7b06c91b54fde520a432ac02858cba0ece (
plain) (
blame)
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
|
/**
* Copyright (C) David Wolfe, 1999. All rights reserved.
* Ported to Qt and adapted for TDDD86, 2015.
*/
#include "GameState.h"
#include "utilities.h"
#include "constants.h"
GameState::GameState(){}
GameState::GameState(int numberOfRobots) {
for (int i = 0; i < numberOfRobots; i++) {
Robot robot;
do {robot = Robot();}
while (!isEmpty (robot));
robots.push_back(robot);
}
teleportHero();
}
void GameState::draw(QGraphicsScene *scene) const {
scene->clear();
for (size_t i = 0; i < robots.size(); ++i)
robots[i].draw(scene);
for (size_t i = 0; i < junks.size(); ++i)
junks[i].draw(scene);
hero.draw(scene);
}
void GameState::teleportHero() {
do hero.teleport();
while (!isEmpty(hero));
}
void GameState::moveRobots() {
for (unsigned int i = 0; i < robots.size(); i++)
robots[i].moveTowards (hero);
}
int GameState::countCollisions() {
int numberDestroyed = 0;
unsigned int i = 0;
while (i < robots.size()) {
bool hitJunk = junkAt (robots[i]);
bool collision = (countRobotsAt (robots[i]) > 1);
if (hitJunk || collision) {
if (!hitJunk) junks.push_back (Junk(robots[i]));
robots[i] = robots[robots.size()-1];
robots.pop_back();
numberDestroyed++;
} else i++;
}
return numberDestroyed;
}
bool GameState::anyRobotsLeft() const {
return (robots.size() != 0);
}
bool GameState::heroDead() const {
return !isEmpty(hero);
}
bool GameState::isSafe(const Unit& unit) const {
for (unsigned int i = 0; i < robots.size(); i++)
if (robots[i].attacks(unit)) return false;
if (junkAt(unit)) return false;
return true;
}
void GameState::moveHeroTowards(const Unit& dir) {
hero.moveTowards(dir);
}
Hero GameState::getHero() const {return hero;}
/*
* Free of robots and junk only
*/
bool GameState::isEmpty(const Unit& unit) const {
return (countRobotsAt(unit) == 0 && !junkAt(unit));
}
/*
* Is there junk at unit?
*/
bool GameState::junkAt(const Unit& unit) const {
for (size_t i = 0; i < junks.size(); ++i)
if (junks[i].at(unit)) return true;
return false;
}
/*
* How many robots are there at unit?
*/
int GameState::countRobotsAt(const Unit& unit) const {
int count = 0;
for (size_t i = 0; i < robots.size(); ++i) {
if (robots[i].at(unit))
count++;
}
return count;
}
|