-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackgroundImage.pde
100 lines (78 loc) · 2.79 KB
/
BackgroundImage.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
class BackgroundImage implements IGraphic, ICanParallax{
Map<Camera, Integer> cameras = new HashMap();//Camera, Layer
PVector position = PVector.zero();
PVector drawPos = PVector.zero();
PVector size;
PImage texture;
boolean fillWidth = false;
boolean fillHeight = false;
private boolean isCulled;
PVector parallaxCoeff = PVector.zero();
boolean canXParallax = false;
boolean canYParallax = false;
BackgroundImage(PVector _position, String texturePath, PVector _size, int _layer, Camera camera, boolean _fillWidth, boolean _fillHeight){
texture = loadImage(texturePath);
size = _size;
position = _position;
fillWidth = _fillWidth;
fillHeight = _fillHeight;
attachCamera(camera, _layer);
addToGraphicsList(camera, _layer);
camera.addToParallaxObjects(this);
texture.resize((int)size.x, (int)size.y);
}
public void display(Camera camera){
PVector startPos = PVector.add(PVector.sub(position, drawPos), camera.getFocusOffset());
image(texture, startPos.x, startPos.y);
}
public void setParallax(PVector coeff){
parallaxCoeff = coeff;
}
public void setCanXParallax(boolean canParallax){
canXParallax = canParallax;
}
public void setCanYParallax(boolean canParallax){
canYParallax = canParallax;
}
public void attachCamera(Camera camera, int layer) {
//if (cameras.get(camera) != null)
cameras.put(camera, layer);
}
public void addToGraphicsList(Camera camera, int layer) {
attachCamera(camera, layer);
setLayer(camera, layer);
camera.addToGraphicsList(this, layer);
}
public void removeFromGraphicsList(Camera camera, int layer) {
camera.removeFromGraphicsList(this, cameras.get(camera));
}
public void setLayer(Camera camera, int _layer) {
camera.removeFromGraphicsList(this, cameras.get(camera));
Integer layer = cameras.get(camera);
layer = _layer;
camera.addToGraphicsList(this, cameras.get(camera));
}
public boolean isCulled() {
return isCulled;
}
public void checkCulling(Camera camera){
}
void manageCulling(Camera camera){}
public void setDrawPosition(Camera camera){
PVector camPos = camera.getPosition();
float x = camPos.x;
float y = camPos.y;
boolean hasFocusPoint = camera.getFocusPoint() != null;
//System.out.println(hasFocusPoint);
if (hasFocusPoint){
PVector focusPosition = camera.getPosition();
PVector focusOffset = camera.getFocusOffset();
if (canXParallax)
x = camPos.x + (focusPosition.x - focusOffset.x) * parallaxCoeff.x;
if (canYParallax)
y = camPos.y + (focusPosition.y - focusOffset.y) * parallaxCoeff.y;
}
//System.out.println(camera.getPosition() + " " + new PVector(x, y));
drawPos = new PVector(x, y);
}
}