-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAstroid.pde
More file actions
183 lines (167 loc) · 4.02 KB
/
Astroid.pde
File metadata and controls
183 lines (167 loc) · 4.02 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
class Astroid
{
PImage enemy,smallenemy;
PVector location,dimension,dir,velocity,ALcenter,AScenter,acceleration;
float accelerationrate;
ArrayList<Astroid> astroidlist;
Astroid Ast,Astcopy,Ast2;
boolean active;
boolean small;
Astroid(float sizex,float sizey, boolean size)
{
float rando = random(0,2);
if(rando<=.5)
{
location = new PVector(0,random(0,height));
}
else if(rando<=1)
{
location = new PVector(random(0,width),0);
}
else if(rando<=1.5)
{
location = new PVector(width,random(0,height));
}
else if(rando<2)
{
location = new PVector(random(0,width),height);
}
dimension = new PVector(sizex,sizey);
enemy = loadImage("Enemy.png");
smallenemy=loadImage("Enemysmall.png");
dir = new PVector(0,0);
velocity = new PVector(0,0);
accelerationrate=.1;
enemy.resize(30,30);
active=true;
small = size;
ALcenter=new PVector(0,0);
AScenter= new PVector(0,0);
acceleration=new PVector(0,0);
}
void createlist(int y)
{
astroidlist= new ArrayList<Astroid>();
for(int x =0;x<y;x++)
{
Ast = new Astroid(30,30,true);
astroidlist.add(Ast);
}
}
void move()
{
for(int x =0;x<astroidlist.size();x++)
{
Astcopy = astroidlist.get(x);
Astcopy.dir = PVector.sub(ship.loc,Astcopy.location);
Astcopy.dir.normalize();
Astcopy.acceleration=Astcopy.dir.mult(accelerationrate);
Astcopy.velocity.add(Astcopy.acceleration);
Astcopy.velocity.limit(.7);
Astcopy.location.add(Astcopy.velocity);
boolean hittingA=Astcopy.hitB(ship.bulletlist);
boolean hittingS= Astcopy.hitS(ship);
if(hittingA==true&&Astcopy.small==true)
{
PVector nloc = Astcopy.location.copy();
astroidlist.remove(x);
Ast = new Astroid(20,20,false);
Ast.location=nloc.copy();
Ast2 = new Astroid(20,20,false);
Ast2.location=nloc.copy();
Ast2.location.x=Ast2.location.x+20;
Ast2.location.y=Ast2.location.y+20;
astroidlist.add(Ast2);
astroidlist.add(Ast);
}
if(hittingA==true&&Astcopy.small==false)
{
astroidlist.remove(x);
}
if(hittingS==true)
{
ship.Active=false;
}
if(Astcopy.small==true)
{
//rect(Astcopy.location.x,Astcopy.location.y,30,30);
image(enemy,Astcopy.location.x,Astcopy.location.y);
}
else if(Astcopy.small==false)
{
//rect(Astcopy.location.x,Astcopy.location.y,10,10);
image(smallenemy,Astcopy.location.x,Astcopy.location.y);
}
if(Astcopy.location.x<0)
{
Astcopy.location.x=width;
}
if(Astcopy.location.x>width)
{
Astcopy.location.x=0;
}
if(Astcopy.location.y<0)
{
Astcopy.location.y=height;
}
if(Astcopy.location.y>height)
{
Astcopy.location.y=0;
}
}
}
Boolean hitB(ArrayList<Bullet> Blist)
{
for(int x =0;x<Blist.size();x++)
{
Bullet B = Blist.get(x);
ALcenter.x =this.location.x+15;
ALcenter.y = this.location.y+15;
float distanceB = PVector.dist(B.location,ALcenter);
if(this.small==true&&B.fired==true)
{
if(distanceB<=16)
{
B.fired=false;
return true;
}
}
if(this.small==false&&B.fired==true)
{
AScenter.x=this.location.x+5;
AScenter.y=this.location.y+5;
distanceB = PVector.dist(B.location,AScenter);
if(distanceB<=6)
{
B.fired=false;
return true;
}
}
}
return false;
}
Boolean hitS(Ship ship)
{
ALcenter.x =this.location.x+15;
ALcenter.y = this.location.y+15;
float distanceS =PVector.dist(ALcenter,ship.loc);
if(this.small==true)
{
if(distanceS<=35)
{
return true;
}
}
if(this.small==false)
{
AScenter.x=this.location.x+5;
AScenter.y=this.location.y+5;
distanceS =PVector.dist(ship.loc,AScenter);
if(distanceS<=25)
{
return true;
}
}
return false;
}
}