void SetOrientation(PartOrientation orientation) { partOrientation = orientation; if (anim) { anim.pointOrientation = orientation; } switch (partOrientation) { case PartOrientation.left: name += "_L"; break; case PartOrientation.right: name += "_R"; break; } foreach (CharacterPartPoint point in anchorPoints) { if (point.pointOrientation == PartOrientation.none) { point.pointOrientation = orientation; } creator.anchorPoints.Add(point); } foreach (CharacterPartPoint point in constraintPoints) { if (point.pointOrientation == PartOrientation.none) { point.pointOrientation = orientation; } } }
/// <summary> /// Tries to rotate the component around its ReferenceIndex. /// A direction of +1 rotetes clockwise and -1 anticlockwise. /// Calls an update of the <see cref="PPSpace"/>s on the <see cref="VoxelGrid"/> /// on every execution. /// </summary> /// <param name="direction">The direction to rotate </param> /// <param name="updateSpaces">Bool to trigger space evaluation update. Default is true</param> /// <param name="freezeAgent">Bool to trigger the freezing of the agent once done with movement. Default is true</param> /// <returns>The boolean representing if the rotation was successful</returns> public bool RotateComponent(int direction, bool updateSpaces = true, bool freezeAgent = true) { //Boolean to evalute the validity of the rotation bool validRotation = true; //New orientation PartOrientation newOrientation = PartOrientation.Horizontal; if (Orientation == PartOrientation.Horizontal) { newOrientation = PartOrientation.Vertical; } //Temporary store new indexes Vector3Int[] tempIndexes = new Vector3Int[OccupiedIndexes.Length]; if (direction == 1 || direction == -1) { //Define the rotation matrix Matrix4x4 rotationMatrix; if (direction == 1) { rotationMatrix = Matrix4x4.Rotate(Quaternion.Euler(0, 90f, 0)); } else { rotationMatrix = Matrix4x4.Rotate(Quaternion.Euler(0, -90f, 0)); } for (int i = 0; i < OccupiedIndexes.Length; i++) { //Rotate index var existingIndex = new Vector3Int(OccupiedIndexes[i].x, OccupiedIndexes[i].y, OccupiedIndexes[i].z); Vector3 rotatedIndex = rotationMatrix.MultiplyPoint(existingIndex - PartPivot) + PartPivot; //Resulting coordinates int x = Mathf.RoundToInt(rotatedIndex.x); int y = Mathf.RoundToInt(rotatedIndex.y); int z = Mathf.RoundToInt(rotatedIndex.z); // If new x position is beyond grid, return false if (x < 0 || x > Grid.Size.x - 1) { return(false); } // If new z position is beyond grid, return false if (z < 0 || z > Grid.Size.z - 1) { return(false); } var voxel = Grid.Voxels[x, y, z]; //If the movement includes a voxel that is not active or //is occupied by a part that isn't this, return false if (!voxel.IsActive || (voxel.IsOccupied && voxel.Part != this)) { validRotation = false; return(validRotation); } //If everything is ok, add new index to temporary array tempIndexes[i] = new Vector3Int(x, y, z); } } else { validRotation = false; return(validRotation); } //Apply Flipping to grid for (int i = 0; i < OccupiedIndexes.Length; i++) { var preVoxel = Grid.Voxels[OccupiedIndexes[i].x, OccupiedIndexes[i].y, OccupiedIndexes[i].z]; preVoxel.IsOccupied = false; preVoxel.Part = null; } for (int i = 0; i < OccupiedIndexes.Length; i++) { var newIndex = tempIndexes[i]; var newVoxel = Grid.Voxels[newIndex.x, newIndex.y, newIndex.z]; newVoxel.IsOccupied = true; newVoxel.Part = this; OccupiedIndexes[i] = newIndex; } Orientation = newOrientation; Rotation = Rotation + direction; if (Rotation == 4) { Rotation = 0; } else if (Rotation < 0) { Rotation = 3; } if (freezeAgent) { CPAgent.FreezeAgent(); } //Call to Update the slab in the environment if (updateSpaces) { _environment.AnalyzeGridUpdateSpaces(); } return(validRotation); }
public void Initialize(PartOrientation orientation) { item = true; creator = GetComponentInParent <CharacterCreator>(); creator.character.playerStats += partStats; partAction = GetComponent <PartAction>(); anim = GetComponentInChildren <CharacterPartAnimator>(); GetMeshes(); SetColors(partColor1, partColor2); SetOrientation(orientation); switch (partType) { case PartType.head: creator.character.headPoint.parent = transform; creator.character.headPoint.localPosition = specialPoint.localPosition; specialPoint.parent = creator.character.headPoint; break; case PartType.arm: if (orientation == PartOrientation.left) { creator.character.leftArm = this; } else if (orientation == PartOrientation.right) { creator.character.rightArm = this; } Transform holdPoint = (orientation == PartOrientation.right) ? creator.character.rightHandPoint : creator.character.leftHandPoint; if (holdPoint) { holdPoint.parent = specialPoint; holdPoint.localPosition = Vector3.zero; } break; case PartType.body: specialPoint.parent = creator.character.headPoint; break; case PartType.torso: transform.parent = creator.torsoPoint; creator.character.torso = this; break; case PartType.leg: transform.parent = creator.torsoPoint; if (orientation == PartOrientation.left) { creator.character.leftLeg = this; } else if (orientation == PartOrientation.right) { creator.character.rightLeg = this; } // foreach (CharacterPartPoint point in creator.character.torso.constraintPoints) // { // if (point.pointOrientation == orientation) // { // findPartPoint = point; // break; // } // } // if (findPartPoint != null) // { // // transform.SetParent(findPartPoint.point); // transform.localScale = Vector3.Scale(transform.localScale, findPartPoint.point.localScale); // transform.localPosition = Vector3.right * (findPartPoint.point.localPosition.x) /* + (Vector3.up * transform.position.y) */ + (Vector3.forward * findPartPoint.point.localPosition.z); // } break; case PartType.backAccessory: creator.character.backAccessory = this; break; } }