[Tutorial][数学向]从零开始的MC特效(五 | BukkitRunnable与粒子特效)

目录:

  • 导读
  • BukkitRunnable之逐渐在玩家身旁出现的粒子

导读

导读
本教程需要读者有一定的空间想象能力(因为我也懒得画图了233)
本教程使用的 Spigot1.10.2-R0.1-SNAPSHOT 核心
在阅读之前请确保你具有高中数学必修4和和Java基础的知识

: 如果你是初中的话,别慌,你有趋向的概念就可以读懂本教程(应该吧...)
: 如果你还未学到关于上面的那本书,别慌学到了再来看也行233 (雾
: 没什么好说的...


BukkitRunnable之逐渐在玩家身旁出现的粒子

如果读者还不知道这是个什么东西的话,可以通过以下几个地方进行了解
Bukkit中文文档 或者 这个 还有就是我自己的 视频教程

首先利用BukkitRunnable的Task性质,我们可以制造一个 粒子特效逐渐渲染 的效果,而不是像以前那样一下子就渲染完毕,我们来看以下的例子

public class CrownEffect extends BukkitRunnable {
    /**
     * 玩家
     */
    private Player player;
    private double degree = 0;

    public CrownEffect(Player player) {
        this.player = player;
    }

    @Override
    public void run() {
        
    }
}

首先我们创建了一个类称之为 CrownEffect 之后我们让它继承于 BukkitRunnable,之后我们需要往run方法里写粒子的出现方式

@Override
public void run() {
    // 用于检查玩家是否不在线的情况
    if (player == null || !player.isOnline()) {
        cancel();
    }
    Location playerLocation = player.getLocation();
    // 转弧度制
    double radians = Math.toRadians(degree);
    
    // 这里我写得简单了一点,我们将玩家的坐标克隆之后直接进行x, y, z的变换
    // 不难看出,我们这里是想建立一个 0.3 为半径的圆,作为我们想要实现的皇冠
    Location playEffectLocation = playerLocation.clone().add(0.3 * Math.cos(radians), 2D, 0.3 * Math.sin(radians));
    // 粒子播放,这里我使用了类库
    ParticleEffect.REDSTONE.display(new ParticleEffect.OrdinaryColor(Color.ORANGE), playEffectLocation, 50);

    // 我们只需要degree在0~360度内即可
    if (degree >= 360) { 
       degree = 0;
    } else {
       // 这里其实就是修改了步长为20 degree
       degree += 20;
    }
}

通过上方的代码,相信你已经可以理解我们所要实现的内容了,之后我们对这个类进行进一步的完善

/**
 * 王冠特效
 *
 * @author Zoyn
 */
public class CrownEffect extends BukkitRunnable {

    /**
     * 玩家
     */
    private Player player;
    private double degree = 0;

    public CrownEffect(Player player) {
        this.player = player;
    }

    @Override
    public void run() {
        if (player == null || !player.isOnline()) {
            cancel();
        }
        Location playerLocation = player.getLocation();
        double radians = Math.toRadians(degree);

        Location playEffectLocation = playerLocation.clone().add(0.3 * Math.cos(radians), 2D, 0.3 * Math.sin(radians));
        ParticleEffect.REDSTONE.display(new ParticleEffect.OrdinaryColor(Color.ORANGE), playEffectLocation, 50);

        if (degree >= 360) {
            degree = 0;
        } else {
            degree += 20;
        }
    }

    /**
     * 开启特效的方法
     */
    public void startEffect() {
        runTaskTimer(主类的实例, 0L, 1L);
    }

    /**
     * 关闭特效的方法
     */
    public void stopEffect() {
        cancel();
    }
}

调用方法

CrownEffect crownEffect = new CrownEffect(player);
crownEffect.startEffect();
王冠
如果我们将run内的绘制算法更改一下会怎样呢?
    @Override
    public void run() {
        // 依然还是要判断玩家
        if (player == null || !player.isOnline()) {
            cancel();
        }
        Location playerLocation = player.getLocation().add(0, 1D, 0);
        double radians = Math.toRadians(degree);
        double x = 半径 * Math.cos(radians);
        double y = Math.sin(radians);
        double z = 半径 * Math.sin(radians);

        Location playEffectLocation = playerLocation.clone().add(x, y, z);
        ParticleEffect.REDSTONE.display(new ParticleEffect.OrdinaryColor(Color.RED), playEffectLocation, 50);

        if (degree >= 360) {
            degree = 0;
        } else {
            degree += 10;
        }
    }

那么你就可以看到这样的效果


[Tutorial][数学向]从零开始的MC特效(五 | BukkitRunnable与粒子特效)_第1张图片
我也不懂叫什么的特效

以上便是BukkitRunnable与粒子特效的小技巧,下面放送一个自己利用前几节课的知识加上本节的知识所写的一个特效

// 会旋转的DNA
public class RotatableDNA extends BukkitRunnable {

    // 原点
    private Location location;

    private float yaw = 0;
    private double y = 0D;

    public RotatableDNA(Location location) {
        this.location = location.clone();
    }

    @Override
    public void run() {
        for (double degree = 0, angle = 180; degree < 480; degree += 5, angle += 5) {
            // 第一条
            double radians = Math.toRadians(degree);
            double x = Math.cos(radians);
            double z = Math.sin(radians);
            Vector vector = VectorUtils.getVector(location, location.clone().add(x, y, z));
            Vector rotatedVector = VectorUtils.rotateVector(vector, yaw, 0);
            location.add(rotatedVector);
            ParticleEffect.REDSTONE.display(new ParticleEffect.OrdinaryColor(Color.RED), location, 50);
            location.subtract(rotatedVector);

            // 第二条
            double radians2 = Math.toRadians(angle);
            double x2 = Math.cos(radians2);
            double z2 = Math.sin(radians2);

            Vector vector2 = VectorUtils.getVector(location, location.clone().add(x2, y, z2));
            Vector rotatedVector2 = VectorUtils.rotateVector(vector2, yaw, 0);
            location.add(rotatedVector2);
            ParticleEffect.REDSTONE.display(new ParticleEffect.OrdinaryColor(Color.RED), location, 20);
            location.subtract(rotatedVector2);

            //中间连线
            if (degree % 30 == 0) {
                Location pointA = location.clone().add(rotatedVector);
                Location pointB = location.clone().add(rotatedVector2);
                EntityNBT.buildLine(pointA, pointB);
            }

            y += 0.1;
        }

        // 将yaw设定在0~360之间进行循环
        if (yaw >= 360) {
            yaw = 0;
        } else {
            yaw += 5;
        }
        y = 0;
    }

    public void startEffect() {
        runTaskTimer(主类实例, 0L, 1L);
    }
}

调用方法

RotatableDNA rotatableDNA = new RotatableDNA(location);
rotatableDNA.startEffect();

具体效果


period为3时
period为1时

结语

到这里这个教程就已经完全结束了,希望你能从这个教程学到一些东西...

高三不易,望读者谅解! —— 撰写: 一个来自普高文科的学生 2019/1/31

你可能感兴趣的:([Tutorial][数学向]从零开始的MC特效(五 | BukkitRunnable与粒子特效))