-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvaderSet.cpp
152 lines (143 loc) · 3.15 KB
/
InvaderSet.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
/* invaderR, space invaders clone
* Copyright (C) 2006 Richard Adem richy486@gmail.com
*
* invaderR is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* invaderR is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include ".\invaderset.h"
CInvaderSet::CInvaderSet(void)
{
side = true;
side2side = 0;
}
CInvaderSet::~CInvaderSet(void)
{
}
CInvaderSet* CInvaderSet::getInstance()
{
static CInvaderSet instance;
return &instance;
}
// Fill up the list with 50 invaders.
void CInvaderSet::spawnNewSet()
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 10; j++)
{
CInvader t_inv;
t_inv.generateInvader();
Point t_pnt;
t_pnt.x = j*30+160;
t_pnt.y = i*30+180;
t_inv.setPos(t_pnt);
I_set.push_back(t_inv);
}
}
}
// Test if a players shot has hit an invader.
bool CInvaderSet::testHits(Point p)
{
list<CInvader>::iterator iter;
iter=I_set.begin();
while(iter!=I_set.end())
{
if(p.x >= iter->getPos().x && p.x <= iter->getPos().x+(IPS*5) &&
p.y >= iter->getPos().y && p.y <= iter->getPos().y+(IPS*5))
{
iter = I_set.erase(iter);
return true;
}
else
{
iter++;
}
}
return false;
}
// Move the invaders adding slop,
// Also has the invaders shoot
// The less invaders the larger the change they will shoot.
void CInvaderSet::move()
{
if(I_set.empty())
{
playing = false;
outcome = true;
}
Point t_p;
list<CInvader>::iterator iter;
double x,y;
if(side == true)
{
side2side++;
x = -1;
if(side2side >= 75)
{
side2side = 0;
side = false;
}
}
else
{
side2side++;
x = 1;
if(side2side >= 75)
{
side2side = 0;
side = true;
}
}
for(iter=I_set.begin(); iter!=I_set.end(); ++iter)
{
t_p.x = iter->getPos().x + x*((double)((rand()%5)+15)/10);
if(t_p.x > 460)
t_p.x -= ((double)((rand()%10)+10)/10);
if(t_p.x < 20)
t_p.x += ((double)((rand()%10)+10)/10);
t_p.y = iter->getPos().y -(double)(rand()%20)/100;
iter->setPos(t_p);
if(iter->getPos().y <= 0)
{
playing = false;
outcome = false;
}
if(rand()%(I_set.size()*20) == 1)
{
CBomber::getInstance()->shoot(t_p);
}
}
}
// Start a new game.
void CInvaderSet::restart()
{
killAll();
spawnNewSet();
side2side = 0;
side = true;
}
// Kill all the invaders.
void CInvaderSet::killAll()
{
I_set.clear();
}
// Call the drawing function for all the invaders.
void CInvaderSet::drawSet()
{
list<CInvader>::iterator iter;
for(iter=I_set.begin(); iter!=I_set.end(); ++iter)
{
iter->draw();
}
}