Skip to content

Commit

Permalink
refactor: RocketEntity to use GCVehicle
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Jun 8, 2024
1 parent 4dcd3b4 commit b3c3288
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public void render(RocketEntity entity, float yaw, float partialTick, PoseStack
matrices.mulPose(Axis.YN.rotationDegrees(yRot));
matrices.translate(0, 0.25D, 0);

float wobbleTicks = (float) entity.getEntityData().get(RocketEntity.DAMAGE_WOBBLE_TICKS) - partialTick;
float wobbleStrength = entity.getEntityData().get(RocketEntity.DAMAGE_WOBBLE_STRENGTH) - partialTick;
float wobbleTicks = (float) entity.getHurtTime() - partialTick;
float wobbleStrength = entity.getDamage() - partialTick;

if (wobbleStrength < 0.0F) {
wobbleStrength = 0.0F;
}

if (wobbleTicks > 0.0F) {
matrices.mulPose(Axis.XP.rotationDegrees(Mth.sin(wobbleTicks) * wobbleTicks * wobbleStrength / 10.0F * (float) entity.getEntityData().get(RocketEntity.DAMAGE_WOBBLE_SIDE)));
matrices.mulPose(Axis.XP.rotationDegrees(Mth.sin(wobbleTicks) * wobbleTicks * wobbleStrength / 10.0F * (float) entity.getHurtDir()));
}

// RenderSystem.setShaderTexture(0, getTextureLocation(entity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void lerpTo(double x, double y, double z, float yaw, float pitch, int int
this.lerpSteps = 10;
}

private void tickLerp() { // Stolen from the boat class to fix the rocket from bugging out
protected void tickLerp() { // Stolen from the boat class to fix the rocket from bugging out
if (this.isControlledByLocalInstance()) {
this.lerpSteps = 0;
this.syncPacketPositionCodec(getX(), getY(), getZ());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2019-2024 Team Galacticraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.galacticraft.mod.content.entity.orbital;

import dev.galacticraft.mod.content.entity.GCVehicle;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.Level;
import org.joml.Quaternionf;

/**
* A entity that's rotation is represented via a quaternion.
*/
public abstract class AdvancedVehicle extends GCVehicle {

protected Quaternionf rotation;

public AdvancedVehicle(EntityType<?> entityType, Level level) {
super(entityType, level);
}

public Quaternionf getRotation() {
return rotation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import dev.galacticraft.mod.content.GCFluids;
import dev.galacticraft.mod.content.advancements.GCTriggers;
import dev.galacticraft.mod.content.block.special.launchpad.AbstractLaunchPad;
import dev.galacticraft.mod.content.block.special.launchpad.LaunchPadBlockEntity;
import dev.galacticraft.mod.content.entity.ControllableEntity;
import dev.galacticraft.mod.content.entity.data.GCEntityDataSerializers;
import dev.galacticraft.mod.content.item.GCItems;
Expand Down Expand Up @@ -94,18 +93,14 @@
import java.util.Objects;

@SuppressWarnings("UnstableApiUsage")
public class RocketEntity extends Entity implements Rocket, IgnoreShift, ControllableEntity {
public class RocketEntity extends AdvancedVehicle implements Rocket, IgnoreShift, ControllableEntity {
private static final ResourceLocation NULL_ID = new ResourceLocation("null");
private static final EntityDataAccessor<LaunchStage> STAGE = SynchedEntityData.defineId(RocketEntity.class, GCEntityDataSerializers.LAUNCH_STAGE);

private static final EntityDataAccessor<Integer> COLOR = SynchedEntityData.defineId(RocketEntity.class, EntityDataSerializers.INT);

private static final EntityDataAccessor<Integer> TIME_AS_STATE = SynchedEntityData.defineId(RocketEntity.class, EntityDataSerializers.INT);

public static final EntityDataAccessor<Integer> DAMAGE_WOBBLE_TICKS = SynchedEntityData.defineId(RocketEntity.class, EntityDataSerializers.INT);
public static final EntityDataAccessor<Integer> DAMAGE_WOBBLE_SIDE = SynchedEntityData.defineId(RocketEntity.class, EntityDataSerializers.INT);
public static final EntityDataAccessor<Float> DAMAGE_WOBBLE_STRENGTH = SynchedEntityData.defineId(RocketEntity.class, EntityDataSerializers.FLOAT);

public static final EntityDataAccessor<Float> SPEED = SynchedEntityData.defineId(RocketEntity.class, EntityDataSerializers.FLOAT);

public static final EntityDataAccessor<ResourceLocation> ROCKET_CONE = SynchedEntityData.defineId(RocketEntity.class, GCEntityDataSerializers.IDENTIFIER);
Expand All @@ -124,12 +119,6 @@ public class RocketEntity extends Entity implements Rocket, IgnoreShift, Control
});
private int timeBeforeLaunch;
private float timeSinceLaunch;
private int lerpSteps;
private double lerpX;
private double lerpY;
private double lerpZ;
private double lerpYRot;
private double lerpXRot;

public RocketEntity(EntityType<?> entityType, Level level) {
super(entityType, level);
Expand Down Expand Up @@ -205,32 +194,6 @@ public void setLaunchStage(LaunchStage launchStage) {
return linkedPad.getDockPos();
}

@Override
public boolean hurt(DamageSource source, float amount) {
if (!this.level().isClientSide && !this.isRemoved()) {
if (this.isInvulnerableTo(source)) {
return false;
} else {
this.entityData.set(DAMAGE_WOBBLE_SIDE, -this.entityData.get(DAMAGE_WOBBLE_SIDE));
this.entityData.set(DAMAGE_WOBBLE_TICKS, 10);
this.entityData.set(DAMAGE_WOBBLE_STRENGTH, this.entityData.get(DAMAGE_WOBBLE_STRENGTH) + amount * 10.0F);
boolean creative = source.getEntity() instanceof Player && ((Player) source.getEntity()).getAbilities().instabuild;
if (creative || this.entityData.get(DAMAGE_WOBBLE_STRENGTH) > 40.0F) {
this.ejectPassengers();
if (creative && !this.hasCustomName()) {
this.remove(RemovalReason.DISCARDED);
} else {
this.dropItems(source, false);
}
}

return true;
}
} else {
return true;
}
}

@Override
public void remove(RemovalReason reason) {
super.remove(reason);
Expand Down Expand Up @@ -388,46 +351,15 @@ protected void reapplyPosition() {
this.getPassengers().forEach(this::positionRider);
}

@Override
public void lerpTo(double x, double y, double z, float yRot, float xRot, int steps) {
this.lerpX = x;
this.lerpY = y;
this.lerpZ = z;
this.lerpYRot = yRot;
this.lerpXRot = xRot;
this.lerpSteps = 10;
}

private void tickLerp() { // Stolen from the boat class to fix the rocket from bugging out
if (this.isControlledByLocalInstance()) {
this.lerpSteps = 0;
this.syncPacketPositionCodec(this.getX(), this.getY(), this.getZ());
}

if (this.lerpSteps > 0) {
double d = this.getX() + (this.lerpX - this.getX()) / (double) this.lerpSteps;
double e = this.getY() + (this.lerpY - this.getY()) / (double) this.lerpSteps;
double f = this.getZ() + (this.lerpZ - this.getZ()) / (double) this.lerpSteps;
double g = Mth.wrapDegrees(this.lerpYRot - (double) this.getYRot());
this.setYRot(this.getYRot() + (float) g / (float) this.lerpSteps);
this.setXRot(this.getXRot() + (float) (this.lerpXRot - (double) this.getXRot()) / (float) this.lerpSteps);
--this.lerpSteps;
this.setPos(d, e, f);
this.setRot(this.getYRot(), this.getXRot());
}
}

@Override
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(STAGE, LaunchStage.IDLE);
this.entityData.define(SPEED, 0.0f);

this.entityData.define(COLOR, -1);

this.entityData.define(TIME_AS_STATE, 0);
this.entityData.define(DAMAGE_WOBBLE_TICKS, 0);
this.entityData.define(DAMAGE_WOBBLE_SIDE, 0);
this.entityData.define(DAMAGE_WOBBLE_STRENGTH, 0.0F);

this.entityData.define(ROCKET_CONE, NULL_ID);
this.entityData.define(ROCKET_BODY, NULL_ID);
Expand Down Expand Up @@ -475,7 +407,6 @@ public void tick() {
setTimeAsState(getTimeAsState() + 1);

super.tick();
tickLerp();

int particleChance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

package dev.galacticraft.mod.content.entity.orbital.lander;

import dev.galacticraft.mod.content.entity.GCVehicle;
import dev.galacticraft.mod.content.entity.orbital.AdvancedVehicle;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.Level;

public abstract class AbstractLanderEntity extends GCVehicle {
public abstract class AbstractLanderEntity extends AdvancedVehicle {
public AbstractLanderEntity(EntityType<?> entityType, Level level) {
super(entityType, level);
}
Expand Down

0 comments on commit b3c3288

Please sign in to comment.