Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ua.com.vendetta8247.libgdxtest;
- import com.badlogic.gdx.ApplicationAdapter;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.InputProcessor;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.g2d.TextureAtlas;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.scenes.scene2d.Stage;
- import com.badlogic.gdx.scenes.scene2d.ui.Skin;
- import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
- import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
- import com.badlogic.gdx.utils.Timer;
- import com.badlogic.gdx.utils.viewport.ExtendViewport;
- import com.badlogic.gdx.utils.viewport.FillViewport;
- import com.badlogic.gdx.utils.viewport.ScreenViewport;
- import com.badlogic.gdx.utils.viewport.StretchViewport;
- import com.badlogic.gdx.utils.viewport.Viewport;
- public class MyGdxGame extends ApplicationAdapter {
- SpriteBatch batch;
- Sprite characterSprite;
- float clearRed = 1, clearGreen = 1, clearBlue = 1;
- boolean movingRight = true;
- float velocityX = 3, velocityY = 20;
- private int currentFrame = 0;
- private TextureAtlas textureAtlas;
- private String currentAtlasKey = new String("0001");
- long startTime = 0;
- long animTime = 0;
- float multiplier = 0.3f;
- Texture background;
- Sprite bgSprite[];
- Sprite bgSprite2;
- private Stage stage;
- private Touchpad touchpad;
- private Touchpad.TouchpadStyle touchpadStyle;
- private Skin touchpadSkin;
- private Drawable touchBackground;
- private Drawable touchKnob;
- private BitmapFont font;
- private Viewport viewport;
- OrthographicCamera camera;
- @Override
- public void create() {
- Gdx.graphics.setVSync(true);
- camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- viewport = new ExtendViewport(1920, 1080, camera);
- viewport.apply();
- viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- batch = new SpriteBatch();
- bgSprite2 = new Sprite(new Texture("bg2.png"));
- bgSprite2.setPosition(0, 0);
- bgSprite2.setSize(bgSprite2.getWidth() * 2, bgSprite2.getHeight() * 2);
- background = new Texture("bg.png");
- bgSprite = new Sprite[2];
- bgSprite[0] = new Sprite(background);
- bgSprite[1] = new Sprite(background);
- bgSprite[0].setSize(bgSprite[0].getWidth() * 2, bgSprite[0].getHeight() * 2);
- bgSprite[1].setSize(bgSprite[1].getWidth() * 2, bgSprite[1].getHeight() * 2);
- bgSprite[0].setPosition(0, -200);
- bgSprite[1].setPosition(bgSprite[0].getX() + bgSprite[0].getWidth(), -200);
- camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
- camera.position.set(0, 0, 0);
- camera.update();
- font = new BitmapFont();
- font.setColor(Color.RED);
- touchpadSkin = new Skin();
- touchpadSkin.add("touchBackground", new Texture("touchBackground.png"));
- touchpadSkin.add("touchKnob", new Texture("touchKnob.png"));
- touchpadStyle = new Touchpad.TouchpadStyle();
- touchBackground = touchpadSkin.getDrawable("touchBackground");
- touchKnob = touchpadSkin.getDrawable("touchKnob");
- touchpadStyle.background = touchBackground;
- touchpadStyle.knob = touchKnob;
- touchpad = new Touchpad(10, touchpadStyle);
- touchpad.setBounds(50, 50, 300, 300);
- stage = new Stage(viewport, batch);
- stage.addActor(touchpad);
- Gdx.input.setInputProcessor(stage);
- startTime = System.currentTimeMillis();
- textureAtlas = new TextureAtlas(Gdx.files.internal("ready.atlas"));
- TextureAtlas.AtlasRegion region = textureAtlas.findRegion("1");
- characterSprite = new Sprite(region);
- characterSprite.setPosition(100, 100);
- characterSprite.setSize(characterSprite.getWidth() * 3, characterSprite.getHeight() * 3);
- Timer.schedule(new Timer.Task() {
- @Override
- public void run() {
- if (System.currentTimeMillis() - startTime > 80) {
- currentFrame++;
- startTime = System.currentTimeMillis();
- }
- if (currentFrame > 7)
- currentFrame = 0;
- currentAtlasKey = String.format("%d", currentFrame);
- characterSprite.setRegion(textureAtlas.findRegion(currentAtlasKey));
- //characterSprite.setPosition(characterSprite.getX() + velocity,characterSprite.getY());
- //TODO collision
- // if(characterSprite.getX() + characterSprite.getWidth()>=Gdx.graphics.getWidth())
- // characterSprite.setPosition(Gdx.graphics.getWidth() - characterSprite.getWidth() + 2, characterSprite.getY());
- }
- }, 0f, 1 / 60f);
- }
- @Override
- public void render() {
- Gdx.gl.glClearColor(85.0f / 255.0f, 200 / 255.0f, 240.0f / 255, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- update();
- draw();
- }
- public void draw() {
- batch.setProjectionMatrix(camera.combined);
- batch.begin();
- bgSprite2.draw(batch);
- //touchpad.draw(batch,1.0f);
- bgSprite[0].draw(batch);
- bgSprite[1].draw(batch);
- characterSprite.draw(batch);
- batch.end();
- }
- public void update() {
- System.out.println(bgSprite[0].getX());
- camera.update();
- if (characterSprite.getX() > bgSprite[1].getX() + bgSprite[1].getWidth() + 100) {
- bgSprite[1].setPosition(bgSprite[0].getX() + bgSprite[0].getWidth(), -200);
- }
- if (characterSprite.getX() > bgSprite[0].getX() + bgSprite[0].getWidth() + 100) {
- bgSprite[0].setPosition(bgSprite[1].getX() + bgSprite[0].getWidth(), -200);
- }
- if (Gdx.input.isTouched()) {
- if (Gdx.input.getX() < Gdx.graphics.getWidth() / 2) {
- velocityX = 15;
- } else
- multiplier = 0.6f;
- } else {
- velocityX = 5;
- multiplier = 0.3f;
- }
- if (System.currentTimeMillis() - animTime >= 1000 / 60) {
- bgSprite2.translate(velocityX, 0);
- //camera.translate(velocityX, 0);
- camera.position.x += velocityX;
- velocityY -= multiplier;
- animTime = System.currentTimeMillis();
- if (characterSprite.getY() < 5)
- velocityY = 20;
- characterSprite.setPosition(characterSprite.getX() + velocityX, characterSprite.getY() + velocityY);
- }
- //characterSprite.setPosition(characterSprite.getX() + velocityX, characterSprite.getY()+velocityY);
- //
- // if (!movingRight)
- // characterSprite.flip(true, false);
- //
- // if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
- // if(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT))
- // characterSprite.translateX(-1f);
- // else
- // characterSprite.translateX(-2.0f);
- // movingRight = false;
- // }
- // if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
- // if(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT))
- // characterSprite.translateX(1f);
- // else
- // characterSprite.translateX(2.0f);
- // movingRight = true;
- // }
- //
- //
- //
- // characterSprite.setX(characterSprite.getX() + touchpad.getKnobPercentX()*5);
- // if(touchpad.getKnobPercentX()<0)
- // movingRight = false;
- // else if(touchpad.getKnobPercentX()>0) movingRight = true;
- // //characterSprite.setY(characterSprite.getY() + touchpad.getKnobPercentY()*5);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement