Advertisement
111poiss111

Game

Jun 25th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.79 KB | None | 0 0
  1. package javagame;
  2.  
  3. import org.newdawn.slick.Color;
  4. import org.newdawn.slick.Image;
  5. import org.newdawn.slick.Renderable;
  6.  
  7. import java.util.*;
  8. import org.newdawn.slick.*;
  9. import org.newdawn.slick.geom.Polygon;
  10. import org.newdawn.slick.state.*;
  11. import org.newdawn.slick.tiled.*;
  12. import org.w3c.dom.Element;
  13.  
  14.  
  15. public class Play extends BasicGameState{
  16.    
  17.     int direction = 0;
  18.     Image worldMap;
  19.     Image creatures;
  20.     SpriteSheet sheet;
  21.     boolean quit = false;
  22.     int[] duration = {200,200};
  23.     float buckyPositionX = 0;
  24.     float buckyPositionY = 0;
  25.     float shiftX = buckyPositionX+320;
  26.     float shiftY = buckyPositionY+320;
  27.     int buckyPosX=10-(int)buckyPositionX/32;
  28.     int buckyPosY=5-(int)buckyPositionY/32;
  29.     TiledMap map;
  30.     Image tileb;
  31.     List<Polygon> entities;
  32.     ArrayList<Float> entLocX;
  33.     ArrayList<Float> entLocY;
  34.     float[][] ent;
  35.     Polygon playerPoly;
  36.     int x =0;
  37.     int y =0;
  38.     Polygon[] polygo;
  39.     int all=0;
  40.    
  41.     public Play(int state){
  42.     }
  43.    
  44.     public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
  45.         entities = new ArrayList<Polygon>();
  46.         entLocX = new ArrayList<Float>();
  47.         entLocY = new ArrayList<Float>();
  48.         tileb = new Image("res/tile_b.png");
  49.         map = new TiledMap("res/map3.tmx");
  50.         creatures = new Image("res/lofi_char.png", false, Image.FILTER_NEAREST);
  51.         sheet = new SpriteSheet(creatures.getScaledCopy(4), 32 , 32);
  52.         worldMap= new Image("res/world.png");
  53.         playerPoly = new Polygon(new float[]{   //makes polygon around player
  54.                 buckyPositionX,buckyPositionY,
  55.                 buckyPositionX+32,buckyPositionY,
  56.                 buckyPositionX+32,buckyPositionY+32,
  57.                 buckyPositionX,buckyPositionY+32
  58.         });
  59.        
  60.     }
  61.    
  62.     public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
  63.         map.render((int)buckyPositionX, (int)buckyPositionY);   //renders map
  64.         g.drawImage(sheet.getSubImage(direction,29), shiftX, shiftY);   //renders player
  65.         g.drawString("X"+ buckyPosX +"\nY"+ buckyPosY, 400, 20);    //used for debug
  66.         g.drawString("X"+buckyPositionX+"\nY"+buckyPositionY, 400, 60); //used for debug
  67.         g.drawString("Col "+isColliding()+"\nY"+entLocY.get(0), 400, 100);  //used for debug
  68.         g.drawString("ID "+map.getTileId(buckyPosX, buckyPosY, map.getLayerIndex("collision")), 400, 140);  //used for debug
  69.         g.drawString("AofC: "+entities.size(), 400, 160);   //used for debug
  70.         g.draw(playerPoly); //draws polygon around player,debug
  71.        
  72.         for(int i=0;i<entities.size();i++){ //draws polygons around colliding objects ,debug
  73.         g.draw(entities.get(i));
  74.         }
  75.     }
  76.    
  77.     public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
  78.         for(int i=0;i<entities.size();i++){ //moves polygons as player moves
  79.             entities.get(i).setX(entLocX.get(i)+buckyPositionX);
  80.             entities.get(i).setY(entLocY.get(i)+buckyPositionY);
  81.         }
  82.        
  83.         playerPoly.setX(shiftX);    //sets player polygon to middle of screen
  84.         playerPoly.setY(shiftY);    //sets player polygon to middle of screen
  85.         buckyPosX=10-(int)buckyPositionX/32;    //makes tile cordinates
  86.         buckyPosY=5-(int)buckyPositionY/32;     //makes tile cordinates
  87.        
  88.         Input input = gc.getInput();
  89.         if(input.isKeyDown(Input.KEY_UP)){  //move up
  90.             direction = 3;
  91.             buckyPositionY += delta* .1f;
  92.             if(isColliding()){
  93.                 buckyPositionY -= delta* .1f;
  94.             }
  95.         }
  96.         if(input.isKeyDown(Input.KEY_DOWN)){    //move down
  97.             direction = 1;
  98.             buckyPositionY -= delta* .1f;
  99.             if(isColliding()){
  100.                 buckyPositionY += delta* .1f;
  101.             }
  102.         }
  103.         if(input.isKeyDown(Input.KEY_LEFT)){    //move left
  104.             direction = 2;
  105.             buckyPositionX += delta* .1f;
  106.             if(isColliding()){
  107.                 buckyPositionX -= delta* .1f;
  108.             }
  109.         }
  110.         if(input.isKeyDown(Input.KEY_RIGHT)){   //move right
  111.             direction = 0;
  112.             buckyPositionX -= delta* .1f;
  113.             if(isColliding()){
  114.                 buckyPositionX += delta* .1f;
  115.             }
  116.         }
  117.         makeCollidePoly();
  118.        
  119.         if(all!=entities.size()){
  120.         for(int en=0;en<entities.size();en++){
  121.             entLocX.add(entities.get(en).getX());
  122.             entLocY.add(entities.get(en).getY());
  123.         }
  124.         all++;
  125.         }
  126.     }
  127.    
  128.     public int getID(){
  129.         return 1;
  130.     }
  131.    
  132.     private boolean isColliding(){  //returns true if player's polygon collides with object's poly
  133.         boolean b = false;
  134.         for(int i=0;i<entities.size();i++){
  135.             if(playerPoly.intersects(entities.get(i))){b=true;}
  136.         }
  137.         return false;//false cause player spawns next to tree and gets stuck,should return boolean b
  138.     }
  139.    
  140.     private void makeCollidePoly(){ //makes polygons around every tile in "collision" layer
  141.         if(entities.size()<=600){
  142.         for (int x = 0; x < map.getWidth(); x++) {
  143.             for (int y = 0; y < map.getHeight(); y++) {
  144.                 int tileID = map.getTileId(x, y, map.getLayerIndex("collision"));
  145.                 if (tileID != 0) {
  146.                     Polygon poly= new Polygon(new float[]{
  147.                             x*32,y*32,
  148.                             x*32+32,y*32,
  149.                             x*32+32,y*32+32,
  150.                             x*32,y*32+32
  151.                     });
  152.                     entities.add(poly);
  153.                 }
  154.             }
  155.         }
  156.         }
  157.     }
  158.    
  159.        
  160.    
  161.    
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement