-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActorBackground.java
215 lines (170 loc) · 7.14 KB
/
ActorBackground.java
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
package biemann.android.miner.screens.map;
import biemann.android.miner.utilities.GFXLoader;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.scenes.scene2d.Actor;
public class ActorBackground extends Actor
{
// Constants
final float FRICTION = 0.01f;
// Member Variables
private Texture mTextureBackground;
private Texture mTextureForeground;
private Sprite mSpriteBackground;
private Sprite mSpriteForeground;
private float mBackLayerHorizontalScrollOffset = 0.0f;
private float mFrontLayerHorizontalScrollOffset = 0.0f;
private float mPlanetLayerHorizontalScrollOffset = 0.0f;
private float mScrollspeed = 0.0f;
private float mScrollspeedBack = 0.0f;//positive will scroll from right to left
private float mScrollspeedFront = 0.0f;//positive will scroll from right to left
private float mScrollspeedPlanets = 0.0f;//positive will scroll from right to left
private float mScreenHeight;
private float mBackgroundWidth;
private float mForegroundWidth;
private float mBackLayerMaxHorizontalUoffset;
private float mBackLayerMaxHorizontalUoffsetOld;
private Color mBackgroundColor;
private BackLayerHorizontalScrollOffsetListener mBackLayerHorizontalScrollOffsetCallback;
public interface BackLayerHorizontalScrollOffsetListener
{
public void onBackLayerHorizontalScrollOffsetChanged(float x);
}
// Constructor
public ActorBackground(GFXLoader gfxloader, int screen_width, int screen_height)
{
//mScreenWidth = screen_width;
mScreenHeight = screen_height;
mBackgroundColor = new Color(0, 0, 0, 1.0f);
// 1) Create background
mTextureBackground = new Texture(gfxloader.loadGraphic("map_background"));
// set wrapping in both directions
mTextureBackground.setWrap(TextureWrap.Repeat, TextureWrap.ClampToEdge);
float fBackgroundScale = 1.0f;
if (screen_height > mTextureBackground.getHeight())
{
// increase image size
fBackgroundScale = screen_height / (float) mTextureBackground.getHeight();
}
else
{
// decrease image size
fBackgroundScale = mTextureBackground.getHeight() / (float) screen_height;
}
// create sprite
mBackgroundWidth = (mTextureBackground.getWidth() * fBackgroundScale);
mSpriteBackground = new Sprite(mTextureBackground, 0, 0, mTextureBackground.getWidth(), mTextureBackground.getHeight());
//mSpriteBackground.setSize(mBackgroundWidth, screen_height); //DO NOT bother with setSize - the image is sized in draw()
// Calculate the maximum offset percent for U that the texture may scroll to so that U2 doesn't go beyond the edge of the texture
mBackLayerMaxHorizontalUoffset = (mBackgroundWidth-screen_width)/mBackgroundWidth;
// 2) Create Foreground layer
mTextureForeground = new Texture(gfxloader.loadGraphic("map_stars"));
// set wrapping in both directions
mTextureForeground.setWrap(TextureWrap.Repeat, TextureWrap.ClampToEdge);
float fForegroundScale = 1.0f;
if (screen_height > mTextureForeground.getHeight())
{
// increase image size
fForegroundScale = screen_height / (float) mTextureForeground.getHeight();
}
else
{
// decrease image size
fForegroundScale = mTextureForeground.getHeight() / (float) screen_height;
}
// create sprite
mForegroundWidth = (mTextureForeground.getWidth() * fForegroundScale);
mSpriteForeground = new Sprite(mTextureForeground, 0, 0, mTextureForeground.getWidth(), mTextureForeground.getHeight());
//mSpriteForeground.setSize(mForegroundWidth, screen_height); //DO NOT bother with setSize - the image is sized in draw()
}
public void setBackLayerHorizontalScrollOffsetListener(BackLayerHorizontalScrollOffsetListener listener)
{
mBackLayerHorizontalScrollOffsetCallback = listener;
}
@Override
public void draw(Batch batch, float parentAlpha)
{
// needed to make actions work
super.act(Gdx.graphics.getDeltaTime());
// needed to make fade work
mBackgroundColor.set(this.getColor().r, this.getColor().g, this.getColor().b, this.getColor().a * parentAlpha);
batch.setColor(mBackgroundColor);
// calculate horizontal texture scroll offset for background
mBackLayerHorizontalScrollOffset += Gdx.graphics.getDeltaTime() * mScrollspeedBack;
if(mBackLayerHorizontalScrollOffset>=mBackLayerMaxHorizontalUoffset)
{
//stop scrolling beyond the right edge
mBackLayerHorizontalScrollOffset = mBackLayerMaxHorizontalUoffset;
setScrollSpeed(0.0f);
}
if(mBackLayerHorizontalScrollOffset<0.0f)
{
//stop scrolling beyond the left edge
mBackLayerHorizontalScrollOffset = 0.0f;
setScrollSpeed(0.0f);
}
mSpriteBackground.setU(mBackLayerHorizontalScrollOffset);
mSpriteBackground.setU2(mBackLayerHorizontalScrollOffset+1.0f);//Texture coordinates go from 0 to 1
// FRONT LAYER: calculate horizontal texture scroll offset
mFrontLayerHorizontalScrollOffset+=Gdx.graphics.getDeltaTime() * mScrollspeedFront;
if(mFrontLayerHorizontalScrollOffset>=1.0f)//Texture coordinates go from 0 to 1
{
mFrontLayerHorizontalScrollOffset = mFrontLayerHorizontalScrollOffset - 1.0f;
}
mSpriteForeground.setU(mFrontLayerHorizontalScrollOffset);
mSpriteForeground.setU2(mFrontLayerHorizontalScrollOffset+1.0f);//Texture coordinates go from 0 to 1
// PLANETS: calculate the horizontal scroll offset for
mPlanetLayerHorizontalScrollOffset = Gdx.graphics.getDeltaTime() * mScrollspeedPlanets;
//Callback for when background scrolls
if ((mBackLayerMaxHorizontalUoffsetOld != mBackLayerHorizontalScrollOffset) && (mBackLayerHorizontalScrollOffsetCallback != null))
{
mBackLayerMaxHorizontalUoffsetOld = mBackLayerHorizontalScrollOffset;
// this will move the planet at the same speed as background !
mBackLayerHorizontalScrollOffsetCallback.onBackLayerHorizontalScrollOffsetChanged(mPlanetLayerHorizontalScrollOffset * mBackgroundWidth);
}
// add friction in either direction
if (mScrollspeed > 0.0001f)//is it positive
{
if (mScrollspeed > FRICTION)
{
mScrollspeed -= FRICTION;
}
else
{
mScrollspeed = 0.0f;
}
}
else if (mScrollspeed < -0.0001f)//is it negative
{
if (mScrollspeed < -FRICTION)
{
mScrollspeed += FRICTION;
}
else
{
mScrollspeed = 0.0f;
}
}
calculateLayerScrollSpeed(mScrollspeed);
// show it
batch.draw(mSpriteBackground, 0, 0, mBackgroundWidth, mScreenHeight);
batch.draw(mSpriteForeground, 0, 0, mForegroundWidth, mScreenHeight);
// if there's a visual glitch, try using between draws :
//batch.setColor(1, 1, 1, 1);
}
public void setScrollSpeed(float scrollSpeed)
{
mScrollspeed = scrollSpeed;
calculateLayerScrollSpeed(scrollSpeed);
}
private void calculateLayerScrollSpeed(float scrollSpeed)
{
mScrollspeedBack = scrollSpeed / 7.0f;
mScrollspeedFront = scrollSpeed / 3.0f;
mScrollspeedPlanets = scrollSpeed / 4.0f;
}
}