-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBird.pde
130 lines (121 loc) · 2.92 KB
/
Bird.pde
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
class Bird{
int birdSize;
int flapSpeed = (int)(Math.random()*6)+1+numDancersDancing;
int flapOffset = 0;
boolean isFlying = false;
PVector location;
int flyDirection = (int)(Math.random()*2);
float noiseIndex = 0;
float yOffset = 0;
int ySpeed = (Math.random()*60)+5;
int perlinDirection = 1;
boolean hasCollided = false;
float chirpIndex = 0;
float chirpSize = 0;
int rColor = (int)(Math.random()*127)+127;;
int gColor = (int)(Math.random()*127)+127;
int bColor = 0;
Bird(int x, int y, int s){
location = new PVector(x,y);
birdSize = s;
}
void update(){
flap();
checkCursor();
beginShape();
noFill();
strokeWeight(5);
stroke(rColor,gColor,bColor);
vertex(location.x, location.y + flapOffset);
vertex(location.x + birdSize/2, birdSize + location.y);
vertex(birdSize + location.x,location.y + flapOffset);
endShape();
if(hasCollided){
chirpHalo();
}
}
void flap(){
if(flapOffset < 0 || flapOffset > birdSize){
flapSpeed *= -1;
}
flapOffset += flapSpeed;
}
void checkCursor(){
//Collision Detection to see if the bird is inside the cursor box
//It's a mess, I know!
if(hasCollided == true){
return;
}
else{
if((location.y >= cursor.location.y &&
location.y <= (cursor.location.y + cursor.cursorSize))
|| (location.y+birdSize >= cursor.location.y &&
location.y <= (cursor.location.y + cursor.cursorSize))){
if((location.x >= cursor.location.x &&
location.x <= (cursor.location.x + cursor.cursorSize))
|| (location.x+birdSize >= cursor.location.x &&
location.x <= (cursor.location.x + cursor.cursorSize))){
//bird has entered bound box, trigger chirp!
hasCollided = true;
score.touchBird();
if(gameSounds.audioIsSupported()){
gameSounds.playChirp();
}
//println("Collision!");
}
}
}
}
void fly(){
if(!isFlying){
isFlying = true;
if(flyDirection){
location.x = 0 - birdSize;
}
else{
location.x = width + birdSize;
}
}
if(flyDirection){
location.x += abs(flapSpeed);
}
else{
location.x -= abs(flapSpeed);
}
yOffset = (noise(noiseIndex)-0.5)*perlinDirection*birdSize*0.5;
if(checkYBounds(location.y + yOffset)){
location.y += yOffset;
//println("Bounds Good");
}
else{
location.y -= yOffset;
perlinDirection *= -1;
//println("Bounds Bad");
}
noiseIndex += 0.01;
update();
}
void chirpHalo(){
strokeWeight(3);
stroke(0,0,255,100);
chirpSize = abs(sin(chirpIndex));
chirpIndex += 0.1;
noFill();
ellipse(location.x+birdSize/2,location.y+birdSize,birdSize*chirpSize,birdSize*chirpSize);
}
boolean checkYBounds(float yVal){
if((yVal < 0) || (yVal > 400 - birdSize)){
return false;
}
else{
return true;
}
}
boolean isDead(){
if((location.x > (width + birdSize*2)) || (location.x < 0 - birdSize*2)){
//println("This little birdy is dead!");
return true;
}
return false;
}
}