From cc9b26b0210f7123a4f76b782ccec1c162e8d6af Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 12 Nov 2013 21:14:55 -0500 Subject: [PATCH] wrote some rotation code --- src/dark/core/helpers/MathHelper.java | 64 +++++++++++++++++++ .../core/prefab/sentry/TileEntitySentry.java | 21 +++++- 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/dark/core/helpers/MathHelper.java b/src/dark/core/helpers/MathHelper.java index 91665979..623ea85c 100644 --- a/src/dark/core/helpers/MathHelper.java +++ b/src/dark/core/helpers/MathHelper.java @@ -84,6 +84,22 @@ public class MathHelper extends net.minecraft.util.MathHelper return var; } + public static float clamp(float var, float min, float max) + { + if (var < min) + { + return min; + } + else if (var > max) + { + return max; + } + else + { + return var; + } + } + /** Clamps an angle to 360 degree circle */ public static float clampAngleTo360(float var) { @@ -110,6 +126,54 @@ public class MathHelper extends net.minecraft.util.MathHelper } } + public static double updateRotation(double from, double to, double speed) + { + from = MathHelper.wrapAngleTo180_double(from); + to = MathHelper.wrapAngleTo180_double(to); + double delta = Math.abs(from - to); + if (delta > 0.001f) + { + if (from > to) + { + from += (delta >= 0) ? speed : -speed; + } + else + { + from += (delta >= 0) ? -speed : speed; + } + + if (delta < speed + 0.1f) + { + from = to; + } + } + return from; + } + + public static double updateRotation(float from, float to, float speed) + { + from = MathHelper.wrapAngleTo180_float(from); + to = MathHelper.wrapAngleTo180_float(to); + double delta = Math.abs(from - to); + if (delta > 0.001f) + { + if (from > to) + { + from += (delta >= 0) ? speed : -speed; + } + else + { + from += (delta >= 0) ? -speed : speed; + } + + if (delta < speed + 0.1f) + { + from = to; + } + } + return from; + } + /** gets the facing direction using the yaw angle */ public static ForgeDirection getFacingDirectionFromAngle(float yaw) { diff --git a/src/dark/core/prefab/sentry/TileEntitySentry.java b/src/dark/core/prefab/sentry/TileEntitySentry.java index 4ee23944..da83e312 100644 --- a/src/dark/core/prefab/sentry/TileEntitySentry.java +++ b/src/dark/core/prefab/sentry/TileEntitySentry.java @@ -1,7 +1,12 @@ package dark.core.prefab.sentry; +import java.util.ArrayList; import java.util.List; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.relauncher.Side; + +import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.potion.PotionEffect; import net.minecraft.tileentity.TileEntity; @@ -9,11 +14,18 @@ import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.DamageSource; import net.minecraftforge.common.ForgeDirection; import universalelectricity.core.vector.Vector3; -import universalelectricity.prefab.tile.TileEntityAdvanced; import dark.api.ISentryGun; +import dark.core.helpers.MathHelper; import dark.core.prefab.EntityTileDamage; import dark.core.prefab.machine.TileEntityMachine; +/** Prefab tileEntity for creating senty guns that can be of type aimed, mounted, or automated. + * Contains most of the code for a sentry gun to operate short of aiming and operating logic. This + * means the classes that extend this still need to tell the sentry were to aim. As well this + * doesn't handle any firing events or damage events. This is only a shell for the sentry to be + * created. Everything else is up to the sub classes of this class. + * + * @author DarkGuardsman */ public abstract class TileEntitySentry extends TileEntityMachine implements ISentryGun { protected EntityTileDamage entitySentry = null; @@ -25,6 +37,7 @@ public abstract class TileEntitySentry extends TileEntityMachine implements ISen private final float maxDamage; private Vector3 rotation = new Vector3(), newRotation = new Vector3(), prevRotation = new Vector3(); + protected float roationSpeed = 10f, minPitch = -30, maxPitch = 30, minYaw = -180, maxYaw = 180, size = 1.0f; public TileEntitySentry(float maxDamage) { @@ -37,7 +50,7 @@ public abstract class TileEntitySentry extends TileEntityMachine implements ISen @Override public void updateEntity() { - super.updateEntity(); + super.updateEntity(); if (this.isFunctioning()) { if (this.entitySentry == null) @@ -84,7 +97,9 @@ public abstract class TileEntitySentry extends TileEntityMachine implements ISen * ****************************************************** */ public void updateRotation() { - + this.prevRotation = this.getRotation(); + this.rotation.x = MathHelper.clamp((float) MathHelper.updateRotation(this.rotation.x, this.newRotation.x, this.roationSpeed), this.minPitch, this.maxPitch); + this.rotation.y = MathHelper.clamp((float) MathHelper.updateRotation(this.rotation.y, this.newRotation.y, this.roationSpeed), this.minYaw, this.maxYaw); } @Override