Skip to content

Commit e9630ed

Browse files
committed
Things
1 parent 0628a78 commit e9630ed

File tree

20 files changed

+245
-43
lines changed

20 files changed

+245
-43
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.carsoncoder.gpws;
2+
3+
public class gpwsConfig {
4+
int PullUpRange = 50;
5+
int BankAngleAngle = -50;
6+
int Volume = 100;
7+
int SoundDelay = 60;
8+
}

src/client/java/com/carsoncoder/gpws/gpwsElytraClient.java

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
package com.carsoncoder.gpws;
22

3+
import org.joml.Vector3f;
34
import org.slf4j.Logger;
45

56
import net.fabricmc.api.ClientModInitializer;
67
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
78
import net.minecraft.client.MinecraftClient;
89
import net.minecraft.entity.Entity;
9-
import net.minecraft.registry.Registries;
10-
import net.minecraft.registry.Registry;
11-
import net.minecraft.sound.SoundEvent;
12-
import net.minecraft.util.Identifier;
10+
import net.minecraft.util.hit.HitResult;
11+
import net.minecraft.util.math.BlockPos;
12+
import net.minecraft.util.math.Vec3d;
13+
import net.minecraft.world.Heightmap;
14+
import net.minecraft.world.chunk.Chunk;
1315
import nl.enjarai.cicada.api.conversation.ConversationManager;
1416
import nl.enjarai.cicada.api.util.CicadaEntrypoint;
1517
import nl.enjarai.cicada.api.util.JsonSource;
1618
import nl.enjarai.cicada.api.util.ProperLogger;
1719

1820
public class gpwsElytraClient implements ClientModInitializer, CicadaEntrypoint {
21+
public static gpwsConfig config = new gpwsConfig();
1922
public static final Logger LOGGER = ProperLogger.getLogger("gpws-elytra");
2023
public static gpwsElytraClient instance;
21-
22-
public static final Identifier MY_SOUND_ID = new Identifier("gpws-elytra:retard");
23-
public static SoundEvent RETARD = SoundEvent.of(MY_SOUND_ID);
24+
public static final gpwsSounds SOUNDS_MANAGER = new gpwsSounds();
2425

2526
private String gpwsState = "Loading";
2627

2728
@Override
2829
public void onInitializeClient() {
2930
instance = this;
31+
SOUNDS_MANAGER.init();
3032
HudRenderCallback.EVENT.register(new gpwsHud(this));
31-
Registry.register(Registries.SOUND_EVENT, gpwsElytraClient.MY_SOUND_ID, RETARD);
3233
}
3334

3435
@Override
@@ -42,27 +43,76 @@ public void registerConversations(ConversationManager conversationManager) {
4243

4344
public void tick()
4445
{
45-
LOGGER.debug("ticked");
4646
gpwsState = "";
4747
}
4848

49-
private String StateLogic() {
50-
int StallAngle = -50;
49+
private boolean PullUp(float delta) {
50+
MinecraftClient client = MinecraftClient.getInstance();
51+
int width = client.getWindow().getScaledWidth();
52+
int height = client.getWindow().getScaledHeight();
53+
Vec3d cameraDirection = client.cameraEntity.getRotationVec(delta);
54+
double fov = client.options.getFov().getValue();
55+
double angleSize = fov/height;
56+
Vector3f verticalRotationAxis = cameraDirection.toVector3f();
57+
verticalRotationAxis.cross(new Vector3f(0, 1, 0));
58+
if(verticalRotationAxis.normalize() == null) {
59+
return true;//The camera is pointing directly up or down, you'll have to fix this one
60+
}
61+
62+
Vector3f horizontalRotationAxis = cameraDirection.toVector3f();
63+
horizontalRotationAxis.cross(verticalRotationAxis);
64+
horizontalRotationAxis.normalize();
65+
66+
verticalRotationAxis = cameraDirection.toVector3f();
67+
verticalRotationAxis.cross(horizontalRotationAxis);
68+
Vec3d direction = gpwsRaycast.map(
69+
(float) angleSize,
70+
cameraDirection,
71+
horizontalRotationAxis,
72+
verticalRotationAxis,
73+
0,
74+
0,
75+
width,
76+
height
77+
);
78+
HitResult hit = gpwsRaycast.raycastInDirection(client, delta, direction);
79+
80+
switch(hit.getType()) {
81+
case MISS:
82+
return false;
83+
default:
84+
return true;
85+
}
86+
}
87+
88+
private String StateLogic(float delta) {
89+
int StallAngle = config.BankAngleAngle;
5190

5291
Entity cam = MinecraftClient.getInstance().getCameraEntity();
92+
BlockPos pos = MinecraftClient.getInstance().player.getBlockPos();
93+
Chunk chunk = MinecraftClient.getInstance().world.getChunk(pos);
94+
Heightmap heightmap = chunk.getHeightmap(Heightmap.Type.WORLD_SURFACE);
95+
96+
int YPos = (int)Math.round(MinecraftClient.getInstance().player.getBodyY(1)) - heightmap.get(Math.max(Math.min(pos.getX() - chunk.getPos().getStartX(), 15), 0), Math.max(Math.min(pos.getX() - chunk.getPos().getStartZ(), 15), 0));
97+
double DownVel = MinecraftClient.getInstance().player.getVelocity().y;
5398
float Pitch = cam.getPitch();
54-
LOGGER.info(String.valueOf(Pitch));
99+
100+
LOGGER.info(String.valueOf(heightmap.get(Math.max(Math.min(pos.getX() - chunk.getPos().getStartX(), 15), 0), Math.max(Math.min(pos.getX() - chunk.getPos().getStartZ(), 15), 0))));
55101

56102
if (Pitch < StallAngle) {
57103
return "Bank Angle";
104+
} else if (PullUp(delta)) {
105+
return "Pull Up";
106+
} else if (YPos <= 2500) {
107+
return String.valueOf(YPos);
58108
}
59109

60110
return "";
61111
}
62112

63-
public String GetState() {
113+
public String GetState(float delta) {
64114
if (gpwsState == "") {
65-
gpwsState = StateLogic();
115+
gpwsState = StateLogic(delta);
66116
}
67117
return gpwsState;
68118
}

src/client/java/com/carsoncoder/gpws/gpwsHud.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.carsoncoder.gpws;
22

33

4+
import java.util.HashMap;
5+
46
import org.slf4j.Logger;
57

68
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
@@ -22,10 +24,10 @@ public class gpwsHud implements HudRenderCallback {
2224

2325
TextRenderer renderer;
2426

25-
int repeatTime = 3;
27+
int repeatTime = gpwsElytraClient.config.SoundDelay;
2628

2729
String prevstate = "";
28-
float lastPlayedSoundTime = 0;
30+
float lastPlayedSoundTime = 100;
2931

3032
class Color {
3133
public int R;
@@ -43,6 +45,8 @@ public Color(int R, int G, int B)
4345
private gpwsElytraClient client;
4446
private Color white = new Color(255, 255, 255);
4547

48+
private HashMap<Integer, gpwsSounds.SOUNDS> times;
49+
4650
public gpwsHud(gpwsElytraClient c) {
4751
client = c;
4852
}
@@ -86,9 +90,9 @@ public void renderText(DrawContext ctx, Text text, Color color, int x, int y) {
8690

8791
private void PlaySound(SoundEvent sound) {
8892
MinecraftClient.getInstance().getSoundManager().play(new EntityTrackingSoundInstance(
89-
gpwsElytraClient.RETARD,
90-
SoundCategory.BLOCKS,
91-
1f,
93+
sound,
94+
SoundCategory.VOICE,
95+
gpwsElytraClient.config.Volume,
9296
1f,
9397
MinecraftClient.getInstance().player,
9498
Random.create().nextLong()));
@@ -103,6 +107,10 @@ private void PlaySound(SoundEvent sound) {
103107
// );
104108
}
105109

110+
private void PlaySound(gpwsSounds.SOUNDS sound) {
111+
PlaySound(gpwsElytraClient.SOUNDS_MANAGER.GetSound(sound));
112+
}
113+
106114
@Override
107115
public void onHudRender(DrawContext drawContext, float tickDelta) {
108116
if (!client.isFallFlying()) {
@@ -114,15 +122,20 @@ public void onHudRender(DrawContext drawContext, float tickDelta) {
114122
renderer = MinecraftClient.getInstance().textRenderer;
115123
}
116124

117-
String state = client.GetState();
125+
String state = client.GetState(tickDelta);
118126

119-
if (state != prevstate) {
120-
PlaySound(gpwsElytraClient.RETARD);
127+
if (state == "Bank Angle" && lastPlayedSoundTime > repeatTime) {
128+
PlaySound(gpwsSounds.SOUNDS.BANK_ANGLE);
129+
lastPlayedSoundTime = 0;
130+
} else if (state == "Pull Up" && lastPlayedSoundTime > repeatTime) {
131+
PlaySound(gpwsSounds.SOUNDS.PULL_UP);
132+
lastPlayedSoundTime = 0;
121133
}
134+
// LOGGER.info(String.valueOf(lastPlayedSoundTime));
122135

123136
// Render Bold Underlined Hello In the center of the screen
124137
Text gpwsText = newText(
125-
client.GetState(),
138+
client.GetState(tickDelta),
126139
false,
127140
false,
128141
false,
@@ -139,5 +152,6 @@ public void onHudRender(DrawContext drawContext, float tickDelta) {
139152
);
140153

141154
prevstate = state;
155+
lastPlayedSoundTime += tickDelta;
142156
}
143157
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.carsoncoder.gpws;
2+
3+
import org.joml.Vector3f;
4+
5+
import net.minecraft.client.MinecraftClient;
6+
import net.minecraft.entity.Entity;
7+
import net.minecraft.entity.LivingEntity;
8+
import net.minecraft.entity.decoration.ItemFrameEntity;
9+
import net.minecraft.entity.projectile.ProjectileUtil;
10+
import net.minecraft.util.hit.EntityHitResult;
11+
import net.minecraft.util.hit.HitResult;
12+
import net.minecraft.util.math.Box;
13+
import net.minecraft.util.math.Vec3d;
14+
import net.minecraft.world.RaycastContext;
15+
16+
public class gpwsRaycast {
17+
public static Vec3d map(float anglePerPixel, Vec3d center, Vector3f horizontalRotationAxis,
18+
Vector3f verticalRotationAxis, int x, int y, int width, int height) {
19+
20+
final Vector3f temp2 = center.toVector3f();
21+
return new Vec3d(temp2);
22+
}
23+
24+
public static HitResult raycastInDirection(MinecraftClient client, float tickDelta, Vec3d direction) {
25+
Vec3d cameraDirection = client.cameraEntity.getRotationVec(tickDelta);
26+
Vector3f verticalRotationAxis = cameraDirection.toVector3f();
27+
verticalRotationAxis.cross(new Vector3f(0, 1, 0));
28+
if(verticalRotationAxis.normalize() == null) {
29+
return null;//The camera is pointing directly up or down, you'll have to fix this one
30+
}
31+
32+
Vector3f horizontalRotationAxis = cameraDirection.toVector3f();
33+
horizontalRotationAxis.cross(verticalRotationAxis);
34+
horizontalRotationAxis.normalize();
35+
36+
verticalRotationAxis = cameraDirection.toVector3f();
37+
verticalRotationAxis.cross(horizontalRotationAxis);
38+
39+
Entity entity = client.getCameraEntity();
40+
if (entity == null || client.world == null) {
41+
return null;
42+
}
43+
44+
double reachDistance = gpwsElytraClient.config.PullUpRange;//Change this to extend the reach
45+
HitResult target = raycast(entity, reachDistance, tickDelta, false, direction);
46+
boolean tooFar = false;
47+
double extendedReach = reachDistance;
48+
49+
Vec3d cameraPos = entity.getCameraPosVec(tickDelta);
50+
51+
extendedReach = extendedReach * extendedReach;
52+
if (target != null) {
53+
extendedReach = target.getPos().squaredDistanceTo(cameraPos);
54+
}
55+
56+
Vec3d vec3d3 = cameraPos.add(direction.multiply(reachDistance));
57+
Box box = entity
58+
.getBoundingBox()
59+
.stretch(entity.getRotationVec(1.0F).multiply(reachDistance))
60+
.expand(1.0D, 1.0D, 1.0D);
61+
EntityHitResult entityHitResult = ProjectileUtil.raycast(
62+
entity,
63+
cameraPos,
64+
vec3d3,
65+
box,
66+
(entityx) -> !entityx.isSpectator(),
67+
extendedReach
68+
);
69+
70+
if (entityHitResult == null) {
71+
return target;
72+
}
73+
74+
Entity entity2 = entityHitResult.getEntity();
75+
Vec3d vec3d4 = entityHitResult.getPos();
76+
double g = cameraPos.squaredDistanceTo(vec3d4);
77+
if (tooFar && g > 9.0D) {
78+
return null;
79+
} else if (g < extendedReach || target == null) {
80+
target = entityHitResult;
81+
if (entity2 instanceof LivingEntity || entity2 instanceof ItemFrameEntity) {
82+
client.targetedEntity = entity2;
83+
}
84+
}
85+
86+
return target;
87+
}
88+
89+
private static HitResult raycast(
90+
Entity entity,
91+
double maxDistance,
92+
float tickDelta,
93+
boolean includeFluids,
94+
Vec3d direction
95+
) {
96+
Vec3d end = entity.getCameraPosVec(tickDelta).add(direction.multiply(maxDistance));
97+
return entity.getWorld().raycast(new RaycastContext(
98+
entity.getCameraPosVec(tickDelta),
99+
end,
100+
RaycastContext.ShapeType.OUTLINE,
101+
includeFluids ? RaycastContext.FluidHandling.ANY : RaycastContext.FluidHandling.NONE,
102+
entity
103+
));
104+
}
105+
}
Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
11
package com.carsoncoder.gpws;
22

3+
import java.util.HashMap;
4+
35
import net.minecraft.registry.Registries;
46
import net.minecraft.registry.Registry;
57
import net.minecraft.sound.SoundEvent;
68
import net.minecraft.util.Identifier;
79

810
public class gpwsSounds {
911

10-
public enum Sounds {
12+
public static enum SOUNDS {
1113
RETARD, PULL_UP, BANK_ANGLE, Y2500, Y500, Y400, Y300, Y200, Y100, Y70, Y60, Y50, Y40, Y30, Y20
1214
}
1315

14-
public static final Identifier RETARD_ID = new Identifier("gpws-elytra:retard");
15-
public static SoundEvent RETARD = SoundEvent.of(RETARD_ID);
16+
private static final Identifier RETARD_ID = new Identifier("gpws-elytra:retard");
17+
private static SoundEvent RETARD = SoundEvent.of(RETARD_ID);
18+
19+
private static final Identifier BANK_ANGLE_ID = new Identifier("gpws-elytra:bank-angle");
20+
private static SoundEvent BANK_ANGLE = SoundEvent.of(BANK_ANGLE_ID);
21+
22+
private static final Identifier PULL_UP_ID = new Identifier("gpws-elytra:pull-up");
23+
private static SoundEvent PULL_UP = SoundEvent.of(PULL_UP_ID);
1624

17-
public static final Identifier BANK_ANGLE_ID = new Identifier("gpws-elytra:retard");
18-
public static SoundEvent BANK_ANGLE = SoundEvent.of(BANK_ANGLE_ID);
25+
private SOUNDS[] Sounds = {SOUNDS.Y2500, SOUNDS.Y500, SOUNDS.Y400, SOUNDS.Y300, SOUNDS.Y200, SOUNDS.Y100, SOUNDS.Y70, SOUNDS.Y60, SOUNDS.Y50, SOUNDS.Y40, SOUNDS.Y30, SOUNDS.Y20};
1926

20-
public static final Identifier PULL_UP_ID = new Identifier("gpws-elytra:retard");
21-
public static SoundEvent PULL_UP = SoundEvent.of(PULL_UP_ID);
27+
private HashMap<SOUNDS, SoundEvent> SoundsDict = new HashMap<SOUNDS, SoundEvent>();
2228

2329
void init() {
2430
Registry.register(Registries.SOUND_EVENT, RETARD_ID, RETARD);
2531
Registry.register(Registries.SOUND_EVENT, BANK_ANGLE_ID, BANK_ANGLE);
2632
Registry.register(Registries.SOUND_EVENT, PULL_UP_ID, PULL_UP);
33+
34+
SoundsDict.put(SOUNDS.BANK_ANGLE, BANK_ANGLE);
35+
SoundsDict.put(SOUNDS.PULL_UP, PULL_UP);
36+
SoundsDict.put(SOUNDS.RETARD, RETARD);
37+
38+
for (int i=0; i<12; i++) {
39+
SOUNDS currentSound = Sounds[i];
40+
41+
gpwsElytraClient.LOGGER.info("Loading Sound: {}", currentSound.name());
42+
43+
Identifier id = new Identifier("gpws-elytra:" + currentSound.name().substring(1));
44+
SoundEvent event = SoundEvent.of(id);
45+
SoundsDict.put(currentSound, event);
46+
Registry.register(Registries.SOUND_EVENT, id, event);
47+
}
48+
}
49+
50+
public SoundEvent GetSound(SOUNDS sound) {
51+
return SoundsDict.get(sound);
2752
}
2853
}

0 commit comments

Comments
 (0)