Skip to content

Commit

Permalink
Add rear attachment system and passenger seats for cars
Browse files Browse the repository at this point in the history
  • Loading branch information
FoundationGames committed May 15, 2022
1 parent 360ab52 commit 54fc726
Show file tree
Hide file tree
Showing 25 changed files with 1,412 additions and 68 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ myron_version=1.6.3+1.18.1
arrp_version=0.5.5
jsonem_version=0.1.2

mod_version = 0.0.9+1.18.2
mod_version = 0.0.10+1.18.2
maven_group = io.github.foundationgames
archives_base_name = automobility

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public record AutomobileFrame(
25,
11,
7,
17
17,
11,
11
)
)
);
Expand All @@ -74,7 +76,9 @@ public record AutomobileFrame(
44f,
6f,
19.5f,
10.5f
10.5f,
23,
23
)
)
);
Expand All @@ -90,7 +94,9 @@ public record AutomobileFrame(
20,
16,
8,
6
6,
9,
9
)
)
);
Expand All @@ -106,7 +112,9 @@ public record AutomobileFrame(
40,
22,
13,
3
3,
24,
24
)
)
);
Expand All @@ -122,7 +130,9 @@ private static AutomobileFrame standard(String color) {
26,
5,
13,
3
3,
18,
22
)
);
}
Expand All @@ -138,7 +148,9 @@ private static AutomobileFrame motorcar(String variant, float weight) {
28,
3,
18,
2
2,
23,
22
)
);
}
Expand All @@ -159,7 +171,9 @@ private static AutomobileFrame tractor(String color) {
24,
9,
9,
8
8,
12,
19
)
);
}
Expand All @@ -180,7 +194,9 @@ public static record FrameModel(
float lengthPx,
float seatHeight,
float enginePosBack,
float enginePosUp
float enginePosUp,
float rearAttachmentPos,
float frontAttachmentPos
) {
@Environment(EnvType.CLIENT)
public Function<EntityRendererFactory.Context, Model> model() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.github.foundationgames.automobility.automobile.attachment;

import io.github.foundationgames.automobility.Automobility;
import io.github.foundationgames.automobility.automobile.AutomobileFrame;
import io.github.foundationgames.automobility.automobile.attachment.rear.EmptyRearAttachment;
import io.github.foundationgames.automobility.automobile.attachment.rear.PassengerSeatRearAttachment;
import io.github.foundationgames.automobility.automobile.attachment.rear.RearAttachment;
import io.github.foundationgames.automobility.entity.AutomobileEntity;
import io.github.foundationgames.automobility.render.AutomobilityModels;
import io.github.foundationgames.automobility.util.SimpleMapContentRegistry;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.model.Model;
import net.minecraft.client.render.entity.EntityRendererFactory;
import net.minecraft.util.Identifier;

import java.util.function.BiFunction;
import java.util.function.Function;

public record RearAttachmentType<T extends RearAttachment>(
Identifier id, BiFunction<RearAttachmentType<T>, AutomobileEntity, T> constructor, RearAttachmentModel model
) implements SimpleMapContentRegistry.Identifiable {
public static final SimpleMapContentRegistry<RearAttachmentType<?>> REGISTRY = new SimpleMapContentRegistry<>();

public static final RearAttachmentType<EmptyRearAttachment> EMPTY = register(new RearAttachmentType<>(
Automobility.id("empty"), EmptyRearAttachment::new, new RearAttachmentModel(new Identifier("empty"), Automobility.id("empty"), 0)
));

public static final RearAttachmentType<PassengerSeatRearAttachment> PASSENGER_SEAT = register(new RearAttachmentType<>(
Automobility.id("passenger_seat"), PassengerSeatRearAttachment::new,
new RearAttachmentModel(Automobility.id("textures/entity/automobile/rear_attachment/passenger_seat.png"), Automobility.id("rearatt_passenger_seat"), 11)
));

public boolean isEmpty() {
return this == EMPTY;
}

@Override
public Identifier getId() {
return this.id;
}

private static <T extends RearAttachment> RearAttachmentType<T> register(RearAttachmentType<T> entry) {
REGISTRY.register(entry);
return entry;
}

public record RearAttachmentModel(Identifier texture, Identifier modelId, float pivotDistPx) {
@Environment(EnvType.CLIENT)
public Function<EntityRendererFactory.Context, Model> model() {
return AutomobilityModels.MODELS.get(modelId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.foundationgames.automobility.automobile.attachment.rear;

import io.github.foundationgames.automobility.automobile.attachment.RearAttachmentType;
import io.github.foundationgames.automobility.entity.AutomobileEntity;

public class EmptyRearAttachment extends RearAttachment {
public EmptyRearAttachment(RearAttachmentType<?> type, AutomobileEntity entity) {
super(type, entity);
}

@Override
public void tick() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.foundationgames.automobility.automobile.attachment.rear;

import io.github.foundationgames.automobility.automobile.attachment.RearAttachmentType;
import io.github.foundationgames.automobility.entity.AutomobileEntity;

public class PassengerSeatRearAttachment extends RearAttachment {
public PassengerSeatRearAttachment(RearAttachmentType<?> type, AutomobileEntity entity) {
super(type, entity);
}

@Override
public boolean isRideable() {
return true;
}

@Override
public double getPassengerHeightOffset() {
return 0.69;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package io.github.foundationgames.automobility.automobile.attachment.rear;

import io.github.foundationgames.automobility.automobile.attachment.RearAttachmentType;
import io.github.foundationgames.automobility.entity.AutomobileEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

public abstract class RearAttachment {
public final RearAttachmentType<?> type;
protected final AutomobileEntity automobile;
private float lastYaw;

protected RearAttachment(RearAttachmentType<?> type, AutomobileEntity entity) {
this.type = type;
this.automobile = entity;
}

protected final World world() {
return this.automobile.world;
}

public final Vec3d yawVec() {
return new Vec3d(0, 0, 1).rotateY((float) Math.toRadians(180 - this.yaw()));
}

public final Vec3d scaledYawVec() {
return this.yawVec().multiply(this.type.model().pivotDistPx() * 0.0625);
}

public final Vec3d origin() {
return this.automobile.getTailPos();
}

public final Vec3d pos() {
return this.origin().add(this.scaledYawVec());
}

public float yaw() {
return this.automobile.getTrackedRearAttachmentYaw();
}

public float yaw(float delta) {
return MathHelper.lerpAngleDegrees(delta, this.lastYaw, this.yaw());
}

public void setYaw(float yaw) {
float diff = MathHelper.wrapDegrees(yaw - this.automobile.getYaw());
if (diff < -90 && diff > -180) yaw = this.automobile.getYaw() - 90;
else if (diff > 90 && diff < 180) yaw = this.automobile.getYaw() + 90;

this.automobile.setTrackedRearAttachmentYaw(yaw);
}

public final void pull(Vec3d movement) {
var vec = this.scaledYawVec().add(movement);
this.setYaw(180 - (float) Math.toDegrees(Math.atan2(vec.x, vec.z)));
}

public void tick() {
this.lastYaw = this.yaw();
}

public void onRemoved() {
}

public boolean isRideable() {
return false;
}

public double getPassengerHeightOffset() {
return 0.5;
}

public void writeNbt(NbtCompound nbt) {
nbt.putFloat("yaw", this.yaw());
}

public void readNbt(NbtCompound nbt) {
this.setYaw(nbt.getFloat("yaw"));
}

public final NbtCompound toNbt() {
var nbt = new NbtCompound();
nbt.putString("type", this.type.id().toString());
this.writeNbt(nbt);
return nbt;
}

public static RearAttachmentType<?> fromNbt(NbtCompound nbt) {
return RearAttachmentType.REGISTRY.get(Identifier.tryParse(nbt.getString("type")));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.foundationgames.automobility.automobile.AutomobileFrame;
import io.github.foundationgames.automobility.automobile.AutomobileWheel;
import io.github.foundationgames.automobility.automobile.WheelBase;
import io.github.foundationgames.automobility.automobile.render.attachment.rear.RearAttachmentRenderModel;
import io.github.foundationgames.automobility.automobile.render.wheel.WheelContextReceiver;
import io.github.foundationgames.automobility.entity.AutomobileEntity;
import net.minecraft.client.model.Model;
Expand All @@ -21,9 +22,12 @@ public enum AutomobileRenderer {;

public static void render(
MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay, float tickDelta,
AutomobileFrame frame, AutomobileWheel wheels, AutomobileEngine engine,
EntityRendererFactory.Context ctx, RenderableAutomobile automobile
) {
var frame = automobile.getFrame();
var wheels = automobile.getWheels();
var engine = automobile.getEngine();

if (skidEffectModel == null || exhaustFumesModel == null) {
skidEffectModel = new SkidEffectModel(ctx);
exhaustFumesModel = new ExhaustFumesModel(ctx);
Expand All @@ -40,6 +44,7 @@ public static void render(
var frameModel = automobile.getFrameModel(ctx);
var wheelModel = automobile.getWheelModel(ctx);
var engineModel = automobile.getEngineModel(ctx);
var rearAttachmentModel = automobile.getRearAttachmentModel(ctx);

matrices.translate(0, -chassisRaise, 0);

Expand Down Expand Up @@ -80,9 +85,22 @@ public static void render(
matrices.pop();
}
}

matrices.pop();

// Rear Attachment
var rearAtt = automobile.getRearAttachmentType();
if (!rearAtt.isEmpty()) {
matrices.push();
matrices.translate(0, chassisRaise, frame.model().rearAttachmentPos() / 16);
matrices.multiply(Vec3f.NEGATIVE_Y.getDegreesQuaternion(automobile.getAutomobileYaw(tickDelta) - automobile.getRearAttachmentYaw(tickDelta)));

matrices.translate(0, 0, rearAtt.model().pivotDistPx() / 16);
if (rearAttachmentModel instanceof RearAttachmentRenderModel rm) {
rm.setWheelAngle((float) Math.toRadians(automobile.getWheelAngle(tickDelta)));
}
rearAttachmentModel.render(matrices, vertexConsumers.getBuffer(rearAttachmentModel.getLayer(rearAtt.model().texture())), light, overlay, 1, 1, 1, 1);
matrices.pop();
}

// WHEELS ----------------------------------------
var wheelBuffer = vertexConsumers.getBuffer(wheelModel.getLayer(wheels.model().texture()));
Expand Down Expand Up @@ -150,7 +168,6 @@ public static void render(
}
}
}

// -----------------------------------------------

matrices.pop();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
package io.github.foundationgames.automobility.automobile.render;

import io.github.foundationgames.automobility.automobile.AutomobileEngine;
import io.github.foundationgames.automobility.automobile.AutomobileFrame;
import io.github.foundationgames.automobility.automobile.AutomobileWheel;
import io.github.foundationgames.automobility.automobile.attachment.RearAttachmentType;
import io.github.foundationgames.automobility.automobile.attachment.rear.RearAttachment;
import net.minecraft.client.model.Model;
import net.minecraft.client.render.entity.EntityRendererFactory;
import net.minecraft.util.math.Vec3f;
import org.jetbrains.annotations.Nullable;

public interface RenderableAutomobile {
AutomobileFrame getFrame();

AutomobileEngine getEngine();

AutomobileWheel getWheels();

RearAttachmentType getRearAttachmentType();

Model getFrameModel(EntityRendererFactory.Context ctx);

Model getWheelModel(EntityRendererFactory.Context ctx);

Model getEngineModel(EntityRendererFactory.Context ctx);

Model getRearAttachmentModel(EntityRendererFactory.Context ctx);

float getAutomobileYaw(float tickDelta);

float getRearAttachmentYaw(float tickDelta);

float getWheelAngle(float tickDelta);

float getSteering(float tickDelta);
Expand Down
Loading

0 comments on commit 54fc726

Please sign in to comment.