-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritcannon.cpp
156 lines (142 loc) · 4.48 KB
/
inheritcannon.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "cannon.h"
#include "gamescene.h"
#include "bullet.h"
#include <QVector2D>
#include <QtMath>
#include <QDebug>
#include <QTimer>
FlameCannon::FlameCannon(QPoint point,QString pName,GameScene * g):
Cannon(point,pName,g)
{
//重新上载图片
pixSize=0.29;
pix=QPixmap(pName);
pix=pix.scaled(QSize(pix.width()*pixSize,pix.height()*pixSize),Qt::KeepAspectRatio);
//更新部分数据成员
type=3;
aggressivity=8;
curHp=maxHp=80;
coin=curHp;
healthWith=54;
attackRate=3800;
range=(int)(Cannon::range*1.6);
delaHeight=(int)(pix.height()*0.668)+1;
delaWidth=(int)(pix.width()*0.687);
bulletPixSize=0.45;
}
void FlameCannon::LockEnemyWithinAttackRange()
{
Cannon::LockEnemyWithinAttackRange();
}
void FlameCannon::AttackEnemy()
{
if(game->isGameLose||game->isGameWin) return;
if(lockedEmemy&&game->enemyList.indexOf(lockedEmemy)!=-1)
{
FlameBullet* flameBullet=new FlameBullet(this,lockedEmemy,":/image/purple_bullet.png",bulletPixSize);
game->LoadBullet(flameBullet);
flameBullet->Move();
}
}
RangeCannon::RangeCannon(QPoint point,QString pName,GameScene * g):
Cannon(point,pName,g),
enemyNum(3)
{
//初始化敌人数组
enemyInRangeList.resize(enemyNum);
pixSize=0.33;
pix=QPixmap(pName);
pix=pix.scaled(QSize(pix.width()*pixSize,pix.height()*pixSize),Qt::KeepAspectRatio);
type=4;
aggressivity=10;
curHp=maxHp=80;
coin=curHp;
healthWith=54;
attackRate=2600;
range=(int)(Cannon::range*1.8);
delaHeight=(int)(pix.height()*0.580)+1;
delaWidth=pix.width()*0.5;
bulletPixSize=0.47;
}
bool RangeCannon::CheckSpaceinEnemyInRangeList(int& indexlist)//是否有空针
{
for(int i=indexlist;i<enemyInRangeList.size();i++)
if(!enemyInRangeList[i]){
indexlist=i;//返回第一个空针位置
return true;
}
return false;
}
bool RangeCannon::CheckEmptyNullinEnemyInRangeList(int& indexlist)//是否全为空针
{
for(int i=0;i<enemyInRangeList.size();i++)
if(enemyInRangeList[i]) {
indexlist=i;
return false;
}
return true;
}
void RangeCannon::LockEnemyWithinAttackRange()
{
if(game->isGameLose||game->isGameWin) return;
if(curHp<=0){
game->AudioPlay(game->cannonDeletePlayer);
game->DeleteCannon(this);
return;
}
//先检查是不是有敌人失效的情况
for(int i=0;i<enemyInRangeList.size();i++){
if(enemyInRangeList[i]&&game->enemyList.indexOf(enemyInRangeList[i])==-1){
enemyInRangeList[i]=NULL;
}
}
//尽可能填满敌人list
int indexlist=0;
if(CheckSpaceinEnemyInRangeList(indexlist)){//只要有空间就填补敌人
foreach(Enemy *enemy,game->enemyList){
if(CircleDistance(pos,range,enemy->getPos(),1)&&!enemy->ProtectedFlag()){
if(enemyInRangeList.indexOf(enemy)!=-1) continue;//已经有的敌人就再不加了
if(CheckSpaceinEnemyInRangeList(indexlist)){
enemyInRangeList[indexlist]=enemy;
indexlist++;
}
else{//没空间就退出循环
break;
}
}
}
}
//旋转炮台准备攻击
indexlist=0;
if(!CheckEmptyNullinEnemyInRangeList(indexlist)){
//炮台对向第一个敌人
QVector2D normal(enemyInRangeList[indexlist]->getPos()-pos);
normal.normalize();
rotateDegree=qRadiansToDegrees(qAtan2(normal.y(),normal.x()))+90;
//检测敌人是否跳脱
for(int i=indexlist;i<enemyInRangeList.size();i++){
if(enemyInRangeList[i]){
if(!CircleDistance(pos,range,enemyInRangeList[i]->getPos(),1)){
enemyInRangeList[i]=NULL;
}
else{//开始攻击
if(!attackTimer->isActive()) {
AttackEnemy();
attackTimer->start(attackRate);
}
}
}
}
}
}
void RangeCannon::AttackEnemy()
{
if(game->isGameLose||game->isGameWin) return;
for(int i=0;i<enemyInRangeList.size();i++){
if(enemyInRangeList[i]&&game->enemyList.indexOf(enemyInRangeList[i])!=-1){
RangeBullet* rangeBullet=new RangeBullet(this,enemyInRangeList[i],":/image/Missile.png",bulletPixSize);
game->LoadBullet(rangeBullet);
rangeBullet->Move();
}
}
}