/
FruitMgr.pde
72 lines (64 loc) · 1.58 KB
/
FruitMgr.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
class FruitMgr{
static final int EMPTY = 0;
static final int BANANA = 1;
static final int HAMI = 2;
ArrayList<Fruit> fruits ;
ArrayList<Bullet> bullets ;
int [][] fMap;
boolean enableFire;
FruitMgr(boolean enanbleFire){
fruits = new ArrayList<Fruit>();
bullets = new ArrayList<Bullet>();
this.enableFire = enanbleFire;
fMap = new int[Util.colNum][Util.rowNum];
for (int i=0; i<Util.colNum ; i++){
for (int j=0; j<Util.rowNum; j++){
fMap[i][j] = EMPTY;
}
}
}
boolean isHitFruit(float x, float y, float w, float h){
for (Fruit f: fruits){
// shift a distance: width/2 --> hit the center
if (Util.isHit(x,y,w,h,f.x,f.y,f.w/2,f.h)){
fruits.remove(f);
fMap[f.col][f.row] = EMPTY;
return true;
}
}
return false;
}
boolean isHitBullet(float x, float y, float w, float h){
for (Bullet b: bullets){
if (Util.isHit(x,y,w,h,b.x,b.y,b.w,b.h)){
bullets.remove(b);
return true;
}
}
return false;
}
boolean add(int type , int col , int row){
if (fMap[col][row] == EMPTY){
fruits.add(new Fruit(type, col, row, this));
fMap[col][row] = type;
return true;
}else{
return false;
}
}
void addBullet(float x ,float y, float speed, PImage image){
bullets.add(new Bullet(x,y,speed,image));
}
void display(){
// show fruits
for (Fruit f : fruits) {
f.display();
}
// show bullets
if (enableFire){
for (Bullet b : bullets) {
b.fly();
}
}
}
}