SHARE
TWEET

Untitled

a guest Sep 18th, 2014 146 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class GameDisplay implements Screen, InputProcessor{
  2.         enum State{READY,START,PAUSED,GAMEOVER,NEXTLVL}
  3.  
  4.         private static final int ScreenHeight = 0;;
  5.        
  6.         State state;
  7.        
  8.         static float currentLevel = 1;
  9.         static float baseLvlHeight = 9600;
  10.        
  11.         public static float LVLHEIGHT;
  12.         public static float WIDTH = 900;
  13.         public static float ScreenHEIGHT = 1600;
  14.        
  15.         Stage stage;
  16.         SpriteBatch batch;
  17.         OrthographicCamera cam;
  18.         InputMultiplexer plex;
  19.         FatBoy fb;
  20.         Image imgBg;
  21.         TextureAtlas manAtlas;
  22.         Animation manAni;
  23.         Texture bg,cloud1,cloud2,cloud3,cloud4,platform1;
  24.         Array<Cloud> clouds = new Array<Cloud>();
  25.         public static  Vector2 gravity = new Vector2(0,-2500);
  26.         float cloudRnd;
  27.         public static Man man;
  28.         public static Array<Platform> platforms;
  29.        
  30.         public static Random rand;
  31.         public static float heightSoFar;
  32.         public static ShapeRenderer debugShapes;
  33.         float aniTime;
  34.         static float platformNum ;
  35.        
  36.         public GameDisplay(FatBoy fb){
  37.                 this.fb = fb;
  38.                 state = State.START;
  39.                 stage = new Stage(new FillViewport(WIDTH, ScreenHEIGHT) );
  40.                 cam = new OrthographicCamera();
  41.                 //cam.position.set(WIDTH/2, ScreenHEIGHT/2, 0);
  42.                  
  43.                
  44.                 batch = new SpriteBatch();
  45.                 plex = new InputMultiplexer();
  46.                 plex.addProcessor(this);
  47.                 plex.addProcessor(stage);
  48.                 Gdx.input.setInputProcessor(plex);
  49.                 bg = Assets.manager.get("images/bg.jpg", Texture.class);
  50.                 bg.setFilter(TextureFilter.Linear, TextureFilter.Linear);
  51.                 platform1 = Assets.manager.get("images/Platform_01.png", Texture.class);
  52.                 platform1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
  53.                 cloud1 = Assets.manager.get("images/cloudOne.png", Texture.class);
  54.                 cloud1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
  55.                 cloud2 = Assets.manager.get("images/cloudTwo.png", Texture.class);
  56.                 cloud2.setFilter(TextureFilter.Linear, TextureFilter.Linear);
  57.                 cloud3 = Assets.manager.get("images/cloudThree.png", Texture.class);
  58.                 cloud3.setFilter(TextureFilter.Linear, TextureFilter.Linear);
  59.                 cloud4 = Assets.manager.get("images/cloudFour.png", Texture.class);
  60.                 cloud4.setFilter(TextureFilter.Linear, TextureFilter.Linear);
  61.                
  62.                 manAtlas = Assets.manager.get("animations/fatboy.txt", TextureAtlas.class);
  63.                 manAni = new Animation(0.05f, manAtlas.findRegions("img"));
  64.                 manAni.setPlayMode(PlayMode.LOOP);
  65.                
  66.                 imgBg = new Image(bg);
  67.                 man = new Man(new Vector2(WIDTH/2 -20,0));
  68.                 platforms = new Array<Platform>();
  69.                 rand = new Random();
  70.                   GameDisplay.heightSoFar = 0;
  71.                   LVLHEIGHT = baseLvlHeight * currentLevel;
  72.                   platformNum = LVLHEIGHT/125;
  73.                   genLevel();
  74.                 actors();
  75.                 addClouds();
  76.                 stage.addAction(Actions.alpha(0));
  77.                 stage.addAction(Actions.fadeIn(.5f));
  78.                 debugShapes = new ShapeRenderer();
  79.                
  80.         }
  81.        
  82.         private static void genLevel() {
  83.                
  84.                 float lastY = 300;
  85.                 float rndX;
  86.                 for(int i =0; i< platformNum; i++){
  87.                         new MathUtils();
  88.                        
  89.                         rndX = MathUtils.random(0, 800);
  90.                         platforms.add(new Platform(new Vector2(rndX,lastY)));
  91.                         lastY += 400;
  92.                 }
  93.                 }
  94.        
  95.        
  96.         private void addClouds() {
  97.                 float cloudNum = 5;
  98.                 clouds = new Array<Cloud>();
  99.                 float rnd;
  100.                 float rndy;
  101.                 //rnd= new MathUtils().random(600, 1400);
  102.                 for(int i = 0; i<platformNum; i ++){
  103.                         new MathUtils();
  104.                         cloudRnd = MathUtils.random(0, 10);
  105.                         new MathUtils();
  106.                         rnd= MathUtils.random(900, LVLHEIGHT);
  107.                         rndy= MathUtils.random(0, 820);
  108.                         clouds.add(new Cloud(new Vector2(rndy,rnd)));
  109.                 }
  110.                
  111.         }
  112.  
  113.  
  114.  
  115.         private void actors() {
  116.                 imgBg.setPosition(0, 0);
  117.                 imgBg.setSize(WIDTH, ScreenHEIGHT);
  118.                 stage.addActor(imgBg);
  119.                
  120.         }
  121.  
  122.  
  123.  
  124.         @Override
  125.         public void render(float delta) {
  126.                 Gdx.gl.glClearColor(0, 0, 0, 0);
  127.                 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  128.                 cam.update();
  129.                
  130.                 stage.act(delta);
  131.                 aniTime+=delta;
  132.                 batch.setProjectionMatrix(stage.getCamera().combined);
  133.                
  134.                
  135.                
  136.                 switch (state) {
  137.                 case READY:
  138.                         stage.draw();
  139.                         break;
  140.                 case START:
  141.                         stage.draw();
  142.                         batch.begin();
  143.                         updateObjects(delta);
  144.                         drawObjects();
  145.                         batch.end();
  146.                         break;
  147.                        
  148.                 case NEXTLVL:
  149.                         stage.draw();
  150.                         break;
  151.                                
  152.                 case GAMEOVER:
  153.                         stage.draw();  
  154.                         break;
  155.                 case PAUSED:
  156.                         stage.draw();
  157.                         break;
  158.                 default:
  159.                         break;
  160.                 }
  161.                
  162.                
  163.                 debug();
  164.         }
  165.  
  166.         public void debug() {
  167.                 debugShapes.setProjectionMatrix(cam.combined);
  168.                 debugShapes.begin(ShapeType.Line);
  169.                 debugShapes.setColor(Color.RED);
  170.                
  171.                 for(int i =0; i< platforms.size; i ++){
  172.                 Platform pl = platforms.get(i);
  173.                         debugShapes.rect(pl.position.x, pl.position.y, pl.WIDTH, pl.HEIGHT);
  174.                 }
  175.                 debugShapes.rect(man.position.x, man.position.y, man.Width, man.Height);
  176.                 debugShapes.rect(imgBg.getX(), imgBg.getY(), imgBg.getWidth(), imgBg.getHeight());
  177.                
  178.                 debugShapes.end();
  179.                
  180.         }
  181.  
  182.         private void updateObjects(float delta) {
  183.                 //Update Clouds
  184.                 for(int i = 0; i<clouds.size; i ++){
  185.                        
  186.                         Cloud cl = clouds.get(i);
  187.                        
  188.                         if(cl.cloudDraw<3)batch.draw(cloud1, cl.position.x, cl.position.y);
  189.                         else if(cl.cloudDraw<5)batch.draw(cloud2, cl.position.x, cl.position.y);
  190.                         else if(cl.cloudDraw<8)batch.draw(cloud3, cl.position.x, cl.position.y);
  191.                         else batch.draw(cloud4, cl.position.x, cl.position.y);
  192.                         if(cl.position.x > WIDTH +300){
  193.                                 cl.position.x = -360;
  194.                         }
  195.                         cl.update(delta);
  196.                 }
  197.                
  198.                
  199.                
  200.                 // update Boy
  201.                 if(man.velocity.y >0 ){
  202.                         man.state = man.state.JUMP;
  203.                 }
  204.        
  205.                 if(man.velocity.y < 0 ){
  206.                                 man.state = man.state.FALL;
  207.                 }
  208.                
  209.                 if(man.state == man.state.FALL){
  210.                         for(int i =0; i<platforms.size; i++){
  211.                                 Platform pl = platforms.get(i);
  212.                 //if (OverlapTester.overlapRectangles(man.bounds, pl.bounds)) {
  213.                                 if(man.position.y > pl.position.y){
  214.                                 if(man.bounds.overlaps(pl.bounds)){
  215.                        
  216.                         man.state = man.state.JUMP;
  217.                         man.velocity.y = man.jump_vel;
  218.                         }
  219.                                 }
  220.                 }
  221.                
  222.                 }
  223.                 if(man.position.y <= 0){
  224.                         man.velocity.y = man.jump_vel;
  225.                 }
  226.                    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
  227.                   man.velocity.x += -40;
  228.                 }
  229.                 if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
  230.                         man.velocity.x += 40;
  231.                 }
  232.                 float accelX = Gdx.input.getAccelerometerX();
  233.                man.velocity.x += -accelX / 10 *40;
  234.                if(man.velocity.x>= 400)man.velocity.x = 400;
  235.                if(man.velocity.x<= -400)man.velocity.x = -400;
  236.                if(man.position.y <= cam.position.y )man.state = man.state.HIT;
  237.             if(man.state == man.state.JUMP && man.position.y > ScreenHEIGHT /2){
  238.                         cam.position.y = man.position.y - ScreenHeight/2;
  239.                 }
  240.             if(cam.position.y > 800){
  241.             if(man.position.y <= cam.position.y - 800 )state = state.GAMEOVER;
  242.             }
  243.                 man.update(delta);
  244.                
  245.                
  246.                
  247.                
  248.                
  249.                 //update Platforms
  250.                 for(int i = 0; i < platforms.size; i++){
  251.                         Platform pl = platforms.get(i);
  252.                         if(pl.position.x < 0  ){
  253.                                 pl.velocity.x = 80;
  254.                         }
  255.                         if(pl.position.x > WIDTH - pl.WIDTH ){
  256.                                 pl.velocity.x = -80;
  257.                        
  258.                 }
  259.                        
  260.                         pl.update(delta);
  261.                 }
  262.         }
  263.  
  264.  
  265.  
  266.         private void drawObjects() {
  267.                 for(int i = 0; i < platforms.size; i++){
  268.                         Platform pl = platforms.get(i);
  269.                         batch.draw(platform1, pl.position.x, pl.position.y,pl.WIDTH,pl.HEIGHT);
  270.                 }
  271.                
  272.                 batch.draw(manAni.getKeyFrame(aniTime), man.position.x, man.position.y,man.Width,man.Height);
  273.                
  274.         }
  275.  
  276.  
  277.  
  278.         @Override
  279.         public void resize(int width, int height) {
  280.                 stage.getViewport().update(width, height, true);
  281.                 cam.setToOrtho(false, stage.getViewport().getWorldWidth(), stage.getViewport().getWorldHeight());
  282.                
  283.         }
  284.  
  285.         @Override
  286.         public void show() {
  287.                 // TODO Auto-generated method stub
  288.  
  289.         }
  290.  
  291.         @Override
  292.         public void hide() {
  293.                 // TODO Auto-generated method stub
  294.  
  295.         }
  296.  
  297.         @Override
  298.         public void pause() {
  299.                 // TODO Auto-generated method stub
  300.  
  301.         }
  302.  
  303.         @Override
  304.         public void resume() {
  305.                 // TODO Auto-generated method stub
  306.  
  307.         }
  308.  
  309.         @Override
  310.         public void dispose() {
  311.                 // TODO Auto-generated method stub
  312.  
  313.         }
  314.  
  315.         @Override
  316.         public boolean keyDown(int keycode) {
  317.                
  318.                 return false;
  319.         }
  320.  
  321.         @Override
  322.         public boolean keyUp(int keycode) {
  323.                 // TODO Auto-generated method stub
  324.                 return false;
  325.         }
  326.  
  327.         @Override
  328.         public boolean keyTyped(char character) {
  329.                 // TODO Auto-generated method stub
  330.                 return false;
  331.         }
  332.  
  333.         @Override
  334.         public boolean touchDown(int screenX, int screenY, int pointer, int button) {
  335.                 // TODO Auto-generated method stub
  336.                 return false;
  337.         }
  338.  
  339.         @Override
  340.         public boolean touchUp(int screenX, int screenY, int pointer, int button) {
  341.                 // TODO Auto-generated method stub
  342.                 return false;
  343.         }
  344.  
  345.         @Override
  346.         public boolean touchDragged(int screenX, int screenY, int pointer) {
  347.                 // TODO Auto-generated method stub
  348.                 return false;
  349.         }
  350.  
  351.         @Override
  352.         public boolean mouseMoved(int screenX, int screenY) {
  353.                 // TODO Auto-generated method stub
  354.                 return false;
  355.         }
  356.  
  357.         @Override
  358.         public boolean scrolled(int amount) {
  359.                 // TODO Auto-generated method stub
  360.                 return false;
  361.         }
  362.  
  363. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top