Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hobocat.game.Household1;
- 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.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;
- private static final float ACCELERATION = 20f;
- // Objects used
- Animation<TextureRegion> walkAnimation; // Must declare frame type (TextureRegion)
- Texture cat ,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;
- SpriteBatch spriteBatch;
- Sprite catsprite,sprite_shoes,sprite_plant;
- Sprite bgsprite,bgsprite2;
- private Texture Background,Background2;
- 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;
- int srcy;
- private Rectangle rectangleCat;
- private Rectangle rectangleShoes;
- private float yPosition = -40;
- int posYBck1=0,posYBck2=0;
- //Screen Size
- OrthographicCamera camera;
- float catSpeed = 50.0f; // 10 pixels per second.
- float catX;
- float catY;
- public IngamedayOne(final MyGdxGame game) {
- this.game = game;
- Gdx.input.setCatchBackKey(true);
- Gdx.graphics.setContinuousRendering(false);
- Gdx.graphics.requestRendering();
- stage = new Stage(new StretchViewport( 720, 1280));
- camera = new OrthographicCamera();
- 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);
- catX=300;
- catY=0;
- 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());
- //Display Items
- carpet = new Texture("equip/carpet2.png");
- desk = new Texture("equip/Desk.png");
- square_carpet = new Texture("equip/Carpet.png");
- plants = new Texture("equip/Plants.png");
- sprite_plant = new Sprite(plants);
- sprite_shoes = new Sprite(new Texture("equip/Shoes.png"));
- sprite_shoes.setPosition(260,580);
- rectangleShoes=new Rectangle(sprite_shoes.getX(),sprite_shoes.getY(),sprite_shoes.getWidth(),sprite_shoes.getHeight());
- rectangleCat = new Rectangle(catsprite.getX(),catsprite.getY(),catsprite.getWidth(),catsprite.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
- Background = new Texture(Gdx.files.internal("floor.png")); //File from assets folder
- Background.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
- bgsprite = new Sprite(Background);
- bgsprite.setPosition(0,0);
- Background2 = new Texture(Gdx.files.internal("floor.png")); //File from assets folder
- Background2.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
- bgsprite2 = new Sprite(Background2);
- bgsprite2.setPosition(0,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.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) {
- 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.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
- stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
- camera.update();
- }
- @Override
- public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
- 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
- stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
- spriteBatch.begin();
- boolean isOverlaping = rectangleCat.overlaps(rectangleShoes);
- if(!isOverlaping) {
- System.out.println("not overlap");
- yPosition = yPosition + (20 * delta);
- }
- if(this.state==State.RESUME);
- stateTime += Gdx.graphics.getDeltaTime();
- TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true);
- spriteBatch.setProjectionMatrix(camera.combined);
- // spriteBatch.draw(square_carpet,150,2,408,800);
- //spriteBatch.draw(carpet,230,980,250,260);
- bgsprite.draw(spriteBatch);
- sprite_shoes.draw(spriteBatch);
- spriteBatch.draw(currentFrame,catX,catY); // Draw current frame at (50, 50)
- spriteBatch.draw(desk,10,1150,160,260);//spriteBatch.draw(plants,500,700,200,260);
- camera.update();
- spriteBatch.draw(meter,190,990);
- spriteBatch.draw(progressbar_background,20,1170);
- spriteBatch.draw(progressbar_knob,18,1170);
- //Moving player on desktop
- if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
- catX -= Gdx.graphics.getDeltaTime() * catSpeed;
- if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
- catX += Gdx.graphics.getDeltaTime() * catSpeed;
- if (Gdx.input.isKeyPressed(Input.Keys.UP))
- catY += Gdx.graphics.getDeltaTime() * catSpeed;
- if (Gdx.input.isKeyPressed(Input.Keys.DOWN))
- catY -= Gdx.graphics.getDeltaTime() * catSpeed;
- //Mobile acceleration
- if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) {
- catX -= Gdx.input.getAccelerometerX();
- catY += Gdx.input.getAccelerometerY();
- }
- if (catX < 0) {
- catX = 0;
- }
- if (catX > Gdx.graphics.getHeight() - 250) {
- catX = 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
- spriteBatch.dispose();
- cat.dispose();
- left_paw.dispose();
- right_paw.dispose();
- stage.dispose();
- Background.dispose();
- progressbar_background.dispose();
- progressbar_knob.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement