Skip to content

Commit bd515cc

Browse files
committed
Add initial jab at the track API
1 parent 5466603 commit bd515cc

4 files changed

Lines changed: 161 additions & 1 deletion

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package trackapi.compat;
2+
3+
import net.minecraft.block.BlockRailBase;
4+
import net.minecraft.block.BlockRailBase.EnumRailDirection;
5+
import net.minecraft.block.state.IBlockState;
6+
import net.minecraft.util.math.BlockPos;
7+
import net.minecraft.util.math.Vec3d;
8+
import net.minecraft.world.World;
9+
import trackapi.lib.ITrackTile;
10+
import trackapi.lib.Util;
11+
12+
public class MinecraftRail implements ITrackTile {
13+
14+
private EnumRailDirection direction;
15+
16+
public MinecraftRail(World world, BlockPos pos) {
17+
IBlockState state = world.getBlockState(pos);
18+
BlockRailBase blockrailbase = (BlockRailBase)state.getBlock();
19+
this.direction = blockrailbase.getRailDirection(world, pos, state, null);
20+
}
21+
22+
@Override
23+
public double getTrackGauge() {
24+
return Util.MINECRAFT_GAUGE;
25+
}
26+
27+
@Override
28+
public double getTrackSlope() {
29+
return direction == EnumRailDirection.ASCENDING_EAST ||
30+
direction == EnumRailDirection.ASCENDING_WEST ||
31+
direction == EnumRailDirection.ASCENDING_NORTH ||
32+
direction == EnumRailDirection.ASCENDING_SOUTH ? 1 : 0;
33+
}
34+
35+
@Override
36+
public Vec3d getNextPosition(Vec3d currentPosition, float rotationYaw, float bogeyYaw, double distance) {
37+
if (distance < 0) {
38+
distance = -distance;
39+
rotationYaw = (rotationYaw + 180) % 360;
40+
bogeyYaw = (bogeyYaw + 180) % 360;
41+
}
42+
43+
//TODO fill in the rest of the states
44+
45+
switch (direction) {
46+
case ASCENDING_EAST:
47+
break;
48+
case ASCENDING_NORTH:
49+
break;
50+
case ASCENDING_SOUTH:
51+
break;
52+
case ASCENDING_WEST:
53+
break;
54+
case EAST_WEST:
55+
break;
56+
case NORTH_EAST:
57+
break;
58+
case NORTH_SOUTH:
59+
if (rotationYaw / 180 == 0) {
60+
return currentPosition.addVector(0, 0, distance);
61+
}
62+
return currentPosition.addVector(0, 0, -distance);
63+
case NORTH_WEST:
64+
break;
65+
case SOUTH_EAST:
66+
break;
67+
case SOUTH_WEST:
68+
break;
69+
}
70+
71+
return currentPosition;
72+
}
73+
74+
public static boolean isRail(World world, BlockPos pos) {
75+
return BlockRailBase.isRailBlock(world, pos);
76+
}
77+
78+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package trackapi.lib;
2+
3+
import net.minecraft.util.math.Vec3d;
4+
5+
public interface ITrackTile {
6+
7+
/**
8+
* The distance between the rails measured in meters
9+
*
10+
* @see Util#STANDARD_GAUGE
11+
* @see Util#MINECRAFT_GAUGE
12+
*/
13+
public double getTrackGauge();
14+
15+
/**
16+
* The slope of the track. For example, going up 1 meter for every 100 meters in
17+
* length would equal 1/100 or 0.01
18+
*/
19+
public double getTrackSlope();
20+
21+
/**
22+
* Used by rolling stock to look up their next position.
23+
*
24+
* @param currentPosition - Current entity or bogey position
25+
* @param rotationYaw - Current entity rotation in degrees
26+
* @param bogieYaw - Current bogey rotation in degrees (set to rotationYaw if unused)
27+
* @param distance - Distanced traveled in meters
28+
* @return The new position of the entity or bogey
29+
*/
30+
public Vec3d getNextPosition(Vec3d currentPosition, float rotationYaw, float bogeyYaw, double distance);
31+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package trackapi.lib;
2+
3+
import net.minecraft.tileentity.TileEntity;
4+
import net.minecraft.util.math.BlockPos;
5+
import net.minecraft.util.math.MathHelper;
6+
import net.minecraft.util.math.Vec3d;
7+
import net.minecraft.world.World;
8+
import trackapi.compat.MinecraftRail;
9+
10+
public class Util {
11+
/**
12+
* US Standard Gauge in meters
13+
*/
14+
public static final double STANDARD_GAUGE = 1.435;
15+
16+
/**
17+
* Minecraft Gauge in meters
18+
*/
19+
public static final double MINECRAFT_GAUGE = 0.632;
20+
21+
private static ITrackTile getInternalTileEntity(World world, Vec3d pos, boolean acceptMinecraftRails) {
22+
BlockPos bp = new BlockPos(MathHelper.floor(pos.x), MathHelper.floor(pos.y), MathHelper.floor(pos.z));
23+
TileEntity te = world.getTileEntity(bp);
24+
if (te instanceof ITrackTile) {
25+
return (ITrackTile) te;
26+
}
27+
if (acceptMinecraftRails) {
28+
if (MinecraftRail.isRail(world, bp)) {
29+
return new MinecraftRail(world, bp);
30+
}
31+
}
32+
return null;
33+
}
34+
35+
public static ITrackTile getTileEntity(World world, Vec3d pos, boolean acceptMinecraftRails) {
36+
ITrackTile track = getInternalTileEntity(world, pos, acceptMinecraftRails);
37+
if (track != null) {
38+
return track;
39+
}
40+
// Allow a bit of vertical fuzziness
41+
track = getInternalTileEntity(world, pos.addVector(0, 0.4, 0), acceptMinecraftRails);
42+
if (track != null) {
43+
return track;
44+
}
45+
track = getInternalTileEntity(world, pos.addVector(0, -0.4, 0), acceptMinecraftRails);
46+
if (track != null) {
47+
return track;
48+
}
49+
return null;
50+
}
51+
}

src/main/resources/pack.mcmeta

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"pack": {
33
"description": "trackapi resources",
4-
"pack_format": 3,
4+
"pack_format": 3
55
}
66
}

0 commit comments

Comments
 (0)