ARRka  1.05
game.cpp
Idź do dokumentacji tego pliku.
1 
8 #include "inc/game.h"
9 #include <QDebug>
10 #include <ctime>
11 #include <QMessageBox>
12 #include <QAbstractButton>
13 #include <QPushButton>
14 #include <QPixmap>
15 #include <cmath>
16 
17 #define accYzero 0
18 #define accZzero 981
19 #define RAD_TO_DEG 57.2957795
20 #define PI 3.1415926
21 
22 Game::Game(QWidget *parent)
23 {
24  srand( time( NULL ) );
25 
26  gameSettings = new GameSettings(800,600,70,70,7,11);
27  _gameStarted = false;
28 
29  //zerowanie danych gry
30  _ECTS=0;
31  _deansLeave=0;
32  _deficit=0;
33  _term=1;
34 
35  player = new QMediaPlayer(this,QMediaPlayer::LowLatency);
36  player->setVolume(40);
37 
38  //dodanie sceny do widoku o określonych rozmiarach
39  _scene = new QGraphicsScene(this);
40 
41  setupScene();
42 
43  //dodanie studenta do sceny
44  _student = new Student(gameSettings,this);
45  _scene->addItem(_student); //dodanie studenta na scenę
46 
48 
49  //zainicjalizwanie kursów
50  _courses = new Courses(gameSettings,this);
51  _scene->addItem(_courses); //dodanie kursów na scenę
52 
53  //zainicjalizowanie timerów gry
54  _timerCourses = new QTimer();
55  _timerMove = new QTimer();
56 
58 
60 }
61 
63 {
64  _ECTS+=ECTS;
65  emit changeECTS(_ECTS);
66 }
67 
69 {
70  _term=term;
71  emit changeTerm(_term);
72 }
73 
75 {
76  _deficit+=ECTS;
77  emit changeDeficit(_deficit);
78 }
79 
81 {
82  _deficit-=ECTS;
83  emit changeDeficit(_deficit);
84 }
85 
87 {
88  QMessageBox::warning(this,tr("Błąd odczytu danych"),tr("Błąd wczytywania danych semestrów. Sprawdź pliki i uruchom grę ponownie."));
89  emit closeGame();
90 }
91 
93 {
94  _gameStarted = true;
95  startTimers();
96  emit startStudent();
97 }
98 
100 {
101  _gameStarted = false;
102  stopTimers();
103  emit stopStudent();
104 }
105 
107 {
108  createNewGame();
109 }
110 
112 {
113  if (_term < 6) {
116  }
117  else {
118  if (_deficit > 0)
120  }
121 }
122 
123 
125 {
126  _gameStarted = false;
127  stopTimers();
128  emit stopStudent();
129 
130  playEndMusic();
131  QMessageBox msgBox;
132  msgBox.setIconPixmap(QPixmap(":/icons/biret.png"));
133  msgBox.setWindowTitle(tr("Koniec"));
134  msgBox.setText(tr("Gratulacje! Udało Ci się przetrwać i zostać prawdziwym robotykiem ;-)"));
135 
136  QPushButton *close = msgBox.addButton(tr("Zamknij grę"), QMessageBox::ActionRole);
137  QPushButton *newGame = msgBox.addButton(tr("Nowa gra"), QMessageBox::ActionRole);
138 
139  msgBox.exec();
140 
141  if (msgBox.clickedButton() == newGame) {
142  createNewGame(); //wygeneruj nową grę
143  }
144  else if (msgBox.clickedButton() == close) {
145  emit closeGame(); //zamknij grę
146  }
147 }
148 
150 {
151  //qDebug() << data.X_XL << data.Y_XL << data.Z_XL << "," << data.X_G << data.Y_G << data.Z_G << "," << data.time;
152  moveData move;
153  move.Y = -data.X_XL / 100;
154  move.X = data.Y_XL / 80;
155  _student->moveStudent(move);
156 }
157 
159 {
160  _timerCourses->stop();
161  _timerMove->stop();
162 }
163 
165 {
166  _timerCourses->start(8000/gameSettings->speed);
167  _timerMove->start(170/gameSettings->speed);
168 }
169 
171 {
172  //powiązanie timerów gry
173  connect(_timerCourses,SIGNAL(timeout()),_courses,SLOT(spawnCourse()));
174  connect(_timerMove,SIGNAL(timeout()),_courses,SLOT(moveCourses()));
175 
176  //dostęp do zawartości gry
177  connect(_courses,SIGNAL(checkDeficitOverstep()),this,SLOT(on_Courses_checkDeficitOverstep()));
178  connect(_courses,SIGNAL(gameFinished()),this,SLOT(on_Courses_gameFinished()));
179 
180  //obsługa wyświetlaczy gry
181  connect(_courses,SIGNAL(increaseDeficit(int)),this,SLOT(on_Courses_increaseDeficit(int)));
182  connect(_courses,SIGNAL(decreaseDeficit(int)),this,SLOT(on_Courses_decreaseDeficit(int)));
183  connect(_courses,SIGNAL(increaseECTS(int)),this,SLOT(on_Courses_increaseECTS(int)));
184  connect(_courses,SIGNAL(changeTerm(int)),this,SLOT(on_Courses_changeTerm(int)));
185  connect(_courses,SIGNAL(failedToReadData()),this,SLOT(on_Courses_failedToReadData()));
186 }
187 
189 {
190  //obsługa możliwości sterowania studentem
191  connect(this,SIGNAL(stopStudent()),_student,SLOT(on_Game_stopStudent()));
192  connect(this,SIGNAL(startStudent()),_student,SLOT(on_Game_startStudent()));
193 }
194 
196 {
198  setBackgroundBrush(QBrush(QColor("white")));
199  setScene(_scene);
200  //wyłaczenienie pasków przewiajania
201  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
202  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
204 }
205 
207 {
208  //jeśli jest załadowana aktualna gra
209  if (!_courses->getAllTerms().empty()) {
210  scene()->removeItem(_courses);
211  //delete _courses;
212  }
213  delete _courses;
214  _courses = new Courses (gameSettings,this);
215  _ECTS=0;
216  _deansLeave=0;
217  _deficit=0;
218  _term=1;
219  emit changeDeficit(_deficit);
220  emit changeECTS(_ECTS);
221  emit changeTerm(_term);
223  connectCourses();
224  scene()->addItem(_courses);
225  emit changeButtonToStart();
228 }
229 
230 
232 {
233  _gameStarted = false;
234  stopTimers();
235  emit stopStudent();
236 
237  if (_deansLeave < 2) { //jeśli nie wzięto jeszcze dwóch dziekanek
238  if (_term > 1) { //jeśli jest się powyżej 1 semestru
239  player->setMedia(QUrl("qrc:/sounds/chose.wav"));
240  player->play();
241 
242  QMessageBox msgBox;
243  msgBox.setIcon(QMessageBox::Question);
244  msgBox.setWindowTitle(tr("Przekroczono deficyt"));
245  msgBox.setText(tr("Przekroczyłeś deficyt punktów. Co chcesz teraz zrobić?"));
246 
247  QPushButton *close = msgBox.addButton(tr("Zamknij grę"), QMessageBox::ActionRole);
248  QPushButton *deansLeave = msgBox.addButton(tr("Dziekanka"), QMessageBox::ActionRole);
249  QPushButton *newGame = msgBox.addButton(tr("Nowa gra"), QMessageBox::ActionRole);
250 
251  msgBox.exec();
252 
253  if (msgBox.clickedButton() == newGame) {
254  createNewGame(); //wygeneruj nową grę
255  } else if (msgBox.clickedButton() == deansLeave) {
256  player->stop();
258  startTimers(); //uruchom timery na nowo
259  emit startStudent(); //odblokuj studenta
260  } else if (msgBox.clickedButton() == close) {
261  emit closeGame(); //zamknij grę
262  }
263  }
264  else { //jeśli przekroczono deficyt na pierwszym semestrze
265  playEndMusic();
266  QMessageBox msgBox;
267 
268  msgBox.setIcon(QMessageBox::Critical);
269  msgBox.setWindowTitle(tr("Koniec"));
270  msgBox.setText(tr("Przekroczyłeś deficyt punktów, już na pierwszym semestrze. Musisz zacząć od nowa lub zrezygnować"));
271 
272  QPushButton *close = msgBox.addButton(tr("Zamknij grę"), QMessageBox::ActionRole);
273  QPushButton *newGame = msgBox.addButton(tr("Nowa gra"), QMessageBox::ActionRole);
274 
275  msgBox.exec();
276 
277  if (msgBox.clickedButton() == newGame) {
278  createNewGame(); //wygeneruj nową grę
279  }
280  else if (msgBox.clickedButton() == close) {
281  emit closeGame(); //zamknij grę
282  }
283  } //_term > 1
284  }
285  else { //jeśli wzięto już 2 dziekanki
286  playEndMusic();
287  QMessageBox msgBox;
288  msgBox.setIcon(QMessageBox::Critical);
289  msgBox.setWindowTitle(tr("Koniec"));
290  msgBox.setText(tr("Niestety dołączyłeś do grupy poległych na placu boju :-( Ale historia nigdy o Tobie nie zapomni..."));
291 
292  QPushButton *close = msgBox.addButton(tr("Zamknij grę"), QMessageBox::ActionRole);
293  QPushButton *newGame = msgBox.addButton(tr("Nowa gra"), QMessageBox::ActionRole);
294 
295  msgBox.exec();
296 
297  if (msgBox.clickedButton() == newGame) {
298  createNewGame(); //wygeneruj nową grę
299  }
300  else if (msgBox.clickedButton() == close) {
301  emit closeGame(); //zamknij grę
302  }
303  }
304 }
305 
307 {
308  player->setMedia(QUrl("qrc:/sounds/round.wav"));
309  player->play();
310 }
311 
313 {
314  player->setMedia(QUrl("qrc:/sounds/end.wav"));
315  player->play();
316 }
void on_MainWindow_inicializeNewGame()
Reaguje na sygnał potrzeby inicjalizacji nowej gry.
Definition: game.cpp:106
void stopStudent()
Zatrzymuje studenta w miejscu.
int Y_XL
Wartość przyspieszenia na osi Y akcelerometru (mg)
Definition: memsdata.h:29
qreal Y
Wartość osi Y o ile trzeba przesunąć studenta.
Definition: memsdata.h:79
void playEndMusic()
Odtwarza muzykę końca gry.
Definition: game.cpp:312
QTimer * _timerMove
Uchwyt do timera przesuwającego kursy.
Definition: game.h:227
void on_Courses_increaseDeficit(int ECTS)
Zwiększa liczbę punktów deficytu.
Definition: game.cpp:74
void on_Courses_gameFinished()
Wyświetla końcowy komunikat w przypadku powodzenia gry.
Definition: game.cpp:124
void changeButtonToStart()
Sygnał zmiany napisu na przyciusku start.
int X_XL
Wartość przyspieszenia na osi X akcelerometru (mg)
Definition: memsdata.h:23
void on_Courses_changeTerm(int term)
Zmienia numer aktualnego semestru.
Definition: game.cpp:68
int _deansLeave
Liczba wzietych urlopów dziekańskich.
Definition: game.h:251
Dane przesunięcia studenta.
Definition: memsdata.h:67
int _term
Aktualny semestr studiów.
Definition: game.h:239
void connectCourses()
Łączy wszystkie sloty i sygnały związane z kursami.
Definition: game.cpp:170
void on_Courses_checkDeficitOverstep()
Sprawdza przekroczenie deficytu.
Definition: game.cpp:111
void resetPosition()
Ustawienie studenta do pozycji początkowej.
Definition: student.cpp:70
void on_Courses_decreaseDeficit(int ECTS)
Zmniejsza liczbę punktów deficytu.
Definition: game.cpp:80
Definicja klasy Game.
void closeGame()
Sygnał zamknięcia gry.
Courses * _courses
Uchwyt do kursów.
Definition: game.h:215
void playStartingMusic()
Odtwarza początkową muzykę
Definition: game.cpp:306
void deficitOversteped()
Obsługuje przekroczenie deficytu punków ECTS.
Definition: game.cpp:231
Student * _student
Uchwyt do studenta.
Definition: game.h:209
void createNewGame()
Generuje nową grę
Definition: game.cpp:206
QMediaPlayer * player
Odtwarzacz muzyki.
Definition: game.h:263
Game(QWidget *parent=0)
Konstruktor klasy Game.
Definition: game.cpp:22
Modeuje pojęcie Studenta.
Definition: student.h:25
Surowe dane otrzymane z kontrolera (płytki)
Definition: memsdata.h:17
GameSettings * gameSettings
Globalne ustawienia gry.
Definition: game.h:46
QGraphicsScene * _scene
Uchwyt do sceny.
Definition: game.h:203
void moveStudent(moveData data)
Poruszanie studenta po scenie.
Definition: student.cpp:52
int sceneHeight
Wysokość sceny (okienka z grą)
Definition: gamesettings.h:19
void on_Courses_increaseECTS(int ECTS)
Zwiększa liczbę sumy punktów ECTS.
Definition: game.cpp:62
int deficitLimit
Maksymalny deficyt punktów ECTS.
Definition: gamesettings.h:29
void startStudent()
Odblokowuje studenta.
void startTimers()
Zatrzymuje wszystkie timery.
Definition: game.cpp:164
void changeTerm(int term)
Zmienia wartość numeru semestru.
QList< QList< Course * > * > getAllTerms()
Umożliwa dostęp do wszystkich semestrów.
Definition: courses.h:50
void setupScene()
Ustawia scenę gry.
Definition: game.cpp:195
void on_startGame_clicked()
Obsługuje naciśnięcie przycisu rozpoczęcia gry.
Definition: game.cpp:92
void stopTimers()
Startuje wszystkie timery.
Definition: game.cpp:158
void changeDeansLeave(int number)
Zmienia wartość ilości dziekanek.
void on_stopGame_clicked()
Obsługuje naciśnięcie przycisu spauzowania gry.
Definition: game.cpp:99
Modeluje pojęcie Courses.
Definition: courses.h:26
int _deficit
Deficyt punktowy.
Definition: game.h:245
void changeECTS(int ECTS)
Zmienia wartość punktów ECTS.
qreal X
Wartość osi X o ile trzeba przesunąć studenta.
Definition: memsdata.h:73
Definuje ustawienia gryModeluje pojęcie ustawień gry, jako struktura odpowiednich pól...
Definition: gamesettings.h:17
int _ECTS
Suma zdobytych punktów ECTS.
Definition: game.h:233
void newDataArrived(rawData data)
Obsługuje przyjścienowych danych z czujników.
Definition: game.cpp:149
void on_Courses_failedToReadData()
Obsługuje błąd odczytu plików semestrów.
Definition: game.cpp:86
QTimer * _timerCourses
Uchwyt do timera generujacego kursy.
Definition: game.h:221
bool _gameStarted
Flaga wystartowania gry.
Definition: game.h:257
int sceneWidth
Szerokość sceny (okienka z grą)
Definition: gamesettings.h:21
void connectStudent()
Łączy wszystkie sloty i sygnały związane ze studentem.
Definition: game.cpp:188
void changeDeficit(int ECTS)
Zmienia wartość deficytu.
int speed
Skalowanie szybkości gry (1-10)
Definition: gamesettings.h:27