Skip to content

Commit 8d3f3e2

Browse files
committed
修复Barrage在速度过快时会掠过实体的问题
1 parent d8e0efd commit 8d3f3e2

7 files changed

Lines changed: 254 additions & 15 deletions

File tree

common/src/main/kotlin/cn/coostack/cooparticlesapi/barrages/AbstractBarrage.kt

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import com.google.common.base.Predicate
66
import net.minecraft.core.BlockPos
77
import net.minecraft.server.level.ServerLevel
88
import net.minecraft.world.entity.LivingEntity
9+
import net.minecraft.world.phys.AABB
910
import net.minecraft.world.phys.Vec3
1011
import java.util.UUID
1112
import kotlin.math.max
13+
import kotlin.math.min
1214

1315
abstract class AbstractBarrage(
1416
override var loc: Vec3,
@@ -61,6 +63,7 @@ abstract class AbstractBarrage(
6163
}
6264

6365
// 判定速度
66+
val previousLoc = loc
6467
if (options.enableSpeed) {
6568
loc = loc.add(direction.normalize().scale(options.speed))
6669
options.speed += options.acceleration
@@ -109,7 +112,7 @@ abstract class AbstractBarrage(
109112
spawnTick++
110113
return
111114
}
112-
val collection = hitBoxEntities().filter {
115+
val collection = hitBoxEntities(previousLoc, loc).filter {
113116
return@filter filterHitEntity(it)
114117
}
115118
if (collection.isNotEmpty()) {
@@ -160,15 +163,43 @@ abstract class AbstractBarrage(
160163
override fun noclip(): Boolean = spawnTick < options.noneHitBoxTick
161164

162165
fun hitBoxEntities(): Set<LivingEntity> {
163-
val res = HashSet<LivingEntity>()
164-
res.addAll(world.getEntitiesOfClass(LivingEntity::class.java, hitBox.ofBox(loc), { true }))
165-
return res
166+
return hitBoxEntities(loc, loc)
166167
}
167168

168169
fun hitBoxEntities(filter: Predicate<LivingEntity>): Set<LivingEntity> {
170+
return hitBoxEntities(loc, loc, filter)
171+
}
172+
173+
fun hitBoxEntities(from: Vec3, to: Vec3): Set<LivingEntity> {
174+
return hitBoxEntities(from, to, Predicate { true })
175+
}
176+
177+
fun hitBoxEntities(from: Vec3, to: Vec3, filter: Predicate<LivingEntity>): Set<LivingEntity> {
169178
val res = HashSet<LivingEntity>()
170-
res.addAll(world.getEntitiesOfClass(LivingEntity::class.java, hitBox.ofBox(loc), filter))
179+
val fromBox = hitBox.ofBox(from)
180+
val toBox = hitBox.ofBox(to)
181+
val sweepBox = AABB(
182+
min(fromBox.minX, toBox.minX),
183+
min(fromBox.minY, toBox.minY),
184+
min(fromBox.minZ, toBox.minZ),
185+
max(fromBox.maxX, toBox.maxX),
186+
max(fromBox.maxY, toBox.maxY),
187+
max(fromBox.maxZ, toBox.maxZ),
188+
)
189+
world.getEntitiesOfClass(LivingEntity::class.java, sweepBox, filter).forEach { entity ->
190+
val expandedEntityBox = AABB(
191+
entity.boundingBox.minX - hitBox.x2,
192+
entity.boundingBox.minY - hitBox.y2,
193+
entity.boundingBox.minZ - hitBox.z2,
194+
entity.boundingBox.maxX - hitBox.x1,
195+
entity.boundingBox.maxY - hitBox.y1,
196+
entity.boundingBox.maxZ - hitBox.z1,
197+
)
198+
if (expandedEntityBox.contains(from) || expandedEntityBox.clip(from, to).isPresent) {
199+
res.add(entity)
200+
}
201+
}
171202
return res
172203
}
173204

174-
}
205+
}

common/src/main/kotlin/cn/coostack/cooparticlesapi/network/particle/composition/ParticleComposition.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ abstract class ParticleComposition(var position: Vec3, var world: Level? = null)
174174
Math3DUtil.rotatePointsToPoint(
175175
map.values.toList(), to, axis
176176
)
177-
this.axis = to
177+
this.axis = to.clone()
178178
}
179179

180180
open fun preRotateAsAxis(map: Map<CompositionData, RelativeLocation>, axis: RelativeLocation, angle: Double) {
181181
Math3DUtil.rotateAsAxis(
182182
map.values.toList(), axis, angle
183183
)
184-
this.axis = axis
184+
this.axis = axis.clone()
185185
}
186186

187187
open fun preRotateAsAxis(map: Map<CompositionData, RelativeLocation>, angle: Double) {
@@ -325,14 +325,23 @@ abstract class ParticleComposition(var position: Vec3, var world: Level? = null)
325325
} else if (this.roll <= -2 * PI) {
326326
this.roll += 2 * PI
327327
}
328+
328329
if (!client) {
329-
axis = to
330+
axis.apply {
331+
this.x = to.x
332+
this.y = to.y
333+
this.z = to.z
334+
}
330335
return
331336
}
332337
Math3DUtil.rotateToWithRoll(
333338
particleRotatedLocations, axis, to, radian
334339
)
335-
axis = to
340+
axis.apply {
341+
this.x = to.x
342+
this.y = to.y
343+
this.z = to.z
344+
}
336345
toggleRelative()
337346
}
338347

common/src/main/kotlin/cn/coostack/cooparticlesapi/network/particle/composition/SequencedParticleComposition.kt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,21 @@ abstract class SequencedParticleComposition(position: Vec3, world: Level? = null
358358

359359
override fun rotateToPoint(to: RelativeLocation) {
360360
if (!client) {
361-
axis = to
361+
axis.apply {
362+
this.x = to.x
363+
this.y = to.y
364+
this.z = to.z
365+
}
362366
return
363367
}
364368
Math3DUtil.rotatePointsToPoint(
365369
particleRotatedLocations, to, axis
366370
)
367-
axis = to
371+
axis.apply {
372+
this.x = to.x
373+
this.y = to.y
374+
this.z = to.z
375+
}
368376
toggleRelative()
369377
}
370378

@@ -376,13 +384,21 @@ abstract class SequencedParticleComposition(position: Vec3, world: Level? = null
376384
this.roll += 2 * PI
377385
}
378386
if (!client) {
379-
axis = to
387+
axis.apply {
388+
this.x = to.x
389+
this.y = to.y
390+
this.z = to.z
391+
}
380392
return
381393
}
382394
Math3DUtil.rotateToWithRoll(
383395
particleRotatedLocations, axis, to, radian
384396
)
385-
axis = to
397+
axis.apply {
398+
this.x = to.x
399+
this.y = to.y
400+
this.z = to.z
401+
}
386402
toggleRelative()
387403
}
388404

common/src/main/kotlin/cn/coostack/cooparticlesapi/test/APITestGroupBuilder.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import cn.coostack.cooparticlesapi.test.options.animate.TestStyleAction
1616
import cn.coostack.cooparticlesapi.test.options.display.BarrageItemDisplayEntity
1717
import cn.coostack.cooparticlesapi.test.options.particle.composition.TestComposition
1818
import cn.coostack.cooparticlesapi.test.options.display.TestBlockDisplayEntity
19+
import cn.coostack.cooparticlesapi.test.options.particle.composition.GenNewComposition
1920
import cn.coostack.cooparticlesapi.test.options.particle.composition.TestFourierPhotoComposition
2021
import cn.coostack.cooparticlesapi.test.options.particle.composition.TestGlowingAnimationComposition
2122
import cn.coostack.cooparticlesapi.test.options.particle.composition.TestModelComposition
@@ -200,6 +201,8 @@ class APITestGroupBuilder(val player: Player) : TestGroupBuilder {
200201
SimpleEmitterOption(TestRespawnEmitter(player.eyePosition, player.level()).apply {
201202
maxTick = 200
202203
}, -1)
204+
}.appendOption {
205+
SimpleCompositionOption(GenNewComposition(player.eyePosition, player.level()), -1)
203206
}
204207
}
205208
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package cn.coostack.cooparticlesapi.test.options.particle.composition
2+
3+
import cn.coostack.cooparticlesapi.annotations.CodecField
4+
import cn.coostack.cooparticlesapi.annotations.CooAutoRegister
5+
import cn.coostack.cooparticlesapi.network.particle.composition.*
6+
import cn.coostack.cooparticlesapi.particles.ParticleDisplayer
7+
import cn.coostack.cooparticlesapi.particles.impl.*
8+
import cn.coostack.cooparticlesapi.utils.RelativeLocation
9+
import cn.coostack.cooparticlesapi.utils.builder.PointsBuilder
10+
import net.minecraft.world.level.Level
11+
import net.minecraft.world.phys.Vec3
12+
import kotlin.math.PI
13+
import kotlin.random.Random
14+
import java.util.SortedMap
15+
import java.util.TreeMap
16+
import org.joml.Vector3f
17+
18+
@CooAutoRegister
19+
class GenNewComposition(position: Vec3, world: Level? = null) : AutoParticleComposition(position, world) {
20+
@CodecField
21+
var color: Vector3f = Vector3f(1f, 0.321569f, 0.321569f)
22+
23+
@CodecField
24+
var tickCount: Int = 0
25+
26+
init {
27+
axis = RelativeLocation.yAxis()
28+
setDisabledInterval(20)
29+
}
30+
31+
override fun getParticles(): Map<CompositionData, RelativeLocation> {
32+
val result = LinkedHashMap<CompositionData, RelativeLocation>()
33+
34+
run {
35+
val rel = RelativeLocation(0.0, 0.0, 0.0)
36+
result[
37+
CompositionData()
38+
.setDisplayerSupplier {
39+
ParticleDisplayer.withComposition(
40+
ParticleShapeComposition(it).apply {
41+
axis = RelativeLocation.yAxis()
42+
loadScaleHelperBezierValue(
43+
0.01,
44+
1.0,
45+
20,
46+
RelativeLocation(6.653289, 1.0, 0.0),
47+
RelativeLocation(11.005575, 1.0, 0.0)
48+
)
49+
applyBuilder(
50+
PointsBuilder()
51+
.addCircle(10.0, 6)
52+
) { shapeRel0 ->
53+
CompositionData()
54+
.setDisplayerSupplier {
55+
ParticleDisplayer.withComposition(
56+
ParticleShapeComposition(it).apply {
57+
axis = RelativeLocation.yAxis()
58+
loadScaleHelperBezierValue(
59+
0.01,
60+
1.0,
61+
18,
62+
RelativeLocation(14.25552, 0.788725, 0.0),
63+
RelativeLocation(13.035452, 1.0, 0.0)
64+
)
65+
applyBuilder(
66+
PointsBuilder()
67+
.addWith {
68+
val res = arrayListOf<RelativeLocation>()
69+
getPolygonInCircleVertices(6, 3.0)
70+
.forEach { it ->
71+
val p = PointsBuilder()
72+
.addFillTriangle(
73+
RelativeLocation(
74+
0.0,
75+
0.0,
76+
2.0
77+
),
78+
RelativeLocation(-1.0, 0.0, 1.0),
79+
RelativeLocation(1.0, 0.0, 1.0),
80+
5.0
81+
)
82+
.addFillTriangle(
83+
RelativeLocation(
84+
0.0,
85+
0.0,
86+
-2.0
87+
),
88+
RelativeLocation(-1.0, 0.0, -1.0),
89+
RelativeLocation(1.0, 0.0, -1.0),
90+
5.0
91+
)
92+
.axis(RelativeLocation(0.0, 0.0, 1.0))
93+
p.rotateTo(-it)
94+
res.addAll(
95+
p
96+
.pointsOnEach { rel -> rel.add(it) }
97+
.createWithoutClone()
98+
)
99+
}
100+
res
101+
}
102+
.addBuilder(
103+
RelativeLocation(0.0, 0.0, 0.0),
104+
PointsBuilder()
105+
.addPolygonInCircle(6, 30, 5.0)
106+
.rotateAsAxis(
107+
0.166667 * PI,
108+
RelativeLocation(0.0, 1.0, 0.0)
109+
)
110+
)
111+
.addPolygonInCircle(6, 30, 5.0)
112+
.addLine(
113+
RelativeLocation(0.0, -7.0, 0.0),
114+
RelativeLocation(0.0, 9.0, 0.0),
115+
200
116+
)
117+
) { shapeRel1 ->
118+
CompositionData()
119+
.setDisplayerSupplier {
120+
ParticleDisplayer.withSingle(ControlableEndRodEffect(it))
121+
}
122+
.addParticleInstanceInit {
123+
particleAlpha = 0.2F
124+
size = 0.2F
125+
color = this@GenNewComposition.color
126+
}
127+
.addParticleControlerInstanceInit {
128+
addPreTickAction {
129+
particleAlpha = (tickCount / 20.0F).toFloat()
130+
if (this@GenNewComposition.status.displayStatus == 2) {
131+
color = this@GenNewComposition.color
132+
} else {
133+
color = Vector3f(1F, 0F, 1F)
134+
}
135+
}
136+
}
137+
}
138+
applyDisplayAction {
139+
addPreTickAction {
140+
rotateToWithAngle(shapeRel0, PI / 32)
141+
}
142+
setReversedScaleOnCompositionStatus(this@GenNewComposition)
143+
}
144+
}
145+
)
146+
}
147+
}
148+
applyDisplayAction {
149+
addPreTickAction {
150+
rotateAsAxis(PI / 256)
151+
}
152+
setReversedScaleOnCompositionStatus(this@GenNewComposition)
153+
}
154+
}
155+
)
156+
}
157+
] = rel
158+
}
159+
160+
return result
161+
}
162+
163+
override fun onDisplay() {
164+
addPreTickAction {
165+
if (tickCount++ > 600) {
166+
status.disable()
167+
}
168+
}
169+
}
170+
}

common/src/main/kotlin/cn/coostack/cooparticlesapi/test/options/particle/emitter/TestRespawnEmitter.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class TestRespawnEmitter(pos: Vec3, world: Level?) : AutoParticleEmitters(pos, w
5959
if (respawnCount > 2) {
6060
return listOf()
6161
}
62-
println("粒子移除 原因: $reason")
6362
return PointsBuilder()
6463
.addBall(0.5, 4)
6564
.createWithoutClone()

common/src/main/kotlin/cn/coostack/cooparticlesapi/utils/helper/StatusHelper.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ abstract class StatusHelper : ParticleHelper {
4747
changeStatus(status)
4848
}
4949

50+
fun isDisable(): Boolean = displayStatus == 2
51+
fun disable() {
52+
displayStatus = 2
53+
}
54+
55+
fun isEnable(): Boolean = displayStatus == 1
56+
57+
fun enable() {
58+
displayStatus = 1
59+
}
60+
5061
fun setStatus(status: Status) {
5162
this.displayStatus = status.id
5263
changeStatus(status.id)

0 commit comments

Comments
 (0)