Advertisement
JazzSarawayun

dayOne

Mar 16th, 2017
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.14 KB | None | 0 0
  1. package com.hobocat.game.Household1;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Input;
  5. import com.badlogic.gdx.InputProcessor;
  6. import com.badlogic.gdx.Screen;
  7. import com.badlogic.gdx.graphics.GL20;
  8. import com.badlogic.gdx.graphics.OrthographicCamera;
  9. import com.badlogic.gdx.graphics.Texture;
  10. import com.badlogic.gdx.graphics.g2d.Animation;
  11. import com.badlogic.gdx.graphics.g2d.Sprite;
  12. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  13. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  14. import com.badlogic.gdx.math.Rectangle;
  15. import com.badlogic.gdx.scenes.scene2d.Actor;
  16. import com.badlogic.gdx.scenes.scene2d.Group;
  17. import com.badlogic.gdx.scenes.scene2d.InputEvent;
  18. import com.badlogic.gdx.scenes.scene2d.InputListener;
  19. import com.badlogic.gdx.scenes.scene2d.Stage;
  20. import com.badlogic.gdx.scenes.scene2d.ui.Image;
  21. import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
  22. import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
  23. import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
  24. import com.badlogic.gdx.utils.viewport.StretchViewport;
  25. import com.badlogic.gdx.utils.viewport.Viewport;
  26. import com.hobocat.game.Difficulty.SelectDiffculty;
  27. import com.hobocat.game.Menu.MainScreen;
  28. import com.hobocat.game.MyGdxGame;
  29. import com.hobocat.game.SelectHouseholds.SelectHouseHold;
  30.  
  31. public class IngamedayOne implements Screen ,InputProcessor {
  32. final MyGdxGame game;
  33. // Constant rows and columns of the sprite sheet
  34. private static final int FRAME_COLS = 5, FRAME_ROWS = 1;
  35. private boolean peripheralAvailable;
  36. private static final float ACCELERATION = 20f;
  37.  
  38. // Objects used
  39. Animation<TextureRegion> walkAnimation; // Must declare frame type (TextureRegion)
  40. Texture cat ,left_paw,right_paw,progressbar_background,progressbar_knob,pause,meter;
  41. Texture carpet,desk,plants,square_carpet,pauseImg,pause_cancel,retry,dayselect_img,household_select,back_to_main_menu;
  42. SpriteBatch spriteBatch;
  43. Sprite catsprite,sprite_shoes,sprite_plant;
  44. Sprite bgsprite,bgsprite2;
  45. private Texture Background,Background2;
  46. ImageButton left_paw_btn,right_paw_btn,pause_btnDialog,pause_btn_exit,retry_btn,dayselect_btn,select_household,back_to_main_menu_btn;
  47. Image pauseImg_text;
  48. Viewport viewport;
  49. private Stage stage;
  50. // A variable for tracking elapsed time for the animation
  51. float stateTime;
  52. private TextureRegion myTextureRegion;
  53. TextureRegion textureRegion;
  54. private TextureRegionDrawable myTexRegionDrawable;
  55.  
  56. private boolean isPause;
  57. private Group pauseGroup;
  58. int srcy;
  59. private Rectangle rectangleCat;
  60. private Rectangle rectangleShoes;
  61.  
  62. private float yPosition = -40;
  63. int posYBck1=0,posYBck2=0;
  64. //Screen Size
  65. OrthographicCamera camera;
  66. float catSpeed = 50.0f; // 10 pixels per second.
  67. float catX;
  68. float catY;
  69.  
  70.  
  71. public IngamedayOne(final MyGdxGame game) {
  72. this.game = game;
  73. Gdx.input.setCatchBackKey(true);
  74. Gdx.graphics.setContinuousRendering(false);
  75. Gdx.graphics.requestRendering();
  76. stage = new Stage(new StretchViewport( 720, 1280));
  77. camera = new OrthographicCamera();
  78. camera.setToOrtho(false, 720, 1280);
  79. camera.translate( 1280/2, 720/2 );
  80. Gdx.input.setInputProcessor(stage);
  81. spriteBatch = new SpriteBatch();
  82. viewport = new StretchViewport(720, 1280);
  83. Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  84. // Load the sprite sheet as a texture
  85.  
  86. cat = new Texture(Gdx.files.internal("cat.png"));
  87. catsprite = new Sprite(cat);
  88. catX=300;
  89. catY=0;
  90.  
  91. Gdx.input.setInputProcessor( this);
  92. peripheralAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer);
  93. int orientation = Gdx.input.getRotation();
  94. Input.Orientation nativeOrientation = Gdx.input.getNativeOrientation();
  95.  
  96.  
  97. viewport = new StretchViewport(720, 1280);
  98. Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  99. //Display Items
  100. carpet = new Texture("equip/carpet2.png");
  101. desk = new Texture("equip/Desk.png");
  102.  
  103. square_carpet = new Texture("equip/Carpet.png");
  104.  
  105. plants = new Texture("equip/Plants.png");
  106. sprite_plant = new Sprite(plants);
  107.  
  108. sprite_shoes = new Sprite(new Texture("equip/Shoes.png"));
  109. sprite_shoes.setPosition(260,580);
  110.  
  111. rectangleShoes=new Rectangle(sprite_shoes.getX(),sprite_shoes.getY(),sprite_shoes.getWidth(),sprite_shoes.getHeight());
  112. rectangleCat = new Rectangle(catsprite.getX(),catsprite.getY(),catsprite.getWidth(),catsprite.getHeight());
  113.  
  114.  
  115. // Progressbar
  116. progressbar_background = new Texture("progression_map.png");
  117. progressbar_knob = new Texture("cat_head.png");
  118.  
  119. //pause
  120. pause = new Texture("pause.png");
  121. myTextureRegion = new TextureRegion(pause);
  122. myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
  123. pause_btnDialog = new ImageButton(myTexRegionDrawable); //Set the button up
  124. pause_btnDialog.setPosition(580,1150);
  125.  
  126. stage.addActor(pause_btnDialog); //Add the button to the stage to perform rendering and take input.
  127. Gdx.input.setInputProcessor(stage);
  128. pause_btnDialog.addListener(new ChangeListener() {
  129. public void changed (ChangeEvent event, Actor actor) {
  130. System.out.println("Pause Button Pressed");
  131. //Show Pause Screen menu
  132. pause();
  133. }
  134. });
  135. stage.addActor(pause_btnDialog);
  136.  
  137. meter = new Texture("meter.png");
  138. //background
  139. Background = new Texture(Gdx.files.internal("floor.png")); //File from assets folder
  140. Background.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
  141. bgsprite = new Sprite(Background);
  142. bgsprite.setPosition(0,0);
  143.  
  144. Background2 = new Texture(Gdx.files.internal("floor.png")); //File from assets folder
  145. Background2.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
  146. bgsprite2 = new Sprite(Background2);
  147. bgsprite2.setPosition(0,0);
  148.  
  149.  
  150.  
  151.  
  152.  
  153. // Use the split utility method to create a 2D array of TextureRegions. This is
  154. // possible because this sprite sheet contains frames of equal size and they are
  155. // all aligned.
  156. TextureRegion[][] tmp = TextureRegion.split(cat, cat.getWidth() / FRAME_COLS, cat.getHeight()/ FRAME_ROWS);
  157. // Place the regions into a 1D array in the correct order, starting from the top
  158. // left, going across first. The Animation constructor requires a 1D array.
  159. TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
  160. int index = 0;
  161. for (int i = 0; i < FRAME_ROWS; i++) {
  162. for (int j = 0; j < FRAME_COLS; j++) {
  163. walkFrames[index++] = tmp[i][j];
  164. }
  165. }
  166. // Initialize the Animation with the frame interval and array of frames
  167. walkAnimation = new Animation<TextureRegion>(0.200f, walkFrames);
  168. // Instantiate a SpriteBatch for drawing and reset the elapsed animation
  169. // time to 0
  170. spriteBatch = new SpriteBatch();
  171. stateTime = 0f;
  172.  
  173. //left_control
  174. left_paw = new Texture(Gdx.files.internal("left_paw.png"));
  175. myTextureRegion = new TextureRegion(left_paw);
  176. myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
  177. left_paw_btn = new ImageButton(myTexRegionDrawable); //Set the button up
  178. left_paw_btn.setPosition(10,25);
  179.  
  180. stage.addActor(left_paw_btn); //Add the button to the stage to perform rendering and take input.
  181. Gdx.input.setInputProcessor(stage);
  182. left_paw_btn.addListener(new InputListener(){
  183. @Override
  184. public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
  185. System.out.println("Left Button Pressed");
  186. //Start Animation
  187.  
  188.  
  189. }
  190. @Override
  191. public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
  192. return true;
  193. }
  194. });
  195. stage.addActor(left_paw_btn);
  196.  
  197. //right_control
  198. right_paw = new Texture(Gdx.files.internal("right_paw.png"));
  199. myTextureRegion = new TextureRegion(right_paw);
  200. myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
  201. right_paw_btn = new ImageButton(myTexRegionDrawable); //Set the button up
  202. right_paw_btn.setPosition(517,25);
  203.  
  204. stage.addActor(right_paw_btn); //Add the button to the stage to perform rendering and take input.
  205. Gdx.input.setInputProcessor(stage);
  206. right_paw_btn.addListener(new InputListener(){
  207. @Override
  208. public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
  209. System.out.println("Right Button Pressed");
  210. //Start Animation
  211. stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
  212. camera.update();
  213.  
  214. }
  215. @Override
  216. public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
  217. return true;
  218. }
  219. });
  220. stage.addActor(right_paw_btn);
  221. }
  222.  
  223.  
  224.  
  225. public enum State
  226. {
  227. PAUSE,
  228. RUN,
  229. RESUME,
  230. STOPPED
  231. }
  232. private State state = State.RUN;
  233.  
  234. @Override
  235. public void show() {
  236.  
  237. }
  238.  
  239. @Override
  240. public void render(float delta) {
  241. // clear previous frame
  242. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen
  243. stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
  244. spriteBatch.begin();
  245. boolean isOverlaping = rectangleCat.overlaps(rectangleShoes);
  246. if(!isOverlaping) {
  247. System.out.println("not overlap");
  248. yPosition = yPosition + (20 * delta);
  249.  
  250. }
  251. if(this.state==State.RESUME);
  252.  
  253. stateTime += Gdx.graphics.getDeltaTime();
  254.  
  255.  
  256. TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true);
  257. spriteBatch.setProjectionMatrix(camera.combined);
  258. // spriteBatch.draw(square_carpet,150,2,408,800);
  259. //spriteBatch.draw(carpet,230,980,250,260);
  260.  
  261.  
  262. bgsprite.draw(spriteBatch);
  263.  
  264. sprite_shoes.draw(spriteBatch);
  265. spriteBatch.draw(currentFrame,catX,catY); // Draw current frame at (50, 50)
  266. spriteBatch.draw(desk,10,1150,160,260);//spriteBatch.draw(plants,500,700,200,260);
  267.  
  268. camera.update();
  269. spriteBatch.draw(meter,190,990);
  270. spriteBatch.draw(progressbar_background,20,1170);
  271. spriteBatch.draw(progressbar_knob,18,1170);
  272.  
  273. //Moving player on desktop
  274. if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
  275. catX -= Gdx.graphics.getDeltaTime() * catSpeed;
  276. if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
  277. catX += Gdx.graphics.getDeltaTime() * catSpeed;
  278.  
  279. if (Gdx.input.isKeyPressed(Input.Keys.UP))
  280. catY += Gdx.graphics.getDeltaTime() * catSpeed;
  281. if (Gdx.input.isKeyPressed(Input.Keys.DOWN))
  282. catY -= Gdx.graphics.getDeltaTime() * catSpeed;
  283.  
  284. //Mobile acceleration
  285. if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) {
  286. catX -= Gdx.input.getAccelerometerX();
  287. catY += Gdx.input.getAccelerometerY();
  288. }
  289.  
  290. if (catX < 0) {
  291. catX = 0;
  292. }
  293. if (catX > Gdx.graphics.getHeight() - 250) {
  294. catX = Gdx.graphics.getHeight() - 250;
  295. }
  296. if(this.state==State.RESUME) {
  297. switch (state) {
  298. case RUN:
  299. //do suff here
  300. break;
  301. case PAUSE:
  302. break;
  303. case RESUME:
  304. break;
  305.  
  306. default:
  307. break;
  308. }
  309. }
  310.  
  311.  
  312. spriteBatch.end();
  313. stage.act(); //acting a stage to calculate positions of actors etc
  314. stage.draw(); //drawing it to render all
  315. }
  316. @Override
  317. public void resize(int width, int height) {
  318. viewport.update(width, height);
  319. camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
  320.  
  321. }
  322. @Override
  323. public void pause() {
  324. this.state = State.PAUSE;
  325. pauseGroup = new Group();
  326. Image semiTransparentBG=new Image(new Texture(Gdx.files.internal("backgroundimage.png")));
  327. // setSize(Size of screen) and make it semi transparent.
  328. pauseGroup.addActor(semiTransparentBG);
  329. //create all other pause UI buttons with listener and add to pauseGroup
  330. //cancel
  331. pause_cancel = new Texture(Gdx.files.internal("pausemenu/exit.png"));
  332. myTextureRegion = new TextureRegion(pause_cancel);
  333. myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
  334. pause_btn_exit = new ImageButton(myTexRegionDrawable); //Set the button up
  335. pause_btn_exit.setPosition(550,1130);
  336.  
  337. stage.addActor(pause_btn_exit); //Add the button to the stage to perform rendering and take input.
  338. Gdx.input.setInputProcessor(stage);
  339. pause_btn_exit.addListener(new InputListener(){
  340. @Override
  341. public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
  342. System.out.println("Cancel Button Pressed");
  343. resume();
  344. }
  345. @Override
  346. public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
  347. return true;
  348. }
  349. });
  350. stage.addActor(pause_btn_exit);
  351. pauseGroup.addActor(pause_btn_exit);
  352.  
  353. pauseImg = new Texture(Gdx.files.internal("pausemenu/pause_text.png"));
  354. myTextureRegion = new TextureRegion(pauseImg);
  355. myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
  356. pauseImg_text = new Image(myTexRegionDrawable); //Set the button up
  357. pauseImg_text.setPosition(260,970);
  358. pauseGroup.addActor(pauseImg_text);
  359.  
  360. //retry
  361. retry = new Texture(Gdx.files.internal("pausemenu/retry.png"));
  362. myTextureRegion = new TextureRegion(retry);
  363. myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
  364. retry_btn = new ImageButton(myTexRegionDrawable); //Set the button up
  365. retry_btn.setPosition(200,700);
  366.  
  367. stage.addActor(retry_btn); //Add the button to the stage to perform rendering and take input.
  368. Gdx.input.setInputProcessor(stage);
  369. retry_btn.addListener(new InputListener(){
  370. @Override
  371. public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
  372. System.out.println("Button Pressed");
  373. //resume
  374. game.setScreen(new IngamedayOne(game));
  375. }
  376. @Override
  377. public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
  378. return true;
  379. }
  380. });
  381. stage.addActor(retry_btn);
  382. pauseGroup.addActor(retry_btn);
  383. //Day Select
  384. dayselect_img = new Texture(Gdx.files.internal("pausemenu/day_select.png"));
  385. myTextureRegion = new TextureRegion(dayselect_img);
  386. myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
  387. dayselect_btn = new ImageButton(myTexRegionDrawable); //Set the button up
  388. dayselect_btn.setPosition(180,490);
  389. stage.addActor(dayselect_btn); //Add the button to the stage to perform rendering and take input.
  390. Gdx.input.setInputProcessor(stage);
  391. dayselect_btn.addListener(new InputListener(){
  392. @Override
  393. public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
  394. System.out.println(" Button Pressed");
  395. game.setScreen(new SelectDiffculty(game));
  396. }
  397. @Override
  398. public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
  399. return true;
  400. }
  401. });
  402. stage.addActor(dayselect_btn);
  403. pauseGroup.addActor(dayselect_btn);
  404. //House Select
  405. household_select = new Texture(Gdx.files.internal("pausemenu/household_select.png"));
  406. myTextureRegion = new TextureRegion(household_select);
  407. myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
  408. select_household = new ImageButton(myTexRegionDrawable); //Set the button up
  409. select_household.setPosition(180,300);
  410.  
  411. stage.addActor(select_household); //Add the button to the stage to perform rendering and take input.
  412. Gdx.input.setInputProcessor(stage);
  413. select_household.addListener(new InputListener(){
  414. @Override
  415. public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
  416. System.out.println(" Button Pressed");
  417. //resume
  418. game.setScreen(new SelectHouseHold(game));
  419.  
  420. }
  421. @Override
  422. public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
  423. return true;
  424. }
  425. });
  426. stage.addActor(select_household);
  427. pauseGroup.addActor(select_household);
  428. //Back to main menu
  429. back_to_main_menu = new Texture(Gdx.files.internal("pausemenu/back_to_main_menu.png"));
  430. myTextureRegion = new TextureRegion(back_to_main_menu);
  431. myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
  432. back_to_main_menu_btn = new ImageButton(myTexRegionDrawable); //Set the button up
  433. back_to_main_menu_btn.setPosition(40,80);
  434. stage.addActor(back_to_main_menu_btn); //Add the button to the stage to perform rendering and take input.
  435. Gdx.input.setInputProcessor(stage);
  436. back_to_main_menu_btn.addListener(new InputListener(){
  437. @Override
  438. public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
  439. System.out.println(" Button Pressed");
  440. //resume
  441. game.setScreen(new MainScreen(game));
  442. }
  443. @Override
  444. public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
  445. return true;
  446. }
  447. });
  448. stage.addActor(back_to_main_menu_btn);
  449. pauseGroup.addActor(back_to_main_menu_btn);
  450. stage.addActor(pauseGroup);
  451. }
  452.  
  453. @Override
  454. public void resume() {
  455. this.state = State.RESUME;
  456. pauseGroup.remove();
  457.  
  458. }
  459. @Override
  460. public boolean keyDown(int keycode) {
  461.  
  462. return true;
  463. }
  464. @Override
  465. public boolean keyUp(int keycode) {
  466. return false;
  467. }
  468. @Override
  469. public boolean keyTyped(char character) {
  470. return false;
  471. }
  472. @Override
  473. public boolean touchDown(int screenX, int screenY, int pointer, int button) {
  474.  
  475. return false;
  476. }
  477.  
  478. @Override
  479. public boolean touchUp(int screenX, int screenY, int pointer, int button) {
  480. return false;
  481. }
  482.  
  483. @Override
  484. public boolean touchDragged(int screenX, int screenY, int pointer) {
  485. return false;
  486. }
  487.  
  488. @Override
  489. public boolean mouseMoved(int screenX, int screenY) {
  490. return false;
  491. }
  492.  
  493. @Override
  494. public boolean scrolled(int amount) {
  495. return false;
  496. }
  497. @Override
  498. public void hide() {
  499.  
  500. }
  501.  
  502. @Override
  503. public void dispose() { // SpriteBatches and Textures must always be disposed
  504. spriteBatch.dispose();
  505. cat.dispose();
  506. left_paw.dispose();
  507. right_paw.dispose();
  508. stage.dispose();
  509. Background.dispose();
  510. progressbar_background.dispose();
  511. progressbar_knob.dispose();
  512.  
  513. }
  514. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement