Guest User

Untitled

a guest
Aug 14th, 2018
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. package net.minefs.MineStrike.Map;
  2.  
  3. import java.awt.Graphics2D;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.image.BufferedImage;
  6.  
  7. import org.bukkit.Location;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.map.MapCanvas;
  10. import org.bukkit.scheduler.BukkitRunnable;
  11.  
  12. import net.minefs.MineStrike.Main;
  13. import net.minefs.MineStrike.Modes.Game;
  14.  
  15. public class RendererTask extends BukkitRunnable {
  16.  
  17.     private Player p; // Người chơi
  18.     private MapCanvas c; // Canvas cần render
  19.     private Main main; // biến plugin của mình
  20.  
  21.     public RendererTask(Player p, MapCanvas c) {
  22.         this.p = p;
  23.         this.c = c;
  24.         main = Main.getInstance();
  25.         runTaskTimerAsynchronously(main = Main.getInstance(), 1, 1); // Chạy async mỗi tick
  26.     }
  27.  
  28.     @Override
  29.     public void run() {
  30.         if (p == null || !p.isOnline()) {
  31.             cancel();
  32.             return;
  33.             // người chơi mà thoát server thì hủy runnable này.
  34.         }
  35.        
  36.         // Đây là biến game/arena của mình
  37.         Game g = main.getManager().getGame(p);
  38.        
  39.         if (g != null && !g.getSpectatorsClone().contains(p) && g.getGameMap() != null) {
  40.             GameMap gmap = g.getGameMap(); // Biến GameMap của arena, nơi lưu background byte arrays của arena tương ứng
  41.            
  42.             Location l = p.getLocation(); // Vị trí của người chơi
  43.            
  44.             // Xác định tọa độ X Y của người chơi trên background byte arrays
  45.             int x = (l.getBlockX() - gmap.getX()) * 2;
  46.             int y = (l.getBlockZ() - gmap.getY()) * 2;
  47.            
  48.             // Xác định góc quay của người chơi tính theo radians
  49.             double rad = Math.toRadians(180 - l.getYaw());
  50.            
  51.             // Tạo một byte arrays mới cắt ra từ background, với tâm là vị trí người chơi, hàm getRotatedBytes được để bên dưới (line#65)
  52.             byte[][] map = gmap.getRotatedBytes(x, y, rad);
  53.            
  54.             // Render các chi tiết trên map dựa trên arena của bạn, mục này mình sẽ giới thiệu sau
  55.             g.renderMap(p, map, x, y, rad);
  56.            
  57.             // Bây giờ ta đưa từng byte vào canvas
  58.             for (int x2 = 0; x2 < 128; ++x2)
  59.                 for (int y2 = 0; y2 < 128; ++y2)
  60.                     c.setPixel(x2, y2, map[x2][y2]);
  61.         }
  62.     }
  63. }
  64.    
  65.     // Đây là hàm GameMap#getRotatedBytes
  66.     public byte[][] getRotatedBytes(int xc, int yc, double rad) {
  67.         // Gọi kq là mảng kết quả, mapbytes là mảng mẹ (background)
  68.         byte[][] kq = new byte[128][128];
  69.        
  70.         rad = Math.PI * 2 - rad; // Góc xoay của mảng kết quả nằm trên mảng mẹ
  71.        
  72.         // Vị trí của kq[0][0] trên mảng mẹ
  73.         int x = xc - 64;
  74.         int y = yc - 64;
  75.        
  76.         if (mapbytes != null)
  77.             for (int x1 = 0; x1 < 128; ++x1)
  78.                 for (int y1 = 0; y1 < 128; ++y1) {
  79.                     // Xác định x2, y2 là vị trí của kq[x1][y1] trên mảng mẹ
  80.                     int x2 = x1 + x;
  81.                     int y2 = y1 + y;
  82.                    
  83.                     // Công thức quay -  Xác định xr, yr là x2, y2 sau khi đã quay một góc rad
  84.                     int xr = (int) ((x2 - xc) * Math.cos(rad) - (y2 - yc) * Math.sin(rad)) + xc;
  85.                     int yr = (int) ((y2 - yc) * Math.cos(rad) + (x2 - xc) * Math.sin(rad)) + yc;
  86.                    
  87.                     // Nếu xr, yr nằm bên ngoài mảng mẹ thì đừng thêm vào
  88.                     if (xr < 0 || yr < 0 || xr >= mapbytes.length || yr >= mapbytes[0].length)
  89.                         kq[x1][y1] = 0;
  90.                     else
  91.                         kq[x1][y1] = mapbytes[xr][yr];
  92.                 }
  93.         return kq;
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment