diff --git a/src/main/java/net/halbear/hvf/Entity/Renderer/Angle.java b/src/main/java/net/halbear/hvf/Entity/Renderer/Angle.java new file mode 100644 index 0000000..aa13951 --- /dev/null +++ b/src/main/java/net/halbear/hvf/Entity/Renderer/Angle.java @@ -0,0 +1,9 @@ +package net.halbear.hvf.Entity.Renderer; + +public enum Angle { + ANGLE_0, + ANGLE_90, + ANGLE_180, + ANGLE_270, + ANGLE_360 +} diff --git a/src/main/java/net/halbear/hvf/Entity/Renderer/TransitionType.java b/src/main/java/net/halbear/hvf/Entity/Renderer/TransitionType.java new file mode 100644 index 0000000..2b24521 --- /dev/null +++ b/src/main/java/net/halbear/hvf/Entity/Renderer/TransitionType.java @@ -0,0 +1,6 @@ +package net.halbear.hvf.Entity.Renderer; + +public enum TransitionType { + LinearInterpolate, + InstantTransition +} diff --git a/src/main/java/net/halbear/hvf/Entity/Renderer/VehicleBaseRenderer.java b/src/main/java/net/halbear/hvf/Entity/Renderer/VehicleBaseRenderer.java index 81119f7..d8147f5 100644 --- a/src/main/java/net/halbear/hvf/Entity/Renderer/VehicleBaseRenderer.java +++ b/src/main/java/net/halbear/hvf/Entity/Renderer/VehicleBaseRenderer.java @@ -91,10 +91,7 @@ public class VehicleBaseRenderer VehicleStates = new HashMap<>(); + public String CurrentState = "DEFAULT"; + + public void SetCurrentState(String StateName){ + CurrentState = StateName; + } + public void AddVehicleState(String StateName, VehicleEntityState newState){ + VehicleStates.put(StateName, newState); + } + public void SetCurrentState(String StateName,VehicleEntityState newState){ + AddVehicleState(StateName, newState); + CurrentState = StateName; + } + public String GetCurrentStateName(){return CurrentState;} + public VehicleEntityState GetCurrentState(){return VehicleStates.get(CurrentState);} public VehicleBaseEntity(EntityType entityTypeIn, World worldIn, VehicleBuildInfo vehicleBuildInfo) { super(entityTypeIn, worldIn); @@ -69,18 +86,9 @@ public class VehicleBaseEntity extends Entity { BASE_DOUBLE_PARAMETERS.put("CENTRE_OF_MASS_X",vehicleBuildInfo.GetCentreOfMass().x); BASE_DOUBLE_PARAMETERS.put("CENTRE_OF_MASS_Y",vehicleBuildInfo.GetCentreOfMass().y); BASE_DOUBLE_PARAMETERS.put("CENTRE_OF_MASS_Z",vehicleBuildInfo.GetCentreOfMass().z); - BASE_DOUBLE_PARAMETERS.put("AT_REST_ANGLE_OFFSET_YAW",vehicleBuildInfo.GetAtRestAngleOffsets().x); - BASE_DOUBLE_PARAMETERS.put("AT_REST_ANGLE_OFFSET_PITCH",vehicleBuildInfo.GetAtRestAngleOffsets().y); - BASE_DOUBLE_PARAMETERS.put("AT_REST_ANGLE_OFFSET_ROLL",vehicleBuildInfo.GetAtRestAngleOffsets().z); BASE_DOUBLE_PARAMETERS.put("VELOCITY_X",0d); BASE_DOUBLE_PARAMETERS.put("VELOCITY_Y",0d); BASE_DOUBLE_PARAMETERS.put("VELOCITY_Z",0d); - BASE_DOUBLE_PARAMETERS.put("ANGLE_OFFSET_YAW",0d); - BASE_DOUBLE_PARAMETERS.put("ANGLE_OFFSET_PITCH",0d); - BASE_DOUBLE_PARAMETERS.put("ANGLE_OFFSET_ROLL",0d); - BASE_DOUBLE_PARAMETERS.put("AT_ROTATE_SPEED_ANGLE_OFFSET_YAW",vehicleBuildInfo.GetRotateAngleOffsets().x); - BASE_DOUBLE_PARAMETERS.put("AT_ROTATE_SPEED_ANGLE_OFFSET_PITCH",vehicleBuildInfo.GetRotateAngleOffsets().y); - BASE_DOUBLE_PARAMETERS.put("AT_ROTATE_SPEED_ANGLE_OFFSET_ROLL",vehicleBuildInfo.GetRotateAngleOffsets().z); BASE_DOUBLE_PARAMETERS.put("ROTATE_VELOCITY",vehicleBuildInfo.GetRotateVelocity()); BASE_FLOAT_PARAMETERS.put("ORIENTATION_YAW",0f); BASE_FLOAT_PARAMETERS.put("ADD_ORIENTATION_YAW_NEXT_TICK",0f); @@ -127,6 +135,20 @@ public class VehicleBaseEntity extends Entity { case Ship: break; } + VehicleStates.put("DEFAULT", + new VehicleEntityState("DEFAULT") + .SetBaseRotations(AnimationAxis.X_AXIS,new double[]{vehicleBuildInfo.GetAtRestAngleOffsets().x,90,180,270}) + .SetBaseRotations(AnimationAxis.Y_AXIS,new double[]{vehicleBuildInfo.GetAtRestAngleOffsets().y,90,180,270}) + .SetBaseRotations(AnimationAxis.Z_AXIS,new double[]{vehicleBuildInfo.GetAtRestAngleOffsets().z,90,180,270}) + .SetAllowedInputs(VehicleByteActions) + ); + VehicleStates.put("NO_MODIFICATIONS", + new VehicleEntityState("NO_MODIFICATIONS") + .SetBaseRotations(AnimationAxis.X_AXIS,new double[]{0,90,180,270}) + .SetBaseRotations(AnimationAxis.Y_AXIS,new double[]{0,90,180,270}) + .SetBaseRotations(AnimationAxis.Z_AXIS,new double[]{0,90,180,270}) + .SetAllowedInputs(VehicleByteActions) + ); this.stepHeight = vehicleBuildInfo.GetStepHeight(); CompoundNBT SyncedData = this.dataManager.get(VEHICLE_SYNCHED_PARAMETERS).copy(); BASE_FLOAT_PARAMETERS.forEach(SyncedData::putFloat); @@ -316,7 +338,12 @@ public class VehicleBaseEntity extends Entity { VehicleVelocity = new Vector3(GetDoubleDataValue("VELOCITY_X"),GetDoubleDataValue("VELOCITY_Y"),GetDoubleDataValue("VELOCITY_Z")); } public boolean TouchingGround(){return GetBooleanDataValue("ON_GROUND");} + public void StartPhysicsTick(double RotateScalar, double HorizontalVelocity,Vector3d Velocity, Vector3d NormalisedVelocity){ + } + public void EndPhysicsTick(double RotateScalar, double HorizontalVelocity,Vector3d Velocity, Vector3d NormalisedVelocity){ + + } public void PhysicsTick(boolean ServerSided){ if(ServerSided) SetBooleanDataValue("ON_GROUND",Math.abs(CurrentPositionVector.y - LastPositionVector.y) < 0.05D); VehicleVelocity = new Vector3(GetDoubleDataValue("VELOCITY_X"),GetDoubleDataValue("VELOCITY_Y"),GetDoubleDataValue("VELOCITY_Z")); @@ -324,19 +351,14 @@ public class VehicleBaseEntity extends Entity { Vector3d NormalisedVelocity = Velocity.normalize(); double OverallVelocity = Velocity.x * NormalisedVelocity.x + Velocity.z * NormalisedVelocity.z; double Scalar = Math.max(Math.min(OverallVelocity/GetDoubleDataValue("ROTATE_VELOCITY"),1),0); - Vector3d AtRest = new Vector3d(GetDoubleDataValue("AT_REST_ANGLE_OFFSET_YAW"),GetDoubleDataValue("AT_REST_ANGLE_OFFSET_PITCH"),GetDoubleDataValue("AT_REST_ANGLE_OFFSET_ROLL")); - Vector3d AtRotate = new Vector3d(GetDoubleDataValue("AT_ROTATE_SPEED_ANGLE_OFFSET_YAW"),GetDoubleDataValue("AT_ROTATE_SPEED_ANGLE_OFFSET_PITCH"),GetDoubleDataValue("AT_ROTATE_SPEED_ANGLE_OFFSET_ROLL")); - Vector3d AngleOffsets = Vector3.Lerp(AtRest,AtRotate,Scalar); - SetDoubleDataValue("ANGLE_OFFSET_YAW",AngleOffsets.x); - SetDoubleDataValue("ANGLE_OFFSET_PITCH",AngleOffsets.y); - SetDoubleDataValue("ANGLE_OFFSET_ROLL",AngleOffsets.z); + StartPhysicsTick(Scalar,OverallVelocity,Velocity,NormalisedVelocity); if(GetBooleanDataValue("ADHERES_TO_GRAVITY")) { if (!this.isOnGround()) { SetDoubleDataValue("VELOCITY_Y",GetDoubleDataValue("VELOCITY_Y") - GetDoubleDataValue("GRAVITY")); } else { - if (GetFloatDataValue("ORIENTATION_PITCH") % 90 != 0) { - SetFloatDataValue("ORIENTATION_PITCH", Math.round(GetFloatDataValue("ORIENTATION_PITCH") / 90.0f) * 90.0f); - } + if (VehicleStates.get(CurrentState).GetYRotation(GetFloatDataValue("ORIENTATION_PITCH")) != GetFloatDataValue("ORIENTATION_PITCH")) SetFloatDataValue("ORIENTATION_PITCH", (float) VehicleStates.get(CurrentState).GetYRotation(GetFloatDataValue("ORIENTATION_PITCH"))); + // if (VehicleStates.get(CurrentState).GetXRotation(GetFloatDataValue("ORIENTATION_YAW")) != GetFloatDataValue("ORIENTATION_YAW")) SetFloatDataValue("ORIENTATION_YAW", (float) VehicleStates.get(CurrentState).GetXRotation(GetFloatDataValue("ORIENTATION_YAW"))); + // if (VehicleStates.get(CurrentState).GetZRotation(GetFloatDataValue("ORIENTATION_ROLL")) != GetFloatDataValue("ORIENTATION_ROLL")) SetFloatDataValue("ORIENTATION_ROLL", (float) VehicleStates.get(CurrentState).GetZRotation(GetFloatDataValue("ORIENTATION_ROLL"))); } } VehicleVelocity = new Vector3(GetDoubleDataValue("VELOCITY_X"),GetDoubleDataValue("VELOCITY_Y"),GetDoubleDataValue("VELOCITY_Z")); @@ -348,10 +370,11 @@ public class VehicleBaseEntity extends Entity { markVelocityChanged(); RotationTick(); if(Scalar < 1&& !GetBooleanDataValue("ON_GROUND") && GetDoubleDataValue("VELOCITY_Y") < 0.015){ - double CenterOfMass = GetDoubleDataValue("CENTRE_OF_MASS_Z"); - float CenterOfMassFloat = (float) CenterOfMass; - SetFloatDataValue("ADD_ORIENTATION_PITCH_NEXT_TICK", (float) (-CenterOfMassFloat * (Math.max(1-Scalar,0)))); + Vector3f CenterOfMass = new Vector3(GetDoubleDataValue("CENTRE_OF_MASS_X"),GetDoubleDataValue("CENTRE_OF_MASS_Y"),GetDoubleDataValue("CENTRE_OF_MASS_Z")).GetFloat(); + if(CenterOfMass.getZ() != 0)SetFloatDataValue("ADD_ORIENTATION_PITCH_NEXT_TICK", (float) (-CenterOfMass.getZ() * (Math.max(1-Scalar,0)))); + if(CenterOfMass.getX() != 0)SetFloatDataValue("ADD_ORIENTATION_ROLL_NEXT_TICK", (float) (-CenterOfMass.getX() * (Math.max(1-Scalar,0)))); } + EndPhysicsTick(Scalar,OverallVelocity,new Vector3d(GetDoubleDataValue("VELOCITY_X"),GetDoubleDataValue("VELOCITY_Y"),GetDoubleDataValue("VELOCITY_Z")),new Vector3d(GetDoubleDataValue("VELOCITY_X"),GetDoubleDataValue("VELOCITY_Y"),GetDoubleDataValue("VELOCITY_Z")).normalize()); } @Override diff --git a/src/main/java/net/halbear/hvf/Entity/VehicleEntityState.java b/src/main/java/net/halbear/hvf/Entity/VehicleEntityState.java index ccf865c..da90133 100644 --- a/src/main/java/net/halbear/hvf/Entity/VehicleEntityState.java +++ b/src/main/java/net/halbear/hvf/Entity/VehicleEntityState.java @@ -1,9 +1,93 @@ package net.halbear.hvf.Entity; +import net.halbear.hvf.Entity.Renderer.Angle; +import net.halbear.hvf.Entity.Renderer.AnimationAxis; +import net.halbear.hvf.Entity.Renderer.AxisType; +import net.minecraft.util.math.vector.Vector3d; + +import java.util.Map; + +import static net.halbear.hvf.Entity.Renderer.Angle.*; +import static net.halbear.hvf.Entity.Renderer.AnimationAxis.*; + public class VehicleEntityState { - private double BaseRotationX = 0.0d; - private double BaseRotationY = 0.0d; - private double BaseRotationZ = 0.0d; + private double[] BaseRotationX = new double[4]; + private double[] BaseRotationY = new double[4]; + private double[] BaseRotationZ = new double[4]; + private byte[] AllowedInputs; + private Map AnimationSpeeds; private String StateName = "DEFAULT"; + private int TransitionTicks = 0; + + public String GetStateName(){return StateName;} + public byte[] GetAllowedInputs(){return AllowedInputs;} + public double[] GetXRotations(){return BaseRotationX;} + public double[] GetYRotations(){return BaseRotationY;} + public double[] GetZRotations(){return BaseRotationZ;} + public double GetXRotation(double Angle){ + int Index = (int)(Math.abs(Angle)/90.0)%4; + return BaseRotationX[Index]; + } + public double GetYRotation(double Angle){ + int Index = (int)(Math.abs(Angle)/90.0)%4; + return BaseRotationY[Index]; + } + public double GetZRotation(double Angle){ + int Index = (int)(Math.abs(Angle)/90.0)%4; + return BaseRotationZ[Index]; + } + public Vector3d GetBaseRotations(double AngleX, double AngleY, double AngleZ){ + int IndexX = (int)(Math.abs(AngleX)/90.0)%4; + int IndexY = (int)(Math.abs(AngleY)/90.0)%4; + int IndexZ = (int)(Math.abs(AngleZ)/90.0)%4; + return new Vector3d(BaseRotationX[IndexX],BaseRotationY[IndexY],BaseRotationZ[IndexZ]); + } + public int GetTransitionTime(){ + return TransitionTicks; + } + public VehicleEntityState SetTransitionTime(int TransitionTicks){ + this.TransitionTicks = TransitionTicks; + return this; + } + public VehicleEntityState(String StateName){ + this.StateName = StateName; + } + public VehicleEntityState AddAnimationModifier(String AnimationName, float AnimationSpeed){ + AnimationSpeeds.put(AnimationName,AnimationSpeed); + return this; + } + public VehicleEntityState SetAllowedInputs(byte[] AllowedInputs){ + this.AllowedInputs = AllowedInputs; + return this; + } + public VehicleEntityState SetBaseRotations(AnimationAxis Axis, double[] value){ + switch (Axis){ + case X_AXIS: + BaseRotationX = value; + break; + case Y_AXIS: + BaseRotationY = value; + break; + case Z_AXIS: + BaseRotationZ = value; + break; + } + return this; + } + public VehicleEntityState SetBaseRotation(Angle BaseAngle, AnimationAxis Axis, double value){ + int Angle = BaseAngle == ANGLE_90 ? 1 : BaseAngle == ANGLE_180 ? 2 : BaseAngle == ANGLE_270 ? 3 : 0; + switch (Axis){ + case X_AXIS: + BaseRotationX[Angle] = value; + break; + case Y_AXIS: + BaseRotationY[Angle] = value; + break; + case Z_AXIS: + BaseRotationZ[Angle] = value; + break; + } + return this; + } } diff --git a/src/main/java/net/halbear/hvf/Entity/VehicleStateSwitch.java b/src/main/java/net/halbear/hvf/Entity/VehicleStateSwitch.java new file mode 100644 index 0000000..eb7e906 --- /dev/null +++ b/src/main/java/net/halbear/hvf/Entity/VehicleStateSwitch.java @@ -0,0 +1,8 @@ +package net.halbear.hvf.Entity; + +import net.halbear.hvf.Entity.Renderer.TransitionType; + +public class VehicleStateSwitch { + public String NextState = "DEFAULT"; + public TransitionType TransitionType = net.halbear.hvf.Entity.Renderer.TransitionType.LinearInterpolate; +} diff --git a/src/main/java/net/halbear/hvf/Registry/ServerVariables/GlobalInputData.java b/src/main/java/net/halbear/hvf/Registry/ServerVariables/GlobalData.java similarity index 77% rename from src/main/java/net/halbear/hvf/Registry/ServerVariables/GlobalInputData.java rename to src/main/java/net/halbear/hvf/Registry/ServerVariables/GlobalData.java index ca3963d..bccf9b8 100644 --- a/src/main/java/net/halbear/hvf/Registry/ServerVariables/GlobalInputData.java +++ b/src/main/java/net/halbear/hvf/Registry/ServerVariables/GlobalData.java @@ -3,8 +3,8 @@ package net.halbear.hvf.Registry.ServerVariables; import net.minecraft.nbt.CompoundNBT; import net.minecraft.world.storage.WorldSavedData; -public class GlobalInputData extends WorldSavedData { - public GlobalInputData(String name) { +public class GlobalData extends WorldSavedData { + public GlobalData(String name) { super(name); } diff --git a/src/main/java/net/halbear/hvf/UsageExamples/Aeroplane/Aeroplane.java b/src/main/java/net/halbear/hvf/UsageExamples/Aeroplane/Aeroplane.java index 75a1069..d09f338 100644 --- a/src/main/java/net/halbear/hvf/UsageExamples/Aeroplane/Aeroplane.java +++ b/src/main/java/net/halbear/hvf/UsageExamples/Aeroplane/Aeroplane.java @@ -46,6 +46,13 @@ public class Aeroplane extends VehicleBaseEntity { GetVehicleController().AddInputFunction("TurnRight",()-> SetFloatDataValue("ADD_ORIENTATION_YAW_NEXT_TICK", GetFloatDataValue("ADD_ORIENTATION_YAW_NEXT_TICK") + 5)); } + @Override + public void StartPhysicsTick(double RotateScalar, double HorizontalVelocity,Vector3d Velocity, Vector3d NormalisedVelocity){ + if(RotateScalar >= 1){ + SetCurrentState("NO_MODIFICATIONS"); + } + } + @Override public void OnPunchedEvent(){ SetRenderState("DAMAGED");