Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.badlogic.gdx.Game;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.math.Rectangle;
- import com.badlogic.gdx.scenes.scene2d.Stage;
- import com.badlogic.gdx.scenes.scene2d.Action;
- import com.badlogic.gdx.scenes.scene2d.actions.Actions;
- import com.badlogic.gdx.graphics.Texture.TextureFilter;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- import com.badlogic.gdx.graphics.g2d.Animation;
- import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
- import com.badlogic.gdx.utils.Array;
- import com.badlogic.gdx.scenes.scene2d.ui.Label;
- import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.Camera;
- import com.badlogic.gdx.math.MathUtils;
- import java.util.ArrayList;
- public class CheesePlease5 extends Game
- {
- private Stage mainStage;
- private Stage uiStage;
- private AnimatedActor mousey;
- private BaseActor cheese0;
- private BaseActor cheese1;
- private BaseActor cheese2;
- private ArrayList<BaseActor> cheeseList;
- private BaseActor trap;
- private BaseActor floor;
- private BaseActor winText;
- private BaseActor loseText;
- private boolean win;
- private boolean lose;
- private Rectangle cheese0Rectangle;
- private Rectangle cheese1Rectangle;
- private Rectangle cheese2Rectangle;
- private Rectangle trapRectangle;
- private Rectangle mouseyRectangle;
- private float timeElapsed;
- private Label timeLabel;
- private Label cheeseLabel;
- // game world dimensions
- final int mapWidth = 800;
- final int mapHeight = 800;
- // window dimensions
- final int viewWidth = 640;
- final int viewHeight = 480;
- public void create()
- {
- mainStage = new Stage();
- uiStage = new Stage();
- timeElapsed = 0;
- floor = new BaseActor();
- floor.setTexture( new Texture(Gdx.files.internal("assets/tiles-800-800.jpg")) );
- floor.setPosition( 0, 0 );
- mainStage.addActor( floor );
- mousey = new AnimatedActor();
- TextureRegion[] frames = new TextureRegion[4];
- for (int n = 0; n < 4; n++)
- {
- String fileName = "assets/mouse" + n + ".png";
- Texture tex = new Texture(Gdx.files.internal(fileName));
- tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
- frames[n] = new TextureRegion( tex );
- }
- Array<TextureRegion> framesArray = new Array<TextureRegion>(frames);
- Animation anim = new Animation(0.1f, framesArray, Animation.PlayMode.LOOP_PINGPONG);
- mousey.setAnimation( anim );
- mousey.setOrigin( mousey.getWidth()/2, mousey.getHeight()/2 );
- mousey.setPosition( 20, 20 );
- mouseyRectangle = mousey.getBoundingRectangle();
- cheeseList = new ArrayList<BaseActor>();
- cheese0 = new BaseActor();
- cheese0.setTexture( new Texture(Gdx.files.internal("assets/cheese.png")) );
- cheese0.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
- cheese0.setOrigin( cheese0.getWidth()/2, cheese0.getHeight()/2 );
- cheese0Rectangle = cheese0.getBoundingRectangle();
- while (cheese0Rectangle.overlaps(mouseyRectangle)) {
- cheese0.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
- }
- mainStage.addActor( cheese0 );
- cheeseList.add( cheese0 );
- cheese1 = new BaseActor();
- cheese1.setTexture( new Texture(Gdx.files.internal("assets/cheese.png")) );
- cheese1.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
- cheese1.setOrigin( cheese1.getWidth()/2, cheese1.getHeight()/2 );
- cheese1Rectangle = cheese1.getBoundingRectangle();
- while(cheese1Rectangle.overlaps(mouseyRectangle) || cheese1Rectangle.overlaps(cheese0Rectangle)) {
- cheese1.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
- }
- mainStage.addActor( cheese1 );
- cheeseList.add( cheese1 );
- cheese2 = new BaseActor();
- cheese2.setTexture( new Texture(Gdx.files.internal("assets/cheese.png")) );
- cheese2.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
- cheese2.setOrigin( cheese2.getWidth()/2, cheese2.getHeight()/2 );
- cheese2Rectangle = cheese2.getBoundingRectangle();
- while(cheese2Rectangle.overlaps(mouseyRectangle) || cheese2Rectangle.overlaps(cheese0Rectangle)
- || cheese2Rectangle.overlaps(cheese1Rectangle)) {
- cheese2.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
- }
- mainStage.addActor( cheese2 );
- cheeseList.add( cheese2 );
- trap = new BaseActor();
- trap.setTexture( new Texture(Gdx.files.internal("assets/mousetrap.jpg")) );
- trap.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
- trap.setOrigin( trap.getWidth()/2, trap.getHeight()/2 );
- trapRectangle = trap.getBoundingRectangle();
- /*
- while(trapRectangle.overlaps(mouseyRectangle)
- || trapRectangle.overlaps(cheese0Rectangle)
- || trapRectangle.overlaps(cheese1Rectangle)
- || trapRectangle.overlaps(cheese2Rectangle)) {
- trap.setPosition( (int) (Math.random() * 700), (int) (Math.random() * 700 ));
- }
- */
- mainStage.addActor( trap );
- mainStage.addActor(mousey);
- winText = new BaseActor();
- winText.setTexture( new Texture(Gdx.files.internal("assets/you-win.png")) );
- winText.setPosition( 170, 60 );
- winText.setVisible( false );
- uiStage.addActor( winText );
- loseText = new BaseActor();
- loseText.setTexture( new Texture(Gdx.files.internal("assets/you-lose.png")) );
- loseText.setPosition( 130, -200 );
- loseText.setVisible( false );
- uiStage.addActor( loseText );
- BitmapFont font = new BitmapFont();
- String text = "Time: 0";
- String text2 = "Cheese: 3";
- LabelStyle style = new LabelStyle( font, Color.NAVY );
- timeLabel = new Label( text, style );
- timeLabel.setFontScale(2);
- timeLabel.setPosition(500,440); // sets bottom left (baseline) corner?
- uiStage.addActor( timeLabel );
- cheeseLabel = new Label( text2, style );
- cheeseLabel.setFontScale(2);
- cheeseLabel.setPosition(500,410);
- uiStage.addActor( cheeseLabel );
- win = false;
- lose = false;
- }
- public void render()
- {
- // process input
- mousey.velocityX = 0;
- mousey.velocityY = 0;
- if (Gdx.input.isKeyPressed(Keys.LEFT))
- mousey.velocityX -= 100;
- if (Gdx.input.isKeyPressed(Keys.RIGHT))
- mousey.velocityX += 100;;
- if (Gdx.input.isKeyPressed(Keys.UP))
- mousey.velocityY += 100;
- if (Gdx.input.isKeyPressed(Keys.DOWN))
- mousey.velocityY -= 100;
- // update
- float dt = Gdx.graphics.getDeltaTime();
- mainStage.act(dt);
- uiStage.act(dt);
- // bound mousey to the rectangle defined by mapWidth, mapHeight
- mousey.setX( MathUtils.clamp( mousey.getX(), 0, mapWidth - mousey.getWidth() ));
- mousey.setY( MathUtils.clamp( mousey.getY(), 0, mapHeight - mousey.getHeight() ));
- // check win condition: mousey must be overlapping cheese
- //Rectangle cheese0Rectangle = cheese0.getBoundingRectangle();
- Rectangle mouseyRectangle = mousey.getBoundingRectangle();
- Action spinShrinkFadeOut = Actions.parallel(
- Actions.alpha(1), // set transparency value
- Actions.rotateBy(360, 1), // rotation amount, duration
- Actions.scaleTo(0,0, 2), // x amount, y amount, duration
- Actions.fadeOut(1) // duration of fade in
- );
- if ( !win && cheese0Rectangle.contains( mouseyRectangle ) )
- {
- cheese0.addAction( spinShrinkFadeOut );
- cheeseList.remove( cheese0 );
- cheeseLabel.setText( "Cheese: " + cheeseList.size() );
- }
- if ( !win && cheese1Rectangle.contains( mouseyRectangle ) )
- {
- cheese1.addAction( spinShrinkFadeOut );
- cheeseList.remove( cheese1 );
- cheeseLabel.setText( "Cheese: " + cheeseList.size() );
- }
- if ( !win && cheese2Rectangle.contains( mouseyRectangle ) )
- {
- cheese2.addAction( spinShrinkFadeOut );
- cheeseList.remove( cheese2 );
- cheeseLabel.setText( "Cheese: " + cheeseList.size() );
- }
- if ( !win && cheeseList.size() == 0)
- {
- win = true;
- Action fadeInColorCycleForever = Actions.sequence(
- Actions.alpha(0), // set transparency value
- Actions.show(), // set visible to true
- Actions.fadeIn(2), // duration of fade out
- Actions.forever(
- Actions.sequence(
- // color shade to approach, duration
- Actions.color( new Color(1,0,0,1), 1 ),
- Actions.color( new Color(0,0,1,1), 1 )
- )
- )
- );
- winText.addAction( fadeInColorCycleForever );
- }
- if ( !lose && trapRectangle.contains( mouseyRectangle ) )
- {
- lose = true;
- mousey.addAction( spinShrinkFadeOut );
- Action fadeInColorCycleForever = Actions.sequence(
- Actions.alpha(0), // set transparency value
- Actions.show(), // set visible to true
- Actions.fadeIn(2), // duration of fade out
- Actions.forever(
- Actions.sequence(
- // color shade to approach, duration
- Actions.color( new Color(1,0,0,1), 1 ),
- Actions.color( new Color(0,0,1,1), 1 )
- )
- )
- );
- Action moveFromBottom = Actions.sequence(
- Actions.show(),
- Actions.moveTo(130, 60, 2)
- );
- loseText.addAction( fadeInColorCycleForever );
- loseText.addAction( moveFromBottom );
- }
- if (!win)
- {
- timeElapsed += dt;
- timeLabel.setText( "Time: " + (int)timeElapsed );
- }
- // draw graphics
- Gdx.gl.glClearColor(0.8f, 0.8f, 1, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- // camera adjustment
- Camera cam = mainStage.getCamera();
- // center camera on player
- cam.position.set( mousey.getX() + mousey.getOriginX(),
- mousey.getY() + mousey.getOriginY(), 0 );
- // bound camera to layout
- cam.position.x = MathUtils.clamp(cam.position.x, viewWidth/2, mapWidth - viewWidth/2);
- cam.position.y = MathUtils.clamp(cam.position.y, viewHeight/2, mapHeight - viewHeight/2);
- cam.update();
- mainStage.draw();
- uiStage.draw();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement