-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallaxLayer.java
More file actions
357 lines (253 loc) · 13.8 KB
/
Copy pathParallaxLayer.java
File metadata and controls
357 lines (253 loc) · 13.8 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package com.smiths;
import java.util.ArrayList;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.Entity;
import org.andengine.entity.shape.IAreaShape;
import org.andengine.opengl.util.GLState;
public class ParallaxLayer extends Entity {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
private final ArrayList<ParallaxEntity> mParallaxEntities = new ArrayList<ParallaxEntity>();
private int mParallaxEntityCount;
protected float mParallaxValueX;
protected float mParallaxScrollValueX;
protected float mParallaxChangePerSecondX;
protected float mParallaxValueY;
protected float mParallaxScrollValueY;
protected float mParallaxChangePerSecondY;
protected float mParallaxScrollFactorX = 0.2f;
protected float mParallaxScrollFactorY = 0.2f;
private Camera mCamera;
private float mCameraPreviousPosX;
private float mCameraOffsetPosX;
private float mCameraPreviousPosY;
private float mCameraOffsetPosY;
private boolean mIsScrollableX = false;
private boolean mIsScrollableY = false;
private int mLevelScaleX = 0;
private int mLevelScaleY = 0;
// ===========================================================
// Constructors
// ===========================================================
public ParallaxLayer() {
}
public ParallaxLayer(final Camera camera, final boolean mIsScrollableX,final boolean mIsScrollableY){
this.mCamera = camera;
this.mIsScrollableX = mIsScrollableX;
this.mIsScrollableY = mIsScrollableY;
mCameraPreviousPosX = camera.getCenterX();
mCameraPreviousPosY = camera.getCenterY();
}
public ParallaxLayer(final Camera camera, final boolean mIsScrollableX,final boolean mIsScrollableY, final int mLevelScaleX,final int mLevelScaleY){
this.mCamera = camera;
this.mIsScrollableX = mIsScrollableX;
this.mIsScrollableX = mIsScrollableY;
this.mLevelScaleX = mLevelScaleX;
this.mLevelScaleY = mLevelScaleY;
mCameraPreviousPosX = camera.getCenterX();
mCameraPreviousPosY = camera.getCenterY();
}
// ===========================================================
// Getter & Setter
// ===========================================================
public void setParallaxValueX(final float pParallaxValueX) {
this.mParallaxValueX = pParallaxValueX;
}
public void setParallaxValueY(final float pParallaxValueY) {
this.mParallaxValueY = pParallaxValueY;
}
public void setParallaxChangePerSecondX(final float pParallaxChangePerSecondX) {
this.mParallaxChangePerSecondX = pParallaxChangePerSecondX;
}
public void setParallaxChangePerSecondY(final float pParallaxChangePerSecondY) {
this.mParallaxChangePerSecondY = pParallaxChangePerSecondY;
}
public void setParallaxScrollFactorX(final float pParallaxScrollFactorX){
this.mParallaxScrollFactorX = pParallaxScrollFactorX;
}
public void setParallaxScrollFactorY(final float pParallaxScrollFactorY){
this.mParallaxScrollFactorY = pParallaxScrollFactorY;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public void onManagedDraw(GLState pGLState, Camera pCamera) {
super.preDraw(pGLState, pCamera);
final float parallaxValueX = this.mParallaxValueX;
final float parallaxScrollValueX = this.mParallaxScrollValueX;
final float parallaxValueY = this.mParallaxValueY;
final float parallaxScrollValueY = this.mParallaxScrollValueY;
float x;
float y;
final ArrayList<ParallaxEntity> parallaxEntities = this.mParallaxEntities;
/*
for(int i = 0; i < this.mParallaxEntityCount; i++) {
if(parallaxEntities.get(i).mIsScrollable){
parallaxEntities.get(i).onDraw(pGLState, pCamera, parallaxScrollValue, mLevelScale, mParallaxOnAxisX);
} else {
parallaxEntities.get(i).onDraw(pGLState, pCamera, parallaxValue, mLevelScale, mParallaxOnAxisX);
}
}
*/
for(int i = 0; i < this.mParallaxEntityCount; i++) {
if(parallaxEntities.get(i).mIsScrollableX == true)
{
x = parallaxScrollValueX;
}
else
{
x = parallaxValueX;
}
if (parallaxEntities.get(i).mIsScrollableY == true)
{
y = parallaxScrollValueY;
}
else
{
y = parallaxValueY;
}
parallaxEntities.get(i).onDraw(pGLState, pCamera, x, y, mLevelScaleX, mLevelScaleY);
}
}
@Override
protected void onManagedUpdate(float pSecondsElapsed) {
final float cameraCenterPosX = this.mCamera.getCenterX();
final float cameraCenterPosY = this.mCamera.getCenterY();
if(mIsScrollableX && mCameraPreviousPosX != cameraCenterPosX){
mCameraOffsetPosX = mCameraPreviousPosX - cameraCenterPosX;
mCameraPreviousPosX = cameraCenterPosX;
this.mParallaxScrollValueX += mCameraOffsetPosX * this.mParallaxScrollFactorX;
mCameraOffsetPosX = 0;
}
this.mParallaxValueX += this.mParallaxChangePerSecondX * pSecondsElapsed;
if(mIsScrollableY && mCameraPreviousPosY != cameraCenterPosY){
mCameraOffsetPosY = mCameraPreviousPosY - cameraCenterPosY;
mCameraPreviousPosY = cameraCenterPosY;
this.mParallaxScrollValueY += mCameraOffsetPosY * this.mParallaxScrollFactorY;
mCameraOffsetPosY = 0;
}
this.mParallaxValueY += this.mParallaxChangePerSecondY * pSecondsElapsed;
super.onManagedUpdate(pSecondsElapsed);
}
// ===========================================================
// Methods
// ===========================================================
public void attachParallaxEntity(final ParallaxEntity parallaxEntity) {
this.mParallaxEntities.add(parallaxEntity);
this.mParallaxEntityCount++;
}
public boolean detachParallaxEntity(final ParallaxEntity pParallaxEntity) {
this.mParallaxEntityCount--;
final boolean success = this.mParallaxEntities.remove(pParallaxEntity);
if(!success) {
this.mParallaxEntityCount++;
}
return success;
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public static class ParallaxEntity {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
final float mParallaxFactorX;
final float mParallaxFactorY;
final IAreaShape mAreaShape;
final boolean mIsScrollableX;
final boolean mIsScrollableY;
final float shapeScaledX;
final float shapeScaledY;
// ===========================================================
// Constructors
// ===========================================================
/*
public ParallaxEntity(final float pParallaxFactorX,final float pParallaxFactorY, final IAreaShape pAreaShape) {
this.mParallaxFactorX = pParallaxFactorX;
this.mAreaShape = pAreaShape;
this.mIsScrollableX = false;
this.mIsScrollableY = false;
this.mParallaxFactorY = pParallaxFactorY;
shapeScaledX = this.mAreaShape.getWidthScaled() ;
shapeScaledY = this.mAreaShape.getHeightScaled();
}
*/
public ParallaxEntity( final float pParallaxFactorX,final float pParallaxFactorY,final IAreaShape pAreaShape, final boolean mIsScrollableX, final boolean mIsScrollableY) {
this.mParallaxFactorX = pParallaxFactorX;
this.mAreaShape = pAreaShape;
this.mIsScrollableX = mIsScrollableX;
this.mParallaxFactorY = pParallaxFactorY;
this.mIsScrollableY = mIsScrollableY;
this.shapeScaledX = this.mAreaShape.getWidthScaled() ;
this.shapeScaledY = this.mAreaShape.getHeightScaled();
}
public ParallaxEntity(final float pParallaxFactorX,final float pParallaxFactorY, final IAreaShape pAreaShape, final boolean mIsScrollableX,final boolean mIsScrollableY, final int mReduceFrequency) {
this.mParallaxFactorX = pParallaxFactorX;
this.mParallaxFactorY = pParallaxFactorY;
this.mAreaShape = pAreaShape;
this.mIsScrollableX = mIsScrollableX;
this.mIsScrollableY = mIsScrollableY;
this.shapeScaledX = this.mAreaShape.getWidthScaled() * mReduceFrequency ;
this.shapeScaledY = this.mAreaShape.getHeightScaled() * mReduceFrequency;
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
public void onDraw(final GLState pGLState, final Camera pCamera, final float pParallaxValueX, final float pParallaxValueY, final float mLevelScaleX, final float mLevelScaleY) {
pGLState.pushModelViewGLMatrix();
{
float Xrange;
float Yrange;
if(mLevelScaleX != 0){
Xrange = mLevelScaleX;
} else {
Xrange = pCamera.getWidth();
}
if(mLevelScaleY != 0){
Yrange = mLevelScaleY;
} else {
Yrange = pCamera.getHeight();
}
float baseOffsetX = (pParallaxValueX * this.mParallaxFactorX) % shapeScaledX;
float baseOffsetY = (pParallaxValueY * this.mParallaxFactorY) % shapeScaledY;
while(baseOffsetX > 0) {
baseOffsetX -= shapeScaledX;
}
while(baseOffsetY > 0) {
baseOffsetY -= shapeScaledY;
}
pGLState.translateModelViewGLMatrixf(baseOffsetX, baseOffsetY, 0);
float currentMaxDimensionX = baseOffsetX;
float currentMaxDimensionY = baseOffsetY;
do {
this.mAreaShape.onDraw(pGLState, pCamera);
pGLState.translateModelViewGLMatrixf(shapeScaledX - 1, 0, 0);
currentMaxDimensionX += shapeScaledX;
} while(currentMaxDimensionX < Xrange);
do {
this.mAreaShape.onDraw(pGLState, pCamera);
pGLState.translateModelViewGLMatrixf(0, shapeScaledY - 1, 0);
currentMaxDimensionY += shapeScaledY;
} while(currentMaxDimensionY < Yrange);
}
pGLState.popModelViewGLMatrix();
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
}