Advertisement
Guest User

game class

a guest
Nov 23rd, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. package potikgame;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import org.lwjgl.input.Mouse;
  6. import org.newdawn.slick.GameContainer;
  7. import org.newdawn.slick.Graphics;
  8. import org.newdawn.slick.Image;
  9. import org.newdawn.slick.Input;
  10. import org.newdawn.slick.SlickException;
  11. import org.newdawn.slick.geom.Polygon;
  12. import org.newdawn.slick.state.BasicGameState;
  13. import org.newdawn.slick.state.GameState;
  14. import org.newdawn.slick.state.StateBasedGame;
  15. import org.newdawn.slick.state.transition.FadeInTransition;
  16. import org.newdawn.slick.state.transition.FadeOutTransition;
  17.  
  18. public class game extends BasicGameState{
  19.     boolean debug;
  20.     Image background;
  21.     Image eButton;
  22.     ArrayList<Polygon> butPol=new ArrayList<Polygon>();
  23.     ArrayList<String> butStr=new ArrayList<String>();
  24.     Polygon mousePoly;
  25.     networking net;
  26.     StateBasedGame par1;
  27.     private boolean begin=false;
  28.    
  29.  
  30.     public game(int game) {
  31.     }
  32.  
  33.  
  34.     public void init(GameContainer gc, StateBasedGame sbg)throws SlickException {
  35.         mousePoly=new Polygon(new float[]{Mouse.getX(),768-Mouse.getY()});
  36.         background=new Image("res/backround.png");
  37.         eButton=new Image("res/nupuPohi.png");
  38.         net=new networking("25.163.75.92");
  39.        
  40.     }
  41.  
  42.  
  43.     public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException {
  44.         background.draw(0, 0);
  45.         renderButton(g);
  46.         if(debug){
  47.         g.drawString("X "+mousePoly.getX(), 100, 50);
  48.         g.drawString("Y "+mousePoly.getY(), 100, 70);
  49.         g.drawString(""+mouseOverButton(), 100, 90);
  50.         }
  51.     }
  52.  
  53.  
  54.     public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException {
  55.        
  56.         updateMousePoly();
  57.         par1 = sbg;
  58.         debug=mainGame.debug;
  59.         if(!begin){
  60.             System.out.println("run");
  61.             net.startRunning();
  62.             begin=true;
  63.             net.sendMessage("test message");
  64.         }
  65.        
  66.     }
  67.  
  68.  
  69.     public int getID() {
  70.         return 1;
  71.     }
  72.    
  73.  
  74.    
  75.     private void buttonDoAction(StateBasedGame sbg) {//tegeleb nupu vajutustega
  76.        
  77.         if(mouseOverButton()!=null){
  78.             try{
  79.                 switch(Integer.parseInt(mouseOverButton())){
  80.                     case 0: buttonTextBoolean(0, "Debug:Sees", "Debug:Väljas"); break;
  81.                     case 1:  break;
  82.                     case 2: sbg.enterState(0, new FadeOutTransition(),new FadeInTransition()); break;
  83.                 }
  84.             }catch(Exception err){
  85.                 System.out.println("Lol,you fucked up");
  86.             }
  87.         }
  88.        
  89.     }
  90.    
  91.     public void updateMousePoly(){//uuendab hiire asukoha markerit
  92.         mousePoly.setX(Mouse.getX());
  93.         mousePoly.setY(768-Mouse.getY());
  94.     }
  95.    
  96.     public void createButton(int X,int Y,String txt){//teeb uue nupu
  97.         butStr.add(txt);
  98.         Polygon poly=new Polygon(new float[]{X,Y,
  99.                                             X+200,Y,
  100.                                             X+200,Y+80,
  101.                                             X,Y+80
  102.                                             });
  103.         butPol.add(poly);
  104.         System.out.printf("state %d nupp X:%d Y:%d txt:%s \n", getID(),X,Y,txt);
  105.     }
  106.    
  107.     public void renderButton(Graphics g){//kuvab nupud
  108.         for (int i=0;i<butPol.size();i++) {
  109.             eButton.draw(butPol.get(i).getMinX(), butPol.get(i).getMinY());
  110.             if(debug){g.draw(butPol.get(i));g.drawString(""+i, butPol.get(i).getX(), butPol.get(i).getY());}
  111.             g.drawString(""+butStr.get(i),butPol.get(i).getMinX()+20,   butPol.get(i).getMinY()+30);
  112.         }
  113.     }
  114.     public String mouseOverButton(){//tagastab nupu Stringi mille kohal hiir on
  115.         for(int i=0;i<butPol.size();i++){
  116.             if (butPol.get(i).contains(mousePoly)){
  117.                 return ""+i;
  118.             }
  119.         }
  120.         return null;
  121.     }
  122.     public void buttonTextBoolean(int i,String on,String off){//muudab nupu teksti sellele vajutades
  123.         if(butStr.get(i)==on){
  124.             butStr.set(i, off);
  125.         }else
  126.         if(butStr.get(i)==off){
  127.             butStr.set(i, on);
  128.         }
  129.     }
  130.     public void mouseClicked(int button,int x,int y,int clickCount){//kutsutakse kui hiirt vajutatakse
  131.         buttonDoAction(par1);
  132.     }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement