Advertisement
Guest User

Main Class

a guest
Sep 2nd, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. package com.me.FirstProject;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6.  
  7. import com.badlogic.gdx.ApplicationAdapter;
  8. import com.badlogic.gdx.Gdx;
  9. //import com.badlogic.gdx.Input.Keys;
  10. import com.badlogic.gdx.InputProcessor;
  11. import com.badlogic.gdx.graphics.GL20;
  12. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  13. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  14. import com.badlogic.gdx.math.Vector2;
  15.  
  16. public class MyFirstProject extends ApplicationAdapter {
  17.     SpriteBatch batch;
  18.     //Texture mario;
  19.     Player player;
  20.     Vector2 position;
  21.     InputProcessor inputProcessor;
  22.     Tree tree, tree2;
  23.     ShapeRenderer sr;
  24.     ArrayList <Tree> trees;
  25.     Iterator<Tree> treeIterator;
  26.    
  27.     @Override
  28.     //places the player in a position, and reads to check if player exists
  29.     public void create () {
  30.         batch = new SpriteBatch();
  31.         //mario = new Texture(Gdx.files.internal("mario.png"));
  32.         sr = new ShapeRenderer();
  33.         tree = new Tree(new Vector2(100, 50), new Vector2( 150, 150));
  34.         tree2 = new Tree(new Vector2(50, 50), new Vector2( 50, 50));
  35.        
  36.         trees = new ArrayList<Tree>();
  37.         trees.add(tree);
  38.         trees.add(tree2);
  39.         System.out.println(trees.size());
  40.        
  41.         if(Gdx.files.local("player.dat").exists()){
  42.             try{
  43.                 player = new Player(new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2), "mario.png");
  44.                 player.setPosition(Player.readPlayer());
  45.                
  46.             }catch(ClassNotFoundException e){
  47.                 e.printStackTrace();
  48.                
  49.             }catch(IOException e){
  50.                 e.printStackTrace();
  51.             }
  52.             System.out.println("Player exists, Reading file");
  53.             }else{
  54.                 player = new Player(new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2), "mario.png");
  55.                 try{
  56.                     Player.savePlayer(player);
  57.                 }catch(IOException e){
  58.                     e.printStackTrace();
  59.                 }
  60.                 System.out.println("PLayer Doesnt Exists, Creating Player");
  61.                
  62.         }
  63.     }
  64.     @Override
  65.     public void dispose(){
  66.         try{
  67.             Player.savePlayer(player);
  68.         }catch(IOException e){
  69.             e.printStackTrace();
  70.         }
  71.     }
  72.  
  73.     @Override
  74.     public void render () {
  75.         Gdx.gl.glClearColor(1, 1, 1, 1);
  76.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  77.        
  78.         tree.update();
  79.         player.update();
  80.         if(player.getBounds().overlaps(tree.getBounds())){
  81.             player.redirect();
  82.            
  83.         }
  84.         batch.begin();
  85.         batch.draw(player.getCurrentFrame(), player.getPosition().x, player.getPosition().y);
  86.         tree.draw(batch);
  87.        
  88.         treeIterator = trees.iterator();
  89.         while(treeIterator.hasNext()){
  90.             Tree cur = treeIterator.next();
  91.             cur.draw(batch);
  92.         }
  93.        
  94.         batch.end();
  95.        
  96.         /*sr.begin(ShapeType.Line);
  97.         sr.setColor(Color.BLUE);
  98.         sr.rect(player.getPosition().x, player.getPosition().y, player.getCurrentFrame().getRegionWidth(), player.getCurrentFrame().getRegionHeight());
  99.         sr.rect(tree.getPosition().x, tree.getPosition().y, tree.getSize().x, tree.getSize().y);
  100.         sr.setColor(Color.RED);
  101.         sr.end();*/
  102.        
  103.        
  104.     }
  105.     @Override
  106.     public void resize(int height, int width){
  107.        
  108.     }
  109.     @Override
  110.     public void pause(){
  111.        
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement