-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoadManager.pde
102 lines (89 loc) · 2.31 KB
/
RoadManager.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
public class RoadManager{
private Pool<RoadBlock> pool;
private RoadBlock top;
private ArrayList<RoadBlock> blocks;
private float spawnThreshold = 0;
private PVector size = new PVector(400, 200, 0);
private float startPosition = 0;
private Player player;
public RoadManager(Player player, PVector blockSize, float startPosition){
spawnThreshold = size.y;
pool = new Pool<RoadBlock>();
blocks = new ArrayList<RoadBlock>();
this.startPosition = startPosition;
this.player = player;
this.size = blockSize;
}
public void update(){
if(needSpawn()){
spawn();
}
int s = blocks.size();
for(int i = 0; i < blocks.size(); i++){
RoadBlock rb = blocks.get(i);
rb.updatePosition(player.getSpeed());
rb.display();
if(checkBounds(rb)){
removeBlock(rb);
}
}
for(int i = 0; i < blocks.size(); i++){
RoadBlock rb = blocks.get(i);
rb.display();
}
//println(blocks.size());
}
private boolean needSpawn(){
if(blocks.isEmpty())
return true;
if(top.getPosition().y > -spawnThreshold)
return true;
return false;
}
private void spawn(){
RoadBlock rb=null;
try{
rb = (RoadBlock)pool.get();
}catch(Exception e){
rb = (RoadBlock)pool.addAndRetrieveObject(new RoadBlock());
}
rb = setupBlock(rb);
top = rb;
blocks.add(rb);
}
private RoadBlock setupBlock(RoadBlock rb){
rb.setSize(size);
float y = 0;
if(top == null){
y = height;
}else{
y = top.getPosition().y - rb.getSize().y;
}
rb.setPosition(new PVector(startPosition, y, 0));
return rb;
}
private boolean checkBounds(RoadBlock rb){
return rb.getPosition().y > height + spawnThreshold;
}
private boolean removeBlock(RoadBlock rb){
boolean b = blocks.remove(rb);
pool.release(rb);
return b;
}
public PVector getRoadSize(){
return size;
}
public float getRoadStart(){
return startPosition;
}
public void reset(){
top = null;
for(int i = 0; i < blocks.size(); i++){
RoadBlock rb = blocks.get(i);
if(removeBlock(rb)){
i--;
}
}
blocks.clear();
}
}