目录:
- 导读
- BukkitRunnable之逐渐在玩家身旁出现的粒子
导读:
本教程需要读者有一定的空间想象能力(因为我也懒得画图了233)
本教程使用的 Spigot1.10.2-R0.1-SNAPSHOT 核心
在阅读之前请确保你具有高中数学必修4和和Java基础的知识
<To初中生>: 如果你是初中的话,别慌,你有趋向的概念就可以读懂本教程(应该吧...)
<To高中生>: 如果你还未学到关于上面的那本书,别慌学到了再来看也行233 (雾
<To大学生>: 没什么好说的...
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() {
- }
- }
- @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;
- }
- }
- /**
- * 王冠特效
- *
- * [url=home.php?mod=space&uid=1231151]@author[/url] 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();
- @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;
- }
- }
以上便是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();
结语
到这里这个教程就已经完全结束了,希望你能从这个教程学到一些东西...
高三不易,望读者谅解! —— 撰写: 一个来自普高文科的学生 2019/1/31
[groupid=1306]Bone Studio[/groupid]