|
| 1 | +package top.zoyn.particlelib.pobject; |
| 2 | + |
| 3 | +import com.google.common.collect.Lists; |
| 4 | +import org.bukkit.Location; |
| 5 | +import org.bukkit.entity.Entity; |
| 6 | +import org.bukkit.util.Consumer; |
| 7 | +import org.bukkit.util.Vector; |
| 8 | + |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.List; |
| 11 | +import java.util.function.Predicate; |
| 12 | + |
| 13 | +/** |
| 14 | + * 代表一个射线 |
| 15 | + * |
| 16 | + * @author Zoyn |
| 17 | + */ |
| 18 | +public class Ray extends ParticleObject { |
| 19 | + |
| 20 | + private Vector direction; |
| 21 | + private double maxLength; |
| 22 | + private double step; |
| 23 | + /** |
| 24 | + * 用于检测实体时获取周围实体的范围 |
| 25 | + */ |
| 26 | + private double range; |
| 27 | + private RayStopType stopType; |
| 28 | + private Consumer<Entity> hitEntityConsumer; |
| 29 | + private Predicate<Entity> entityFilter; |
| 30 | + |
| 31 | + public Ray(Location origin, Vector direction, double maxLength) { |
| 32 | + this(origin, direction, maxLength, 0.2D); |
| 33 | + } |
| 34 | + |
| 35 | + public Ray(Location origin, Vector direction, double maxLength, double step) { |
| 36 | + this(origin, direction, maxLength, step, 0.5D, RayStopType.MAX_LENGTH, null); |
| 37 | + } |
| 38 | + |
| 39 | + public Ray(Location origin, Vector direction, double maxLength, double step, double range, RayStopType stopType, Consumer<Entity> hitEntityConsumer) { |
| 40 | + this(origin, direction, maxLength, step, range, stopType, hitEntityConsumer, null); |
| 41 | + } |
| 42 | + |
| 43 | + public Ray(Location origin, Vector direction, double maxLength, double step, double range, RayStopType stopType, Consumer<Entity> hitEntityConsumer, Predicate<Entity> entityFilter) { |
| 44 | + setOrigin(origin); |
| 45 | + this.direction = direction; |
| 46 | + this.maxLength = maxLength; |
| 47 | + this.step = step; |
| 48 | + this.range = range; |
| 49 | + this.stopType = stopType; |
| 50 | + this.hitEntityConsumer = hitEntityConsumer; |
| 51 | + this.entityFilter = entityFilter; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void show() { |
| 56 | + for (double i = 0; i < maxLength; i += step) { |
| 57 | + Vector vectorTemp = direction.clone().multiply(i); |
| 58 | + Location spawnLocation = getOrigin().clone().add(vectorTemp); |
| 59 | + spawnParticle(spawnLocation); |
| 60 | + |
| 61 | + if (stopType.equals(RayStopType.HIT_ENTITY)) { |
| 62 | + Collection<Entity> nearbyEntities = spawnLocation.getNearbyEntities(range, range, range); |
| 63 | + List<Entity> entities = Lists.newArrayList(); |
| 64 | + // 检测有无过滤器 |
| 65 | + if (entityFilter != null) { |
| 66 | + for (Entity entity : nearbyEntities) { |
| 67 | + if (!entityFilter.test(entity)) { |
| 68 | + entities.add(entity); |
| 69 | + } |
| 70 | + } |
| 71 | + } else { |
| 72 | + entities = (List<Entity>) nearbyEntities; |
| 73 | + } |
| 74 | + |
| 75 | + // 获取首个实体 |
| 76 | + if (entities.size() != 0) { |
| 77 | + hitEntityConsumer.accept(entities.get(0)); |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public Vector getDirection() { |
| 85 | + return direction; |
| 86 | + } |
| 87 | + |
| 88 | + public Ray setDirection(Vector direction) { |
| 89 | + this.direction = direction; |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + public double getMaxLength() { |
| 94 | + return maxLength; |
| 95 | + } |
| 96 | + |
| 97 | + public Ray setMaxLength(double maxLength) { |
| 98 | + this.maxLength = maxLength; |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + public double getStep() { |
| 103 | + return step; |
| 104 | + } |
| 105 | + |
| 106 | + public Ray setStep(double step) { |
| 107 | + this.step = step; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public double getRange() { |
| 112 | + return range; |
| 113 | + } |
| 114 | + |
| 115 | + public Ray setRange(double range) { |
| 116 | + this.range = range; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public RayStopType getStopType() { |
| 121 | + return stopType; |
| 122 | + } |
| 123 | + |
| 124 | + public Ray setStopType(RayStopType stopType) { |
| 125 | + this.stopType = stopType; |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + public Consumer<Entity> getHitEntityConsumer() { |
| 130 | + return hitEntityConsumer; |
| 131 | + } |
| 132 | + |
| 133 | + public Ray setHitEntityConsumer(Consumer<Entity> hitEntityConsumer) { |
| 134 | + this.hitEntityConsumer = hitEntityConsumer; |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + public Predicate<Entity> getEntityFilter() { |
| 139 | + return entityFilter; |
| 140 | + } |
| 141 | + |
| 142 | + public Ray setEntityFilter(Predicate<Entity> entityFilter) { |
| 143 | + this.entityFilter = entityFilter; |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + public enum RayStopType { |
| 148 | + /** |
| 149 | + * 固定长度(同时也是最大长度) |
| 150 | + */ |
| 151 | + MAX_LENGTH, |
| 152 | + /** |
| 153 | + * 碰撞至实体时停止 |
| 154 | + */ |
| 155 | + HIT_ENTITY, |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments