- public class GameDisplay implements Screen, InputProcessor{
- enum State{READY,START,PAUSED,GAMEOVER,NEXTLVL}
- private static final int ScreenHeight = 0;;
- State state;
- static float currentLevel = 1;
- static float baseLvlHeight = 9600;
- public static float LVLHEIGHT;
- public static float WIDTH = 900;
- public static float ScreenHEIGHT = 1600;
- Stage stage;
- SpriteBatch batch;
- OrthographicCamera cam;
- InputMultiplexer plex;
- FatBoy fb;
- Image imgBg;
- TextureAtlas manAtlas;
- Animation manAni;
- Texture bg,cloud1,cloud2,cloud3,cloud4,platform1;
- Array<Cloud> clouds = new Array<Cloud>();
- public static Vector2 gravity = new Vector2(0,-2500);
- float cloudRnd;
- public static Man man;
- public static Array<Platform> platforms;
- public static Random rand;
- public static float heightSoFar;
- public static ShapeRenderer debugShapes;
- float aniTime;
- static float platformNum ;
- public GameDisplay(FatBoy fb){
- this.fb = fb;
- state = State.START;
- stage = new Stage(new FillViewport(WIDTH, ScreenHEIGHT) );
- cam = new OrthographicCamera();
- //cam.position.set(WIDTH/2, ScreenHEIGHT/2, 0);
- batch = new SpriteBatch();
- plex = new InputMultiplexer();
- plex.addProcessor(this);
- plex.addProcessor(stage);
- Gdx.input.setInputProcessor(plex);
- bg = Assets.manager.get("images/bg.jpg", Texture.class);
- bg.setFilter(TextureFilter.Linear, TextureFilter.Linear);
- platform1 = Assets.manager.get("images/Platform_01.png", Texture.class);
- platform1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
- cloud1 = Assets.manager.get("images/cloudOne.png", Texture.class);
- cloud1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
- cloud2 = Assets.manager.get("images/cloudTwo.png", Texture.class);
- cloud2.setFilter(TextureFilter.Linear, TextureFilter.Linear);
- cloud3 = Assets.manager.get("images/cloudThree.png", Texture.class);
- cloud3.setFilter(TextureFilter.Linear, TextureFilter.Linear);
- cloud4 = Assets.manager.get("images/cloudFour.png", Texture.class);
- cloud4.setFilter(TextureFilter.Linear, TextureFilter.Linear);
- manAtlas = Assets.manager.get("animations/fatboy.txt", TextureAtlas.class);
- manAni = new Animation(0.05f, manAtlas.findRegions("img"));
- manAni.setPlayMode(PlayMode.LOOP);
- imgBg = new Image(bg);
- man = new Man(new Vector2(WIDTH/2 -20,0));
- platforms = new Array<Platform>();
- rand = new Random();
- GameDisplay.heightSoFar = 0;
- LVLHEIGHT = baseLvlHeight * currentLevel;
- platformNum = LVLHEIGHT/125;
- genLevel();
- actors();
- addClouds();
- stage.addAction(Actions.alpha(0));
- stage.addAction(Actions.fadeIn(.5f));
- debugShapes = new ShapeRenderer();
- }
- private static void genLevel() {
- float lastY = 300;
- float rndX;
- for(int i =0; i< platformNum; i++){
- new MathUtils();
- rndX = MathUtils.random(0, 800);
- platforms.add(new Platform(new Vector2(rndX,lastY)));
- lastY += 400;
- }
- }
- private void addClouds() {
- float cloudNum = 5;
- clouds = new Array<Cloud>();
- float rnd;
- float rndy;
- //rnd= new MathUtils().random(600, 1400);
- for(int i = 0; i<platformNum; i ++){
- new MathUtils();
- cloudRnd = MathUtils.random(0, 10);
- new MathUtils();
- rnd= MathUtils.random(900, LVLHEIGHT);
- rndy= MathUtils.random(0, 820);
- clouds.add(new Cloud(new Vector2(rndy,rnd)));
- }
- }
- private void actors() {
- imgBg.setPosition(0, 0);
- imgBg.setSize(WIDTH, ScreenHEIGHT);
- stage.addActor(imgBg);
- }
- @Override
- public void render(float delta) {
- Gdx.gl.glClearColor(0, 0, 0, 0);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- cam.update();
- stage.act(delta);
- aniTime+=delta;
- batch.setProjectionMatrix(stage.getCamera().combined);
- switch (state) {
- case READY:
- stage.draw();
- break;
- case START:
- stage.draw();
- batch.begin();
- updateObjects(delta);
- drawObjects();
- batch.end();
- break;
- case NEXTLVL:
- stage.draw();
- break;
- case GAMEOVER:
- stage.draw();
- break;
- case PAUSED:
- stage.draw();
- break;
- default:
- break;
- }
- debug();
- }
- public void debug() {
- debugShapes.setProjectionMatrix(cam.combined);
- debugShapes.begin(ShapeType.Line);
- debugShapes.setColor(Color.RED);
- for(int i =0; i< platforms.size; i ++){
- Platform pl = platforms.get(i);
- debugShapes.rect(pl.position.x, pl.position.y, pl.WIDTH, pl.HEIGHT);
- }
- debugShapes.rect(man.position.x, man.position.y, man.Width, man.Height);
- debugShapes.rect(imgBg.getX(), imgBg.getY(), imgBg.getWidth(), imgBg.getHeight());
- debugShapes.end();
- }
- private void updateObjects(float delta) {
- //Update Clouds
- for(int i = 0; i<clouds.size; i ++){
- Cloud cl = clouds.get(i);
- if(cl.cloudDraw<3)batch.draw(cloud1, cl.position.x, cl.position.y);
- else if(cl.cloudDraw<5)batch.draw(cloud2, cl.position.x, cl.position.y);
- else if(cl.cloudDraw<8)batch.draw(cloud3, cl.position.x, cl.position.y);
- else batch.draw(cloud4, cl.position.x, cl.position.y);
- if(cl.position.x > WIDTH +300){
- cl.position.x = -360;
- }
- cl.update(delta);
- }
- // update Boy
- if(man.velocity.y >0 ){
- man.state = man.state.JUMP;
- }
- if(man.velocity.y < 0 ){
- man.state = man.state.FALL;
- }
- if(man.state == man.state.FALL){
- for(int i =0; i<platforms.size; i++){
- Platform pl = platforms.get(i);
- //if (OverlapTester.overlapRectangles(man.bounds, pl.bounds)) {
- if(man.position.y > pl.position.y){
- if(man.bounds.overlaps(pl.bounds)){
- man.state = man.state.JUMP;
- man.velocity.y = man.jump_vel;
- }
- }
- }
- }
- if(man.position.y <= 0){
- man.velocity.y = man.jump_vel;
- }
- if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
- man.velocity.x += -40;
- }
- if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
- man.velocity.x += 40;
- }
- float accelX = Gdx.input.getAccelerometerX();
- man.velocity.x += -accelX / 10 *40;
- if(man.velocity.x>= 400)man.velocity.x = 400;
- if(man.velocity.x<= -400)man.velocity.x = -400;
- if(man.position.y <= cam.position.y )man.state = man.state.HIT;
- if(man.state == man.state.JUMP && man.position.y > ScreenHEIGHT /2){
- cam.position.y = man.position.y - ScreenHeight/2;
- }
- if(cam.position.y > 800){
- if(man.position.y <= cam.position.y - 800 )state = state.GAMEOVER;
- }
- man.update(delta);
- //update Platforms
- for(int i = 0; i < platforms.size; i++){
- Platform pl = platforms.get(i);
- if(pl.position.x < 0 ){
- pl.velocity.x = 80;
- }
- if(pl.position.x > WIDTH - pl.WIDTH ){
- pl.velocity.x = -80;
- }
- pl.update(delta);
- }
- }
- private void drawObjects() {
- for(int i = 0; i < platforms.size; i++){
- Platform pl = platforms.get(i);
- batch.draw(platform1, pl.position.x, pl.position.y,pl.WIDTH,pl.HEIGHT);
- }
- batch.draw(manAni.getKeyFrame(aniTime), man.position.x, man.position.y,man.Width,man.Height);
- }
- @Override
- public void resize(int width, int height) {
- stage.getViewport().update(width, height, true);
- cam.setToOrtho(false, stage.getViewport().getWorldWidth(), stage.getViewport().getWorldHeight());
- }
- @Override
- public void show() {
- // TODO Auto-generated method stub
- }
- @Override
- public void hide() {
- // TODO Auto-generated method stub
- }
- @Override
- public void pause() {
- // TODO Auto-generated method stub
- }
- @Override
- public void resume() {
- // TODO Auto-generated method stub
- }
- @Override
- public void dispose() {
- // TODO Auto-generated method stub
- }
- @Override
- public boolean keyDown(int keycode) {
- return false;
- }
- @Override
- public boolean keyUp(int keycode) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean keyTyped(char character) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean touchDown(int screenX, int screenY, int pointer, int button) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean touchUp(int screenX, int screenY, int pointer, int button) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean touchDragged(int screenX, int screenY, int pointer) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean mouseMoved(int screenX, int screenY) {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean scrolled(int amount) {
- // TODO Auto-generated method stub
- return false;
- }
- }
SHARE
TWEET
Untitled
a guest
Sep 18th, 2014
146
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
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.
