Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.minefs.MineStrike.Map;
- import java.awt.Graphics2D;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- import org.bukkit.Location;
- import org.bukkit.entity.Player;
- import org.bukkit.map.MapCanvas;
- import org.bukkit.scheduler.BukkitRunnable;
- import net.minefs.MineStrike.Main;
- import net.minefs.MineStrike.Modes.Game;
- public class RendererTask extends BukkitRunnable {
- private Player p; // Người chơi
- private MapCanvas c; // Canvas cần render
- private Main main; // biến plugin của mình
- public RendererTask(Player p, MapCanvas c) {
- this.p = p;
- this.c = c;
- main = Main.getInstance();
- runTaskTimerAsynchronously(main = Main.getInstance(), 1, 1); // Chạy async mỗi tick
- }
- @Override
- public void run() {
- if (p == null || !p.isOnline()) {
- cancel();
- return;
- // người chơi mà thoát server thì hủy runnable này.
- }
- // Đây là biến game/arena của mình
- Game g = main.getManager().getGame(p);
- if (g != null && !g.getSpectatorsClone().contains(p) && g.getGameMap() != null) {
- GameMap gmap = g.getGameMap(); // Biến GameMap của arena, nơi lưu background byte arrays của arena tương ứng
- Location l = p.getLocation(); // Vị trí của người chơi
- // Xác định tọa độ X Y của người chơi trên background byte arrays
- int x = (l.getBlockX() - gmap.getX()) * 2;
- int y = (l.getBlockZ() - gmap.getY()) * 2;
- // Xác định góc quay của người chơi tính theo radians
- double rad = Math.toRadians(180 - l.getYaw());
- // 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)
- byte[][] map = gmap.getRotatedBytes(x, y, rad);
- // 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
- g.renderMap(p, map, x, y, rad);
- // Bây giờ ta đưa từng byte vào canvas
- for (int x2 = 0; x2 < 128; ++x2)
- for (int y2 = 0; y2 < 128; ++y2)
- c.setPixel(x2, y2, map[x2][y2]);
- }
- }
- }
- // Đây là hàm GameMap#getRotatedBytes
- public byte[][] getRotatedBytes(int xc, int yc, double rad) {
- // Gọi kq là mảng kết quả, mapbytes là mảng mẹ (background)
- byte[][] kq = new byte[128][128];
- rad = Math.PI * 2 - rad; // Góc xoay của mảng kết quả nằm trên mảng mẹ
- // Vị trí của kq[0][0] trên mảng mẹ
- int x = xc - 64;
- int y = yc - 64;
- if (mapbytes != null)
- for (int x1 = 0; x1 < 128; ++x1)
- for (int y1 = 0; y1 < 128; ++y1) {
- // Xác định x2, y2 là vị trí của kq[x1][y1] trên mảng mẹ
- int x2 = x1 + x;
- int y2 = y1 + y;
- // Công thức quay - Xác định xr, yr là x2, y2 sau khi đã quay một góc rad
- int xr = (int) ((x2 - xc) * Math.cos(rad) - (y2 - yc) * Math.sin(rad)) + xc;
- int yr = (int) ((y2 - yc) * Math.cos(rad) + (x2 - xc) * Math.sin(rad)) + yc;
- // Nếu xr, yr nằm bên ngoài mảng mẹ thì đừng thêm vào
- if (xr < 0 || yr < 0 || xr >= mapbytes.length || yr >= mapbytes[0].length)
- kq[x1][y1] = 0;
- else
- kq[x1][y1] = mapbytes[xr][yr];
- }
- return kq;
- }
Advertisement
Add Comment
Please, Sign In to add comment