Advertisement
teaowl

UI Class

Sep 7th, 2018
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. package system;
  2.  
  3. import org.newdawn.slick.Color;
  4. import org.newdawn.slick.GameContainer;
  5. import org.newdawn.slick.Graphics;
  6. import org.newdawn.slick.Image;
  7. import org.newdawn.slick.SlickException;
  8.  
  9. import core.MyWorld;
  10. import entities.TwilightSparkle;
  11.  
  12. public class WorldUI {
  13.    
  14.     //мир, которому принадлежит интерфейс
  15.     MyWorld parent;
  16.    
  17.     //нижняя панель
  18.     protected Image bottomBar;
  19.     protected Image battleBottomBar;
  20.    
  21.     //верхняя панель
  22.     protected Image topBar;
  23.    
  24.     //полосы жизни, и очков магии
  25.     protected Image HP;
  26.     protected Image IP;
  27.    
  28.     //относительные координаты интерфейса
  29.     float interfaceX = 0;
  30.     float interfaceY = 0;
  31.  
  32.     private boolean debug;
  33.    
  34.     public WorldUI(MyWorld parent, GameContainer container) {
  35.         this.parent = parent;
  36.         interfaceX = (container.getWidth() - parent.twilie.hitboxWidth)/2;
  37.         interfaceY = (container.getHeight() - parent.twilie.hitboxHeight)/2;
  38.     }
  39.  
  40.     public void init() throws SlickException {
  41.         topBar = new Image("textures/topBar.png");
  42.         bottomBar = new Image("textures/Interface2.png");
  43.         battleBottomBar = new Image("textures/Interface3.png");
  44.         HP = new Image("textures/IP.png");
  45.         IP = new Image("textures/HP.png");
  46.     }
  47.    
  48.     public void draw(Graphics g) {
  49.         //жизнь и мана
  50.         HP.draw(interfaceX+100, interfaceY+parent.container.getHeight()-90);//520  
  51.         IP.draw(interfaceX+100, interfaceY+parent.container.getHeight()-50);//560  
  52.        
  53.         //верхняя и нижняя рамка интерфейса
  54.         topBar.draw(interfaceX, interfaceY);   
  55.         bottomBar.draw(interfaceX, interfaceY+parent.container.getHeight()-128);
  56.        
  57.         //Отладка
  58.         if(debug) {
  59.             g.setColor(Color.red);
  60.             g.drawString(this.toString(), interfaceX+parent.container.getWidth()/2, interfaceY+20);
  61.         }
  62.     }
  63.    
  64.     public void update() {
  65.         System.out.println("--- updates! ---");
  66.         //полезные переменные для сокращения кода
  67.         TwilightSparkle pony = MyWorld.twilie;
  68.        
  69.         int worldWidth = parent.width;
  70.         int worldHeight = parent.height;
  71.         int screenWidth = parent.container.getWidth();
  72.         int screenHeight = parent.container.getHeight();       
  73.         int halfOfScreenWidth = (screenWidth - pony.hitboxWidth)/2;
  74.         int halfOfScreenHeight = (screenHeight - pony.hitboxHeight)/2;
  75.        
  76.         //если мы не достигли границ экрана, то координаты интерфейса следуют за лошадью     
  77.         float halfOfPonyPositionX = pony.x - halfOfScreenWidth;
  78.         float halfOfPonyPositionY = pony.y - halfOfScreenHeight;
  79.  
  80.         if(debug) {
  81.             System.out.println("worldWidth = "+worldWidth);
  82.             System.out.println("worldHeight = "+worldHeight);
  83.             System.out.println("halfOfScreenWidth = "+halfOfScreenWidth);
  84.             System.out.println("halfOfScreenHeight = "+halfOfScreenHeight);
  85.            
  86.             System.out.println();
  87.            
  88.             System.out.println("interfaceX = " + interfaceX);      
  89.             System.out.println("interfaceY = " + interfaceY);
  90.            
  91.             System.out.println("halfOfPonyPositionX > halfOfScreenWidth  = "+(halfOfPonyPositionX > halfOfScreenWidth ));
  92.             System.out.println("halfOfPonyPositionX + screenWidth < worldWidth = "+(halfOfPonyPositionX + screenWidth < worldWidth));  
  93.    
  94.             System.out.println("halfOfPonyPositionY > halfOfScreenHeight  = "+(halfOfPonyPositionY > halfOfScreenHeight));
  95.             System.out.println("halfOfPonyPositionY + screenHeight < worldHeight = "+(halfOfPonyPositionY + screenHeight < worldHeight));  
  96.         }
  97.        
  98.         if(halfOfPonyPositionX  +100/* + 18*/ > halfOfScreenWidth && halfOfPonyPositionX + screenWidth < worldWidth) {
  99.             interfaceX = halfOfPonyPositionX;
  100.             System.out.println("upd X");
  101.             System.out.println("interfaceX = halfOfPonyPositionX = "+(interfaceX));
  102.         }
  103.        
  104.         if(halfOfPonyPositionY > halfOfScreenHeight /*+ 60*/ && halfOfPonyPositionY + screenHeight < worldHeight) {
  105.              interfaceY = halfOfPonyPositionY;
  106.                 System.out.println("upd Y");
  107.                 System.out.println("interfaceY = halfOfPonyPositionY = "+(interfaceY));
  108.         }
  109.  
  110.     }
  111.    
  112.     public void debug(boolean debug) {
  113.         this.debug = debug;
  114.     }
  115.    
  116.     @Override
  117.     public String toString() {
  118.         StringBuilder log = new StringBuilder("--- Interface Log ---");
  119.         log.append("\n"+"interfaceX = " + interfaceX);
  120.         log.append("\n"+"interfaceY = " + interfaceY);
  121.        
  122.         log.append("\n"+"halfOfScreenWidth = " + parent.container.getWidth()/2);
  123.         log.append("\n"+"halfOfScreenHeight = " + parent.container.getHeight()/2);
  124.        
  125.         log.append("\n"+"Twi X = " + parent.twilie.x);
  126.         log.append("\n"+"Twi Y = " + parent.twilie.y);
  127.        
  128.         log.append( "\n"+"worldWidth = " + parent.width);
  129.         log.append( "\n"+"screenHeight = " + parent.height);
  130.        
  131.         return log.toString();
  132.     }
  133.    
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement