Skip to content

Commit

Permalink
Merge pull request #604 from Abrackadabra/development
Browse files Browse the repository at this point in the history
Fix AEM translation inconsistencies.
  • Loading branch information
nxPublic authored Jun 2, 2021
2 parents 2b10fb3 + 8c0a180 commit 93236a0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 111 deletions.
9 changes: 5 additions & 4 deletions ValheimPlus/AdvancedBuildingMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,17 @@ private static void listenToHotKeysAndDoWork()
// CONTROL PRESSED
if (Input.GetKeyDown(KeyCode.LeftControl)) { controlFlag = true; }
if (Input.GetKeyUp(KeyCode.LeftControl)) { controlFlag = false; }

// SHIFT PRESSED
if (Input.GetKeyDown(KeyCode.LeftShift)) { shiftFlag = true; }
if (Input.GetKeyUp(KeyCode.LeftShift)) { shiftFlag = false; }
changeModificationSpeed();

// LEFT ALT PRESSED
if (Input.GetKeyDown(KeyCode.LeftAlt)) { altFlag = true; }
if (Input.GetKeyUp(KeyCode.LeftAlt)) { altFlag = false; }

changeModificationSpeed();

if (Input.GetKeyUp(Configuration.Current.AdvancedBuildingMode.copyObjectRotation)) {
if (Input.GetKeyUp(Configuration.Current.AdvancedBuildingMode.copyObjectRotation))
{
savedRotation = component.transform.rotation;
}
if (Input.GetKeyUp(Configuration.Current.AdvancedBuildingMode.pasteObjectRotation))
Expand Down
186 changes: 79 additions & 107 deletions ValheimPlus/AdvancedEditingMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ namespace ValheimPlus
class AEM
{
// Status
public static bool isActive = false;
public static bool isActive;

// Player Instance
public static Player PlayerInstance;

// Control Flags
static bool controlFlag;
static bool shiftFlag;
static bool altFlag;

// Hit Object Data
public static Vector3 HitPoint;
public static Vector3 HitNormal;
Expand All @@ -22,20 +27,18 @@ class AEM
private static Quaternion InitialRotation;
private static Vector3 InitialPosition;

private static bool isInExsistence;

// Hotkey Flags
private static bool controlFlag = false;
private static bool shiftFlag = false;
private static bool altFlag = false;

// Modification Strenghts
private static float gDistance = 2;
private static float gScrollDistance = 2;

private static bool isInExistence;

// Modification Speeds
const float BASE_TRANSLATION_DISTANCE = (float) 0.1; // 1/10th of a 1m pole
const float BASE_ROTATION_ANGLE_DEGREES = 3;

static float currentModificationSpeed = 1;
const float MIN_MODIFICATION_SPEED = 1;
const float MAX_MODIFICATION_SPEED = 30;

// Save and Load object rotation
static Quaternion savedRotation = new Quaternion();
static Quaternion savedRotation;

// Executing the raycast to find the object
public static bool ExecuteRayCast(Player playerInstance)
Expand All @@ -62,25 +65,27 @@ public static bool ExecuteRayCast(Player playerInstance)

return isValidRayCastTarget();
}
else
{
resetObjectInfo();
return false;
}

resetObjectInfo();
return false;
}

// Exiting variables
public static bool forceExitNextIteration = false;
public static bool forceExitNextIteration;


// Initializing class
public static bool checkForObject()
{
if (PlayerInstance == null)
{
return false;
}

if (!ExecuteRayCast(PlayerInstance))
{
return false;
}

return true;
}
Expand All @@ -103,10 +108,12 @@ public static void run()
// CHECK FOR BUILD MODE
if (isInBuildMode())
{
if (isActive) {
if (isActive)
{
exitMode();
resetObjectTransform();
}

return;
}

Expand Down Expand Up @@ -140,8 +147,8 @@ public static void run()

if (isActive)
{
// If object is not in exsistence anymore
if (hitPieceStillExsists())
// If object is not in existence anymore
if (hitPieceStillExists())
{
// Try to prevent znet error, relatively untested yet if this is any solution.
// ghetto solution, will be improved in future version if it proofs to be effective.
Expand All @@ -161,15 +168,14 @@ public static void run()
exitMode();
}

AEM.isRunning();
isRunning();
listenToHotKeysAndDoWork();
}
else
{
exitMode();
}
}

}

private static void listenToHotKeysAndDoWork()
Expand All @@ -186,15 +192,18 @@ private static void listenToHotKeysAndDoWork()
if (Input.GetKeyDown(Configuration.Current.AdvancedEditingMode.confirmPlacementOfAdvancedEditingMode))
{
if (isContainer())
{
dropContainerContents();
}

// PLACE NEW
GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(HitPiece.gameObject, HitPiece.transform.position, HitPiece.transform.rotation);
HitPiece.m_placeEffect.Create(HitPiece.transform.position, HitPiece.transform.rotation, gameObject2.transform, 1f);

// REMOVE OLD
ZNetView component1 = HitPiece.GetComponent<ZNetView>();
if ((UnityEngine.Object)component1 == (UnityEngine.Object)null) {
if ((UnityEngine.Object)component1 == (UnityEngine.Object)null)
{
Debug.Log("AEM: Error, network object empty.");

resetObjectTransform();
Expand All @@ -213,31 +222,14 @@ private static void listenToHotKeysAndDoWork()
// CONTROL PRESSED
if (Input.GetKeyDown(KeyCode.LeftControl)) { controlFlag = true; }
if (Input.GetKeyUp(KeyCode.LeftControl)) { controlFlag = false; }

// SHIFT PRESSED
float distance = gDistance;
float scrollDistance = gScrollDistance;

if (Input.GetKeyDown(KeyCode.LeftShift)) { shiftFlag = true; }
if (Input.GetKeyUp(KeyCode.LeftShift)) { shiftFlag = false; }

changeModificationSpeeds(shiftFlag);

if (shiftFlag)
{
distance = gDistance * 3;
scrollDistance = gScrollDistance * 3;
}
else
{
distance = gDistance;
scrollDistance = gScrollDistance;
}

// LEFT ALT PRESSED
if (Input.GetKeyDown(KeyCode.LeftAlt)) { altFlag = true; }
if (Input.GetKeyUp(KeyCode.LeftAlt)) { altFlag = false; }


changeModificationSpeed();

if (Input.GetKeyUp(Configuration.Current.AdvancedEditingMode.copyObjectRotation))
{
Expand All @@ -255,27 +247,24 @@ private static void listenToHotKeysAndDoWork()
exitMode();
}

// SCROLL CONTROLS
var currentRotationAngleDegrees = BASE_ROTATION_ANGLE_DEGREES * currentModificationSpeed;
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
Quaternion rotation;
if (controlFlag)
{
rX++;
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x + (scrollDistance * (float)rX), HitPiece.transform.eulerAngles.y, HitPiece.transform.eulerAngles.z); // forward to backwards
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x + (currentRotationAngleDegrees * rX), HitPiece.transform.eulerAngles.y, HitPiece.transform.eulerAngles.z); // forward to backwards
}
else if (altFlag)
{
rZ++;
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x, HitPiece.transform.eulerAngles.y, HitPiece.transform.eulerAngles.z + (currentRotationAngleDegrees * rZ)); // diagonal
}
else
{
if (altFlag)
{
rZ++;
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x, HitPiece.transform.eulerAngles.y, HitPiece.transform.eulerAngles.z + (scrollDistance * (float)rZ)); // diagonal
}
else
{
rY++;
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x, HitPiece.transform.eulerAngles.y + (scrollDistance * (float)rY), HitPiece.transform.eulerAngles.z); // left<->right
}
rY++;
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x, HitPiece.transform.eulerAngles.y + (currentRotationAngleDegrees * rY), HitPiece.transform.eulerAngles.z); // left<->right
}
HitPiece.transform.rotation = rotation;
}
Expand All @@ -285,75 +274,71 @@ private static void listenToHotKeysAndDoWork()
if (controlFlag)
{
rX--;
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x + (scrollDistance * (float)rX), HitPiece.transform.eulerAngles.y, HitPiece.transform.eulerAngles.z); // forward to backwards
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x + (currentRotationAngleDegrees * rX), HitPiece.transform.eulerAngles.y, HitPiece.transform.eulerAngles.z); // forward to backwards
}
else if (altFlag)
{
rZ--;
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x, HitPiece.transform.eulerAngles.y, HitPiece.transform.eulerAngles.z + (currentRotationAngleDegrees * rZ)); // diagonal
}
else
{
if (altFlag)
{
rZ--;
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x, HitPiece.transform.eulerAngles.y, HitPiece.transform.eulerAngles.z + (scrollDistance * (float)rZ)); // diagonal
}
else
{
rY--;
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x, HitPiece.transform.eulerAngles.y + (scrollDistance * (float)rY), HitPiece.transform.eulerAngles.z); // left<->right
}
rY--;
rotation = Quaternion.Euler(HitPiece.transform.eulerAngles.x, HitPiece.transform.eulerAngles.y + (currentRotationAngleDegrees * rY), HitPiece.transform.eulerAngles.z); // left<->right
}

HitPiece.transform.rotation = rotation;
}

// NUMPAD CONTROL
var currentTranslationDistance = BASE_TRANSLATION_DISTANCE * currentModificationSpeed;
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (controlFlag)
{
HitPiece.transform.Translate(Vector3.up * distance * Time.deltaTime);
HitPiece.transform.Translate(Vector3.up * currentTranslationDistance);
}
else
{
HitPiece.transform.Translate(Vector3.forward * distance * Time.deltaTime);
HitPiece.transform.Translate(Vector3.forward * currentTranslationDistance);
}
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
if (controlFlag)
{
HitPiece.transform.Translate(Vector3.down * distance * Time.deltaTime);
HitPiece.transform.Translate(Vector3.down * currentTranslationDistance);
}
else
{
HitPiece.transform.Translate(Vector3.back * distance * Time.deltaTime);
HitPiece.transform.Translate(Vector3.back * currentTranslationDistance);
}
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
HitPiece.transform.Translate(Vector3.left * distance * Time.deltaTime);
HitPiece.transform.Translate(Vector3.left * currentTranslationDistance);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
HitPiece.transform.Translate(Vector3.right * distance * Time.deltaTime);
HitPiece.transform.Translate(Vector3.right * currentTranslationDistance);
}
}

// Hit Piece still is a valid target
private static bool hitPieceStillExsists()
private static bool hitPieceStillExists()
{
try
{ // check to see if the hit object still exsists
{ // check to see if the hit object still exists
if (isActive)
{
Vector3 exsistenceCheck = HitPiece.transform.position;
isInExsistence = true;
isInExistence = true;
}
}
catch
{
isInExsistence = false;
isInExistence = false;
}

return isInExsistence;
return isInExistence;
}

// Check for access to object
Expand Down Expand Up @@ -439,51 +424,38 @@ private static bool isContainer()
{
Container ContainerInstance = HitPiece.GetComponent<Container>();

if (ContainerInstance != null)
{
return true;
}
return false;
return ContainerInstance != null;
}

private static bool dropContainerContents()
private static void dropContainerContents()
{
Container ContainerInstance = HitPiece.GetComponent<Container>();
ContainerInstance.DropAllItems();
return true;
}

private static void changeModificationSpeeds(bool isShiftPressed)
private static void changeModificationSpeed()
{
float incValue = 1;
float speedDelta = 1;
if (shiftFlag)
incValue = 10;
{
speedDelta = 10;
}

if (Input.GetKeyDown(Configuration.Current.AdvancedEditingMode.increaseScrollSpeed))
{
currentModificationSpeed = Mathf.Clamp(currentModificationSpeed + speedDelta, MIN_MODIFICATION_SPEED,
MAX_MODIFICATION_SPEED);

if ((gScrollDistance - incValue) < 360)
gScrollDistance += incValue;

if ((gDistance - incValue) < 360)
gDistance += incValue;
notifyUser("Modification Speed: " + gDistance);
Debug.Log("Modification Speed: " + gDistance);
notifyUser("Modification Speed: " + currentModificationSpeed);
}

if (Input.GetKeyDown(Configuration.Current.AdvancedEditingMode.decreaseScrollSpeed))
{
currentModificationSpeed = Mathf.Clamp(currentModificationSpeed - speedDelta, MIN_MODIFICATION_SPEED,
MAX_MODIFICATION_SPEED);

if ((gScrollDistance - incValue) > 0)
gScrollDistance = gScrollDistance - incValue;

if ((gDistance - incValue) > 0)
gDistance = gDistance - incValue;

notifyUser("Modification Speed: " + gDistance);
Debug.Log("Modification Speed: " + gDistance);
notifyUser("Modification Speed: " + currentModificationSpeed);
}
}


}
}
Loading

0 comments on commit 93236a0

Please sign in to comment.