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
|
package ch.asynk.tankontank.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.MathUtils;
import ch.asynk.tankontank.TankOnTank;
public class GameScreen extends AbstractScreen
{
static private final int MOVE_STEP = 6;
static private final float ZOOM_STEP = 0.04f;
static private final float ZOOM_FACTOR = 5.0f;
private float maxZoom;
private OrthographicCamera cam;
private FitViewport viewport;
private SpriteBatch batch;
private BitmapFont font;
private Sprite mapSprite;
private int touchX;
private int touchY;
public GameScreen(final TankOnTank game)
{
super(game);
batch = new SpriteBatch();
font = new BitmapFont();
final Texture mapTexture = game.manager.get("images/map_a.png", Texture.class);
mapSprite = new Sprite(mapTexture);
mapSprite.setPosition(0, 0);
mapSprite.setSize(mapTexture.getWidth(), mapTexture.getHeight());
cam = new OrthographicCamera();
cam.position.set((mapSprite.getWidth()/2), (mapSprite.getHeight()/2), 0);
cam.zoom = 2f;
viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), cam);
Gdx.input.setInputProcessor(getMultiplexer());
}
private InputMultiplexer getMultiplexer()
{
final InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(new GestureDetector(new GestureAdapter() {
@Override
public boolean zoom(float initialDistance, float distance)
{
cam.zoom += ((initialDistance - distance) / 300.f);
cam.zoom = MathUtils.clamp(cam.zoom, 0.4f, maxZoom);
return true;
}
}));
multiplexer.addProcessor(new InputAdapter() {
@Override
public boolean touchDragged(int x, int y, int pointer)
{
cam.translate((touchX - x) * cam.zoom, (y - touchY) * cam.zoom, 0);
touchX = x;
touchY = y;
return true;
}
@Override
public boolean touchDown(int x, int y, int pointer, int button)
{
if (button == Input.Buttons.LEFT) {
touchX = x;
touchY = y;
}
return true;
}
@Override
public boolean scrolled(int amount)
{
cam.zoom += amount / ZOOM_FACTOR;
cam.zoom = MathUtils.clamp(cam.zoom, 0.4f, maxZoom);
return true;
}
});
return multiplexer;
}
@Override
public void render(float delta)
{
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// handleInput();
float width = cam.viewportWidth * cam.zoom / 2f;
float height = cam.viewportHeight * cam.zoom / 2f;
cam.position.x = MathUtils.clamp(cam.position.x, width, (mapSprite.getWidth() - width));
cam.position.y = MathUtils.clamp(cam.position.y, height, (mapSprite.getHeight() - height));
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
mapSprite.draw(batch);
font.draw(batch, "fps: " + Gdx.graphics.getFramesPerSecond(), 20, 30);
batch.end();
if(Gdx.input.isTouched()) {
// TODO
}
}
@Override
public void resize(int width, int height)
{
Gdx.app.debug("GameScreen", "resize (" + width + "," + height + ")");
viewport.update(width, height);
maxZoom = Math.min((mapSprite.getWidth() / cam.viewportWidth), (mapSprite.getHeight() / cam.viewportHeight));
}
@Override
public void dispose()
{
Gdx.app.debug("GameScreen", "dispose()");
batch.dispose();
font.dispose();
}
@Override
public void show()
{
Gdx.app.debug("GameScreen", "show()");
}
@Override
public void hide()
{
Gdx.app.debug("GameScreen", "hide()");
}
@Override
public void pause()
{
Gdx.app.debug("GameScreen", "pause()");
}
@Override
public void resume()
{
Gdx.app.debug("GameScreen", "resume()");
}
}
|