Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.51 KB | None | 0 0
  1. import com.badlogic.gdx.Game;
  2. import com.badlogic.gdx.Gdx;
  3. import com.badlogic.gdx.Input.Keys;
  4. import com.badlogic.gdx.graphics.Color;
  5. import com.badlogic.gdx.graphics.GL20;
  6. import com.badlogic.gdx.graphics.Texture;
  7. import com.badlogic.gdx.math.Rectangle;
  8. import com.badlogic.gdx.scenes.scene2d.Stage;
  9. import com.badlogic.gdx.scenes.scene2d.Action;
  10. import com.badlogic.gdx.scenes.scene2d.actions.Actions;
  11. import com.badlogic.gdx.graphics.Texture.TextureFilter;
  12. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  13. import com.badlogic.gdx.graphics.g2d.Animation;
  14. import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
  15. import com.badlogic.gdx.utils.Array;
  16.  
  17. import com.badlogic.gdx.scenes.scene2d.ui.Label;
  18. import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
  19. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  20. import com.badlogic.gdx.graphics.Camera;
  21. import com.badlogic.gdx.math.MathUtils;
  22. import java.util.ArrayList;
  23.  
  24. public class CheesePlease5 extends Game
  25. {
  26.     private Stage mainStage;
  27.     private Stage uiStage;
  28.  
  29.     private AnimatedActor mousey;
  30.     private BaseActor cheese0;
  31.     private BaseActor cheese1;
  32.     private BaseActor cheese2;
  33.     private ArrayList<BaseActor> cheeseList;
  34.     private BaseActor trap;
  35.     private BaseActor floor;
  36.     private BaseActor winText;
  37.     private BaseActor loseText;
  38.     private boolean win;
  39.     private boolean lose;
  40.    
  41.     private Rectangle cheese0Rectangle;
  42.     private Rectangle cheese1Rectangle;
  43.     private Rectangle cheese2Rectangle;
  44.     private Rectangle trapRectangle;
  45.     private Rectangle mouseyRectangle;
  46.  
  47.     private float timeElapsed;
  48.     private Label timeLabel;
  49.     private Label cheeseLabel;
  50.  
  51.     // game world dimensions
  52.     final int mapWidth = 800;
  53.     final int mapHeight = 800;
  54.     // window dimensions
  55.     final int viewWidth = 640;
  56.     final int viewHeight = 480;
  57.  
  58.     public void create()
  59.     {        
  60.         mainStage = new Stage();
  61.         uiStage = new Stage();
  62.         timeElapsed = 0;
  63.  
  64.         floor = new BaseActor();
  65.         floor.setTexture( new Texture(Gdx.files.internal("assets/tiles-800-800.jpg")) );
  66.         floor.setPosition( 0, 0 );
  67.         mainStage.addActor( floor );
  68.        
  69.         mousey = new AnimatedActor();
  70.  
  71.         TextureRegion[] frames = new TextureRegion[4];
  72.         for (int n = 0; n < 4; n++)
  73.         {  
  74.             String fileName = "assets/mouse" + n + ".png";
  75.             Texture tex = new Texture(Gdx.files.internal(fileName));
  76.             tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
  77.             frames[n] = new TextureRegion( tex );
  78.         }
  79.         Array<TextureRegion> framesArray = new Array<TextureRegion>(frames);
  80.  
  81.         Animation anim = new Animation(0.1f, framesArray, Animation.PlayMode.LOOP_PINGPONG);
  82.  
  83.         mousey.setAnimation( anim );
  84.         mousey.setOrigin( mousey.getWidth()/2, mousey.getHeight()/2 );
  85.         mousey.setPosition( 20, 20 );
  86.         mouseyRectangle = mousey.getBoundingRectangle();
  87.        
  88.         cheeseList = new ArrayList<BaseActor>();
  89.  
  90.         cheese0 = new BaseActor();
  91.         cheese0.setTexture( new Texture(Gdx.files.internal("assets/cheese.png")) );
  92.         cheese0.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
  93.         cheese0.setOrigin( cheese0.getWidth()/2, cheese0.getHeight()/2 );
  94.         cheese0Rectangle = cheese0.getBoundingRectangle();
  95.         while (cheese0Rectangle.overlaps(mouseyRectangle)) {
  96.             cheese0.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
  97.         }
  98.         mainStage.addActor( cheese0 );
  99.         cheeseList.add( cheese0 );
  100.        
  101.         cheese1 = new BaseActor();
  102.         cheese1.setTexture( new Texture(Gdx.files.internal("assets/cheese.png")) );
  103.         cheese1.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
  104.         cheese1.setOrigin( cheese1.getWidth()/2, cheese1.getHeight()/2 );
  105.         cheese1Rectangle = cheese1.getBoundingRectangle();
  106.         while(cheese1Rectangle.overlaps(mouseyRectangle) || cheese1Rectangle.overlaps(cheese0Rectangle)) {
  107.             cheese1.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
  108.         }
  109.         mainStage.addActor( cheese1 );
  110.         cheeseList.add( cheese1 );
  111.        
  112.         cheese2 = new BaseActor();
  113.         cheese2.setTexture( new Texture(Gdx.files.internal("assets/cheese.png")) );
  114.         cheese2.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
  115.         cheese2.setOrigin( cheese2.getWidth()/2, cheese2.getHeight()/2 );
  116.         cheese2Rectangle = cheese2.getBoundingRectangle();
  117.         while(cheese2Rectangle.overlaps(mouseyRectangle) || cheese2Rectangle.overlaps(cheese0Rectangle)
  118.         || cheese2Rectangle.overlaps(cheese1Rectangle)) {
  119.             cheese2.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
  120.         }
  121.         mainStage.addActor( cheese2 );
  122.         cheeseList.add( cheese2 );
  123.        
  124.         trap = new BaseActor();
  125.         trap.setTexture( new Texture(Gdx.files.internal("assets/mousetrap.jpg")) );
  126.         trap.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
  127.         trap.setOrigin( trap.getWidth()/2, trap.getHeight()/2 );
  128.         trapRectangle = trap.getBoundingRectangle();
  129.         /*
  130.         while(trapRectangle.overlaps(mouseyRectangle)
  131.         || trapRectangle.overlaps(cheese0Rectangle)
  132.         || trapRectangle.overlaps(cheese1Rectangle)
  133.         || trapRectangle.overlaps(cheese2Rectangle)) {
  134.             trap.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
  135.         }
  136.         */
  137.         mainStage.addActor( trap );
  138.        
  139.         mainStage.addActor(mousey);
  140.  
  141.         winText = new BaseActor();
  142.         winText.setTexture( new Texture(Gdx.files.internal("assets/you-win.png")) );
  143.         winText.setPosition( 170, 60 );
  144.         winText.setVisible( false );
  145.         uiStage.addActor( winText );
  146.        
  147.         loseText = new BaseActor();
  148.         loseText.setTexture( new Texture(Gdx.files.internal("assets/you-lose.png")) );
  149.         loseText.setPosition( 130, -200 );
  150.         loseText.setVisible( false );
  151.         uiStage.addActor( loseText );
  152.  
  153.         BitmapFont font = new BitmapFont();
  154.         String text = "Time: 0";
  155.         String text2 = "Cheese: 3";
  156.         LabelStyle style = new LabelStyle( font, Color.NAVY );
  157.         timeLabel = new Label( text, style );
  158.         timeLabel.setFontScale(2);
  159.         timeLabel.setPosition(500,440); // sets bottom left (baseline) corner?
  160.         uiStage.addActor( timeLabel );
  161.         cheeseLabel = new Label( text2, style );
  162.         cheeseLabel.setFontScale(2);
  163.         cheeseLabel.setPosition(500,410);
  164.         uiStage.addActor( cheeseLabel );
  165.  
  166.         win = false;
  167.         lose = false;
  168.     }
  169.  
  170.     public void render()
  171.     {  
  172.         // process input
  173.         mousey.velocityX = 0;
  174.         mousey.velocityY = 0;
  175.  
  176.         if (Gdx.input.isKeyPressed(Keys.LEFT))
  177.             mousey.velocityX -= 100;
  178.         if (Gdx.input.isKeyPressed(Keys.RIGHT))
  179.             mousey.velocityX += 100;;
  180.         if (Gdx.input.isKeyPressed(Keys.UP))
  181.             mousey.velocityY += 100;
  182.         if (Gdx.input.isKeyPressed(Keys.DOWN))
  183.             mousey.velocityY -= 100;
  184.  
  185.         // update
  186.         float dt = Gdx.graphics.getDeltaTime();
  187.  
  188.         mainStage.act(dt);
  189.         uiStage.act(dt);
  190.  
  191.         // bound mousey to the rectangle defined by mapWidth, mapHeight
  192.         mousey.setX( MathUtils.clamp( mousey.getX(), 0,  mapWidth - mousey.getWidth() ));
  193.         mousey.setY( MathUtils.clamp( mousey.getY(), 0,  mapHeight - mousey.getHeight() ));
  194.  
  195.         // check win condition: mousey must be overlapping cheese
  196.        
  197.         //Rectangle cheese0Rectangle = cheese0.getBoundingRectangle();
  198.         Rectangle mouseyRectangle = mousey.getBoundingRectangle();
  199.        
  200.         Action spinShrinkFadeOut = Actions.parallel(
  201.                 Actions.alpha(1),         // set transparency value
  202.                 Actions.rotateBy(360, 1), // rotation amount, duration
  203.                 Actions.scaleTo(0,0, 2),  // x amount, y amount, duration
  204.                 Actions.fadeOut(1)        // duration of fade in
  205.             );
  206.  
  207.         if ( !win && cheese0Rectangle.contains( mouseyRectangle ) )
  208.         {
  209.             cheese0.addAction( spinShrinkFadeOut );
  210.             cheeseList.remove( cheese0 );
  211.             cheeseLabel.setText( "Cheese: " + cheeseList.size() );
  212.         }
  213.        
  214.         if ( !win && cheese1Rectangle.contains( mouseyRectangle ) )
  215.         {
  216.             cheese1.addAction( spinShrinkFadeOut );
  217.             cheeseList.remove( cheese1 );
  218.             cheeseLabel.setText( "Cheese: " + cheeseList.size() );
  219.         }
  220.        
  221.         if ( !win && cheese2Rectangle.contains( mouseyRectangle ) )
  222.         {
  223.             cheese2.addAction( spinShrinkFadeOut );
  224.             cheeseList.remove( cheese2 );
  225.             cheeseLabel.setText( "Cheese: " + cheeseList.size() );
  226.         }
  227.        
  228.         if ( !win && cheeseList.size() == 0)
  229.         {
  230.             win = true;
  231.            
  232.             Action fadeInColorCycleForever = Actions.sequence(
  233.                 Actions.alpha(0),   // set transparency value
  234.                 Actions.show(),     // set visible to true
  235.                 Actions.fadeIn(2),  // duration of fade out
  236.                 Actions.forever(    
  237.                     Actions.sequence(
  238.                         // color shade to approach, duration
  239.                         Actions.color( new Color(1,0,0,1), 1 ),
  240.                         Actions.color( new Color(0,0,1,1), 1 )
  241.                     )
  242.                 )
  243.             );
  244.            
  245.             winText.addAction( fadeInColorCycleForever );
  246.         }
  247.        
  248.         if ( !lose && trapRectangle.contains( mouseyRectangle ) )
  249.         {
  250.             lose = true;
  251.  
  252.             mousey.addAction( spinShrinkFadeOut );
  253.  
  254.             Action fadeInColorCycleForever = Actions.sequence(
  255.                     Actions.alpha(0),   // set transparency value
  256.                     Actions.show(),     // set visible to true
  257.                     Actions.fadeIn(2),  // duration of fade out
  258.                     Actions.forever(    
  259.                         Actions.sequence(
  260.                             // color shade to approach, duration
  261.                             Actions.color( new Color(1,0,0,1), 1 ),
  262.                             Actions.color( new Color(0,0,1,1), 1 )
  263.                         )
  264.                     )
  265.                 );
  266.  
  267.             Action moveFromBottom = Actions.sequence(
  268.                     Actions.show(),
  269.                     Actions.moveTo(130, 60, 2)
  270.                 );
  271.  
  272.             loseText.addAction( fadeInColorCycleForever );
  273.             loseText.addAction( moveFromBottom );
  274.         }
  275.  
  276.         if (!win)
  277.         {
  278.             timeElapsed += dt;
  279.             timeLabel.setText( "Time: " + (int)timeElapsed );
  280.         }
  281.         // draw graphics
  282.         Gdx.gl.glClearColor(0.8f, 0.8f, 1, 1);
  283.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  284.  
  285.         // camera adjustment
  286.         Camera cam = mainStage.getCamera();
  287.  
  288.         // center camera on player
  289.         cam.position.set( mousey.getX() + mousey.getOriginX(),
  290.             mousey.getY() + mousey.getOriginY(), 0 );
  291.  
  292.         // bound camera to layout
  293.         cam.position.x = MathUtils.clamp(cam.position.x, viewWidth/2,  mapWidth - viewWidth/2);
  294.         cam.position.y = MathUtils.clamp(cam.position.y, viewHeight/2, mapHeight - viewHeight/2);
  295.         cam.update();
  296.  
  297.         mainStage.draw();
  298.         uiStage.draw();
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement