summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/screens/GameScreen.java
blob: b3477417eb88403a3eda0e5324adda0a2f1349ab (plain)
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
package ch.asynk.tankontank.screens;

import com.badlogic.gdx.Gdx;

import com.badlogic.gdx.Input;
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.Camera;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;

import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;

import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

import ch.asynk.tankontank.TankOnTank;
import ch.asynk.tankontank.actors.Pawn;
import ch.asynk.tankontank.actors.HexMap;

public class GameScreen extends AbstractScreen
{
    static private final float ZOOM_MAX = 0.2f;
    static private final float ZOOM_GESTURE_FACTOR = .01f;
    static private final float ZOOM_SCROLL_FACTOR = .1f;

    private float maxZoomOut;
    final OrthographicCamera cam;

    private HexMap map;
    private Image selectedHex;
    private Label fps;

    private Stage hud;
    private Stage gameStage;

    private Vector2 screenToViewport = new Vector2();       // ratio
    private Vector3 touchPos = new Vector3();               // world coordinates
    private Vector2 dragPos = new Vector2();                // screen coordinates

    private Pawn draggedPawn = null;
    private GridPoint2 cell = new GridPoint2(-1, -1);    // current map cell

    public GameScreen(final TankOnTank game)
    {
        super(game);

        fps = new Label("FPS: 0", game.skin);
        fps.setPosition( 10, Gdx.graphics.getHeight() - 40);

        map = new HexMap(11, 9, game.manager.get("images/map_a.png", Texture.class));
        selectedHex = new Image(game.manager.get("images/hex.png", Texture.class));
        selectedHex.setVisible(false);

        cam = new OrthographicCamera();
        cam.setToOrtho(false);
        // cam.position.set((map.getWidth()/2), (map.getHeight()/2), 0);

        gameStage = new Stage(new FitViewport(map.getWidth(), map.getHeight(), cam));
        gameStage.addActor(map);
        gameStage.addActor(selectedHex);

        hud = new Stage(new ScreenViewport());
        hud.addActor(fps);

        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)
            {
                if (initialDistance > distance)
                    cam.zoom += ZOOM_GESTURE_FACTOR;
                else
                    cam.zoom -= ZOOM_GESTURE_FACTOR;
                cam.zoom = MathUtils.clamp(cam.zoom, ZOOM_MAX, maxZoomOut);
                clampCameraPos();
                return true;
            }
        }));
        multiplexer.addProcessor(new InputAdapter() {
            @Override
            public boolean touchDragged(int x, int y, int pointer)
            {
                float deltaX = ((x - dragPos.x) * cam.zoom * screenToViewport.x);
                float deltaY = ((dragPos.y - y) * cam.zoom * screenToViewport.y);
                dragPos.set(x, y);
                if (draggedPawn == null) {
                    cam.translate(-deltaX, -deltaY, 0);
                    clampCameraPos();
                } else {
                    draggedPawn.moveBy(deltaX, deltaY);
                    cam.unproject(touchPos.set(x, y, 0));
                    map.getCellAt(cell, touchPos.x, touchPos.y);
                    map.setImageCenterAt(selectedHex, cell);

                }
                return true;
            }
            @Override
            public boolean touchDown(int x, int y, int pointer, int button)
            {
                if (button == Input.Buttons.LEFT) {
                    dragPos.set(x, y);
                    cam.unproject(touchPos.set(x, y, 0));
                    map.getCellAt(cell, touchPos.x, touchPos.y);
                    draggedPawn = map.getTopPawnAt(cell);
                    if (draggedPawn != null) draggedPawn.setZIndex(Pawn.DRAGGED_Z_INDEX);
                    map.setImageCenterAt(selectedHex, cell);
                    selectedHex.setVisible(true);
                }
                return true;
            }
            @Override
            public boolean touchUp(int x, int y, int pointer, int button)
            {
                if (button == Input.Buttons.LEFT) {
                    cam.unproject(touchPos.set(x, y, 0));
                    if (draggedPawn != null) {
                        map.getCellAt(cell, touchPos.x, touchPos.y);
                        draggedPawn.moveTo(cell);
                    }
                    selectedHex.setVisible(false);
                }
                return true;
            }
            @Override
            public boolean scrolled(int amount)
            {
                cam.zoom += amount * ZOOM_SCROLL_FACTOR;
                cam.zoom = MathUtils.clamp(cam.zoom, ZOOM_MAX, maxZoomOut);
                clampCameraPos();
                return true;
            }
        });

        return multiplexer;
    }

    private void clampCameraPos()
    {
        float cx = cam.viewportWidth * cam.zoom / 2f;
        float cy = cam.viewportHeight * cam.zoom / 2f;
        cam.position.x = MathUtils.clamp(cam.position.x, cx, (map.getWidth() - cx));
        cam.position.y = MathUtils.clamp(cam.position.y, cy, (map.getHeight() - cy));
    }

    @Override
    public void render(float delta)
    {
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        cam.update();

        fps.setText("FPS: " + Gdx.graphics.getFramesPerSecond());

        gameStage.act(delta);
        gameStage.draw();

        hud.act(delta);
        hud.draw();
    }

    @Override
    public void resize(int width, int height)
    {
        Gdx.app.debug("GameScreen", "resize (" + width + "," + height + ")");
        hud.getViewport().update(width, height, true);
        gameStage.getViewport().update(width, height);
        maxZoomOut = Math.min((map.getWidth() / cam.viewportWidth), (map.getHeight() / cam.viewportHeight));
        cam.zoom = MathUtils.clamp(cam.zoom, ZOOM_MAX, maxZoomOut);
        screenToViewport.set((cam.viewportWidth / width), (cam.viewportHeight / height));
    }

    @Override
    public void dispose()
    {
        Gdx.app.debug("GameScreen", "dispose()");
        hud.dispose();
        gameStage.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()");
    }
}