-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.cpp
More file actions
131 lines (125 loc) · 4.69 KB
/
Copy pathcontroller.cpp
File metadata and controls
131 lines (125 loc) · 4.69 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
129
130
131
#include "controller.h"
#include <QSound>
#include <QRandomGenerator>
Controller::Controller(QString lvl)
{
//lvl_ = new Level(":/levels/"+QString::number(lvl)+".json");
lvl_ = new Level(lvl);
monstrs_=lvl_->getMonstrs();
play_.setPlace(QPoint(350, 850));
play_.setEndPlace(QPoint(350, 850));
play_.setLives(3);
time_ = 0;
}
void Controller::generateShot(){
int num;
if (!lvl_->getHard()){
double a= QRandomGenerator::global()->generateDouble();
num = a*monstrs_.size();
}else{
num = 0;
for(int i = 1; i<monstrs_.size();i++){
int minDist = sqrt((monstrs_[num].getPlace().x() - play_.getPlace().x())*(monstrs_[num].getPlace().x() - play_.getPlace().x())+
(monstrs_[num].getPlace().y() - play_.getPlace().y())*(monstrs_[num].getPlace().y() - play_.getPlace().y()));
int currrentDist = sqrt((monstrs_[i].getPlace().x() - play_.getPlace().x())*(monstrs_[i].getPlace().x() - play_.getPlace().x())+
(monstrs_[i].getPlace().y() - play_.getPlace().y())*(monstrs_[i].getPlace().y() - play_.getPlace().y()));
if(currrentDist<minDist){
num = i;
}
}
}
Shot newShot;
newShot.setOwner(true);
newShot.setPlace(monstrs_[num].getPlace());
newShot.setEndPlace(QPoint(monstrs_[num].getPlace().x(), 900));
addShot(newShot);
}
void Controller::addShot(Shot shot){
shots_.push_back(shot);
}
int Controller::endOfLevel(){
for (int i = 0; i < monstrs_.size(); i++){
if (monstrs_[i].getPlace().y()==850){
return -1;
}
}
if (play_.getLives() < 1){
return -1;
} else if (monstrs_.empty()){
return 1;
} else{
return 0;
}
}
void Controller::shotOnCreature(){
int i = 0;
while( i<shots_.size()){
int dist = sqrt((shots_[i].getPlace().x() - play_.getPlace().x())*(shots_[i].getPlace().x() - play_.getPlace().x())+
(shots_[i].getPlace().y() - play_.getPlace().y())*(shots_[i].getPlace().y() - play_.getPlace().y()));
if (shots_[i].getOwner() && dist<20){
play_.shoted();
shots_.erase(shots_.begin()+i);
}else{
i++;
}
}
i =0;
while(i <monstrs_.size()){
int j = 0;
while (j<shots_.size()){
int dist = sqrt((shots_[j].getPlace().x() - monstrs_[i].getPlace().x())*(shots_[j].getPlace().x() - monstrs_[i].getPlace().x())+
(shots_[j].getPlace().y() - monstrs_[i].getPlace().y())*(shots_[j].getPlace().y() - monstrs_[i].getPlace().y()));
if (!shots_[j].getOwner() && dist<20){
monstrs_[i].shoted();
shots_.erase(shots_.begin()+j);
if (monstrs_[i].getLives()==0){
monstrs_.erase(monstrs_.begin()+i);
j =shots_.size();
i--;
}
}else{
j++;
}
}
i++;
}
}
void Controller::timeout(){
time_ += 50;
shotOnCreature();
if (time_ % lvl_->getShotPeriod() == 0){
generateShot();
}
if (time_ % lvl_->getMovePeriod() == 0){
for (int i = 0; i <monstrs_.size(); i++){
if (monstrs_[i].getPlace().x()==25 && monstrs_[i].getPlace().y()%100!=0){
QPoint newPlace = QPoint(monstrs_[i].getPlace().x(),monstrs_[i].getPlace().y()+50);
monstrs_[i].setEndPlace(newPlace);
}else if (monstrs_[i].getPlace().x()==675 && monstrs_[i].getPlace().y()%100==0){
QPoint newPlace = QPoint(monstrs_[i].getPlace().x(),monstrs_[i].getPlace().y()+50);
monstrs_[i].setEndPlace(newPlace);
}else if(monstrs_[i].getPlace().y()%100==0){
QPoint newPlace = QPoint(monstrs_[i].getPlace().x()+50,monstrs_[i].getPlace().y());
monstrs_[i].setEndPlace(newPlace);
}else{
QPoint newPlace = QPoint(monstrs_[i].getPlace().x()-50,monstrs_[i].getPlace().y());
monstrs_[i].setEndPlace(newPlace);
}
/*QPoint newPlace = QPoint(monstrs_[i].getPlace().x(),monstrs_[i].getPlace().y()+100);
monstrs_[i].setEndPlace(newPlace);*/
}
}
int i = 0;
while( i<shots_.size()){
if (shots_[i].getOwner() && shots_[i].getPlace().y()>890){
shots_.erase(shots_.begin()+i);
}else if(!shots_[i].getOwner() && shots_[i].getPlace().y()<10) {
shots_.erase(shots_.begin()+i);
}else{
i++;
}
}
if (time_ % lvl_->getShotPeriod() == 0 && time_ % lvl_->getMovePeriod() == 0){
time_ = 0;
}
}