Advertisement
Guest User

Untitled

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