-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lane.pde
66 lines (56 loc) · 1.6 KB
/
Lane.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
public abstract class Lane implements GameObject, Collidable {
protected int direction, frames, blockSize, spacing, y;
private ArrayList<AutoMovingBlock> blocks = new ArrayList();
private int elapsedFrames = 0;
private color bg;
public Lane(int speed, int blockSize, int spacing, int y, color bg){
this.direction = speed > 0 ? LEFT : RIGHT;
this.frames = 300 / Math.abs(speed);
this.blockSize = blockSize;
this.spacing = spacing;
this.y = y;
this.bg = bg;
}
public abstract AutoMovingBlock make(int direction, int frames, int x, int y);
public void show(){
fill(bg);
rect(0, y * Constants.GRID_SIZE, 512, Constants.GRID_SIZE);
for(AutoMovingBlock b: blocks){
b.show();
}
}
public boolean doCollision(Player p){
for(AutoMovingBlock b: blocks){
if(b.doCollision(p)){
return true;
}
}
return false;
}
public void update(){
spawnNewBlocks();
for(AutoMovingBlock b: blocks){
b.update();
}
removeBlocksOutsideOfView();
elapsedFrames = (elapsedFrames + 1) % (frames * (blockSize + spacing));
}
private void spawnNewBlocks(){
if(elapsedFrames % frames == 0){
int sequenceIndex = elapsedFrames / frames;
if(sequenceIndex < blockSize){
blocks.add(
make(direction, frames, direction == RIGHT ? 0 : 15, y)
);
}
}
}
private void removeBlocksOutsideOfView(){
if(blocks.size() > 0) {
AutoMovingBlock first = blocks.get(0);
if (first.pos.x < 0 || first.pos.x >= 16){
blocks.remove(first);
}
}
}
}