-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackgroundManager.pde
66 lines (58 loc) · 1.51 KB
/
BackgroundManager.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 class BackgroundManager{
private ArrayList<GridLine> grid;
private GridLine lastSpawn;
private float blockSize = 20;
private Player player;
private Pool<GridLine> pool;
public BackgroundManager(Player player){
pool = new Pool<GridLine>();
grid = new ArrayList<GridLine>();
this.player = player;
}
public void update(){
while(lastSpawn == null || lastSpawn.getY() + player.getSpeed() / frameRate > 0)
spawn();
for(int i = 0; i < grid.size(); i++) {
GridLine line = grid.get(i);
line.updatePosition(player.getSpeed());
if(checkBounds(line)){
despawn(line);
}
}
for(int i = 0; i < grid.size(); i++) {
GridLine line = grid.get(i);
line.display();
}
}
private void spawn(){
GridLine line = null;
float y = 0;
if(lastSpawn == null)
y = height;
else
y = lastSpawn.getY() - blockSize ;
try{
line = pool.get();
}catch(Exception e){
line = pool.addAndRetrieveObject(new GridLine(blockSize, y));
};
line.setY(y);
grid.add(line);
lastSpawn = line;
}
private boolean checkBounds(GridLine line){
return line.getY() > height;
}
private boolean despawn(GridLine line){
pool.release(line);
return grid.remove(line);
}
public void reset(){
lastSpawn = null;
for(int i = 0; i < grid.size(); i++){
if(despawn(grid.get(i))){
i--;
}
}
}
}