/// <summary> /// Creates the GameObject of the ConfigurablePart /// </summary> private void CreateGameObject(int rotation) { var voxelSize = Grid.VoxelSize; GameObject reference = Resources.Load <GameObject>("GameObjects/ConfigurableComponentAgent"); CPGameObject = GameObject.Instantiate(reference, Grid.GridGO.transform.parent); CPGameObject.transform.name = Name; SetGOTransformations(); CPAgent = CPGameObject.GetComponent <ConfigurablePartAgent>(); CPAgent.SetPart(this); }
/// <summary> /// Tries to move the ConfigurablePart object in the X direction on the Grid /// Calls an update of the <see cref="PPSpace"/>s on the <see cref="VoxelGrid"/> /// on every execution. /// </summary> /// <param name="distance">The distance to be moved</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 movement was successful</returns> public bool MoveInZ(int distance, bool updateSpaces = true, bool freezeAgent = true) { ////Boolean to evalute the validity of the movement //bool validMovement = true; //Vector3Int[] tempIndexes = new Vector3Int[OccupiedIndexes.Length]; //if (distance == 1 || distance == -1) //{ // //Temporarily store the position of the new indexes // //While checking their validity // for (int i = 0; i < OccupiedIndexes.Length; i++) // { // int x = OccupiedIndexes[i].x; // int y = OccupiedIndexes[i].y; // int z = OccupiedIndexes[i].z + distance; // // 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)) // { // validMovement = false; // return validMovement; // } // //If everything is ok, add new index to temporary array // tempIndexes[i] = new Vector3Int(x, y, z); // } //} //else //{ // //If distance is not +1 or -1, return false // validMovement = false; // return validMovement; //} ////Apply movement 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; //} ////Move reference index and Pivot //ReferenceIndex += new Vector3Int(0, 0, distance); //SetPivot(); //if (freezeAgent) CPAgent.FreezeAgent(); ////Call to Update the slab in the environment //if (updateSpaces) //{ // _environment.AnalyzeGridUpdateSpaces(); //} //return validMovement; //Boolean to evalute the validity of the movement bool validMovement = true; Vector3Int[] tempIndexes = new Vector3Int[OccupiedIndexes.Length]; if (distance == 1 || distance == -1) { //Temporarily store the position of the new indexes //While checking their validity for (int i = 0; i < OccupiedIndexes.Length; i++) { int x = OccupiedIndexes[i].x; int y = OccupiedIndexes[i].y; int z = OccupiedIndexes[i].z + distance; // If new x 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)) { validMovement = false; return(validMovement); } //If everything is ok, add new index to temporary array tempIndexes[i] = new Vector3Int(x, y, z); } } else { //If distance is not +1 or -1, return false validMovement = false; return(validMovement); } //Apply movement 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; } //Move reference index and Pivot ReferenceIndex += new Vector3Int(0, 0, distance); SetPivot(); if (freezeAgent) { CPAgent.FreezeAgent(); } //Call to Update the slab in the environment if (updateSpaces) { _environment.AnalyzeGridUpdateSpaces(); } return(validMovement); }
/// <summary> /// Modifies the visibility state of the GameObject /// </summary> /// <param name="visible">Boolean to set</param> public void SetGOVisibility(bool visible) { //CPGameObject.SetActive(visible); CPAgent.SetVisibility(visible); }
/// <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); }