Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hobocat.game.Household1;
- /**
- * Created by Nocturlabs-001 on 3/1/2017.
- */
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.InputProcessor;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Animation;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
- import com.badlogic.gdx.math.Circle;
- import com.badlogic.gdx.math.Intersector;
- import com.badlogic.gdx.math.Rectangle;
- import com.badlogic.gdx.scenes.scene2d.Actor;
- import com.badlogic.gdx.scenes.scene2d.Group;
- 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.Image;
- import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
- import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
- import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
- import com.badlogic.gdx.utils.viewport.StretchViewport;
- import com.badlogic.gdx.utils.viewport.Viewport;
- import com.hobocat.game.Difficulty.SelectDiffculty;
- import com.hobocat.game.Menu.MainScreen;
- import com.hobocat.game.MyGdxGame;
- import com.hobocat.game.SelectHouseholds.SelectHouseHold;
- public class IngamedayOne implements Screen ,InputProcessor {
- final MyGdxGame game;
- // Constant rows and columns of the sprite sheet
- private static final int FRAME_COLS = 5, FRAME_ROWS = 1;
- private boolean peripheralAvailable;
- // Objects used
- Animation<TextureRegion> walkAnimation; // Must declare frame type (TextureRegion)
- Texture left_paw,right_paw,progressbar_background,progressbar_knob,pause,meter;
- Texture carpet,desk,plants,square_carpet,pauseImg,pause_cancel,retry,dayselect_img,household_select,back_to_main_menu,basketball;
- SpriteBatch spriteBatch;
- Sprite catsprite;
- private Texture Background1,Background2;
- float yMax, yCoordBg1, yCoordBg2;
- final int BACKGROUND_MOVE_SPEED = -100;
- ImageButton left_paw_btn,right_paw_btn,pause_btnDialog,pause_btn_exit,retry_btn,dayselect_btn,select_household,back_to_main_menu_btn;
- Image pauseImg_text;
- Viewport viewport;
- private Stage stage;
- // A variable for tracking elapsed time for the animation
- float stateTime;
- private TextureRegion myTextureRegion;
- TextureRegion textureRegion;
- private TextureRegionDrawable myTexRegionDrawable;
- private boolean isPause;
- private Group pauseGroup;
- Texture cat,dog;
- private float yPosition = -40;
- //Screen Size
- OrthographicCamera camera;
- float catSpeed = 50.0f; // 10 pixels per second.
- float playerX;
- float playerY;
- ShapeRenderer shapes;
- Rectangle player;
- Circle ball;
- public IngamedayOne(final MyGdxGame game) {
- this.game = game;
- Gdx.input.setCatchBackKey(false);
- Gdx.graphics.setContinuousRendering(false);
- Gdx.graphics.requestRendering();
- stage = new Stage(new StretchViewport( 720, 1280));
- camera = new OrthographicCamera(1280 ,720);
- camera.update();
- camera.setToOrtho(false, 720, 1280);
- camera.translate( 1280/2, 720/2 );
- Gdx.input.setInputProcessor(stage);
- spriteBatch = new SpriteBatch();
- viewport = new StretchViewport(720, 1280);
- Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- // Load the sprite sheet as a texture
- cat = new Texture(Gdx.files.internal("cat.png"));
- catsprite = new Sprite(cat);
- player = new Rectangle();
- player.width = 20;
- player.height = 80;
- playerX = 300;
- playerY = 0;
- basketball = new Texture(Gdx.files.internal("equip/Basketball.png"));
- ball = new Circle();
- ball.x = Gdx.graphics.getWidth() / 2;
- ball.y = Gdx.graphics.getHeight() / 2;
- ball.radius = basketball.getWidth() / 2;
- Gdx.input.setInputProcessor( this);
- peripheralAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer);
- int orientation = Gdx.input.getRotation();
- Input.Orientation nativeOrientation = Gdx.input.getNativeOrientation();
- viewport = new StretchViewport(720, 1280);
- Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- // Progressbar
- progressbar_background = new Texture("progression_map.png");
- progressbar_knob = new Texture("cat_head.png");
- //pause
- pause = new Texture("pause.png");
- myTextureRegion = new TextureRegion(pause);
- myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
- pause_btnDialog = new ImageButton(myTexRegionDrawable); //Set the button up
- pause_btnDialog.setPosition(580,1150);
- stage.addActor(pause_btnDialog); //Add the button to the stage to perform rendering and take input.
- Gdx.input.setInputProcessor(stage);
- pause_btnDialog.addListener(new ChangeListener() {
- public void changed (ChangeEvent event, Actor actor) {
- System.out.println("Pause Button Pressed");
- //Show Pause Screen menu
- pause();
- }
- });
- stage.addActor(pause_btnDialog);
- meter = new Texture("meter.png");
- //background
- Background1 = new Texture(Gdx.files.internal("floor.png"));
- Background2 = Background1;
- yMax = -1270;
- yCoordBg1 = yMax*(-1);
- yCoordBg2 = 0;
- // Use the split utility method to create a 2D array of TextureRegions. This is
- // possible because this sprite sheet contains frames of equal size and they are
- // all aligned.
- TextureRegion[][] tmp = TextureRegion.split(cat, cat.getWidth() / FRAME_COLS, cat.getHeight()/ FRAME_ROWS);
- // Place the regions into a 1D array in the correct order, starting from the top
- // left, going across first. The Animation constructor requires a 1D array.
- TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
- int index = 0;
- for (int i = 0; i < FRAME_ROWS; i++) {
- for (int j = 0; j < FRAME_COLS; j++) {
- walkFrames[index++] = tmp[i][j];
- }
- }
- // Initialize the Animation with the frame interval and array of frames
- walkAnimation = new Animation<TextureRegion>(0.200f, walkFrames);
- // Instantiate a SpriteBatch for drawing and reset the elapsed animation
- // time to 0
- spriteBatch = new SpriteBatch();
- stateTime = 0f;
- //left_control
- left_paw = new Texture(Gdx.files.internal("left_paw.png"));
- myTextureRegion = new TextureRegion(left_paw);
- myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
- left_paw_btn = new ImageButton(myTexRegionDrawable); //Set the button up
- left_paw_btn.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("left_paw.png"))));
- //the hover
- left_paw_btn.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw.png"))));
- left_paw_btn.setPosition(10,25);
- stage.addActor(left_paw_btn); //Add the button to the stage to perform rendering and take input.
- Gdx.input.setInputProcessor(stage);
- left_paw_btn.addListener(new InputListener(){
- @Override
- public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
- System.out.println("Left Button Pressed");
- //Start Animation
- }
- @Override
- public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
- System.out.print("Released");
- return true;
- }
- });
- stage.addActor(left_paw_btn);
- //right_control
- right_paw = new Texture(Gdx.files.internal("right_paw.png"));
- myTextureRegion = new TextureRegion(right_paw);
- myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
- right_paw_btn = new ImageButton(myTexRegionDrawable); //Set the button up
- right_paw_btn.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw.png"))));
- //the hover
- right_paw_btn.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("left_paw.png"))));
- right_paw_btn.setPosition(517,25);
- stage.addActor(right_paw_btn); //Add the button to the stage to perform rendering and take input.
- Gdx.input.setInputProcessor(stage);
- right_paw_btn.addListener(new InputListener(){
- @Override
- public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
- System.out.println("Right Button Pressed");
- //Start Animation
- }
- @Override
- public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
- System.out.print("Released");
- return true;
- }
- });
- stage.addActor(right_paw_btn);
- }
- public enum State
- {
- PAUSE,
- RUN,
- RESUME,
- STOPPED
- }
- private State state = State.RUN;
- @Override
- public void show() {
- }
- @Override
- public void render(float delta) {
- // clear previous frame
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen
- camera.update();
- stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
- yCoordBg1+= BACKGROUND_MOVE_SPEED * Gdx.graphics.getDeltaTime();
- yCoordBg2 = yCoordBg1 + yMax; // We move the background, not the camera
- if (yCoordBg1 <= 0) {
- yCoordBg1 = yMax*(-1);
- yCoordBg2 = 3;
- }
- spriteBatch.begin();
- if(this.state==State.RESUME);
- stateTime += Gdx.graphics.getDeltaTime();
- TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true);
- spriteBatch.setProjectionMatrix(camera.combined);
- spriteBatch.draw(Background1, 0,yCoordBg1);
- spriteBatch.draw(Background2, 0,yCoordBg2);
- spriteBatch.draw(basketball, ball.x-ball.radius, ball.y-ball.radius);
- spriteBatch.draw(currentFrame,playerX, playerY);// Draw current frame at (50, 50)
- spriteBatch.draw(meter,190,990);
- spriteBatch.draw(progressbar_background,20,1170);
- spriteBatch.draw(progressbar_knob,18,1170);
- // update player position
- if(Gdx.input.isKeyPressed(Input.Keys.DOWN))playerY -= 250 * Gdx.graphics.getDeltaTime();
- if(Gdx.input.isKeyPressed(Input.Keys.UP)) playerY += 250 * Gdx.graphics.getDeltaTime();
- if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) playerX -= 250 * Gdx.graphics.getDeltaTime();
- if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) playerX += 250 * Gdx.graphics.getDeltaTime();
- // don't let the player leave the field
- if(playerY < 0) playerY = 0;
- if(playerY > 700 - 80) playerY = 700 - 80;
- // check collision
- if (Intersector.overlaps(ball, player))
- Gdx.app.log("overlaps", "yes");
- //Mobile acceleration
- if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) {
- playerX -= Gdx.input.getAccelerometerX();
- playerY += Gdx.input.getAccelerometerY();
- }
- if (playerY > Gdx.graphics.getHeight() - 100) {
- playerY = Gdx.graphics.getHeight() - 100;
- }
- if (playerX < 0) {
- playerX = 0;
- playerX += Gdx.graphics.getDeltaTime() * catSpeed;
- }
- if (playerX > Gdx.graphics.getHeight() - 250) {
- playerX = Gdx.graphics.getHeight() - 250;
- }
- if(this.state==State.RESUME) {
- switch (state) {
- case RUN:
- //do suff here
- break;
- case PAUSE:
- break;
- case RESUME:
- break;
- default:
- break;
- }
- }
- spriteBatch.end();
- stage.act(); //acting a stage to calculate positions of actors etc
- stage.draw(); //drawing it to render all
- }
- @Override
- public void resize(int width, int height) {
- viewport.update(width, height);
- camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
- }
- @Override
- public void pause() {
- this.state = State.PAUSE;
- pauseGroup = new Group();
- Image semiTransparentBG=new Image(new Texture(Gdx.files.internal("backgroundimage.png")));
- // setSize(Size of screen) and make it semi transparent.
- pauseGroup.addActor(semiTransparentBG);
- //create all other pause UI buttons with listener and add to pauseGroup
- //cancel
- pause_cancel = new Texture(Gdx.files.internal("pausemenu/exit.png"));
- myTextureRegion = new TextureRegion(pause_cancel);
- myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
- pause_btn_exit = new ImageButton(myTexRegionDrawable); //Set the button up
- pause_btn_exit.setPosition(550,1130);
- stage.addActor(pause_btn_exit); //Add the button to the stage to perform rendering and take input.
- Gdx.input.setInputProcessor(stage);
- pause_btn_exit.addListener(new InputListener(){
- @Override
- public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
- System.out.println("Cancel Button Pressed");
- resume();
- }
- @Override
- public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
- return true;
- }
- });
- stage.addActor(pause_btn_exit);
- pauseGroup.addActor(pause_btn_exit);
- pauseImg = new Texture(Gdx.files.internal("pausemenu/pause_text.png"));
- myTextureRegion = new TextureRegion(pauseImg);
- myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
- pauseImg_text = new Image(myTexRegionDrawable); //Set the button up
- pauseImg_text.setPosition(260,970);
- pauseGroup.addActor(pauseImg_text);
- //retry
- retry = new Texture(Gdx.files.internal("pausemenu/retry.png"));
- myTextureRegion = new TextureRegion(retry);
- myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
- retry_btn = new ImageButton(myTexRegionDrawable); //Set the button up
- retry_btn.setPosition(200,700);
- stage.addActor(retry_btn); //Add the button to the stage to perform rendering and take input.
- Gdx.input.setInputProcessor(stage);
- retry_btn.addListener(new InputListener(){
- @Override
- public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
- System.out.println("Button Pressed");
- //resume
- game.setScreen(new IngamedayOne(game));
- }
- @Override
- public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
- return true;
- }
- });
- stage.addActor(retry_btn);
- pauseGroup.addActor(retry_btn);
- //Day Select
- dayselect_img = new Texture(Gdx.files.internal("pausemenu/day_select.png"));
- myTextureRegion = new TextureRegion(dayselect_img);
- myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
- dayselect_btn = new ImageButton(myTexRegionDrawable); //Set the button up
- dayselect_btn.setPosition(180,490);
- stage.addActor(dayselect_btn); //Add the button to the stage to perform rendering and take input.
- Gdx.input.setInputProcessor(stage);
- dayselect_btn.addListener(new InputListener(){
- @Override
- public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
- System.out.println(" Button Pressed");
- game.setScreen(new SelectDiffculty(game));
- }
- @Override
- public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
- return true;
- }
- });
- stage.addActor(dayselect_btn);
- pauseGroup.addActor(dayselect_btn);
- //House Select
- household_select = new Texture(Gdx.files.internal("pausemenu/household_select.png"));
- myTextureRegion = new TextureRegion(household_select);
- myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
- select_household = new ImageButton(myTexRegionDrawable); //Set the button up
- select_household.setPosition(180,300);
- stage.addActor(select_household); //Add the button to the stage to perform rendering and take input.
- Gdx.input.setInputProcessor(stage);
- select_household.addListener(new InputListener(){
- @Override
- public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
- System.out.println(" Button Pressed");
- //resume
- game.setScreen(new SelectHouseHold(game));
- }
- @Override
- public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
- return true;
- }
- });
- stage.addActor(select_household);
- pauseGroup.addActor(select_household);
- //Back to main menu
- back_to_main_menu = new Texture(Gdx.files.internal("pausemenu/back_to_main_menu.png"));
- myTextureRegion = new TextureRegion(back_to_main_menu);
- myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
- back_to_main_menu_btn = new ImageButton(myTexRegionDrawable); //Set the button up
- back_to_main_menu_btn.setPosition(40,80);
- stage.addActor(back_to_main_menu_btn); //Add the button to the stage to perform rendering and take input.
- Gdx.input.setInputProcessor(stage);
- back_to_main_menu_btn.addListener(new InputListener(){
- @Override
- public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
- System.out.println(" Button Pressed");
- //resume
- game.setScreen(new MainScreen(game));
- }
- @Override
- public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
- return true;
- }
- });
- stage.addActor(back_to_main_menu_btn);
- pauseGroup.addActor(back_to_main_menu_btn);
- stage.addActor(pauseGroup);
- }
- @Override
- public void resume() {
- this.state = State.RESUME;
- pauseGroup.remove();
- }
- @Override
- public boolean keyDown(int keycode) {
- return true;
- }
- @Override
- public boolean keyUp(int keycode) {
- return false;
- }
- @Override
- public boolean keyTyped(char character) {
- return false;
- }
- @Override
- public boolean touchDown(int screenX, int screenY, int pointer, int button) {
- return false;
- }
- @Override
- public boolean touchUp(int screenX, int screenY, int pointer, int button) {
- return false;
- }
- @Override
- public boolean touchDragged(int screenX, int screenY, int pointer) {
- return false;
- }
- @Override
- public boolean mouseMoved(int screenX, int screenY) {
- return false;
- }
- @Override
- public boolean scrolled(int amount) {
- return false;
- }
- @Override
- public void hide() {
- }
- @Override
- public void dispose() { // SpriteBatches and Textures must always be disposed
- shapes.dispose();
- spriteBatch.dispose();
- cat.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement