com.theosirian.ppioo.Puno.java Source code

Java tutorial

Introduction

Here is the source code for com.theosirian.ppioo.Puno.java

Source

/*
 * The MIT License (MIT)
 * Copyright (c) 2015-2016, theosirian
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.theosirian.ppioo;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.theosirian.ppioo.controllers.Controller;
import com.theosirian.ppioo.controllers.MenuController;
import com.theosirian.ppioo.controllers.SoundController;
import com.theosirian.ppioo.util.GUI;
import com.theosirian.ppioo.util.GameData;
import com.theosirian.ppioo.util.ScreenshotFactory;

import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;

public class Puno extends ApplicationAdapter {
    public Skin skin;
    public Stage stage;
    public GameData data;
    public Class<? extends Controller> changeController;

    private SpriteBatch batch;
    private Controller controller;
    private Controller nextController;
    private SoundController soundController;
    private boolean pic;

    @Override
    public void create() {
        data = new GameData().load();

        soundController = new SoundController();
        soundController.loadSounds();

        batch = new SpriteBatch();
        stage = new Stage(new ScreenViewport(), batch);
        stage.addListener(new InputListener() {
            @Override
            public boolean scrolled(InputEvent event, float x, float y, int amount) {
                if (amount > 0) {
                    soundController.setVolume(soundController.getVolume() - soundController.getRate());
                } else {
                    soundController.setVolume(soundController.getVolume() + soundController.getRate());
                }
                return true;
            }
        });
        skin = new Skin();
        Gdx.input.setInputProcessor(stage);

        GUI.configureSkin(skin);

        nextController = null;
        controller = new MenuController(this);
        controller.start();
        pic = true;
    }

    @Override
    public void render() {
        // Changing Controllers
        if (changeController != null) {
            try {
                nextController = changeController.getConstructor(this.getClass()).newInstance(this);
                changeController = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (nextController != null) {
                nextController.start();
                nextController.rootLayout.addAction(
                        sequence(moveBy(Gdx.graphics.getWidth(), 0, 0), moveBy(-Gdx.graphics.getWidth(), 0, .75f)));
                controller.rootLayout
                        .addAction(sequence(moveBy(-controller.rootLayout.getWidth(), 0, .75f), run(new Runnable() {
                            @Override
                            public void run() {
                                pic = true;
                                controller.dispose();
                                controller = nextController;
                                nextController = null;
                            }
                        })));
            }
        }
        // Variables
        float deltaTime = Gdx.graphics.getDeltaTime();
        // Update
        controller.update(deltaTime);
        // Draw
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        controller.draw(batch);
        if (nextController != null) {
            nextController.draw(batch);
        }
        if (pic) {
            ScreenshotFactory.saveScreenshot();
            pic = false;
        }
    }

    @Override
    public void dispose() {
        skin.dispose();
        stage.dispose();
    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height, true);
        controller.invalidate();
        if (nextController != null) {
            nextController.invalidate();
        }
    }

    @Override
    public void pause() {
        super.pause();
    }

    @Override
    public void resume() {
        super.resume();
    }

    public SoundController getSoundController() {
        return soundController;
    }
}