Skip to content

Commit 97aff0a

Browse files
author
gegy1000
committed
fix hoop intersection
1 parent dfedccf commit 97aff0a

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

src/main/java/io/github/restioson/loopdeloop/game/LoopDeLoopActive.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@
77
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
88
import net.minecraft.entity.Entity;
99
import net.minecraft.entity.EntityType;
10-
import net.minecraft.entity.projectile.FireworkRocketEntity;
11-
import net.minecraft.world.World;
12-
import xyz.nucleoid.plasmid.game.GameWorld;
13-
import xyz.nucleoid.plasmid.game.event.*;
14-
import xyz.nucleoid.plasmid.game.player.JoinResult;
15-
import xyz.nucleoid.plasmid.game.rule.GameRule;
16-
import xyz.nucleoid.plasmid.game.rule.RuleResult;
17-
import xyz.nucleoid.plasmid.util.ItemStackBuilder;
1810
import net.minecraft.entity.EquipmentSlot;
1911
import net.minecraft.entity.damage.DamageSource;
12+
import net.minecraft.entity.projectile.FireworkRocketEntity;
2013
import net.minecraft.item.ItemStack;
2114
import net.minecraft.item.Items;
2215
import net.minecraft.network.packet.s2c.play.TitleS2CPacket;
@@ -35,8 +28,26 @@
3528
import net.minecraft.util.math.MathHelper;
3629
import net.minecraft.util.math.Vec3d;
3730
import net.minecraft.world.GameMode;
31+
import xyz.nucleoid.plasmid.game.GameWorld;
32+
import xyz.nucleoid.plasmid.game.event.GameCloseListener;
33+
import xyz.nucleoid.plasmid.game.event.GameOpenListener;
34+
import xyz.nucleoid.plasmid.game.event.GameTickListener;
35+
import xyz.nucleoid.plasmid.game.event.OfferPlayerListener;
36+
import xyz.nucleoid.plasmid.game.event.PlayerAddListener;
37+
import xyz.nucleoid.plasmid.game.event.PlayerDamageListener;
38+
import xyz.nucleoid.plasmid.game.event.PlayerDeathListener;
39+
import xyz.nucleoid.plasmid.game.event.PlayerRemoveListener;
40+
import xyz.nucleoid.plasmid.game.player.JoinResult;
41+
import xyz.nucleoid.plasmid.game.rule.GameRule;
42+
import xyz.nucleoid.plasmid.game.rule.RuleResult;
43+
import xyz.nucleoid.plasmid.util.ItemStackBuilder;
44+
3845
import javax.annotation.Nullable;
39-
import java.util.*;
46+
import java.util.ArrayList;
47+
import java.util.Iterator;
48+
import java.util.List;
49+
import java.util.Map;
50+
import java.util.Set;
4051

4152
public final class LoopDeLoopActive {
4253
private final GameWorld gameWorld;
@@ -259,7 +270,7 @@ private void tickPlayers(long time) {
259270
//
260271
// If they are moving slow, there may not be enough precision to detect this, so the slow path is fallen back
261272
// to, simply checking if the end coordinate is inside of the hoop.
262-
if (hoop.intersectsSegment(state.lastPos, player.getPos()) || hoop.contains(player.getBlockPos())) {
273+
if (hoop.intersectsSegment(state.lastPos, player.getPos())) {
263274
player.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 1.0F, 1.0F);
264275
giveRocket(player, 1);
265276
state.lastHoop += 1;

src/main/java/io/github/restioson/loopdeloop/game/map/LoopDeLoopHoop.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.restioson.loopdeloop.game.map;
22

33
import net.minecraft.util.math.BlockPos;
4+
import net.minecraft.util.math.MathHelper;
45
import net.minecraft.util.math.Vec3d;
56

67
import javax.annotation.Nullable;
@@ -15,25 +16,36 @@ public LoopDeLoopHoop(BlockPos centre, int radius) {
1516
}
1617

1718
public boolean intersectsSegment(Vec3d begin, Vec3d end) {
18-
Vec3d intersection = lineIntersectsPlane(begin, end, centre.getZ() + 0.5);
19+
// if we contain the end position, we are inside the hoop
20+
if (this.contains(end)) {
21+
return true;
22+
}
1923

24+
// find the intersection between the line and the hoop plane
25+
Vec3d intersection = lineIntersectsPlane(begin, end, centre.getZ() + 0.5);
2026
if (intersection == null) {
21-
return false; // No intersection - line parallel
27+
// no intersection
28+
return false;
2229
}
2330

24-
// radius - 1 is to avoid allowing people to go on top of corners
25-
return this.contains(new BlockPos(intersection));
31+
// check if the intersection point is contained within the loop
32+
return this.contains(intersection.x, intersection.y);
33+
}
34+
35+
public boolean contains(Vec3d pos) {
36+
return MathHelper.floor(pos.getZ()) == this.centre.getZ()
37+
&& this.contains(pos.getX(), pos.getY());
2638
}
2739

28-
public boolean contains(BlockPos pos) {
40+
private boolean contains(double x, double y) {
2941
int adjRadius = this.radius - 1; // radius - 1 is to avoid allowing people to go on top of corners
30-
int dx = pos.getX() - this.centre.getX();
31-
int dy = pos.getY() - this.centre.getY();
32-
return pos.getZ() == this.centre.getZ() && (dx * dx + dy * dy <= adjRadius * adjRadius);
42+
int dx = MathHelper.floor(x) - this.centre.getX();
43+
int dy = MathHelper.floor(y) - this.centre.getY();
44+
return dx * dx + dy * dy <= adjRadius * adjRadius;
3345
}
3446

3547
@Nullable
36-
public static Vec3d lineIntersectsPlane(Vec3d origin, Vec3d target, double planeZ) {
48+
private static Vec3d lineIntersectsPlane(Vec3d origin, Vec3d target, double planeZ) {
3749
Vec3d ray = target.subtract(origin);
3850
if (Math.abs(ray.z) <= 1e-5) {
3951
return null;

0 commit comments

Comments
 (0)