forked from carykh/SpiderEvoSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwatter.pde
More file actions
128 lines (125 loc) · 3.99 KB
/
Swatter.pde
File metadata and controls
128 lines (125 loc) · 3.99 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
class Swatter{
int index = 0;
int visIndex = 0;
float[] coor;
float percentage = 0;
float BAR_THICKNESS = 10;
float SWATTER_THICKNESS = 6;
int killCount = 0;
public Swatter(int i, float p, Room room, ArrayList<Swatter> swatters){
index = i;
refresh(room, false, swatters);
percentage = p;
}
void refresh(Room room, boolean newP, ArrayList<Swatter> swatters){
coor = new float[2];
boolean freeToPlace = false;
int attempts = 0;
while(!freeToPlace){
for(int d = 0; d < 2; d++){
float max_ = room.getMaxDim(d);
coor[d] = random(0,max_);
coor[d] = min(max(coor[d],0+R),max_-R);
}
freeToPlace = true;
attempts++;
if(attempts < 40){
for(int i = 0; i < swatters.size(); i++){
if(i != index){
if(abs(swatters.get(i).coor[0] - coor[0]) < 280 && abs(swatters.get(i).coor[1] - coor[1]) < 280){
freeToPlace = false; // This would overlap with another swatter, so try to place it somewhere else.
}
}
}
}
}
visIndex = totalSwatterIndex;
totalSwatterIndex++;
killCount = 0;
if(newP){
percentage = 1.0;
}
}
void drawSwatter(Room room){
float[] swatterCoor = room.swatterHelper(coor, R);
for(int s = 0; s < swatterCoor.length-1; s++){
float W = swatterCoor[s+1]-swatterCoor[s];
float[] this_coor = {swatterCoor[s],coor[1]};
float[] realCoor = room.wallCoor_to_realCoor(this_coor);
g.pushMatrix();
aTranslate(realCoor);
//g.fill(darken(WALL_COLOR, sqrt(percentage)));
g.fill(darken(WALL_COLOR, pow(percentage,0.25)));
g.rotateZ(realCoor[3]);
g.rotateX(PI/2);
g.translate(0,0,-EPS);
if(percentage >= 0){
g.rect(0,-R,W,R*2);
}
float p = max(0,percentage);
g.translate(W/2,0,-SWATTER_THICKNESS*0.5);
g.translate(0,R*2,0);
float tiltDown = sqrt(min(p,0.5)/0.5);
g.rotateX(PI/2*tiltDown);
float scale_ = min(min(1.0,(1-p)/0.5), cosInter((percentage+0.4)/0.4));
g.scale(scale_);
g.fill(60);
g.translate(0,-R,0);
g.box(BAR_THICKNESS,R*2,SWATTER_THICKNESS-EPS);
g.translate(0,-R,0);
g.fill(150);
g.box(W+EPS,R*2,SWATTER_THICKNESS);
if(W >= min(60,R) && killCount >= 1){
g.pushMatrix();
g.translate(0,0,-SWATTER_THICKNESS*0.5-EPS);
g.rotateX(PI);
g.scale(min(1.0,R/60.0));
g.fill(0);
g.textAlign(CENTER);
g.textSize(65);
g.text(killCount,0,0);
g.textSize(40);
String killText = (killCount >= 2) ? "kills" : "kill";
g.text(killText,0,40);
g.popMatrix();
}
g.popMatrix();
}
}
float cosInter(float x){
float x2 = min(max(x,0),1);
return 0.5-0.5*cos(x2*PI);
}
void iterate(Room room, ArrayList<Spider> spiders, ArrayList<Swatter> swatters){
percentage -= SWAT_SPEED;
if(percentage < 0 && percentage+SWAT_SPEED >= 0){ // smash! Let's see if we killed any spiders.
swat(spiders);
}
if(percentage < -0.4){
refresh(room, true, swatters);
}
}
void swat(ArrayList<Spider> spiders){
boolean killed = false;
for(int s = 0; s < spiders.size(); s++){
Spider sp = spiders.get(s);
if(abs(sp.coor[0]-coor[0]) < R && abs(sp.coor[1]-coor[1]) < R){ // This spider was in our range! We killed it!
sp.reincarnate(spiders);
killCount++;
totalIndex++;
killed = true;
}
}
float[] realWorldCoor = room.wallCoor_to_realCoor(coor);
float dist_ = dist(player.coor[0],player.coor[1],player.coor[2],realWorldCoor[0],realWorldCoor[1],realWorldCoor[2]);
int choice = (int)random(0,3);
if(killed){
choice += 3;
}
if(playback_speed <= 2){
sfx[choice].play();
sfx[choice].amp(5/(1+dist_/33));
sfx[choice].rate(random(0.8,1.2));
}
}
}